source: trunk/macosx/Badger.m @ 678

Last change on this file since 678 was 678, checked in by livings124, 16 years ago

simplify rect width and height measurements

  • Property svn:keywords set to Date Rev Author Id
File size: 6.7 KB
Line 
1/******************************************************************************
2 * $Id: Badger.m 678 2006-07-23 19:22:24Z livings124 $
3 *
4 * Copyright (c) 2006 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 "Badger.h"
26#import "StringAdditions.h"
27
28@interface Badger (Private)
29
30- (void) badgeString: (NSString *) string forRect: (NSRect) rect;
31
32@end
33
34@implementation Badger
35
36- (id) init
37{
38    if ((self = [super init]))
39    {
40        fBadge = [NSImage imageNamed: @"Badge"];
41        fDockIcon = [[NSApp applicationIconImage] copy];
42        fBadgedDockIcon = [fDockIcon copy];
43        fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
44        fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
45       
46        NSShadow * stringShadow = [[NSShadow alloc] init];
47        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
48        [stringShadow setShadowBlurRadius: 4.0];
49       
50        NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont:
51                                [NSFont fontWithName: @"Helvetica" size: 28.0] toHaveTrait: NSBoldFontMask];
52       
53        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
54            [NSColor whiteColor], NSForegroundColorAttributeName,
55            boldFont, NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
56       
57        [stringShadow release];
58       
59        fCompleted = 0;
60        fSpeedShown = NO;
61    }
62   
63    return self;
64}
65
66- (void) dealloc
67{
68    [fDockIcon release];
69    [fBadgedDockIcon release];
70    [fAttributes release];
71
72    [super dealloc];
73}
74
75- (void) updateBadgeWithCompleted: (int) completed
76                    uploadRate: (float) uploadRate
77                    downloadRate: (float) downloadRate
78{
79    NSImage * dockIcon = nil;
80    NSSize iconSize = [fDockIcon size];
81
82    //set completed badge
83    if (fCompleted != completed)
84    {
85        fCompleted = completed;
86        dockIcon = [fDockIcon copy];
87       
88        //set completed badge to top right
89        if (completed > 0)
90        {
91            NSRect badgeRect;
92            badgeRect.size = [fBadge size];
93            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
94            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
95                                       
96            [dockIcon lockFocus];
97           
98            //place badge
99            [fBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
100           
101            //ignore shadow of badge when placing string
102            float badgeBottomExtra = 5.0;
103            badgeRect.size.height -= badgeBottomExtra;
104            badgeRect.origin.y += badgeBottomExtra;
105           
106            //place badge text
107            [self badgeString: [NSString stringWithInt: completed] forRect: badgeRect];
108                       
109            [dockIcon unlockFocus];
110        }
111       
112        [fBadgedDockIcon release];
113        fBadgedDockIcon = [dockIcon copy];
114    }
115
116    //set upload and download rate badges
117    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
118    NSString * uploadRateString = uploadRate >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"]
119                                    ? [NSString stringForSpeedAbbrev: uploadRate] : nil,
120            * downloadRateString = downloadRate >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"]
121                                    ? [NSString stringForSpeedAbbrev: downloadRate] : nil;
122   
123    BOOL speedShown = uploadRateString || downloadRateString;
124    if (speedShown)
125    {
126        NSRect badgeRect;
127        badgeRect.size = [fUploadBadge size];
128        badgeRect.origin = NSZeroPoint;
129       
130        //ignore shadow of badge when placing string
131        NSRect stringRect = badgeRect;
132        float badgeBottomExtra = 2.0;
133        stringRect.size.height -= badgeBottomExtra;
134        stringRect.origin.y += badgeBottomExtra;
135
136        if (!dockIcon)
137            dockIcon = [fBadgedDockIcon copy];
138       
139        [dockIcon lockFocus];
140       
141        if (uploadRateString)
142        {
143            //place badge
144            [fUploadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
145           
146            //place badge text
147            [self badgeString: uploadRateString forRect: stringRect];
148        }
149       
150        if (downloadRateString)
151        {
152            //download rate above upload rate
153            if (uploadRateString)
154            {
155                float spaceBetween = badgeRect.size.height + 2.0;
156                badgeRect.origin.y += spaceBetween;
157                stringRect.origin.y += spaceBetween;
158            }
159       
160            //place badge
161            [fDownloadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
162           
163            //place badge text
164            [self badgeString: downloadRateString forRect: stringRect];
165        }
166       
167        [dockIcon unlockFocus];
168    }
169
170    if (dockIcon || fSpeedShown)
171    {
172        if (!dockIcon)
173            dockIcon = [fBadgedDockIcon copy];
174           
175        [NSApp setApplicationIconImage: dockIcon];
176        [dockIcon release];
177    }
178   
179    fSpeedShown = speedShown;
180}
181
182- (void) clearBadge
183{
184    [fBadgedDockIcon release];
185    fBadgedDockIcon = [fDockIcon copy];
186
187    [NSApp setApplicationIconImage: fDockIcon];
188}
189
190@end
191
192@implementation Badger (Private)
193
194//dock icon must have locked focus
195- (void) badgeString: (NSString *) string forRect: (NSRect) rect
196{
197    NSSize stringSize = [string sizeWithAttributes: fAttributes];
198   
199    //string is in center of image
200    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
201    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
202                       
203    [string drawAtPoint: rect.origin withAttributes: fAttributes];
204}
205
206@end
Note: See TracBrowser for help on using the repository browser.