source: trunk/macosx/TorrentCell.m @ 858

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

advanced bar doesn't need to generate it's own border

  • Property svn:keywords set to Date Rev Author Id
File size: 12.0 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 858 2006-09-12 00:51:11Z 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 "TorrentCell.h"
26#import "TorrentTableView.h"
27#import "StringAdditions.h"
28
29//also defined in Torrent.m
30#define BAR_HEIGHT 12.0
31
32@interface TorrentCell (Private)
33
34- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point;
35- (void) buildSimpleBar: (float) width point: (NSPoint) point;
36//- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point;
37
38@end
39
40@implementation TorrentCell
41
42- (id) init
43{
44    if ((self = [super init]))
45    {
46        fDefaults = [NSUserDefaults standardUserDefaults];
47       
48        fStatusRegular = [fDefaults boolForKey: @"SmallStatusRegular"];
49   
50        NSSize startSize = NSMakeSize(100.0, BAR_HEIGHT);
51       
52        fProgressWhite = [NSImage imageNamed: @"ProgressBarWhite.png"];
53        [fProgressWhite setScalesWhenResized: YES];
54       
55        fProgressBlue = [NSImage imageNamed: @"ProgressBarBlue.png"];
56        [fProgressBlue setScalesWhenResized: YES];
57        [fProgressBlue setSize: startSize];
58       
59        fProgressGray = [NSImage imageNamed: @"ProgressBarGray.png"];
60        [fProgressGray setScalesWhenResized: YES];
61        [fProgressGray setSize: startSize];
62       
63        fProgressGreen = [NSImage imageNamed: @"ProgressBarGreen.png"];
64        [fProgressGreen setScalesWhenResized: YES];
65       
66        fProgressAdvanced = [NSImage imageNamed: @"ProgressBarAdvanced.png"];
67        [fProgressAdvanced setScalesWhenResized: YES];
68       
69        fProgressEndWhite = [NSImage imageNamed: @"ProgressBarEndWhite.png"];
70        fProgressEndBlue = [NSImage imageNamed: @"ProgressBarEndBlue.png"];
71        fProgressEndGray = [NSImage imageNamed: @"ProgressBarEndGray.png"];
72        fProgressEndGreen = [NSImage imageNamed: @"ProgressBarEndGreen.png"];
73        fProgressEndAdvanced = [NSImage imageNamed: @"ProgressBarEndAdvanced.png"];
74       
75        fErrorImage = [[NSImage imageNamed: @"Error.tiff"] copy];
76        [fErrorImage setFlipped: YES];
77    }
78    return self;
79}
80
81- (void) dealloc
82{
83    #warning should work?
84    //[fErrorImage release];
85    [super dealloc];
86}
87
88- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point
89{
90    if ([barImage size].width < width)
91        [barImage setSize: NSMakeSize(width * 2.0, BAR_HEIGHT)];
92
93    [barImage compositeToPoint: point fromRect: NSMakeRect(0, 0, width, BAR_HEIGHT) operation: NSCompositeSourceOver];
94}
95
96- (void) buildSimpleBar: (float) width point: (NSPoint) point
97{
98    NSDictionary * info = [self objectValue];
99
100    width -= 2.0;
101    if ([[info objectForKey: @"Seeding"] boolValue])
102    {
103        [fProgressEndGreen compositeToPoint: point operation: NSCompositeSourceOver];
104       
105        point.x += 1.0;
106        [self placeBar: fProgressGreen width: width point: point];
107       
108        point.x += width;
109        [fProgressEndGreen compositeToPoint: point operation: NSCompositeSourceOver];
110    }
111    else
112    {
113        float completedWidth = [[info objectForKey: @"Progress"] floatValue] * width,
114                remainingWidth = width - completedWidth;
115        BOOL isActive = [[info objectForKey: @"Active"] boolValue];
116       
117        //left end
118        NSImage * barLeftEnd;
119        if (remainingWidth == width)
120            barLeftEnd = fProgressEndWhite;
121        else if (isActive)
122            barLeftEnd = fProgressEndBlue;
123        else
124            barLeftEnd = fProgressEndGray;
125       
126        [barLeftEnd compositeToPoint: point operation: NSCompositeSourceOver];
127       
128        //active bar
129        point.x += 1.0;
130        [self placeBar: isActive ? fProgressBlue : fProgressGray width: completedWidth point: point];
131       
132        //remaining bar
133        point.x += completedWidth;
134        [self placeBar: fProgressWhite width: remainingWidth point: point];
135       
136        //right end
137        NSImage * barRightEnd;
138        if (completedWidth < width)
139            barRightEnd = fProgressEndWhite;
140        else if (isActive)
141            barRightEnd = fProgressEndBlue;
142        else
143            barRightEnd = fProgressEndGray;
144       
145        point.x += remainingWidth;
146        [barRightEnd compositeToPoint: point operation: NSCompositeSourceOver];
147    }
148}
149
150- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point
151{
152    NSDictionary * info = [self objectValue];
153   
154    //if seeding, there's no need for the advanced bar
155    if ([[info objectForKey: @"Seeding"] boolValue])
156    {
157        [self buildSimpleBar: widthFloat point: point];
158        return;
159    }
160   
161    //draw overlay over advanced bar
162    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
163   
164    widthFloat -= 2.0;
165    point.x += 1.0;
166   
167    //place actual advanced bar
168    NSImage * img = [info objectForKey: @"AdvancedBar"];
169    [img setSize: NSMakeSize(widthFloat, BAR_HEIGHT)];
170    [img compositeToPoint: point operation: NSCompositeSourceOver];
171   
172    [self placeBar: fProgressAdvanced width: widthFloat point: point];
173   
174    point.x += widthFloat;
175    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
176}
177
178- (void) toggleMinimalStatus
179{
180    fStatusRegular = !fStatusRegular;
181    [fDefaults setBool: fStatusRegular forKey: @"SmallStatusRegular"];
182}
183
184- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
185{
186    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view]
187                                                        isEqual: [NSColor alternateSelectedControlColor]];
188    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
189                    highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
190                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName, nil];
191    NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
192                    highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
193                    [NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
194
195    NSPoint pen = cellFrame.origin;
196    const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0;
197   
198    NSDictionary * info = [self objectValue];
199   
200    if (![fDefaults boolForKey: @"SmallView"]) //regular size
201    {
202        //icon
203        NSImage * icon = [info objectForKey: @"Icon"];
204        NSSize iconSize = [icon size];
205       
206        pen.x += PADDING;
207        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
208       
209        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
210                operation: NSCompositeSourceOver fraction: 1.0];
211       
212        //error badge
213        if ([[info objectForKey: @"Error"] boolValue])
214        {
215            NSSize errorIconSize = [fErrorImage size];
216            [fErrorImage drawAtPoint: NSMakePoint(pen.x + iconSize.width - errorIconSize.width,
217                                                    pen.y + iconSize.height  - errorIconSize.height)
218                fromRect: NSMakeRect(0, 0, errorIconSize.width, errorIconSize.height)
219                operation: NSCompositeSourceOver fraction: 1.0];
220        }
221
222        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
223
224        //name string
225        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
226        pen.y = cellFrame.origin.y + PADDING;
227        NSAttributedString * nameString = [[info objectForKey: @"Name"] attributedStringFittingInWidth: mainWidth
228                                                attributes: nameAttributes];
229        [nameString drawAtPoint: pen];
230       
231        //progress string
232        pen.y += [nameString size].height + LINE_PADDING - 1.0;
233       
234        NSAttributedString * progressString = [[info objectForKey: @"ProgressString"]
235            attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
236        [progressString drawAtPoint: pen];
237
238        //progress bar
239        pen.x -= EXTRA_NAME_SHIFT;
240        pen.y += [progressString size].height + LINE_PADDING + BAR_HEIGHT;
241       
242        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
243       
244        if ([fDefaults boolForKey: @"UseAdvancedBar"])
245            [self buildAdvancedBar: barWidth point: pen];
246        else
247            [self buildSimpleBar: barWidth point: pen];
248
249        //status string
250        pen.x += EXTRA_NAME_SHIFT;
251        pen.y += LINE_PADDING;
252        NSAttributedString * statusString = [[info objectForKey: @"StatusString"]
253            attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
254        [statusString drawAtPoint: pen];
255    }
256    else //small size
257    {
258        //icon
259        NSImage * icon = ![[info objectForKey: @"Error"] boolValue] ? [info objectForKey: @"Icon"] : fErrorImage;
260        NSSize iconSize = [icon size];
261       
262        pen.x += PADDING;
263        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
264       
265        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
266                operation: NSCompositeSourceOver fraction: 1.0];
267
268        //name and status string
269        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
270       
271        NSString * realStatusString = !fStatusRegular && [[info objectForKey: @"Active"] boolValue]
272                                        ? [info objectForKey: @"RemainingTimeString"]
273                                        : [info objectForKey: @"ShortStatusString"];
274       
275        NSAttributedString * statusString = [[[NSAttributedString alloc] initWithString: realStatusString
276                                                    attributes: statusAttributes] autorelease];
277        NSAttributedString * nameString = [[info objectForKey: @"Name"] attributedStringFittingInWidth:
278                                mainWidth - [statusString size].width - LINE_PADDING attributes: nameAttributes];
279                     
280        //place name string
281        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
282        pen.y = cellFrame.origin.y + LINE_PADDING;
283
284        [nameString drawAtPoint: pen];
285       
286        //place status string
287        pen.x = NSMaxX(cellFrame) - PADDING - [statusString size].width;
288        pen.y += ([nameString size].height - [statusString size].height) * 0.5;
289       
290        [statusString drawAtPoint: pen];
291       
292        //progress bar
293        pen.x = cellFrame.origin.x + iconSize.width + 2.0 * PADDING;
294        pen.y = cellFrame.origin.y + [nameString size].height + LINE_PADDING + PADDING + BAR_HEIGHT;
295       
296        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
297       
298        if ([fDefaults boolForKey: @"UseAdvancedBar"])
299            [self buildAdvancedBar: barWidth point: pen];
300        else
301            [self buildSimpleBar: barWidth point: pen];
302    }
303   
304    [nameAttributes release];
305    [statusAttributes release];
306}
307
308@end
Note: See TracBrowser for help on using the repository browser.