source: trunk/macosx/TorrentCell.m @ 856

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

Advanced bar should work again.

  • Property svn:keywords set to Date Rev Author Id
File size: 12.0 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 856 2006-09-12 00:08:30Z 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    NSImage * img = [info objectForKey: @"AdvancedBar"];
162    [img setSize: NSMakeSize(widthFloat, BAR_HEIGHT)];
163
164    [img compositeToPoint: point operation: NSCompositeSourceOver];
165   
166    //draw overlay over advanced bar
167    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
168   
169    widthFloat -= 2.0;
170    point.x += 1.0;
171    [self placeBar: fProgressAdvanced width: widthFloat point: point];
172   
173    point.x += widthFloat;
174    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
175}
176
177- (void) toggleMinimalStatus
178{
179    fStatusRegular = !fStatusRegular;
180    [fDefaults setBool: fStatusRegular forKey: @"SmallStatusRegular"];
181}
182
183- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
184{
185    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view]
186                                                        isEqual: [NSColor alternateSelectedControlColor]];
187    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
188                    highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
189                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName, nil];
190    NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
191                    highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
192                    [NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
193
194    NSPoint pen = cellFrame.origin;
195    const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0;
196   
197    NSDictionary * info = [self objectValue];
198   
199    if (![fDefaults boolForKey: @"SmallView"]) //regular size
200    {
201        //icon
202        NSImage * icon = [info objectForKey: @"Icon"];
203        NSSize iconSize = [icon size];
204       
205        pen.x += PADDING;
206        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
207       
208        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
209                operation: NSCompositeSourceOver fraction: 1.0];
210       
211        //error badge
212        if ([[info objectForKey: @"Error"] boolValue])
213        {
214            NSSize errorIconSize = [fErrorImage size];
215            [fErrorImage drawAtPoint: NSMakePoint(pen.x + iconSize.width - errorIconSize.width,
216                                                    pen.y + iconSize.height  - errorIconSize.height)
217                fromRect: NSMakeRect(0, 0, errorIconSize.width, errorIconSize.height)
218                operation: NSCompositeSourceOver fraction: 1.0];
219        }
220
221        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
222
223        //name string
224        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
225        pen.y = cellFrame.origin.y + PADDING;
226        NSAttributedString * nameString = [[info objectForKey: @"Name"] attributedStringFittingInWidth: mainWidth
227                                                attributes: nameAttributes];
228        [nameString drawAtPoint: pen];
229       
230        //progress string
231        pen.y += [nameString size].height + LINE_PADDING - 1.0;
232       
233        NSAttributedString * progressString = [[info objectForKey: @"ProgressString"]
234            attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
235        [progressString drawAtPoint: pen];
236
237        //progress bar
238        pen.x -= EXTRA_NAME_SHIFT;
239        pen.y += [progressString size].height + LINE_PADDING + BAR_HEIGHT;
240       
241        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
242       
243        if ([fDefaults boolForKey: @"UseAdvancedBar"])
244            [self buildAdvancedBar: barWidth point: pen];
245        else
246            [self buildSimpleBar: barWidth point: pen];
247
248        //status string
249        pen.x += EXTRA_NAME_SHIFT;
250        pen.y += LINE_PADDING;
251        NSAttributedString * statusString = [[info objectForKey: @"StatusString"]
252            attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
253        [statusString drawAtPoint: pen];
254    }
255    else //small size
256    {
257        //icon
258        NSImage * icon = ![[info objectForKey: @"Error"] boolValue] ? [info objectForKey: @"Icon"] : fErrorImage;
259        NSSize iconSize = [icon size];
260       
261        pen.x += PADDING;
262        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
263       
264        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
265                operation: NSCompositeSourceOver fraction: 1.0];
266
267        //name and status string
268        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
269       
270        NSString * realStatusString = !fStatusRegular && [[info objectForKey: @"Active"] boolValue]
271                                        ? [info objectForKey: @"RemainingTimeString"]
272                                        : [info objectForKey: @"ShortStatusString"];
273       
274        NSAttributedString * statusString = [[[NSAttributedString alloc] initWithString: realStatusString
275                                                    attributes: statusAttributes] autorelease];
276        NSAttributedString * nameString = [[info objectForKey: @"Name"] attributedStringFittingInWidth:
277                                mainWidth - [statusString size].width - LINE_PADDING attributes: nameAttributes];
278                     
279        //place name string
280        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
281        pen.y = cellFrame.origin.y + LINE_PADDING;
282
283        [nameString drawAtPoint: pen];
284       
285        //place status string
286        pen.x = NSMaxX(cellFrame) - PADDING - [statusString size].width;
287        pen.y += ([nameString size].height - [statusString size].height) * 0.5;
288       
289        [statusString drawAtPoint: pen];
290       
291        //progress bar
292        pen.x = cellFrame.origin.x + iconSize.width + 2.0 * PADDING;
293        pen.y = cellFrame.origin.y + [nameString size].height + LINE_PADDING + PADDING + BAR_HEIGHT;
294       
295        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
296       
297        if ([fDefaults boolForKey: @"UseAdvancedBar"])
298            [self buildAdvancedBar: barWidth point: pen];
299        else
300            [self buildSimpleBar: barWidth point: pen];
301    }
302   
303    [nameAttributes release];
304    [statusAttributes release];
305}
306
307@end
Note: See TracBrowser for help on using the repository browser.