PageRenderTime 153ms CodeModel.GetById 50ms app.highlight 2ms RepoModel.GetById 99ms 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
 23#import "PluginProgressView.h"
 24
 25
 26@implementation PluginProgressView
 27
 28// Modifed function from DemoView.m in PathDemo
 29static void addRoundedRectToPath(CGContextRef context, CGRect rect,
 30                                 float ovalWidth,float ovalHeight)
 31{
 32    float fw, fh;
 33 
 34    CGContextSaveGState(context);// 2
 35 
 36    CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
 37                         CGRectGetMinY(rect));
 38    CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
 39    fw = CGRectGetWidth (rect) / ovalWidth;// 5
 40    fh = CGRectGetHeight (rect) / ovalHeight;// 6
 41 
 42    CGContextMoveToPoint(context, fw, fh/2); // 7
 43    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
 44    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
 45    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
 46    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
 47    CGContextFillPath(context); // 12
 48 
 49    CGContextRestoreGState(context);// 13
 50}
 51
 52static NSString* stringFromByteSize(int size)
 53{
 54  double value = size / 1024;
 55  if (value < 1023)
 56    return [NSString localizedStringWithFormat:@"%1.1f KB", value];
 57  value = value / 1024;
 58  if (value < 1023)
 59    return [NSString localizedStringWithFormat:@"%1.1f MB", value];
 60  value = value / 1024;
 61  return [NSString localizedStringWithFormat:@"%1.1f GB", value];
 62
 63}
 64
 65- (void)awakeFromNib
 66{
 67  NSImage* image = [[NSWorkspace sharedWorkspace] iconForFileType:@"pdf"];
 68  if (image) {
 69    [imageView setImage:image];
 70  }
 71}
 72
 73- (void)setProgress:(int)progress total:(int)total
 74{
 75  NSBundle* bundle = [NSBundle bundleForClass:[self class]];
 76  NSString* progressString = NSLocalizedStringFromTableInBundle(
 77      @"Loading", nil, bundle, @"Loading PDF");
 78
 79  if (total == 0) {
 80    [progressBar setIndeterminate:true];
 81    return;
 82  }
 83  [progressBar setMaxValue:total];
 84  [progressBar setDoubleValue:progress];
 85  
 86  [progressText setStringValue:
 87    [NSString localizedStringWithFormat:
 88      progressString,
 89      stringFromByteSize(progress),
 90      stringFromByteSize(total)]];
 91}
 92
 93- (void)downloadFailed
 94{
 95  [progressBar setHidden:YES];
 96
 97  NSBundle* bundle = [NSBundle bundleForClass:[self class]];
 98  [progressText setStringValue:
 99    NSLocalizedStringFromTableInBundle(
100        @"Failed", nil, bundle, @"Download failed")];
101  [progressText setFrameOrigin:NSMakePoint(51, 23)];
102  [filenameText setFrameOrigin:NSMakePoint(51, 43)];
103}
104
105- (void)setFilename:(NSString*)filename
106{
107  [filenameText setStringValue:filename];
108}
109
110- (void)setFrame:(NSRect)frame
111{
112  NSView* superview = [self superview];
113  if (superview) {
114    frame.origin.x = MAX(0, ([superview frame].size.width - frame.size.width) / 2);
115    frame.origin.y = MAX(0, ([superview frame].size.height - frame.size.height) / 2);
116  }
117  [super setFrame:frame];
118}
119
120- (void)drawRect:(NSRect)dirty
121{
122  NSRect rect = [self bounds];
123  CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
124  CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
125  
126  CGContextSetGrayFillColor(context, 1.0, 1.0);
127  addRoundedRectToPath(context, r, 10.0, 10.0);
128}
129
130@end