source: branches/new_api/macosx/Badger.m @ 156

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

Added [NSString stringWithInt:] for convenience.
UI cosmetic: bigger margin left from the pause/resume button

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