source: trunk/macosx/Badger.m @ 261

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

Updated svn:keywords

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