PageRenderTime 16ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PluginProgressView.mm

http://firefox-mac-pdf.googlecode.com/
Objective C++ | 130 lines | 87 code | 21 blank | 22 comment | 6 complexity | 7896858914724452b57d4cf471ba1588 MD5 | raw file
  1. /*
  2. * Copyright (c) 2009 Samuel Gross.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #import "PluginProgressView.h"
  23. @implementation PluginProgressView
  24. // Modifed function from DemoView.m in PathDemo
  25. static void addRoundedRectToPath(CGContextRef context, CGRect rect,
  26. float ovalWidth,float ovalHeight)
  27. {
  28. float fw, fh;
  29. CGContextSaveGState(context);// 2
  30. CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
  31. CGRectGetMinY(rect));
  32. CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
  33. fw = CGRectGetWidth (rect) / ovalWidth;// 5
  34. fh = CGRectGetHeight (rect) / ovalHeight;// 6
  35. CGContextMoveToPoint(context, fw, fh/2); // 7
  36. CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
  37. CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
  38. CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
  39. CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
  40. CGContextFillPath(context); // 12
  41. CGContextRestoreGState(context);// 13
  42. }
  43. static NSString* stringFromByteSize(int size)
  44. {
  45. double value = size / 1024;
  46. if (value < 1023)
  47. return [NSString localizedStringWithFormat:@"%1.1f KB", value];
  48. value = value / 1024;
  49. if (value < 1023)
  50. return [NSString localizedStringWithFormat:@"%1.1f MB", value];
  51. value = value / 1024;
  52. return [NSString localizedStringWithFormat:@"%1.1f GB", value];
  53. }
  54. - (void)awakeFromNib
  55. {
  56. NSImage* image = [[NSWorkspace sharedWorkspace] iconForFileType:@"pdf"];
  57. if (image) {
  58. [imageView setImage:image];
  59. }
  60. }
  61. - (void)setProgress:(int)progress total:(int)total
  62. {
  63. NSBundle* bundle = [NSBundle bundleForClass:[self class]];
  64. NSString* progressString = NSLocalizedStringFromTableInBundle(
  65. @"Loading", nil, bundle, @"Loading PDF");
  66. if (total == 0) {
  67. [progressBar setIndeterminate:true];
  68. return;
  69. }
  70. [progressBar setMaxValue:total];
  71. [progressBar setDoubleValue:progress];
  72. [progressText setStringValue:
  73. [NSString localizedStringWithFormat:
  74. progressString,
  75. stringFromByteSize(progress),
  76. stringFromByteSize(total)]];
  77. }
  78. - (void)downloadFailed
  79. {
  80. [progressBar setHidden:YES];
  81. NSBundle* bundle = [NSBundle bundleForClass:[self class]];
  82. [progressText setStringValue:
  83. NSLocalizedStringFromTableInBundle(
  84. @"Failed", nil, bundle, @"Download failed")];
  85. [progressText setFrameOrigin:NSMakePoint(51, 23)];
  86. [filenameText setFrameOrigin:NSMakePoint(51, 43)];
  87. }
  88. - (void)setFilename:(NSString*)filename
  89. {
  90. [filenameText setStringValue:filename];
  91. }
  92. - (void)setFrame:(NSRect)frame
  93. {
  94. NSView* superview = [self superview];
  95. if (superview) {
  96. frame.origin.x = MAX(0, ([superview frame].size.width - frame.size.width) / 2);
  97. frame.origin.y = MAX(0, ([superview frame].size.height - frame.size.height) / 2);
  98. }
  99. [super setFrame:frame];
  100. }
  101. - (void)drawRect:(NSRect)dirty
  102. {
  103. NSRect rect = [self bounds];
  104. CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  105. CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  106. CGContextSetGrayFillColor(context, 1.0, 1.0);
  107. addRoundedRectToPath(context, r, 10.0, 10.0);
  108. }
  109. @end