source: trunk/macosx/Badger.m @ 272

Last change on this file since 272 was 272, checked in by livings124, 16 years ago

First commit on my own 8-)

Sorting by progress, sorting no longer causes crashes.
New look for the table.
Info is now inspector: can set individual and multiple ratio limits, shows listing of files, more info in general, resizes when changing tabs, can change tabs with cmd-left and cmd-right.
Menu items moved to "Transfers" menu.
Sliding status bar in its own view.
Prefs moved into their own nib.
Toolbar items for pause and resume selected (many wanted this, but it needs better icons)
New icons for Transfers and General.
A lot of tweaking of main window to fix alignment etc.
Sparkle used for updated (still needs to be added to website to work).

And a lot more tweaking and changes that I'm too lazy to list.

...now let's hope I commit this right

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