1
/******************************************************************************
2
* $Id: TorrentCell.m 4423 2008-01-02 16:55:05Z livings124 $
4
* Copyright (c) 2006-2008 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
25
#import "TorrentCell.h"
26
#import "TorrentTableView.h"
27
#import "GroupsWindowController.h"
28
#import "NSApplicationAdditions.h"
29
#import "NSStringAdditions.h"
30
#import "NSBezierPathAdditions.h"
31
#import "CTGradientAdditions.h"
33
#define BAR_HEIGHT 12.0
35
#define IMAGE_SIZE_REG 32.0
36
#define IMAGE_SIZE_MIN 16.0
38
//end up being larger than font height
39
#define HEIGHT_TITLE 16.0
40
#define HEIGHT_STATUS 12.0
42
#define PADDING_HORIZONAL 2.0
43
#define PADDING_ABOVE_IMAGE_REG 9.0
44
#define PADDING_BETWEEN_IMAGE_AND_TITLE 5.0
45
#define PADDING_BETWEEN_IMAGE_AND_BAR 7.0
46
#define PADDING_ABOVE_TITLE 3.0
47
#define PADDING_ABOVE_MIN_STATUS 4.0
48
#define PADDING_BETWEEN_TITLE_AND_MIN_STATUS 2.0
49
#define PADDING_BETWEEN_TITLE_AND_PROGRESS 1.0
50
#define PADDING_BETWEEN_PROGRESS_AND_BAR 2.0
51
#define PADDING_BETWEEN_TITLE_AND_BAR_MIN 3.0
52
#define PADDING_BETWEEN_BAR_AND_STATUS 2.0
54
#define WIDTH_GROUP 40.0
55
#define WIDTH_GROUP_MIN 24.0
57
#define MAX_PIECES 324
59
@interface TorrentCell (Private)
61
- (void) drawBar: (NSRect) barRect;
62
- (void) drawRegularBar: (NSRect) barRect;
63
- (void) drawPiecesBar: (NSRect) barRect;
65
- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
66
- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect
67
inBounds: (NSRect) bounds;
68
- (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
69
- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
71
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
72
- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color;
76
@implementation TorrentCell
78
//only called once, so don't worry about releasing
81
if ((self = [super init]))
83
fDefaults = [NSUserDefaults standardUserDefaults];
85
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
86
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
88
fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
89
[NSFont messageFontOfSize: 12.0], NSFontAttributeName,
90
paragraphStyle, NSParagraphStyleAttributeName, nil];
91
fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
92
[NSFont messageFontOfSize: 9.0], NSFontAttributeName,
93
paragraphStyle, NSParagraphStyleAttributeName, nil];
94
[paragraphStyle release];
97
fGrayColor = [[NSColor colorWithCalibratedRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0] retain];
98
fBlue1Color = [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] retain];
99
fBlue2Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] retain];
100
fBlue3Color = [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] retain];
101
fBlue4Color = [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] retain];
102
fBlueColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
103
fOrangeColor = [[NSColor orangeColor] retain];
105
fBarOverlayColor = [[NSColor colorWithDeviceWhite: 0.0 alpha: 0.2] retain];
110
- (id) copyWithZone: (NSZone *) zone
112
TorrentCell * copy = [super copyWithZone: zone];
124
- (NSRect) iconRectForBounds: (NSRect) bounds
126
NSRect result = bounds;
128
result.origin.x += PADDING_HORIZONAL;
131
if ([fDefaults boolForKey: @"SmallView"])
133
imageSize = IMAGE_SIZE_MIN;
134
result.origin.y += (result.size.height - imageSize) * 0.5;
138
imageSize = IMAGE_SIZE_REG;
139
result.origin.y += PADDING_ABOVE_IMAGE_REG;
142
result.size = NSMakeSize(imageSize, imageSize);
147
- (NSRect) titleRectForBounds: (NSRect) bounds
149
return [self rectForTitleWithString: [self attributedTitleWithColor: nil]
150
basedOnMinimalStatusRect: [self minimalStatusRectForBounds: bounds] inBounds: bounds];
153
- (NSRect) minimalStatusRectForBounds: (NSRect) bounds
155
Torrent * torrent = [self representedObject];
156
NSString * string = [fDefaults boolForKey: @"DisplaySmallStatusRegular"]
157
? [torrent shortStatusString] : [torrent remainingTimeString];
158
return [self rectForMinimalStatusWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
161
- (NSRect) progressRectForBounds: (NSRect) bounds
163
return [self rectForProgressWithString: [self attributedStatusString: [[self representedObject] progressString] withColor: nil]
167
- (NSRect) barRectForBounds: (NSRect) bounds
169
BOOL minimal = [fDefaults boolForKey: @"SmallView"];
171
NSRect result = bounds;
172
result.size.height = BAR_HEIGHT;
173
result.origin.x = PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_BAR;
175
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE;
177
result.origin.y += PADDING_BETWEEN_TITLE_AND_BAR_MIN;
179
result.origin.y += PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
181
result.size.width = round(NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL - BUTTONS_TOTAL_WIDTH);
186
- (NSRect) statusRectForBounds: (NSRect) bounds
188
return [self rectForStatusWithString: [self attributedStatusString: [[self representedObject] statusString] withColor: nil]
192
- (NSUInteger) hitTestForEvent: (NSEvent *) event inRect: (NSRect) cellFrame ofView: (NSView *) controlView
194
return NSCellHitContentArea;
197
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
199
[super drawWithFrame: cellFrame inView: controlView];
201
Torrent * torrent = [self representedObject];
203
BOOL minimal = [fDefaults boolForKey: @"SmallView"];
206
NSRect iconRect = [self iconRectForBounds: cellFrame];
208
int groupValue = [torrent groupValue];
209
if (groupValue != -1)
211
NSRect groupRect = NSInsetRect(iconRect, -2.0, -3.0);
214
groupRect.size.height--;
215
groupRect.origin.y--;
218
[[[GroupsWindowController groups] gradientForIndex: groupValue] fillBezierPath:
219
[NSBezierPath bezierPathWithRoundedRect: groupRect radius: 6.0] angle: 90.0];
223
BOOL error = [torrent isError];
224
if (error && !fErrorImage)
226
fErrorImage = [[NSImage imageNamed: @"Error.png"] copy];
227
[fErrorImage setFlipped: YES];
231
NSImage * icon = minimal && error ? fErrorImage : [torrent icon];
232
[icon drawInRect: iconRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
234
if (error && !minimal)
236
NSRect errorRect = NSMakeRect(NSMaxX(iconRect) - IMAGE_SIZE_MIN, NSMaxY(iconRect) - IMAGE_SIZE_MIN,
237
IMAGE_SIZE_MIN, IMAGE_SIZE_MIN);
238
[fErrorImage drawInRect: errorRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
242
NSColor * titleColor, * statusColor;
243
if ([NSApp isOnLeopardOrBetter] ? [self backgroundStyle] == NSBackgroundStyleDark : [self isHighlighted]
244
&& [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]])
246
titleColor = [NSColor whiteColor];
247
statusColor = [NSColor whiteColor];
251
titleColor = [NSColor controlTextColor];
252
statusColor = [NSColor darkGrayColor];
256
NSRect minimalStatusRect;
259
NSString * string = [fDefaults boolForKey: @"DisplaySmallStatusRegular"]
260
? [torrent shortStatusString] : [torrent remainingTimeString];
261
NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor];
262
minimalStatusRect = [self rectForMinimalStatusWithString: minimalString inBounds: cellFrame];
264
[minimalString drawInRect: minimalStatusRect];
268
NSAttributedString * titleString = [self attributedTitleWithColor: titleColor];
269
NSRect titleRect = [self rectForTitleWithString: titleString basedOnMinimalStatusRect: minimalStatusRect inBounds: cellFrame];
270
[titleString drawInRect: titleRect];
275
NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor];
276
NSRect progressRect = [self rectForProgressWithString: progressString inBounds: cellFrame];
277
[progressString drawInRect: progressRect];
281
[self drawBar: [self barRectForBounds: cellFrame]];
286
NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor];
287
[statusString drawInRect: [self rectForStatusWithString: statusString inBounds: cellFrame]];
293
@implementation TorrentCell (Private)
295
- (void) drawBar: (NSRect) barRect
297
if ([fDefaults boolForKey: @"PiecesBar"])
299
NSRect regularBarRect = barRect, piecesBarRect = barRect;
300
regularBarRect.size.height /= 2.5;
301
piecesBarRect.origin.y += regularBarRect.size.height;
302
piecesBarRect.size.height -= regularBarRect.size.height;
304
[self drawRegularBar: regularBarRect];
305
[self drawPiecesBar: piecesBarRect];
311
[[self representedObject] setPreviousAmountFinished: NULL];
313
[self drawRegularBar: barRect];
316
[fBarOverlayColor set];
317
[NSBezierPath strokeRect: NSInsetRect(barRect, 0.5, 0.5)];
320
- (void) drawRegularBar: (NSRect) barRect
322
Torrent * torrent = [self representedObject];
324
int leftWidth = barRect.size.width;
325
float progress = [torrent progress];
329
float rightProgress = 1.0 - progress, progressLeft = [torrent progressLeft];
330
int rightWidth = leftWidth * rightProgress;
331
leftWidth -= rightWidth;
333
if (progressLeft < rightProgress)
335
int rightNoIncludeWidth = rightWidth * ((rightProgress - progressLeft) / rightProgress);
336
rightWidth -= rightNoIncludeWidth;
338
NSRect noIncludeRect = barRect;
339
noIncludeRect.origin.x += barRect.size.width - rightNoIncludeWidth;
340
noIncludeRect.size.width = rightNoIncludeWidth;
342
if (!fLightGrayGradient)
343
fLightGrayGradient = [[CTGradient progressLightGrayGradient] retain];
344
[fLightGrayGradient fillRect: noIncludeRect angle: -90];
349
if ([torrent isActive] && ![torrent allDownloaded] && [fDefaults boolForKey: @"DisplayProgressBarAvailable"])
351
int notAvailableWidth = MIN(ceil(barRect.size.width * [torrent notAvailableDesired]), rightWidth);
352
rightWidth -= notAvailableWidth;
354
if (notAvailableWidth > 0)
356
NSRect notAvailableRect = barRect;
357
notAvailableRect.origin.x += leftWidth + rightWidth;
358
notAvailableRect.size.width = notAvailableWidth;
361
fRedGradient = [[CTGradient progressRedGradient] retain];
362
[fRedGradient fillRect: notAvailableRect angle: -90];
368
NSRect includeRect = barRect;
369
includeRect.origin.x += leftWidth;
370
includeRect.size.width = rightWidth;
373
fWhiteGradient = [[CTGradient progressWhiteGradient] retain];
374
[fWhiteGradient fillRect: includeRect angle: -90];
381
NSRect completeRect = barRect;
382
completeRect.size.width = leftWidth;
384
if ([torrent isActive])
386
if ([torrent isChecking])
388
if (!fYellowGradient)
389
fYellowGradient = [[CTGradient progressYellowGradient] retain];
390
[fYellowGradient fillRect: completeRect angle: -90];
392
else if ([torrent isSeeding])
394
int ratioLeftWidth = leftWidth * (1.0 - [torrent progressStopRatio]);
395
leftWidth -= ratioLeftWidth;
397
if (ratioLeftWidth > 0)
399
NSRect ratioLeftRect = barRect;
400
ratioLeftRect.origin.x += leftWidth;
401
ratioLeftRect.size.width = ratioLeftWidth;
403
if (!fLightGreenGradient)
404
fLightGreenGradient = [[CTGradient progressLightGreenGradient] retain];
405
[fLightGreenGradient fillRect: ratioLeftRect angle: -90];
410
completeRect.size.width = leftWidth;
413
fGreenGradient = [[CTGradient progressGreenGradient] retain];
414
[fGreenGradient fillRect: completeRect angle: -90];
420
fBlueGradient = [[CTGradient progressBlueGradient] retain];
421
[fBlueGradient fillRect: completeRect angle: -90];
426
if ([torrent waitingToStart])
428
if ([torrent progressLeft] <= 0.0)
430
if (!fDarkGreenGradient)
431
fDarkGreenGradient = [[CTGradient progressDarkGreenGradient] retain];
432
[fDarkGreenGradient fillRect: completeRect angle: -90];
436
if (!fDarkBlueGradient)
437
fDarkBlueGradient = [[CTGradient progressDarkBlueGradient] retain];
438
[fDarkBlueGradient fillRect: completeRect angle: -90];
444
fGrayGradient = [[CTGradient progressGrayGradient] retain];
445
[fGrayGradient fillRect: completeRect angle: -90];
451
- (void) drawPiecesBar: (NSRect) barRect
454
fBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil
455
pixelsWide: MAX_PIECES pixelsHigh: barRect.size.height bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES
456
isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
458
Torrent * torrent = [self representedObject];
460
int pieceCount = MIN([torrent pieceCount], MAX_PIECES);
461
float * piecePercent = malloc(pieceCount * sizeof(float)),
462
* previousPiecePercent = [torrent getPreviousAmountFinished];
463
[torrent getAmountFinished: piecePercent size: pieceCount];
466
float increment = (float)pieceCount / MAX_PIECES;
467
NSColor * pieceColor;
468
for (i = 0; i < MAX_PIECES; i++)
470
index = i * increment;
471
if (piecePercent[index] >= 1.0)
473
if (previousPiecePercent != NULL && previousPiecePercent[index] < 1.0)
474
pieceColor = fOrangeColor;
476
pieceColor = fBlueColor;
478
else if (piecePercent[index] <= 0.0)
479
pieceColor = fGrayColor;
480
else if (piecePercent[index] <= 0.25)
481
pieceColor = fBlue1Color;
482
else if (piecePercent[index] <= 0.5)
483
pieceColor = fBlue2Color;
484
else if (piecePercent[index] <= 0.75)
485
pieceColor = fBlue3Color;
487
pieceColor = fBlue4Color;
489
if (![pieceColor isEqual: [fBitmap colorAtX: i y: 0]])
490
for (h = 0; h < barRect.size.height; h++)
491
[fBitmap setColor: pieceColor atX: i y: h];
494
[torrent setPreviousAmountFinished: piecePercent];
496
//actually draw image
497
[fBitmap drawInRect: barRect];
499
if (!fTransparentGradient)
500
fTransparentGradient = [[CTGradient progressTransparentGradient] retain];
501
[fTransparentGradient fillRect: barRect angle: -90];
504
- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
506
if (![fDefaults boolForKey: @"SmallView"])
509
NSRect result = bounds;
510
result.size = [string size];
512
result.origin.x += bounds.size.width - result.size.width - PADDING_HORIZONAL;
513
result.origin.y += PADDING_ABOVE_MIN_STATUS;
518
- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect
519
inBounds: (NSRect) bounds
521
BOOL minimal = [fDefaults boolForKey: @"SmallView"];
523
NSRect result = bounds;
524
result.origin.y += PADDING_ABOVE_TITLE;
525
result.origin.x += PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE;
527
result.size = [string size];
528
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL
529
- (minimal ? PADDING_BETWEEN_TITLE_AND_MIN_STATUS + statusRect.size.width : 0));
534
- (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
536
if ([fDefaults boolForKey: @"SmallView"])
539
NSRect result = bounds;
540
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS;
541
result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
543
result.size = [string size];
544
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
549
- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
551
if ([fDefaults boolForKey: @"SmallView"])
554
NSRect result = bounds;
555
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS
556
+ PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS;
557
result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
559
result.size = [string size];
560
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
565
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
568
[fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
570
NSString * title = [[self representedObject] name];
571
return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
574
- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color
577
[fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
579
return [[[NSAttributedString alloc] initWithString: string attributes: fStatusAttributes] autorelease];