source: trunk/macosx/TorrentCell.m @ 1125

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

New button images from macHD. Resume without waiting is now orange.

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