source: trunk/macosx/TorrentCell.m @ 10403

Last change on this file since 10403 was 10403, checked in by livings124, 12 years ago

hide the progress bar when selecting row in compact view

  • Property svn:keywords set to Date Rev Author Id
File size: 31.4 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 10403 2010-03-20 05:16:03Z livings124 $
3 *
4 * Copyright (c) 2006-2010 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 "GroupsController.h"
27#import "NSApplicationAdditions.h"
28#import "NSStringAdditions.h"
29#import "ProgressGradients.h"
30#import "Torrent.h"
31#import "TorrentTableView.h"
32
33#define BAR_HEIGHT 12.0
34
35#define IMAGE_SIZE_REG 32.0
36#define IMAGE_SIZE_MIN 16.0
37#define ERROR_IMAGE_SIZE 20.0
38
39#define NORMAL_BUTTON_WIDTH 14.0
40#define ACTION_BUTTON_WIDTH 16.0
41
42#define PRIORITY_ICON_WIDTH 14.0
43#define PRIORITY_ICON_HEIGHT 14.0
44
45//ends up being larger than font height
46#define HEIGHT_TITLE 16.0
47#define HEIGHT_STATUS 12.0
48
49#define PADDING_HORIZONTAL 3.0
50#define PADDING_BETWEEN_IMAGE_AND_TITLE 5.0
51#define PADDING_BETWEEN_IMAGE_AND_BAR 7.0
52#define PADDING_BETWEEN_TITLE_AND_PRIORITY 4.0
53#define PADDING_ABOVE_TITLE 4.0
54#define PADDING_ABOVE_MIN_STATUS 6.0
55#define PADDING_BETWEEN_TITLE_AND_MIN_STATUS 2.0
56#define PADDING_BETWEEN_TITLE_AND_PROGRESS 1.0
57#define PADDING_BETWEEN_PROGRESS_AND_BAR 2.0
58#define PADDING_BETWEEN_TITLE_AND_BAR_MIN 3.0
59#define PADDING_BETWEEN_BAR_AND_STATUS 2.0
60
61#define PIECES_TOTAL_PERCENT 0.6
62
63#define MAX_PIECES (18*18)
64
65@interface TorrentCell (Private)
66
67- (void) drawBar: (NSRect) barRect;
68- (void) drawRegularBar: (NSRect) barRect;
69- (void) drawPiecesBar: (NSRect) barRect;
70
71- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
72- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect inBounds: (NSRect) bounds;
73- (NSRect) rectForProgressWithStringInBounds: (NSRect) bounds;
74- (NSRect) rectForStatusWithStringInBounds: (NSRect) bounds;
75- (NSRect) barRectForBounds: (NSRect) bounds;
76
77- (NSRect) controlButtonRectForBounds: (NSRect) bounds;
78- (NSRect) revealButtonRectForBounds: (NSRect) bounds;
79- (NSRect) actionButtonRectForBounds: (NSRect) bounds;
80
81- (NSAttributedString *) attributedTitle;
82- (NSAttributedString *) attributedStatusString: (NSString *) string;
83
84- (NSString *) buttonString;
85- (NSString *) statusString;
86- (NSString *) minimalStatusString;
87
88- (void) drawImage: (NSImage *) image inRect: (NSRect) rect; //use until 10.5 dropped
89
90@end
91
92@implementation TorrentCell
93
94//only called once and the main table is always needed, so don't worry about releasing
95- (id) init
96{
97    if ((self = [super init]))
98        {
99        fDefaults = [NSUserDefaults standardUserDefaults];
100       
101        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
102        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
103       
104        fTitleAttributes = [[NSMutableDictionary alloc] initWithCapacity: 3];
105        [fTitleAttributes setObject: [NSFont messageFontOfSize: 12.0] forKey: NSFontAttributeName];
106        [fTitleAttributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
107       
108        fStatusAttributes = [[NSMutableDictionary alloc] initWithCapacity: 3];
109        [fStatusAttributes setObject: [NSFont messageFontOfSize: 9.0] forKey: NSFontAttributeName];
110        [fStatusAttributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
111       
112        [paragraphStyle release];
113       
114        fBluePieceColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
115        #warning show border in compact view
116        fBarBorderColor = [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.2] retain];
117    }
118        return self;
119}
120
121- (NSRect) iconRectForBounds: (NSRect) bounds
122{
123    const CGFloat imageSize = [fDefaults boolForKey: @"SmallView"] ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG;
124   
125    return NSMakeRect(NSMinX(bounds) + PADDING_HORIZONTAL, floor(NSMidY(bounds) - imageSize * 0.5),
126                        imageSize, imageSize);
127}
128
129- (NSUInteger) hitTestForEvent: (NSEvent *) event inRect: (NSRect) cellFrame ofView: (NSView *) controlView
130{
131    NSPoint point = [controlView convertPoint: [event locationInWindow] fromView: nil];
132   
133    if (NSMouseInRect(point, [self controlButtonRectForBounds: cellFrame], [controlView isFlipped])
134        || NSMouseInRect(point, [self revealButtonRectForBounds: cellFrame], [controlView isFlipped]))
135        return NSCellHitContentArea | NSCellHitTrackableArea;
136   
137    return NSCellHitContentArea;
138}
139
140+ (BOOL) prefersTrackingUntilMouseUp
141{
142    return YES;
143}
144
145- (BOOL) trackMouse: (NSEvent *) event inRect: (NSRect) cellFrame ofView: (NSView *) controlView untilMouseUp: (BOOL) flag
146{
147    fTracking = YES;
148   
149    [self setControlView: controlView];
150   
151    NSPoint point = [controlView convertPoint: [event locationInWindow] fromView: nil];
152   
153    const NSRect controlRect= [self controlButtonRectForBounds: cellFrame];
154    const BOOL checkControl = NSMouseInRect(point, controlRect, [controlView isFlipped]);
155   
156    const NSRect revealRect = [self revealButtonRectForBounds: cellFrame];
157    const BOOL checkReveal = NSMouseInRect(point, revealRect, [controlView isFlipped]);
158   
159    [(TorrentTableView *)controlView removeTrackingAreas];
160   
161    while ([event type] != NSLeftMouseUp)
162    {
163        point = [controlView convertPoint: [event locationInWindow] fromView: nil];
164       
165        if (checkControl)
166        {
167            const BOOL inControlButton = NSMouseInRect(point, controlRect, [controlView isFlipped]);
168            if (fMouseDownControlButton != inControlButton)
169            {
170                fMouseDownControlButton = inControlButton;
171                [controlView setNeedsDisplayInRect: cellFrame];
172            }
173        }
174        else if (checkReveal)
175        {
176            const BOOL inRevealButton = NSMouseInRect(point, revealRect, [controlView isFlipped]);
177            if (fMouseDownRevealButton != inRevealButton)
178            {
179                fMouseDownRevealButton = inRevealButton;
180                [controlView setNeedsDisplayInRect: cellFrame];
181            }
182        }
183        else;
184       
185        //send events to where necessary
186        if ([event type] == NSMouseEntered || [event type] == NSMouseExited)
187            [NSApp sendEvent: event];
188        event = [[controlView window] nextEventMatchingMask:
189                    (NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSMouseEnteredMask | NSMouseExitedMask)];
190    }
191   
192    fTracking = NO;
193
194    if (fMouseDownControlButton)
195    {
196        fMouseDownControlButton = NO;
197       
198        [(TorrentTableView *)controlView toggleControlForTorrent: [self representedObject]];
199    }
200    else if (fMouseDownRevealButton)
201    {
202        fMouseDownRevealButton = NO;
203        [controlView setNeedsDisplayInRect: cellFrame];
204       
205        if ([NSApp isOnSnowLeopardOrBetter])
206        {
207            NSString * location = [[self representedObject] dataLocation];
208            if (location)
209            {
210                NSURL * file = [NSURL fileURLWithPath: location];
211                [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: [NSArray arrayWithObject: file]];
212            }
213        }
214        else
215        {
216            NSString * location = [[self representedObject] dataLocation];
217            if (location)
218                [[NSWorkspace sharedWorkspace] selectFile: location inFileViewerRootedAtPath: nil];
219        }
220    }
221    else;
222   
223    [controlView updateTrackingAreas];
224   
225    return YES;
226}
227
228- (void) addTrackingAreasForView: (NSView *) controlView inRect: (NSRect) cellFrame withUserInfo: (NSDictionary *) userInfo
229            mouseLocation: (NSPoint) mouseLocation
230{
231    NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
232   
233    //whole row
234    NSTrackingAreaOptions rowOptions = options;
235    if (NSMouseInRect(mouseLocation, cellFrame, [controlView isFlipped]))
236    {
237        rowOptions |= NSTrackingAssumeInside;
238        [(TorrentTableView *)controlView setRowHover: [[userInfo objectForKey: @"Row"] integerValue]];
239    }
240   
241    NSMutableDictionary * rowInfo = [userInfo mutableCopy];
242    [rowInfo setObject: @"Row" forKey: @"Type"];
243    NSTrackingArea * area = [[NSTrackingArea alloc] initWithRect: cellFrame options: rowOptions owner: controlView userInfo: rowInfo];
244    [controlView addTrackingArea: area];
245    [rowInfo release];
246    [area release];
247   
248    //control button
249    NSRect controlButtonRect = [self controlButtonRectForBounds: cellFrame];
250    NSTrackingAreaOptions controlOptions = options;
251    if (NSMouseInRect(mouseLocation, controlButtonRect, [controlView isFlipped]))
252    {
253        controlOptions |= NSTrackingAssumeInside;
254        [(TorrentTableView *)controlView setControlButtonHover: [[userInfo objectForKey: @"Row"] integerValue]];
255    }
256   
257    NSMutableDictionary * controlInfo = [userInfo mutableCopy];
258    [controlInfo setObject: @"Control" forKey: @"Type"];
259    area = [[NSTrackingArea alloc] initWithRect: controlButtonRect options: controlOptions owner: controlView
260                                userInfo: controlInfo];
261    [controlView addTrackingArea: area];
262    [controlInfo release];
263    [area release];
264   
265    //reveal button
266    NSRect revealButtonRect = [self revealButtonRectForBounds: cellFrame];
267    NSTrackingAreaOptions revealOptions = options;
268    if (NSMouseInRect(mouseLocation, revealButtonRect, [controlView isFlipped]))
269    {
270        revealOptions |= NSTrackingAssumeInside;
271        [(TorrentTableView *)controlView setRevealButtonHover: [[userInfo objectForKey: @"Row"] integerValue]];
272    }
273   
274    NSMutableDictionary * revealInfo = [userInfo mutableCopy];
275    [revealInfo setObject: @"Reveal" forKey: @"Type"];
276    area = [[NSTrackingArea alloc] initWithRect: revealButtonRect options: revealOptions owner: controlView userInfo: revealInfo];
277    [controlView addTrackingArea: area];
278    [revealInfo release];
279    [area release];
280   
281    //action button
282    NSRect actionButtonRect = [self iconRectForBounds: cellFrame]; //use the whole icon
283    NSTrackingAreaOptions actionOptions = options;
284    if (NSMouseInRect(mouseLocation, actionButtonRect, [controlView isFlipped]))
285    {
286        actionOptions |= NSTrackingAssumeInside;
287        [(TorrentTableView *)controlView setActionButtonHover: [[userInfo objectForKey: @"Row"] integerValue]];
288    }
289   
290    NSMutableDictionary * actionInfo = [userInfo mutableCopy];
291    [actionInfo setObject: @"Action" forKey: @"Type"];
292    area = [[NSTrackingArea alloc] initWithRect: actionButtonRect options: actionOptions owner: controlView userInfo: actionInfo];
293    [controlView addTrackingArea: area];
294    [actionInfo release];
295    [area release];
296}
297
298- (void) setHover: (BOOL) hover
299{
300    fHover = hover;
301}
302
303- (void) setControlHover: (BOOL) hover
304{
305    fHoverControl = hover;
306}
307
308- (void) setRevealHover: (BOOL) hover
309{
310    fHoverReveal = hover;
311}
312
313- (void) setActionHover: (BOOL) hover
314{
315    fHoverAction = hover;
316}
317
318- (void) setActionPushed: (BOOL) pushed
319{
320    fMouseDownActionButton = pushed;
321}
322
323- (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
324{
325    Torrent * torrent = [self representedObject];
326   
327    const BOOL minimal = [fDefaults boolForKey: @"SmallView"];
328   
329    //bar
330    if (!minimal || [self backgroundStyle] != NSBackgroundStyleDark)
331        [self drawBar: [self barRectForBounds: cellFrame]];
332   
333    //group coloring
334    const NSRect iconRect = [self iconRectForBounds: cellFrame];
335   
336    const NSInteger groupValue = [torrent groupValue];
337    if (groupValue != -1)
338    {
339        NSRect groupRect = NSInsetRect(iconRect, -1.0, -2.0);
340        if (!minimal)
341        {
342            groupRect.size.height -= 1.0;
343            groupRect.origin.y -= 1.0;
344        }
345        const CGFloat radius = minimal ? 3.0 : 6.0;
346       
347        NSColor * groupColor = [[GroupsController groups] colorForIndex: groupValue],
348                * darkGroupColor = [groupColor blendedColorWithFraction: 0.2 ofColor: [NSColor whiteColor]];
349       
350        //border
351        NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: groupRect xRadius: radius yRadius: radius];
352        [darkGroupColor set];
353        [bp setLineWidth: 2.0];
354        [bp stroke];
355       
356        //inside
357        bp = [NSBezierPath bezierPathWithRoundedRect: groupRect xRadius: radius yRadius: radius];
358        NSGradient * gradient = [[NSGradient alloc] initWithStartingColor: [groupColor blendedColorWithFraction: 0.7
359                                    ofColor: [NSColor whiteColor]] endingColor: darkGroupColor];
360        [gradient drawInBezierPath: bp angle: 90.0];
361        [gradient release];
362    }
363   
364    const BOOL error = [torrent isAnyErrorOrWarning];
365   
366    //icon
367    if (!minimal || !(!fTracking && fHoverAction)) //don't show in minimal mode when hovered over
368    {
369        NSImage * icon = (minimal && error) ? [NSImage imageNamed: [NSApp isOnSnowLeopardOrBetter] ? NSImageNameCaution : @"Error.png"]
370                                            : [torrent icon];
371        [self drawImage: icon inRect: iconRect];
372    }
373   
374    //error badge
375    if (error && !minimal)
376    {
377        NSRect errorRect = NSMakeRect(NSMaxX(iconRect) - ERROR_IMAGE_SIZE, NSMaxY(iconRect) - ERROR_IMAGE_SIZE,
378                                        ERROR_IMAGE_SIZE, ERROR_IMAGE_SIZE);
379        [self drawImage: [NSImage imageNamed: [NSApp isOnSnowLeopardOrBetter] ? NSImageNameCaution : @"Error.png"] inRect: errorRect];
380    }
381   
382    //text color
383    NSColor * titleColor, * statusColor;
384    if ([self backgroundStyle] == NSBackgroundStyleDark)
385        titleColor = statusColor = [NSColor whiteColor];
386    else
387    {
388        titleColor = [NSColor controlTextColor];
389        statusColor = [NSColor darkGrayColor];
390    }
391   
392    [fTitleAttributes setObject: titleColor forKey: NSForegroundColorAttributeName];
393    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
394   
395    //minimal status
396    NSRect minimalStatusRect;
397    if (minimal && !fHover)
398    {
399        NSAttributedString * minimalString = [self attributedStatusString: [self minimalStatusString]];
400        minimalStatusRect = [self rectForMinimalStatusWithString: minimalString inBounds: cellFrame];
401       
402        [minimalString drawInRect: minimalStatusRect];
403    }
404   
405    //title
406    NSAttributedString * titleString = [self attributedTitle];
407    NSRect titleRect = [self rectForTitleWithString: titleString basedOnMinimalStatusRect: minimalStatusRect inBounds: cellFrame];
408    [titleString drawInRect: titleRect];
409   
410    //priority icon
411    if ([torrent priority] != TR_PRI_NORMAL)
412    {
413        NSImage * priorityImage = [torrent priority] == TR_PRI_HIGH ? [NSImage imageNamed: @"PriorityHigh.png"]
414                                                                    : [NSImage imageNamed: @"PriorityLow.png"];
415        //take line out completely when 10.6-only
416        priorityImage = [NSApp isOnSnowLeopardOrBetter] ? [priorityImage retain] : [priorityImage copy];
417       
418        NSRect priorityRect = NSMakeRect(NSMaxX(titleRect) + PADDING_BETWEEN_TITLE_AND_PRIORITY,
419                                        NSMidY(titleRect) - PRIORITY_ICON_HEIGHT  * 0.5,
420                                        PRIORITY_ICON_WIDTH, PRIORITY_ICON_HEIGHT);
421       
422        [self drawImage: priorityImage inRect: priorityRect];
423        [priorityImage release];
424    }
425   
426    //progress
427    if (!minimal)
428    {
429        NSAttributedString * progressString = [self attributedStatusString: [torrent progressString]];
430        NSRect progressRect = [self rectForProgressWithStringInBounds: cellFrame];
431       
432        [progressString drawInRect: progressRect];
433    }
434   
435    if (!minimal || fHover)
436    {
437        //control button
438        NSString * controlImageSuffix;
439        if (fMouseDownControlButton)
440            controlImageSuffix = @"On.png";
441        else if (!fTracking && fHoverControl)
442            controlImageSuffix = @"Hover.png";
443        else
444            controlImageSuffix = @"Off.png";
445       
446        NSImage * controlImage;
447        if ([torrent isActive])
448            controlImage = [NSImage imageNamed: [@"Pause" stringByAppendingString: controlImageSuffix]];
449        else
450        {
451            if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
452                controlImage = [NSImage imageNamed: [@"ResumeNoWait" stringByAppendingString: controlImageSuffix]];
453            else if ([torrent waitingToStart])
454                controlImage = [NSImage imageNamed: [@"Pause" stringByAppendingString: controlImageSuffix]];
455            else
456                controlImage = [NSImage imageNamed: [@"Resume" stringByAppendingString: controlImageSuffix]];
457        }
458       
459        [self drawImage: controlImage inRect: [self controlButtonRectForBounds: cellFrame]];
460       
461        //reveal button
462        NSString * revealImageString;
463        if (fMouseDownRevealButton)
464            revealImageString = @"RevealOn.png";
465        else if (!fTracking && fHoverReveal)
466            revealImageString = @"RevealHover.png";
467        else
468            revealImageString = @"RevealOff.png";
469       
470        NSImage * revealImage = [NSImage imageNamed: revealImageString];
471        [self drawImage: revealImage inRect: [self revealButtonRectForBounds: cellFrame]];
472       
473        //action button
474        NSString * actionImageString;
475        if (fMouseDownActionButton)
476            actionImageString = @"ActionOn.png";
477        else if (!fTracking && fHoverAction)
478            actionImageString = @"ActionHover.png";
479        else
480            actionImageString = nil;
481       
482        if (actionImageString)
483        {
484            NSImage * actionImage = [NSImage imageNamed: actionImageString];
485            [self drawImage: actionImage inRect: [self actionButtonRectForBounds: cellFrame]];
486        }
487    }
488   
489    //status
490    if (!minimal)
491    {
492        NSAttributedString * statusString = [self attributedStatusString: [self statusString]];
493        [statusString drawInRect: [self rectForStatusWithStringInBounds: cellFrame]];
494    }
495}
496
497@end
498
499@implementation TorrentCell (Private)
500
501- (void) drawBar: (NSRect) barRect
502{
503    const BOOL minimal = [fDefaults boolForKey: @"SmallView"];
504   
505    const CGFloat piecesBarPercent = [(TorrentTableView *)[self controlView] piecesBarPercent];
506    if (piecesBarPercent > 0.0 && ([NSApp isOnSnowLeopardOrBetter] || !minimal))
507    {
508        NSRect piecesBarRect, regularBarRect;
509        NSDivideRect(barRect, &piecesBarRect, &regularBarRect, floor(NSHeight(barRect) * PIECES_TOTAL_PERCENT * piecesBarPercent),
510                    NSMaxYEdge);
511       
512        [self drawRegularBar: regularBarRect];
513        [self drawPiecesBar: piecesBarRect];
514    }
515    else
516    {
517        [[self representedObject] setPreviousFinishedPieces: nil];
518       
519        [self drawRegularBar: barRect];
520    }
521   
522    if (!minimal)
523    {
524        [fBarBorderColor set];
525        [NSBezierPath strokeRect: NSInsetRect(barRect, 0.5, 0.5)];
526    }
527}
528
529- (void) drawRegularBar: (NSRect) barRect
530{
531    Torrent * torrent = [self representedObject];
532   
533    NSRect haveRect, missingRect;
534    NSDivideRect(barRect, &haveRect, &missingRect, round([torrent progress] * NSWidth(barRect)), NSMinXEdge);
535   
536    if (!NSIsEmptyRect(haveRect))
537    {
538        if ([torrent isActive])
539        {
540            if ([torrent isChecking])
541                [[ProgressGradients progressYellowGradient] drawInRect: haveRect angle: 90];
542            else if ([torrent isSeeding])
543            {
544                NSRect ratioHaveRect, ratioRemainingRect;
545                NSDivideRect(haveRect, &ratioHaveRect, &ratioRemainingRect, round([torrent progressStopRatio] * NSWidth(haveRect)),
546                            NSMinXEdge);
547               
548                [[ProgressGradients progressGreenGradient] drawInRect: ratioHaveRect angle: 90];
549                [[ProgressGradients progressLightGreenGradient] drawInRect: ratioRemainingRect angle: 90];
550            }
551            else
552                [[ProgressGradients progressBlueGradient] drawInRect: haveRect angle: 90];
553        }
554        else
555        {
556            if ([torrent waitingToStart])
557            {
558                if ([torrent allDownloaded])
559                    [[ProgressGradients progressDarkGreenGradient] drawInRect: haveRect angle: 90];
560                else
561                    [[ProgressGradients progressDarkBlueGradient] drawInRect: haveRect angle: 90];
562            }
563            else
564                [[ProgressGradients progressGrayGradient] drawInRect: haveRect angle: 90];
565        }
566    }
567   
568    if (![torrent allDownloaded])
569    {
570        const CGFloat widthRemaining = round(NSWidth(barRect) * [torrent progressLeft]);
571       
572        NSRect wantedRect;
573        NSDivideRect(missingRect, &wantedRect, &missingRect, widthRemaining, NSMinXEdge);
574       
575        //not-available section
576        if ([torrent isActive] && ![torrent isChecking] && [torrent availableDesired] < 1.0
577            && [fDefaults boolForKey: @"DisplayProgressBarAvailable"])
578        {
579            NSRect unavailableRect;
580            NSDivideRect(wantedRect, &wantedRect, &unavailableRect, round(NSWidth(wantedRect) * [torrent availableDesired]),
581                        NSMinXEdge);
582           
583            [[ProgressGradients progressRedGradient] drawInRect: unavailableRect angle: 90];
584        }
585       
586        //remaining section
587        [[ProgressGradients progressWhiteGradient] drawInRect: wantedRect angle: 90];
588    }
589   
590    //unwanted section
591    if (!NSIsEmptyRect(missingRect))
592    {
593        if (![torrent isMagnet])
594            [[ProgressGradients progressLightGrayGradient] drawInRect: missingRect angle: 90];
595        else
596            [[ProgressGradients progressRedGradient] drawInRect: missingRect angle: 90];
597    }
598}
599
600- (void) drawPiecesBar: (NSRect) barRect
601{
602    Torrent * torrent = [self representedObject];
603   
604    //fill an all-white bar for magnet links
605    if ([torrent isMagnet])
606    {
607        [[NSColor whiteColor] set];
608        NSRectFill(barRect);
609        return;
610    }
611   
612    NSInteger pieceCount = MIN([torrent pieceCount], MAX_PIECES);
613    float * piecesPercent = malloc(pieceCount * sizeof(float));
614    [torrent getAmountFinished: piecesPercent size: pieceCount];
615   
616    NSBitmapImageRep * bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil
617                                    pixelsWide: pieceCount pixelsHigh: 1 bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES
618                                    isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
619   
620    NSIndexSet * previousFinishedIndexes = [torrent previousFinishedPieces];
621    NSMutableIndexSet * finishedIndexes = [NSMutableIndexSet indexSet];
622   
623    for (NSInteger i = 0; i < pieceCount; i++)
624    {
625        NSColor * pieceColor;
626        if (piecesPercent[i] == 1.0f)
627        {
628            if (previousFinishedIndexes && ![previousFinishedIndexes containsIndex: i])
629                pieceColor = [NSColor orangeColor];
630            else
631                pieceColor = fBluePieceColor;
632            [finishedIndexes addIndex: i];
633        }
634        else
635            pieceColor = [[NSColor whiteColor] blendedColorWithFraction: piecesPercent[i] ofColor: fBluePieceColor];
636       
637        //it's faster to just set color instead of checking previous color
638        [bitmap setColor: pieceColor atX: i y: 0];
639    }
640   
641    free(piecesPercent);
642   
643    [torrent setPreviousFinishedPieces: [finishedIndexes count] > 0 ? finishedIndexes : nil]; //don't bother saving if none are complete
644   
645    //actually draw image
646    if ([NSApp isOnSnowLeopardOrBetter])
647        [bitmap drawInRect: barRect fromRect: NSZeroRect operation: NSCompositeSourceOver
648            fraction: ([fDefaults boolForKey: @"SmallView"] ? 0.125 : 1.0) respectFlipped: YES hints: nil];
649    else
650        [bitmap drawInRect: barRect];
651
652    [bitmap release];
653}
654
655- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
656{
657    NSRect result;
658    result.size = [string size];
659   
660    result.origin.x = NSMaxX(bounds) - (NSWidth(result) + PADDING_HORIZONTAL);
661    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_MIN_STATUS;
662   
663    return result;
664}
665
666- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect inBounds: (NSRect) bounds
667{
668    const BOOL minimal = [fDefaults boolForKey: @"SmallView"];
669   
670    NSRect result;
671    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE;
672    result.origin.x = NSMinX(bounds) + PADDING_HORIZONTAL
673                        + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE;
674   
675    result.size.height = HEIGHT_TITLE;
676    result.size.width = NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONTAL;
677    if (minimal)
678        result.size.width -= PADDING_BETWEEN_TITLE_AND_MIN_STATUS + NSWidth(statusRect);
679    if ([[self representedObject] priority] != TR_PRI_NORMAL)
680    {
681        result.size.width -= PRIORITY_ICON_WIDTH + PADDING_BETWEEN_TITLE_AND_PRIORITY;
682        result.size.width = MIN(NSWidth(result), [string size].width); //only need to force it smaller for the priority icon
683    }
684   
685    return result;
686}
687
688- (NSRect) rectForProgressWithStringInBounds: (NSRect) bounds
689{
690    NSRect result;
691    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS;
692    result.origin.x = NSMinX(bounds) + PADDING_HORIZONTAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
693   
694    result.size.height = HEIGHT_STATUS;
695    result.size.width = NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONTAL;
696   
697    return result;
698}
699
700- (NSRect) rectForStatusWithStringInBounds: (NSRect) bounds
701{
702    NSRect result;
703    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS
704                        + PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS;
705    result.origin.x = NSMinX(bounds) + PADDING_HORIZONTAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
706   
707    result.size.height = HEIGHT_STATUS;
708    result.size.width = NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONTAL;
709   
710    return result;
711}
712
713- (NSRect) barRectForBounds: (NSRect) bounds
714{
715   
716    if ([fDefaults boolForKey: @"SmallView"])
717        return NSInsetRect(bounds, 2.0, 2.0);
718    else
719    {
720        NSRect result;
721        result.size.height = BAR_HEIGHT;
722       
723        result.origin.x = NSMinX(bounds) + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_BAR;
724        result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS
725                            + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
726       
727        result.size.width = floor(NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONTAL
728                            - 2.0 * (PADDING_HORIZONTAL + NORMAL_BUTTON_WIDTH));
729       
730        return result;
731    }
732}
733
734- (NSRect) controlButtonRectForBounds: (NSRect) bounds
735{
736    NSRect result;
737    result.size.height = NORMAL_BUTTON_WIDTH;
738    result.size.width = NORMAL_BUTTON_WIDTH;
739    result.origin.x = NSMaxX(bounds) - 2.0 * (PADDING_HORIZONTAL + NORMAL_BUTTON_WIDTH);
740   
741    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE;
742    if (![fDefaults boolForKey: @"SmallView"])
743        result.origin.y += HEIGHT_TITLE - (NORMAL_BUTTON_WIDTH - BAR_HEIGHT) * 0.5
744                            + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
745   
746    return result;
747}
748
749- (NSRect) revealButtonRectForBounds: (NSRect) bounds
750{
751    NSRect result;
752    result.size.height = NORMAL_BUTTON_WIDTH;
753    result.size.width = NORMAL_BUTTON_WIDTH;
754    result.origin.x = NSMaxX(bounds) - (PADDING_HORIZONTAL + NORMAL_BUTTON_WIDTH);
755   
756    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE;
757    if (![fDefaults boolForKey: @"SmallView"])
758        result.origin.y += HEIGHT_TITLE - (NORMAL_BUTTON_WIDTH - BAR_HEIGHT) * 0.5
759                            + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
760   
761    return result;
762}
763
764- (NSRect) actionButtonRectForBounds: (NSRect) bounds
765{
766    const NSRect iconRect = [self iconRectForBounds: bounds];
767   
768    //in minimal view the rect will be the icon rect, but avoid the extra defaults lookup with some cheap math
769    return NSMakeRect(NSMidX(iconRect) - ACTION_BUTTON_WIDTH * 0.5, NSMidY(iconRect) - ACTION_BUTTON_WIDTH * 0.5,
770                        ACTION_BUTTON_WIDTH, ACTION_BUTTON_WIDTH);
771}
772
773- (NSAttributedString *) attributedTitle
774{
775    NSString * title = [[self representedObject] name];
776    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
777}
778
779- (NSAttributedString *) attributedStatusString: (NSString *) string
780{
781    return [[[NSAttributedString alloc] initWithString: string attributes: fStatusAttributes] autorelease];
782}
783
784- (NSString *) buttonString
785{
786    if (fMouseDownRevealButton || (!fTracking && fHoverReveal))
787        return NSLocalizedString(@"Show the data file in Finder", "Torrent cell -> button info");
788    else if (fMouseDownControlButton || (!fTracking && fHoverControl))
789    {
790        Torrent * torrent = [self representedObject];
791        if ([torrent isActive])
792            return NSLocalizedString(@"Pause the transfer", "Torrent Table -> tooltip");
793        else
794        {
795            if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
796                return NSLocalizedString(@"Resume the transfer right away", "Torrent cell -> button info");
797            else if ([torrent waitingToStart])
798                return NSLocalizedString(@"Stop waiting to start", "Torrent cell -> button info");
799            else
800                return NSLocalizedString(@"Resume the transfer", "Torrent cell -> button info");
801        }
802    }
803    else if (!fTracking && fHoverAction)
804        return NSLocalizedString(@"Change transfer settings", "Torrent Table -> tooltip");
805    else
806        return nil;
807}
808
809- (NSString *) statusString
810{
811    NSString * buttonString;
812    if ((buttonString = [self buttonString]))
813        return buttonString;
814    else
815        return [[self representedObject] statusString];
816}
817
818- (NSString *) minimalStatusString
819{
820    NSString * buttonString;
821    if ((buttonString = [self buttonString]))
822        return buttonString;
823    else
824    {
825        Torrent * torrent = [self representedObject];
826        return [fDefaults boolForKey: @"DisplaySmallStatusRegular"] ? [torrent shortStatusString] : [torrent remainingTimeString];
827    }
828}
829
830- (void) drawImage: (NSImage *) image inRect: (NSRect) rect
831{
832    if ([NSApp isOnSnowLeopardOrBetter])
833        [image drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0 respectFlipped: YES hints: nil];
834    else
835    {
836        [image setFlipped: YES];
837        [image drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
838    }
839}
840
841@end
Note: See TracBrowser for help on using the repository browser.