source: trunk/macosx/BadgeView.m @ 11617

Last change on this file since 11617 was 11617, checked in by livings124, 11 years ago

update the copyright years in the Mac code to 2011

  • Property svn:keywords set to Date Rev Author Id
File size: 4.9 KB
Line 
1/******************************************************************************
2 * $Id: BadgeView.m 11617 2011-01-01 20:42:14Z livings124 $
3 *
4 * Copyright (c) 2007-2011 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "BadgeView.h"
26#import "NSStringAdditions.h"
27
28#define BETWEEN_PADDING 2.0
29
30@interface BadgeView (Private)
31
32- (void) badge: (NSImage *) badge string: (NSString *) string atHeight: (CGFloat) height adjustForQuit: (BOOL) quit;
33
34@end
35
36@implementation BadgeView
37
38- (id) initWithFrame: (NSRect) frame lib: (tr_session *) lib
39{
40    if ((self = [super initWithFrame: frame]))
41    {
42        fLib = lib;
43       
44        fDownloadRate = 0.0;
45        fUploadRate = 0.0;
46        fQuitting = NO;
47    }
48    return self;
49}
50
51- (void) dealloc
52{
53    [fAttributes release];
54    [super dealloc];
55}
56
57- (BOOL) setRatesWithDownload: (CGFloat) downloadRate upload: (CGFloat) uploadRate
58{
59    //only needs update if the badges were displayed or are displayed now
60    if (fDownloadRate == downloadRate && fUploadRate == uploadRate)
61        return NO;
62   
63    fDownloadRate = downloadRate;
64    fUploadRate = uploadRate;
65    return YES;
66}
67
68- (void) setQuitting
69{
70    fQuitting = YES;
71}
72
73- (void) drawRect: (NSRect) rect
74{
75    [[NSApp applicationIconImage] drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
76   
77    if (fQuitting)
78    {
79        NSImage * quitBadge = [NSImage imageNamed: @"QuitBadge.png"];
80        [self badge: quitBadge string: NSLocalizedString(@"Quitting", "Dock Badger -> quit")
81                atHeight: (NSHeight(rect) - [quitBadge size].height) * 0.5 adjustForQuit: YES];
82        return;
83    }
84   
85    const BOOL upload = fUploadRate >= 0.1,
86            download = fDownloadRate >= 0.1;
87    CGFloat bottom = 0.0;
88    if (upload)
89    {
90        NSImage * uploadBadge = [NSImage imageNamed: @"UploadBadge.png"];
91        [self badge: uploadBadge string: [NSString stringForSpeedAbbrev: fUploadRate] atHeight: bottom adjustForQuit: NO];
92        if (download)
93            bottom += [uploadBadge size].height + BETWEEN_PADDING; //download rate above upload rate
94    }
95    if (download)
96        [self badge: [NSImage imageNamed: @"DownloadBadge.png"] string: [NSString stringForSpeedAbbrev: fDownloadRate]
97                atHeight: bottom adjustForQuit: NO];
98}
99
100@end
101
102@implementation BadgeView (Private)
103
104- (void) badge: (NSImage *) badge string: (NSString *) string atHeight: (CGFloat) height adjustForQuit: (BOOL) quit
105{
106    if (!fAttributes)
107    {
108        NSShadow * stringShadow = [[NSShadow alloc] init];
109        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
110        [stringShadow setShadowBlurRadius: 4.0];
111       
112        fAttributes = [[NSMutableDictionary alloc] initWithCapacity: 3];
113        [fAttributes setObject: [NSColor whiteColor] forKey: NSForegroundColorAttributeName];
114        [fAttributes setObject: stringShadow forKey: NSShadowAttributeName];
115       
116        [stringShadow release];
117    }
118   
119    NSRect badgeRect;
120    badgeRect.size = [badge size];
121    badgeRect.origin.x = 0.0;
122    badgeRect.origin.y = height;
123   
124    [badge drawInRect: badgeRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
125   
126    //make sure text fits on the badge
127    CGFloat fontSize = 26.0;
128    NSSize stringSize;
129    do
130    {
131        [fAttributes setObject: [NSFont boldSystemFontOfSize: fontSize] forKey: NSFontAttributeName];
132        stringSize = [string sizeWithAttributes: fAttributes];
133        fontSize -= 1.0;
134    } while (NSWidth(badgeRect) < stringSize.width);
135   
136    //string is in center of image
137    NSRect stringRect;
138    stringRect.origin.x = NSMidX(badgeRect) - stringSize.width * 0.5;
139    stringRect.origin.y = NSMidY(badgeRect) - stringSize.height * 0.5 + (quit ? 2.0 : 1.0); //adjust for shadow, extra for quit
140    stringRect.size = stringSize;
141   
142    [string drawInRect: stringRect withAttributes: fAttributes];
143}
144
145@end
Note: See TracBrowser for help on using the repository browser.