source: trunk/macosx/TorrentCell.m @ 12444

Last change on this file since 12444 was 12444, checked in by livings124, 10 years ago

implement missing copyWithZone in TorrentCell?

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