source: trunk/macosx/Badger.m @ 1088

Last change on this file since 1088 was 1088, checked in by livings124, 15 years ago

quick fix for last commit

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