1 | /****************************************************************************** |
---|
2 | * $Id: TorrentCell.m 3719 2007-11-05 19:15:45Z 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 | #define MAX_PIECES 324 |
---|
52 | #define BLANK_PIECE -99 |
---|
53 | |
---|
54 | @interface TorrentCell (Private) |
---|
55 | |
---|
56 | // Used to optimize drawing. They contain packed RGBA pixels for every color needed. |
---|
57 | static uint32_t kBlue = OSSwapBigToHostConstInt32(0x50A0FFFF), //80, 160, 255 |
---|
58 | kBlue1 = OSSwapBigToHostConstInt32(0x84FFFFFF), //204, 255, 255 |
---|
59 | kBlue2 = OSSwapBigToHostConstInt32(0x6BFFFFFF), //153, 255, 255 |
---|
60 | kBlue3 = OSSwapBigToHostConstInt32(0x6B84FFFF), //153, 204, 255 |
---|
61 | kBlue4 = OSSwapBigToHostConstInt32(0x426BFFFF), //102, 153, 255 |
---|
62 | kGray = OSSwapBigToHostConstInt32(0xE9E9E9FF); //100, 100, 100 |
---|
63 | |
---|
64 | - (void) drawBar: (NSRect) barRect; |
---|
65 | - (void) drawRegularBar: (NSRect) barRect; |
---|
66 | - (void) drawPiecesBar: (NSRect) barRect; |
---|
67 | |
---|
68 | - (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds; |
---|
69 | - (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect |
---|
70 | inBounds: (NSRect) bounds; |
---|
71 | - (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds; |
---|
72 | - (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds; |
---|
73 | |
---|
74 | - (NSAttributedString *) attributedTitleWithColor: (NSColor *) color; |
---|
75 | - (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color; |
---|
76 | |
---|
77 | @end |
---|
78 | |
---|
79 | @implementation TorrentCell |
---|
80 | |
---|
81 | //only called one, so don't worry about release |
---|
82 | - (id) init |
---|
83 | { |
---|
84 | if ((self = [super init])) |
---|
85 | { |
---|
86 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
87 | |
---|
88 | NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
---|
89 | [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail]; |
---|
90 | |
---|
91 | fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
---|
92 | [NSFont messageFontOfSize: 12.0], NSFontAttributeName, |
---|
93 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
94 | fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
---|
95 | [NSFont messageFontOfSize: 9.0], NSFontAttributeName, |
---|
96 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
97 | [paragraphStyle release]; |
---|
98 | |
---|
99 | fBarOverlayColor = [[NSColor colorWithDeviceWhite: 0.0 alpha: 0.2] retain]; |
---|
100 | } |
---|
101 | return self; |
---|
102 | } |
---|
103 | |
---|
104 | - (id) copyWithZone: (NSZone *) zone |
---|
105 | { |
---|
106 | TorrentCell * copy = [super copyWithZone: zone]; |
---|
107 | |
---|
108 | copy->fBitmap = nil; |
---|
109 | copy->fPieces = NULL; |
---|
110 | |
---|
111 | return copy; |
---|
112 | } |
---|
113 | |
---|
114 | - (void) dealloc |
---|
115 | { |
---|
116 | [fBitmap release]; |
---|
117 | if (fPieces) |
---|
118 | free(fPieces); |
---|
119 | |
---|
120 | [super dealloc]; |
---|
121 | } |
---|
122 | |
---|
123 | - (NSRect) iconRectForBounds: (NSRect) bounds |
---|
124 | { |
---|
125 | NSRect result = bounds; |
---|
126 | |
---|
127 | result.origin.x += PADDING_HORIZONAL; |
---|
128 | |
---|
129 | float imageSize; |
---|
130 | if ([fDefaults boolForKey: @"SmallView"]) |
---|
131 | { |
---|
132 | imageSize = IMAGE_SIZE_MIN; |
---|
133 | result.origin.y += (result.size.height - imageSize) * 0.5; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | imageSize = IMAGE_SIZE_REG; |
---|
138 | result.origin.y += PADDING_ABOVE_IMAGE_REG; |
---|
139 | } |
---|
140 | |
---|
141 | result.size = NSMakeSize(imageSize, imageSize); |
---|
142 | |
---|
143 | return result; |
---|
144 | } |
---|
145 | |
---|
146 | - (NSRect) titleRectForBounds: (NSRect) bounds |
---|
147 | { |
---|
148 | return [self rectForTitleWithString: [self attributedTitleWithColor: nil] |
---|
149 | basedOnMinimalStatusRect: [self minimalStatusRectForBounds: bounds] inBounds: bounds]; |
---|
150 | } |
---|
151 | |
---|
152 | - (NSRect) minimalStatusRectForBounds: (NSRect) bounds |
---|
153 | { |
---|
154 | Torrent * torrent = [self representedObject]; |
---|
155 | NSString * string = [fDefaults boolForKey: @"DisplaySmallStatusRegular"] |
---|
156 | ? [torrent shortStatusString] : [torrent remainingTimeString]; |
---|
157 | return [self rectForMinimalStatusWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds]; |
---|
158 | } |
---|
159 | |
---|
160 | - (NSRect) progressRectForBounds: (NSRect) bounds |
---|
161 | { |
---|
162 | return [self rectForProgressWithString: [self attributedStatusString: [[self representedObject] progressString] withColor: nil] |
---|
163 | inBounds: bounds]; |
---|
164 | } |
---|
165 | |
---|
166 | - (NSRect) barRectForBounds: (NSRect) bounds |
---|
167 | { |
---|
168 | BOOL minimal = [fDefaults boolForKey: @"SmallView"]; |
---|
169 | |
---|
170 | NSRect result = bounds; |
---|
171 | result.size.height = BAR_HEIGHT; |
---|
172 | result.origin.x = PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_BAR; |
---|
173 | |
---|
174 | result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE; |
---|
175 | if (minimal) |
---|
176 | result.origin.y += PADDING_BETWEEN_TITLE_AND_BAR_MIN; |
---|
177 | else |
---|
178 | result.origin.y += PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR; |
---|
179 | |
---|
180 | result.size.width = round(NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL - BUTTONS_TOTAL_WIDTH); |
---|
181 | |
---|
182 | return result; |
---|
183 | } |
---|
184 | |
---|
185 | - (NSRect) statusRectForBounds: (NSRect) bounds |
---|
186 | { |
---|
187 | return [self rectForStatusWithString: [self attributedStatusString: [[self representedObject] statusString] withColor: nil] |
---|
188 | inBounds: bounds]; |
---|
189 | } |
---|
190 | |
---|
191 | - (NSUInteger) hitTestForEvent: (NSEvent *) event inRect: (NSRect) cellFrame ofView: (NSView *) controlView |
---|
192 | { |
---|
193 | return NSCellHitContentArea; |
---|
194 | } |
---|
195 | |
---|
196 | - (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView |
---|
197 | { |
---|
198 | [super drawInteriorWithFrame: cellFrame inView: controlView]; |
---|
199 | |
---|
200 | Torrent * torrent = [self representedObject]; |
---|
201 | |
---|
202 | BOOL minimal = [fDefaults boolForKey: @"SmallView"]; |
---|
203 | |
---|
204 | //error image |
---|
205 | BOOL error = [torrent isError]; |
---|
206 | if (error && !fErrorImage) |
---|
207 | { |
---|
208 | fErrorImage = [[NSImage imageNamed: @"Error.png"] copy]; |
---|
209 | [fErrorImage setFlipped: YES]; |
---|
210 | } |
---|
211 | |
---|
212 | //icon |
---|
213 | NSImage * icon = minimal && error ? fErrorImage : [torrent icon]; |
---|
214 | NSRect iconRect = [self iconRectForBounds: cellFrame]; |
---|
215 | [icon drawInRect: iconRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; |
---|
216 | |
---|
217 | if (error && !minimal) |
---|
218 | { |
---|
219 | NSRect errorRect = NSMakeRect(NSMaxX(iconRect) - IMAGE_SIZE_MIN, NSMaxY(iconRect) - IMAGE_SIZE_MIN, |
---|
220 | IMAGE_SIZE_MIN, IMAGE_SIZE_MIN); |
---|
221 | [fErrorImage drawInRect: errorRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; |
---|
222 | } |
---|
223 | |
---|
224 | //text color |
---|
225 | NSColor * titleColor, * statusColor; |
---|
226 | if ([self isHighlighted] |
---|
227 | && [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]]) |
---|
228 | { |
---|
229 | titleColor = [NSColor whiteColor]; |
---|
230 | statusColor = [NSColor whiteColor]; |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | titleColor = [NSColor controlTextColor]; |
---|
235 | statusColor = [NSColor darkGrayColor]; |
---|
236 | } |
---|
237 | |
---|
238 | //minimal status |
---|
239 | NSRect minimalStatusRect; |
---|
240 | if (minimal) |
---|
241 | { |
---|
242 | NSString * string = [fDefaults boolForKey: @"DisplaySmallStatusRegular"] |
---|
243 | ? [torrent shortStatusString] : [torrent remainingTimeString]; |
---|
244 | NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor]; |
---|
245 | minimalStatusRect = [self rectForMinimalStatusWithString: minimalString inBounds: cellFrame]; |
---|
246 | |
---|
247 | [minimalString drawInRect: minimalStatusRect]; |
---|
248 | } |
---|
249 | |
---|
250 | //title |
---|
251 | NSAttributedString * titleString = [self attributedTitleWithColor: titleColor]; |
---|
252 | NSRect titleRect = [self rectForTitleWithString: titleString basedOnMinimalStatusRect: minimalStatusRect inBounds: cellFrame]; |
---|
253 | [titleString drawInRect: titleRect]; |
---|
254 | |
---|
255 | //progress |
---|
256 | if (!minimal) |
---|
257 | { |
---|
258 | NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor]; |
---|
259 | NSRect progressRect = [self rectForProgressWithString: progressString inBounds: cellFrame]; |
---|
260 | [progressString drawInRect: progressRect]; |
---|
261 | } |
---|
262 | |
---|
263 | //bar |
---|
264 | [self drawBar: [self barRectForBounds: cellFrame]]; |
---|
265 | |
---|
266 | //status |
---|
267 | if (!minimal) |
---|
268 | { |
---|
269 | NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor]; |
---|
270 | [statusString drawInRect: [self rectForStatusWithString: statusString inBounds: cellFrame]]; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | @end |
---|
275 | |
---|
276 | @implementation TorrentCell (Private) |
---|
277 | |
---|
278 | - (void) drawBar: (NSRect) barRect |
---|
279 | { |
---|
280 | if ([fDefaults boolForKey: @"PiecesBar"]) |
---|
281 | { |
---|
282 | NSRect regularBarRect = barRect, piecesBarRect = barRect; |
---|
283 | regularBarRect.size.height /= 3; |
---|
284 | piecesBarRect.origin.y += regularBarRect.size.height; |
---|
285 | piecesBarRect.size.height -= regularBarRect.size.height; |
---|
286 | |
---|
287 | [self drawRegularBar: regularBarRect]; |
---|
288 | [self drawPiecesBar: piecesBarRect]; |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | if (fPieces) |
---|
293 | { |
---|
294 | free(fPieces); |
---|
295 | fPieces = NULL; |
---|
296 | [fBitmap release]; |
---|
297 | fBitmap = nil; |
---|
298 | } |
---|
299 | |
---|
300 | [self drawRegularBar: barRect]; |
---|
301 | } |
---|
302 | |
---|
303 | [fBarOverlayColor set]; |
---|
304 | [NSBezierPath strokeRect: NSInsetRect(barRect, 0.5, 0.5)]; |
---|
305 | } |
---|
306 | |
---|
307 | - (void) drawRegularBar: (NSRect) barRect |
---|
308 | { |
---|
309 | Torrent * torrent = [self representedObject]; |
---|
310 | |
---|
311 | int leftWidth = barRect.size.width; |
---|
312 | float progress = [torrent progress]; |
---|
313 | |
---|
314 | if (progress < 1.0) |
---|
315 | { |
---|
316 | float rightProgress = 1.0 - progress, progressLeft = [torrent progressLeft]; |
---|
317 | int rightWidth = leftWidth * rightProgress; |
---|
318 | leftWidth -= rightWidth; |
---|
319 | |
---|
320 | if (progressLeft < rightProgress) |
---|
321 | { |
---|
322 | int rightNoIncludeWidth = rightWidth * ((rightProgress - progressLeft) / rightProgress); |
---|
323 | rightWidth -= rightNoIncludeWidth; |
---|
324 | |
---|
325 | NSRect noIncludeRect = barRect; |
---|
326 | noIncludeRect.origin.x += barRect.size.width - rightNoIncludeWidth; |
---|
327 | noIncludeRect.size.width = rightNoIncludeWidth; |
---|
328 | |
---|
329 | if (!fLightGrayGradient) |
---|
330 | fLightGrayGradient = [[CTGradient progressLightGrayGradient] retain]; |
---|
331 | [fLightGrayGradient fillRect: noIncludeRect angle: -90]; |
---|
332 | } |
---|
333 | |
---|
334 | if (rightWidth > 0) |
---|
335 | { |
---|
336 | int availableWidth = 0; |
---|
337 | /*if (![fDefaults boolForKey: @"DisplayProgressBarAvailable"]) |
---|
338 | { |
---|
339 | //NSLog(@"notAvailableWidth %d rightWidth %d", notAvailableWidth, rightWidth); |
---|
340 | availableWidth = MAX(0, (float)rightWidth - barRect.size.width * [torrent notAvailableDesired]); |
---|
341 | |
---|
342 | if (availableWidth > 0) |
---|
343 | { |
---|
344 | rightWidth -= availableWidth; |
---|
345 | |
---|
346 | NSRect availableRect = barRect; |
---|
347 | availableRect.origin.x += leftWidth; |
---|
348 | availableRect.size.width = availableWidth; |
---|
349 | |
---|
350 | if (!fYellowGradient) |
---|
351 | fYellowGradient = [[CTGradient progressYellowGradient] retain]; |
---|
352 | [fYellowGradient fillRect: availableRect angle: -90]; |
---|
353 | } |
---|
354 | }*/ |
---|
355 | |
---|
356 | if (rightWidth > 0) |
---|
357 | { |
---|
358 | NSRect includeRect = barRect; |
---|
359 | includeRect.origin.x += leftWidth + availableWidth; |
---|
360 | includeRect.size.width = rightWidth; |
---|
361 | |
---|
362 | if (!fWhiteGradient) |
---|
363 | fWhiteGradient = [[CTGradient progressWhiteGradient] retain]; |
---|
364 | [fWhiteGradient fillRect: includeRect angle: -90]; |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | if (leftWidth > 0) |
---|
370 | { |
---|
371 | NSRect completeRect = barRect; |
---|
372 | completeRect.size.width = leftWidth; |
---|
373 | |
---|
374 | if ([torrent isActive]) |
---|
375 | { |
---|
376 | if ([torrent isChecking]) |
---|
377 | { |
---|
378 | if (!fYellowGradient) |
---|
379 | fYellowGradient = [[CTGradient progressYellowGradient] retain]; |
---|
380 | [fYellowGradient fillRect: completeRect angle: -90]; |
---|
381 | } |
---|
382 | else if ([torrent isSeeding]) |
---|
383 | { |
---|
384 | int ratioLeftWidth = leftWidth * (1.0 - [torrent progressStopRatio]); |
---|
385 | leftWidth -= ratioLeftWidth; |
---|
386 | |
---|
387 | if (ratioLeftWidth > 0) |
---|
388 | { |
---|
389 | NSRect ratioLeftRect = barRect; |
---|
390 | ratioLeftRect.origin.x += leftWidth; |
---|
391 | ratioLeftRect.size.width = ratioLeftWidth; |
---|
392 | |
---|
393 | if (!fLightGreenGradient) |
---|
394 | fLightGreenGradient = [[CTGradient progressLightGreenGradient] retain]; |
---|
395 | [fLightGreenGradient fillRect: ratioLeftRect angle: -90]; |
---|
396 | } |
---|
397 | |
---|
398 | if (leftWidth > 0) |
---|
399 | { |
---|
400 | completeRect.size.width = leftWidth; |
---|
401 | |
---|
402 | if (!fGreenGradient) |
---|
403 | fGreenGradient = [[CTGradient progressGreenGradient] retain]; |
---|
404 | [fGreenGradient fillRect: completeRect angle: -90]; |
---|
405 | } |
---|
406 | } |
---|
407 | else |
---|
408 | { |
---|
409 | if (!fBlueGradient) |
---|
410 | fBlueGradient = [[CTGradient progressBlueGradient] retain]; |
---|
411 | [fBlueGradient fillRect: completeRect angle: -90]; |
---|
412 | } |
---|
413 | } |
---|
414 | else |
---|
415 | { |
---|
416 | if ([torrent waitingToStart]) |
---|
417 | { |
---|
418 | if ([torrent progressLeft] <= 0.0) |
---|
419 | { |
---|
420 | if (!fDarkGreenGradient) |
---|
421 | fDarkGreenGradient = [[CTGradient progressDarkGreenGradient] retain]; |
---|
422 | [fDarkGreenGradient fillRect: completeRect angle: -90]; |
---|
423 | } |
---|
424 | else |
---|
425 | { |
---|
426 | if (!fDarkBlueGradient) |
---|
427 | fDarkBlueGradient = [[CTGradient progressDarkBlueGradient] retain]; |
---|
428 | [fDarkBlueGradient fillRect: completeRect angle: -90]; |
---|
429 | } |
---|
430 | } |
---|
431 | else |
---|
432 | { |
---|
433 | if (!fGrayGradient) |
---|
434 | fGrayGradient = [[CTGradient progressGrayGradient] retain]; |
---|
435 | [fGrayGradient fillRect: completeRect angle: -90]; |
---|
436 | } |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | - (void) drawPiecesBar: (NSRect) barRect |
---|
442 | { |
---|
443 | if (!fBitmap) |
---|
444 | fBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil |
---|
445 | pixelsWide: MAX_PIECES pixelsHigh: barRect.size.height bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES |
---|
446 | isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0]; |
---|
447 | |
---|
448 | uint32_t * p; |
---|
449 | uint8_t * bitmapData = [fBitmap bitmapData]; |
---|
450 | int bytesPerRow = [fBitmap bytesPerRow]; |
---|
451 | |
---|
452 | if (!fPieces) |
---|
453 | { |
---|
454 | fPieces = malloc(MAX_PIECES); |
---|
455 | int i; |
---|
456 | for (i = 0; i < MAX_PIECES; i++) |
---|
457 | fPieces[i] = BLANK_PIECE; |
---|
458 | } |
---|
459 | |
---|
460 | #warning add flashing orange |
---|
461 | |
---|
462 | Torrent * torrent = [self representedObject]; |
---|
463 | |
---|
464 | int pieceCount = MIN([torrent pieceCount], MAX_PIECES); |
---|
465 | float * piecePercent = malloc(pieceCount * sizeof(float)); |
---|
466 | [torrent getAmountFinished: piecePercent size: pieceCount]; |
---|
467 | |
---|
468 | //lines 2 to 14: blue, green, or gray depending on piece availability |
---|
469 | int i, h, index; |
---|
470 | float increment = (float)pieceCount / MAX_PIECES; |
---|
471 | uint32_t color; |
---|
472 | BOOL change; |
---|
473 | for (i = 0; i < MAX_PIECES; i++) |
---|
474 | { |
---|
475 | index = i * increment; |
---|
476 | change = NO; |
---|
477 | |
---|
478 | if (piecePercent[index] >= 1.0) |
---|
479 | { |
---|
480 | if (fPieces[i] != -1) |
---|
481 | { |
---|
482 | color = kBlue; |
---|
483 | fPieces[i] = -1; |
---|
484 | change = YES; |
---|
485 | } |
---|
486 | } |
---|
487 | else if (piecePercent[index] <= 0.0) |
---|
488 | { |
---|
489 | if (fPieces[i] != 0) |
---|
490 | { |
---|
491 | color = kGray; |
---|
492 | fPieces[i] = 0; |
---|
493 | change = YES; |
---|
494 | } |
---|
495 | } |
---|
496 | else if (piecePercent[index] <= 0.25) |
---|
497 | { |
---|
498 | if (fPieces[i] != 1) |
---|
499 | { |
---|
500 | color = kBlue1; |
---|
501 | fPieces[i] = 1; |
---|
502 | change = YES; |
---|
503 | } |
---|
504 | } |
---|
505 | else if (piecePercent[index] <= 0.5) |
---|
506 | { |
---|
507 | if (fPieces[i] != 2) |
---|
508 | { |
---|
509 | color = kBlue2; |
---|
510 | fPieces[i] = 2; |
---|
511 | change = YES; |
---|
512 | } |
---|
513 | } |
---|
514 | else if (piecePercent[index] <= 0.75) |
---|
515 | { |
---|
516 | if (fPieces[i] != 3) |
---|
517 | { |
---|
518 | color = kBlue3; |
---|
519 | fPieces[i] = 3; |
---|
520 | change = YES; |
---|
521 | } |
---|
522 | } |
---|
523 | else |
---|
524 | { |
---|
525 | if (fPieces[i] != 4) |
---|
526 | { |
---|
527 | color = kBlue4; |
---|
528 | fPieces[i] = 4; |
---|
529 | change = YES; |
---|
530 | } |
---|
531 | } |
---|
532 | |
---|
533 | if (change) |
---|
534 | { |
---|
535 | //draw vertically |
---|
536 | p = (uint32_t *)(bitmapData) + i; |
---|
537 | for (h = 0; h < barRect.size.height; h++) |
---|
538 | { |
---|
539 | p[0] = color; |
---|
540 | p = (uint32_t *)((uint8_t *)p + bytesPerRow); |
---|
541 | } |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | free(piecePercent); |
---|
546 | |
---|
547 | //actually draw image |
---|
548 | NSImage * bar = [[NSImage alloc] initWithSize: [fBitmap size]]; |
---|
549 | [bar setFlipped: YES]; |
---|
550 | [bar addRepresentation: fBitmap]; |
---|
551 | |
---|
552 | [bar drawInRect: barRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; |
---|
553 | [bar release]; |
---|
554 | |
---|
555 | if (!fTransparentGradient) |
---|
556 | fTransparentGradient = [[CTGradient progressTransparentGradient] retain]; |
---|
557 | [fTransparentGradient fillRect: barRect angle: -90]; |
---|
558 | } |
---|
559 | |
---|
560 | - (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds |
---|
561 | { |
---|
562 | if (![fDefaults boolForKey: @"SmallView"]) |
---|
563 | return NSZeroRect; |
---|
564 | |
---|
565 | NSRect result = bounds; |
---|
566 | result.size = [string size]; |
---|
567 | |
---|
568 | result.origin.x += bounds.size.width - result.size.width - PADDING_HORIZONAL; |
---|
569 | result.origin.y += PADDING_ABOVE_MIN_STATUS; |
---|
570 | |
---|
571 | return result; |
---|
572 | } |
---|
573 | |
---|
574 | - (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect |
---|
575 | inBounds: (NSRect) bounds |
---|
576 | { |
---|
577 | BOOL minimal = [fDefaults boolForKey: @"SmallView"]; |
---|
578 | |
---|
579 | NSRect result = bounds; |
---|
580 | result.origin.y += PADDING_ABOVE_TITLE; |
---|
581 | result.origin.x += PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE; |
---|
582 | |
---|
583 | result.size = [string size]; |
---|
584 | result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL |
---|
585 | - (minimal ? PADDING_BETWEEN_TITLE_AND_MIN_STATUS + statusRect.size.width : 0)); |
---|
586 | |
---|
587 | return result; |
---|
588 | } |
---|
589 | |
---|
590 | - (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds |
---|
591 | { |
---|
592 | if ([fDefaults boolForKey: @"SmallView"]) |
---|
593 | return NSZeroRect; |
---|
594 | |
---|
595 | NSRect result = bounds; |
---|
596 | result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS; |
---|
597 | result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE; |
---|
598 | |
---|
599 | result.size = [string size]; |
---|
600 | result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); |
---|
601 | |
---|
602 | return result; |
---|
603 | } |
---|
604 | |
---|
605 | - (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds |
---|
606 | { |
---|
607 | if ([fDefaults boolForKey: @"SmallView"]) |
---|
608 | return NSZeroRect; |
---|
609 | |
---|
610 | NSRect result = bounds; |
---|
611 | result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS |
---|
612 | + PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS; |
---|
613 | result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE; |
---|
614 | |
---|
615 | result.size = [string size]; |
---|
616 | result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); |
---|
617 | |
---|
618 | return result; |
---|
619 | } |
---|
620 | |
---|
621 | - (NSAttributedString *) attributedTitleWithColor: (NSColor *) color |
---|
622 | { |
---|
623 | if (color) |
---|
624 | [fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName]; |
---|
625 | |
---|
626 | NSString * title = [[self representedObject] name]; |
---|
627 | return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease]; |
---|
628 | } |
---|
629 | |
---|
630 | - (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color |
---|
631 | { |
---|
632 | if (color) |
---|
633 | [fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName]; |
---|
634 | |
---|
635 | return [[[NSAttributedString alloc] initWithString: string attributes: fStatusAttributes] autorelease]; |
---|
636 | } |
---|
637 | |
---|
638 | @end |
---|