source: trunk/macosx/BadgeView.m @ 8227

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

assorted trivial cleanup

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