source: trunk/macosx/Badger.m @ 191

Last change on this file since 191 was 191, checked in by titer, 16 years ago

Merge compat-10.2 branch. This adds Jaguar compatibility on OS X, and
completes the Jam -> Make/XCode switch. Hope it doesn't break too much.

File size: 6.4 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005-2006 Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "Badger.h"
24#import "StringAdditions.h"
25#import "Utils.h"
26
27@interface Badger (Private)
28
29- (void) badgeString: (NSString *) string forRect: (NSRect) rect;
30
31@end
32
33@implementation Badger
34
35- (id) init
36{
37    if ((self = [super init]))
38    {
39        fBadge = [NSImage imageNamed: @"Badge"];
40        fDockIcon = [[NSApp applicationIconImage] copy];
41        fBadgedDockIcon = [fDockIcon copy];
42        fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
43        fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
44       
45       
46        fAttributes = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
47            [NSColor whiteColor], NSForegroundColorAttributeName,
48            [NSFont fontWithName: @"Helvetica-Bold" size: 28], NSFontAttributeName,
49            nil] retain];
50
51        if( OSX_VERSION >= 10.3 )
52        {
53            NSShadow * stringShadow = [[NSShadow alloc] init];
54            [stringShadow setShadowOffset: NSMakeSize(2, -2)];
55            [stringShadow setShadowBlurRadius: 4];
56            [fAttributes setObject: stringShadow
57                forKey: NSShadowAttributeName];
58            [stringShadow release];
59        }
60       
61        fCompleted = 0;
62        fSpeedShown = NO;
63    }
64   
65    return self;
66}
67
68- (void) dealloc
69{
70    [fDockIcon release];
71    [fBadgedDockIcon release];
72
73    [fAttributes release];
74
75    [super dealloc];
76}
77
78- (void) updateBadgeWithCompleted: (int) completed
79                    uploadRate: (NSString *) uploadRate
80                    downloadRate: (NSString *) downloadRate
81{
82    NSImage * dockIcon = nil;
83    NSSize iconSize = [fDockIcon size];
84
85    //set seeding and downloading badges if there was a change
86    if (completed != fCompleted)
87    {
88        fCompleted = completed;
89       
90        dockIcon = [fDockIcon copy];
91       
92        //set completed badge to top right
93        if (completed > 0)
94        {
95            NSRect badgeRect;
96            badgeRect.size = [fBadge size];
97            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
98            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
99                                       
100            [dockIcon lockFocus];
101           
102            //place badge
103            [fBadge compositeToPoint: badgeRect.origin
104                        operation: NSCompositeSourceOver];
105           
106            //ignore shadow of badge when placing string
107            float badgeBottomExtra = 5.0;
108            badgeRect.size.height -= badgeBottomExtra;
109            badgeRect.origin.y += badgeBottomExtra;
110           
111            //place badge text
112            [self badgeString: [NSString stringWithInt: completed]
113                        forRect: badgeRect];
114                       
115            [dockIcon unlockFocus];
116        }
117       
118        [fBadgedDockIcon release];
119        fBadgedDockIcon = [dockIcon copy];
120    }
121
122    //display upload and download rates
123    BOOL speedShown = NO;
124    if (uploadRate || downloadRate)
125    {
126        speedShown = YES;
127   
128        NSRect badgeRect, stringRect;
129        badgeRect.size = [fUploadBadge size];
130        badgeRect.origin = NSZeroPoint;
131       
132        //ignore shadow of badge when placing string
133        float badgeBottomExtra = 2.0;
134        stringRect = badgeRect;
135        stringRect.size.height -= badgeBottomExtra;
136        stringRect.origin.y += badgeBottomExtra;
137
138        if (!dockIcon)
139            dockIcon = [fBadgedDockIcon copy];
140       
141        [dockIcon lockFocus];
142       
143        if (uploadRate)
144        {
145            //place badge
146            [fUploadBadge compositeToPoint: badgeRect.origin
147                        operation: NSCompositeSourceOver];
148           
149            //place badge text
150            [self badgeString: uploadRate forRect: stringRect];
151        }
152       
153        if (downloadRate)
154        {
155            //download rate above upload rate
156            if (uploadRate)
157            {
158                float spaceBetween = badgeRect.size.height + 2.0;
159                badgeRect.origin.y += spaceBetween;
160                stringRect.origin.y += spaceBetween;
161            }
162       
163            //place badge
164            [fDownloadBadge compositeToPoint: badgeRect.origin
165                        operation: NSCompositeSourceOver];
166           
167            //place badge text
168            [self badgeString: downloadRate forRect: stringRect];
169        }
170       
171        [dockIcon unlockFocus];
172    }
173
174    if (dockIcon || fSpeedShown)
175    {
176        if (!dockIcon)
177            dockIcon = [fBadgedDockIcon copy];
178           
179        [NSApp setApplicationIconImage: dockIcon];
180       
181        [dockIcon release];
182    }
183   
184    fSpeedShown = speedShown;
185}
186
187- (void) clearBadge
188{
189    [fBadgedDockIcon release];
190    fBadgedDockIcon = [fDockIcon copy];
191
192    [NSApp setApplicationIconImage: fDockIcon];
193}
194
195@end
196
197@implementation Badger (Private)
198
199//dock icon must have locked focus
200- (void) badgeString: (NSString *) string forRect: (NSRect) rect
201{
202    NSSize stringSize = [string sizeWithAttributes: fAttributes];
203   
204    //string is in center of image
205    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
206    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
207                       
208    [string drawAtPoint: rect.origin withAttributes: fAttributes];
209}
210
211@end
Note: See TracBrowser for help on using the repository browser.