source: trunk/macosx/TorrentCell.m @ 3196

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

more cleanup from advanced bar removal

  • Property svn:keywords set to Date Rev Author Id
File size: 16.1 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 3196 2007-09-27 02:46:48Z livings124 $
3 *
4 * Copyright (c) 2006-2007 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 "NSStringAdditions.h"
28#import "CTGradientAdditions.h"
29
30#define BAR_HEIGHT 12.0
31
32#define IMAGE_SIZE_REG 32.0
33#define IMAGE_SIZE_MIN 16.0
34
35//end up being larger than font height
36#define HEIGHT_TITLE 16.0
37#define HEIGHT_STATUS 12.0
38
39#define PADDING_HORIZONAL 2.0
40#define PADDING_ABOVE_IMAGE_REG 9.0
41#define PADDING_BETWEEN_IMAGE_AND_TITLE 5.0
42#define PADDING_BETWEEN_IMAGE_AND_BAR 7.0
43#define PADDING_ABOVE_TITLE 3.0
44#define PADDING_ABOVE_MIN_STATUS 4.0
45#define PADDING_BETWEEN_TITLE_AND_MIN_STATUS 2.0
46#define PADDING_BETWEEN_TITLE_AND_PROGRESS 1.0
47#define PADDING_BETWEEN_PROGRESS_AND_BAR 2.0
48#define PADDING_BETWEEN_TITLE_AND_BAR_MIN 3.0
49#define PADDING_BETWEEN_BAR_AND_STATUS 2.0
50
51@interface TorrentCell (Private)
52
53- (void) drawBar: (NSRect) barRect;
54
55- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
56- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect
57            inBounds: (NSRect) bounds;
58- (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
59- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
60
61- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
62- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color;
63
64@end
65
66@implementation TorrentCell
67
68//only called one, so don't worry about release
69- (id) init
70{
71    if ((self = [super init]))
72        {
73        fDefaults = [NSUserDefaults standardUserDefaults];
74       
75        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
76        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
77   
78        fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
79                                [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
80                                paragraphStyle, NSParagraphStyleAttributeName, nil];
81        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
82                                [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
83                                paragraphStyle, NSParagraphStyleAttributeName, nil];
84        [paragraphStyle release];
85       
86        fBarOverlayColor = [[NSColor colorWithDeviceWhite: 0.0 alpha: 0.2] retain];
87    }
88        return self;
89}
90
91- (NSRect) iconRectForBounds: (NSRect) bounds
92{
93    NSRect result = bounds;
94   
95    result.origin.x += PADDING_HORIZONAL;
96   
97    float imageSize;
98    if ([fDefaults boolForKey: @"SmallView"])
99    {
100        imageSize = IMAGE_SIZE_MIN;
101        result.origin.y += (result.size.height - imageSize) * 0.5;
102    }
103    else
104    {
105        imageSize = IMAGE_SIZE_REG;
106        result.origin.y += PADDING_ABOVE_IMAGE_REG;
107    }
108   
109    result.size = NSMakeSize(imageSize, imageSize);
110   
111    return result;
112}
113
114- (NSRect) titleRectForBounds: (NSRect) bounds
115{
116    return [self rectForTitleWithString: [self attributedTitleWithColor: nil]
117            basedOnMinimalStatusRect: [self minimalStatusRectForBounds: bounds] inBounds: bounds];
118}
119
120- (NSRect) minimalStatusRectForBounds: (NSRect) bounds
121{
122    Torrent * torrent = [self representedObject];
123    NSString * string = [torrent isActive] && ![fDefaults boolForKey: @"DisplaySmallStatusRegular"]
124                            ? [torrent remainingTimeString] : [torrent shortStatusString];
125    return [self rectForMinimalStatusWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
126}
127
128- (NSRect) progressRectForBounds: (NSRect) bounds
129{
130    return [self rectForProgressWithString: [self attributedStatusString: [[self representedObject] progressString] withColor: nil]
131                    inBounds: bounds];
132}
133
134- (NSRect) barRectForBounds: (NSRect) bounds
135{
136    BOOL minimal = [fDefaults boolForKey: @"SmallView"];
137   
138    NSRect result = bounds;
139    result.size.height = BAR_HEIGHT;
140    result.origin.x = PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_BAR;
141   
142    result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE;
143    if (minimal)
144        result.origin.y += PADDING_BETWEEN_TITLE_AND_BAR_MIN;
145    else
146        result.origin.y += PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
147   
148    result.size.width = round(NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL - BUTTONS_TOTAL_WIDTH);
149   
150    return result;
151}
152
153- (NSRect) statusRectForBounds: (NSRect) bounds
154{
155    return [self rectForStatusWithString: [self attributedStatusString: [[self representedObject] statusString] withColor: nil]
156                    inBounds: bounds];
157}
158
159- (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
160{
161    [super drawInteriorWithFrame: cellFrame inView: controlView];
162   
163    Torrent * torrent = [self representedObject];
164   
165    BOOL minimal = [fDefaults boolForKey: @"SmallView"];
166   
167    //error image
168    BOOL error = [torrent isError];
169    if (error && !fErrorImage)
170    {
171        fErrorImage = [[NSImage imageNamed: @"Error.png"] copy];
172        [fErrorImage setFlipped: YES];
173    }
174   
175    //icon
176    NSImage * icon = minimal && error ? fErrorImage : [torrent icon];
177    NSRect iconRect = [self iconRectForBounds: cellFrame];
178    [icon drawInRect: iconRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
179   
180    if (error && !minimal)
181    {
182        NSRect errorRect = NSMakeRect(NSMaxX(iconRect) - IMAGE_SIZE_MIN, NSMaxY(iconRect) - IMAGE_SIZE_MIN,
183                                        IMAGE_SIZE_MIN, IMAGE_SIZE_MIN);
184        [fErrorImage drawInRect: errorRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
185    }
186   
187    //text color
188    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
189                                                isEqual: [NSColor alternateSelectedControlColor]];
190    NSColor * titleColor, * statusColor;
191    if ([self isHighlighted]
192            && [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]])
193    {
194        titleColor = [NSColor whiteColor];
195        statusColor = [NSColor whiteColor];
196    }
197    else
198    {
199        titleColor = [NSColor controlTextColor];
200        statusColor = [NSColor darkGrayColor];
201    }
202   
203    //minimal status
204    NSRect minimalStatusRect;
205    if (minimal)
206    {
207        NSString * string = [torrent isActive] && ![fDefaults boolForKey: @"DisplaySmallStatusRegular"]
208                                ? [torrent remainingTimeString] : [torrent shortStatusString];
209        NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor];
210        minimalStatusRect = [self rectForMinimalStatusWithString: minimalString inBounds: cellFrame];
211       
212        [minimalString drawInRect: minimalStatusRect];
213    }
214   
215    //title
216    NSAttributedString * titleString = [self attributedTitleWithColor: titleColor];
217    NSRect titleRect = [self rectForTitleWithString: titleString basedOnMinimalStatusRect: minimalStatusRect inBounds: cellFrame];
218    [titleString drawInRect: titleRect];
219   
220    //progress
221    if (!minimal)
222    {
223        NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor];
224        NSRect progressRect = [self rectForProgressWithString: progressString inBounds: cellFrame];
225        [progressString drawInRect: progressRect];
226    }
227   
228    //bar
229    NSRect barRect = [self barRectForBounds: cellFrame];
230    [self drawBar: barRect];
231   
232    //status
233    if (!minimal)
234    {
235        NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor];
236        [statusString drawInRect: [self rectForStatusWithString: statusString inBounds: cellFrame]];
237    }
238}
239
240@end
241
242@implementation TorrentCell (Private)
243
244- (void) drawBar: (NSRect) barRect
245{
246    Torrent * torrent = [self representedObject];
247   
248    int leftWidth = barRect.size.width;
249    float progress = [torrent progress];
250   
251    if (progress < 1.0)
252    {
253        float rightProgress = 1.0 - progress, progressLeft = [torrent progressLeft];
254        int rightWidth = leftWidth * rightProgress;
255        leftWidth -= rightWidth;
256       
257        if (progressLeft < rightProgress)
258        {
259            int rightNoIncludeWidth = rightWidth * ((rightProgress - progressLeft) / rightProgress);
260            rightWidth -= rightNoIncludeWidth;
261           
262            NSRect noIncludeRect = barRect;
263            noIncludeRect.origin.x += barRect.size.width - rightNoIncludeWidth;
264            noIncludeRect.size.width = rightNoIncludeWidth;
265           
266            if (!fLightGrayGradient)
267                fLightGrayGradient = [[CTGradient progressLightGrayGradient] retain];
268            [fLightGrayGradient fillRect: noIncludeRect angle: -90];
269        }
270       
271        if (rightWidth > 0)
272        {
273            NSRect includeRect = barRect;
274            includeRect.origin.x += leftWidth;
275            includeRect.size.width = rightWidth;
276           
277            if (!fWhiteGradient)
278                fWhiteGradient = [[CTGradient progressWhiteGradient] retain];
279            [fWhiteGradient fillRect: includeRect angle: -90];
280        }
281    }
282   
283    if (leftWidth > 0)
284    {
285        NSRect completeRect = barRect;
286        completeRect.size.width = leftWidth;
287       
288        if ([torrent isActive])
289        {
290            if ([torrent isChecking])
291            {
292                if (!fYellowGradient)
293                    fYellowGradient = [[CTGradient progressYellowGradient] retain];
294                [fYellowGradient fillRect: completeRect angle: -90];
295            }
296            else if ([torrent isSeeding])
297            {
298                int ratioLeftWidth = leftWidth * (1.0 - [torrent progressStopRatio]);
299                leftWidth -= ratioLeftWidth;
300               
301                if (ratioLeftWidth > 0)
302                {
303                    NSRect ratioLeftRect = barRect;
304                    ratioLeftRect.origin.x += leftWidth;
305                    ratioLeftRect.size.width = ratioLeftWidth;
306                   
307                    if (!fLightGreenGradient)
308                        fLightGreenGradient = [[CTGradient progressLightGreenGradient] retain];
309                    [fLightGreenGradient fillRect: ratioLeftRect angle: -90];
310                }
311               
312                if (leftWidth > 0)
313                {
314                    completeRect.size.width = leftWidth;
315                   
316                    if (!fGreenGradient)
317                        fGreenGradient = [[CTGradient progressGreenGradient] retain];
318                    [fGreenGradient fillRect: completeRect angle: -90];
319                }
320            }
321            else
322            {
323                if (!fBlueGradient)
324                    fBlueGradient = [[CTGradient progressBlueGradient] retain];
325                [fBlueGradient fillRect: completeRect angle: -90];
326            }
327        }
328        else
329        {
330            if ([torrent waitingToStart])
331            {
332                if ([torrent progressLeft] <= 0.0)
333                {
334                    if (!fDarkGreenGradient)
335                        fDarkGreenGradient = [[CTGradient progressDarkGreenGradient] retain];
336                    [fDarkGreenGradient fillRect: completeRect angle: -90];
337                }
338                else
339                {
340                    if (!fDarkBlueGradient)
341                        fDarkBlueGradient = [[CTGradient progressDarkBlueGradient] retain];
342                    [fDarkBlueGradient fillRect: completeRect angle: -90];
343                }
344            }
345            else
346            {
347                if (!fGrayGradient)
348                    fGrayGradient = [[CTGradient progressGrayGradient] retain];
349                [fGrayGradient fillRect: completeRect angle: -90];
350            }
351        }
352    }
353   
354    [fBarOverlayColor set];
355    [NSBezierPath strokeRect: NSInsetRect(barRect, 0.5, 0.5)];
356}
357
358- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
359{
360    if (![fDefaults boolForKey: @"SmallView"])
361        return NSZeroRect;
362   
363    NSRect result = bounds;
364    result.size = [string size];
365   
366    result.origin.x += bounds.size.width - result.size.width - PADDING_HORIZONAL;
367    result.origin.y += PADDING_ABOVE_MIN_STATUS;
368   
369    return result;
370}
371
372- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect
373            inBounds: (NSRect) bounds
374{
375    BOOL minimal = [fDefaults boolForKey: @"SmallView"];
376   
377    NSRect result = bounds;
378    result.origin.y += PADDING_ABOVE_TITLE;
379    result.origin.x += PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE;
380   
381    result.size = [string size];
382    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL
383                            - (minimal ? PADDING_BETWEEN_TITLE_AND_MIN_STATUS + statusRect.size.width : 0));
384   
385    return result;
386}
387
388- (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
389{
390    if ([fDefaults boolForKey: @"SmallView"])
391        return NSZeroRect;
392   
393    NSRect result = bounds;
394    result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS;
395    result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
396   
397    result.size = [string size];
398    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
399   
400    return result;
401}
402
403- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
404{
405    if ([fDefaults boolForKey: @"SmallView"])
406        return NSZeroRect;
407   
408    NSRect result = bounds;
409    result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS
410                        + PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS;
411    result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
412   
413    result.size = [string size];
414    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
415   
416    return result;
417}
418
419- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
420{
421    if (color)
422        [fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
423   
424    NSString * title = [[self representedObject] name];
425    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
426}
427
428- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color
429{
430    if (color)
431        [fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
432   
433    return [[[NSAttributedString alloc] initWithString: string attributes: fStatusAttributes] autorelease];
434}
435
436@end
Note: See TracBrowser for help on using the repository browser.