source: trunk/macosx/Badger.m @ 256

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

Merged Mitchell's changes, this includes:
Adds stop at ratio,
Adds sorting by name, date or state,
Seperates Info window into its own nib,
Adds option to enable or disabled autostarting downloads,
Seperates Network / Transfers in the Preferences

File size: 6.3 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        NSShadow * stringShadow = [[NSShadow alloc] init];
46        [stringShadow setShadowOffset: NSMakeSize(2, -2)];
47        [stringShadow setShadowBlurRadius: 4];
48       
49        fAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
50            [NSColor whiteColor], NSForegroundColorAttributeName,
51            [NSFont fontWithName: @"Helvetica-Bold" size: 28], NSFontAttributeName,
52            stringShadow, NSShadowAttributeName,
53            nil] retain];
54       
55        [stringShadow release];
56       
57        fCompleted = 0;
58        fSpeedShown = NO;
59    }
60   
61    return self;
62}
63
64- (void) dealloc
65{
66    [fDockIcon release];
67    [fBadgedDockIcon release];
68
69    [fAttributes release];
70
71    [super dealloc];
72}
73
74- (void) updateBadgeWithCompleted: (int) completed
75                    uploadRate: (NSString *) uploadRate
76                    downloadRate: (NSString *) downloadRate
77{
78    NSImage * dockIcon = nil;
79    NSSize iconSize = [fDockIcon size];
80
81    //set completed badge
82    if (fCompleted != completed)
83    {
84        fCompleted = completed;
85       
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
100                        operation: NSCompositeSourceOver];
101           
102            //ignore shadow of badge when placing string
103            float badgeBottomExtra = 5.0;
104            badgeRect.size.height -= badgeBottomExtra;
105            badgeRect.origin.y += badgeBottomExtra;
106           
107            //place badge text
108            [self badgeString: [NSString stringWithInt: completed]
109                        forRect: badgeRect];
110                       
111            [dockIcon unlockFocus];
112        }
113       
114        [fBadgedDockIcon release];
115        fBadgedDockIcon = [dockIcon copy];
116    }
117
118    //set upload and download rate badges
119    BOOL speedShown = uploadRate || downloadRate;
120    if (speedShown)
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.