source: trunk/macosx/BadgeView.m @ 5740

Last change on this file since 5740 was 5740, checked in by livings124, 13 years ago

generate the inspector tab button images dynamically when selected/deselected instead of storing them

  • Property svn:keywords set to Date Rev Author Id
File size: 4.7 KB
Line 
1/******************************************************************************
2 * $Id: BadgeView.m 5740 2008-05-02 13:08:23Z livings124 $
3 *
4 * Copyright (c) 2007-2008 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 "BadgeView.h"
26#import "NSStringAdditions.h"
27
28#define BETWEEN_PADDING 2.0
29
30@interface BadgeView (Private)
31
32- (void) badge: (NSImage *) badge string: (NSString *) string atHeight: (float) height adjustForQuit: (BOOL) quit;
33
34@end
35
36@implementation BadgeView
37
38- (id) initWithFrame: (NSRect) frame lib: (tr_handle *) lib
39{
40    if ((self = [super initWithFrame: frame]))
41    {
42        fLib = lib;
43       
44        fDownloadRate = 0.0;
45        fUploadRate = 0.0;
46        fQuitting = NO;
47    }
48    return self;
49}
50
51- (void) dealloc
52{
53    [fAttributes release];
54    [super dealloc];
55}
56
57- (BOOL) setRatesWithDownload: (float) downloadRate upload: (float) uploadRate
58{
59    //only needs update if the badges were displayed or are displayed now
60    BOOL needsUpdate = fDownloadRate != downloadRate || fUploadRate != uploadRate;
61    if (needsUpdate)
62    {
63        fDownloadRate = downloadRate;
64        fUploadRate = uploadRate;
65    }
66   
67    return needsUpdate;
68}
69
70- (void) setQuitting
71{
72    fQuitting = YES;
73}
74
75- (void) drawRect: (NSRect) rect
76{
77    [[NSImage imageNamed: @"NSApplicationIcon"] drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
78   
79    if (fQuitting)
80    {
81        NSImage * quitBadge = [NSImage imageNamed: @"QuitBadge.png"];
82        [self badge: quitBadge string: NSLocalizedString(@"Quitting", "Dock Badger -> quit")
83                atHeight: (rect.size.height - [quitBadge size].height) * 0.5 adjustForQuit: YES];
84        return;
85    }
86   
87    BOOL upload = fUploadRate >= 0.1,
88        download = fDownloadRate >= 0.1;
89    float bottom = 0.0;
90    if (upload)
91    {
92        NSImage * uploadBadge = [NSImage imageNamed: @"UploadBadge.png"];
93        [self badge: uploadBadge string: [NSString stringForSpeedAbbrev: fUploadRate] atHeight: bottom adjustForQuit: NO];
94        if (download)
95            bottom += [uploadBadge size].height + BETWEEN_PADDING; //download rate above upload rate
96    }
97    if (download)
98        [self badge: [NSImage imageNamed: @"DownloadBadge.png"] string: [NSString stringForSpeedAbbrev: fDownloadRate]
99                atHeight: bottom adjustForQuit: NO];
100}
101
102@end
103
104@implementation BadgeView (Private)
105
106//dock icon must have locked focus
107- (void) badge: (NSImage *) badge string: (NSString *) string atHeight: (float) height adjustForQuit: (BOOL) quit
108{
109    if (!fAttributes)
110    {
111        NSShadow * stringShadow = [[NSShadow alloc] init];
112        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
113        [stringShadow setShadowBlurRadius: 4.0];
114       
115        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
116            [NSColor whiteColor], NSForegroundColorAttributeName,
117            [NSFont boldSystemFontOfSize: 26.0], NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
118       
119        [stringShadow release];
120    }
121   
122    NSRect badgeRect = NSZeroRect;
123    badgeRect.size = [badge size];
124    badgeRect.origin.y = height;
125   
126    [badge drawInRect: badgeRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
127   
128    //string is in center of image
129    NSSize stringSize = [string sizeWithAttributes: fAttributes];
130   
131    NSRect stringRect = badgeRect;
132    stringRect.origin.x += (badgeRect.size.width - stringSize.width) * 0.5;
133    stringRect.origin.y += (badgeRect.size.height - stringSize.height) * 0.5 + (quit ? 2.0 : 1.0); //adjust for shadow, extra for quit
134    stringRect.size = stringSize;
135   
136    [string drawInRect: stringRect withAttributes: fAttributes];
137}
138
139@end
Note: See TracBrowser for help on using the repository browser.