source: trunk/macosx/TorrentCell.m @ 842

Last change on this file since 842 was 842, checked in by livings124, 17 years ago

Display a warning when in debug mode.

  • Property svn:keywords set to Date Rev Author Id
File size: 14.7 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 842 2006-09-03 17:06:43Z livings124 $
3 *
4 * Copyright (c) 2006 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 "StringAdditions.h"
28
29#define BAR_HEIGHT 12.0
30
31@interface TorrentCell (Private)
32
33- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point;
34- (void) buildSimpleBar: (float) width point: (NSPoint) point;
35- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point;
36
37@end
38
39@implementation TorrentCell
40
41// Used to optimize drawing. They contain packed RGBA pixels for every color needed.
42#define BE OSSwapBigToHostConstInt32
43static uint32_t kBorder[] =
44    { BE(0x00000005), BE(0x00000010), BE(0x00000015), BE(0x00000015),
45      BE(0x00000015), BE(0x00000015), BE(0x00000015), BE(0x00000015),
46      BE(0x00000015), BE(0x00000015), BE(0x00000010), BE(0x00000005) };
47
48static uint32_t kBack[] = { BE(0xB4B4B4FF), BE(0xE3E3E3FF) };
49
50static uint32_t kRed   = BE(0xFF6450FF), //255, 100, 80
51                kBlue1 = BE(0xA0DCFFFF), //160, 220, 255
52                kBlue2 = BE(0x78BEFFFF), //120, 190, 255
53                kBlue3 = BE(0x50A0FFFF), //80, 160, 255
54                kBlue4 = BE(0x1E46B4FF), //30, 70, 180
55                kGray  = BE(0x828282FF), //130, 130, 130
56                kGreen = BE(0x00FF00FF); //0, 255, 0
57
58- (id) init
59{
60    if ((self = [super init]))
61    {
62        fDefaults = [NSUserDefaults standardUserDefaults];
63       
64        fStatusRegular = [fDefaults boolForKey: @"SmallStatusRegular"];
65   
66        NSSize startSize = NSMakeSize(100.0, BAR_HEIGHT);
67       
68        fProgressWhite = [NSImage imageNamed: @"ProgressBarWhite.png"];
69        [fProgressWhite setScalesWhenResized: YES];
70       
71       
72        fProgressBlue = [NSImage imageNamed: @"ProgressBarBlue.png"];
73        [fProgressBlue setScalesWhenResized: YES];
74        [fProgressBlue setSize: startSize];
75       
76        fProgressGray = [NSImage imageNamed: @"ProgressBarGray.png"];
77        [fProgressGray setScalesWhenResized: YES];
78        [fProgressGray setSize: startSize];
79       
80        fProgressGreen = [NSImage imageNamed: @"ProgressBarGreen.png"];
81        [fProgressGreen setScalesWhenResized: YES];
82       
83        fProgressAdvanced = [NSImage imageNamed: @"ProgressBarAdvanced.png"];
84        [fProgressAdvanced setScalesWhenResized: YES];
85       
86       
87        fProgressEndWhite = [NSImage imageNamed: @"ProgressBarEndWhite.png"];
88        fProgressEndBlue = [NSImage imageNamed: @"ProgressBarEndBlue.png"];
89        fProgressEndGray = [NSImage imageNamed: @"ProgressBarEndGray.png"];
90        fProgressEndGreen = [NSImage imageNamed: @"ProgressBarEndGreen.png"];
91        fProgressEndAdvanced = [NSImage imageNamed: @"ProgressBarEndAdvanced.png"];
92       
93        fErrorImage = [[NSImage imageNamed: @"Error.tiff"] copy];
94        [fErrorImage setFlipped: YES];
95    }
96    return self;
97}
98
99- (void) dealloc
100{
101    [fErrorImage release];
102    [super dealloc];
103}
104
105- (void) setTorrent: (Torrent *) torrent
106{
107    fTorrent = torrent;
108}
109
110- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point
111{
112    if ([barImage size].width < width)
113        [barImage setSize: NSMakeSize(width * 1.5, BAR_HEIGHT)];
114
115    [barImage compositeToPoint: point fromRect: NSMakeRect(0, 0, width, BAR_HEIGHT) operation: NSCompositeSourceOver];
116}
117
118- (void) buildSimpleBar: (float) width point: (NSPoint) point
119{
120    width -= 2.0;
121    if ([fTorrent isSeeding])
122    {
123        [fProgressEndGreen compositeToPoint: point operation: NSCompositeSourceOver];
124       
125        point.x += 1.0;
126        [self placeBar: fProgressGreen width: width point: point];
127       
128        point.x += width;
129        [fProgressEndGreen compositeToPoint: point operation: NSCompositeSourceOver];
130    }
131    else
132    {
133        float completedWidth = [fTorrent progress] * width,
134                remainingWidth = width - completedWidth;
135        BOOL isActive = [fTorrent isActive];
136       
137        //left end
138        NSImage * barLeftEnd;
139        if (remainingWidth == width)
140            barLeftEnd = fProgressEndWhite;
141        else if (isActive)
142            barLeftEnd = fProgressEndBlue;
143        else
144            barLeftEnd = fProgressEndGray;
145       
146        [barLeftEnd compositeToPoint: point operation: NSCompositeSourceOver];
147       
148        //active bar
149        point.x += 1.0;
150        [self placeBar: isActive ? fProgressBlue : fProgressGray width: completedWidth point: point];
151       
152        //remaining bar
153        point.x += completedWidth;
154        [self placeBar: fProgressWhite width: remainingWidth point: point];
155       
156        //right end
157        NSImage * barRightEnd;
158        if (completedWidth < width)
159            barRightEnd = fProgressEndWhite;
160        else if (isActive)
161            barRightEnd = fProgressEndBlue;
162        else
163            barRightEnd = fProgressEndGray;
164       
165        point.x += remainingWidth;
166        [barRightEnd compositeToPoint: point operation: NSCompositeSourceOver];
167    }
168}
169
170- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point
171{
172    //if seeding, there's no need for the advanced bar
173    if ([fTorrent isSeeding])
174    {
175        [self buildSimpleBar: widthFloat point: point];
176        return;
177    }
178
179    int width = widthFloat; //integers for bars
180   
181    NSBitmapImageRep * bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil
182        pixelsWide: width pixelsHigh: BAR_HEIGHT bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES
183        isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
184
185    int h, w;
186    uint32_t * p;
187    uint8_t * bitmapData = [bitmap bitmapData];
188    int bytesPerRow = [bitmap bytesPerRow];
189
190    //left and right borders
191    p = (uint32_t *) bitmapData;
192    for(h = 0; h < BAR_HEIGHT; h++)
193    {
194        p[0] = kBorder[h];
195        p[width - 1] = kBorder[h];
196        p += bytesPerRow / 4;
197    }
198
199    int8_t * pieces = malloc(width);
200    [fTorrent getAvailability: pieces size: width];
201    int avail = 0;
202    for (w = 0; w < width; w++)
203        if (pieces[w] != 0)
204            avail++;
205
206    //first two lines: dark blue to show progression, green to show available
207    int end = lrintf(floor([fTorrent progress] * (width - 2)));
208    p = (uint32_t *) (bitmapData) + 1;
209
210    for (w = 0; w < end; w++)
211    {
212        p[w] = kBlue4;
213        p[w + bytesPerRow / 4] = kBlue4;
214    }
215    for (; w < avail; w++)
216    {
217        p[w] = kGreen;
218        p[w + bytesPerRow / 4] = kGreen;
219    }
220    for (; w < width - 2; w++)
221    {
222        p[w] = kBack[0];
223        p[w + bytesPerRow / 4] = kBack[1];
224    }
225   
226    //lines 2 to 14: blue or grey depending on whether we have the piece or not
227    uint32_t color;
228    for( w = 0; w < width - 2; w++ )
229    {
230        //point to pixel ( 2 + w, 2 ). We will then draw "vertically"
231        p = (uint32_t *) ( bitmapData + 2 * bytesPerRow ) + 1 + w;
232
233        if (pieces[w] < 0)
234            color = kGray;
235        else if (pieces[w] == 0)
236            color = kRed;
237        else if (pieces[w] == 1)
238            color = kBlue1;
239        else if (pieces[w] == 2)
240            color = kBlue2;
241        else
242            color = kBlue3;
243
244        for( h = 2; h < BAR_HEIGHT; h++ )
245        {
246            p[0] = color;
247            p = (uint32_t *) ( (uint8_t *) p + bytesPerRow );
248        }
249    }
250
251    free( pieces );
252   
253    //actually draw image
254    NSImage * img = [[NSImage alloc] initWithSize: [bitmap size]];
255    [img addRepresentation: bitmap];
256   
257    //bar size with float, not double, to match standard bar
258    [img setScalesWhenResized: YES];
259    [img setSize: NSMakeSize(widthFloat, BAR_HEIGHT)];
260   
261    [img compositeToPoint: point operation: NSCompositeSourceOver];
262    [img release];
263    [bitmap release];
264   
265    //draw overlay over advanced bar
266    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
267   
268    widthFloat -= 2.0;
269    point.x += 1.0;
270    [self placeBar: fProgressAdvanced width: widthFloat point: point];
271   
272    point.x += widthFloat;
273    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
274}
275
276- (void) toggleMinimalStatus
277{
278    fStatusRegular = !fStatusRegular;
279    [fDefaults setBool: fStatusRegular forKey: @"SmallStatusRegular"];
280}
281
282- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
283{
284    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view]
285                                                        isEqual: [NSColor alternateSelectedControlColor]];
286    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
287                    highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
288                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName, nil];
289    NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
290                    highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
291                    [NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
292
293    NSPoint pen = cellFrame.origin;
294    const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0;
295
296    if (![fDefaults boolForKey: @"SmallView"]) //regular size
297    {
298        //icon
299        NSImage * icon = [fTorrent iconFlipped];
300        NSSize iconSize = [icon size];
301       
302        pen.x += PADDING;
303        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
304       
305        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
306                operation: NSCompositeSourceOver fraction: 1.0];
307       
308        //error badge
309        if ([fTorrent isError])
310        {
311            NSSize errorIconSize = [fErrorImage size];
312            [fErrorImage drawAtPoint: NSMakePoint(pen.x + iconSize.width - errorIconSize.width,
313                                                    pen.y + iconSize.height  - errorIconSize.height)
314                fromRect: NSMakeRect(0, 0, errorIconSize.width, errorIconSize.height)
315                operation: NSCompositeSourceOver fraction: 1.0];
316        }
317
318        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
319
320        //name string
321        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
322        pen.y = cellFrame.origin.y + PADDING;
323        NSAttributedString * nameString = [[fTorrent name] attributedStringFittingInWidth: mainWidth
324                                                attributes: nameAttributes];
325        [nameString drawAtPoint: pen];
326       
327        //progress string
328        pen.y += [nameString size].height + LINE_PADDING - 1.0;
329       
330        NSAttributedString * progressString = [[fTorrent progressString]
331            attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
332        [progressString drawAtPoint: pen];
333
334        //progress bar
335        pen.x -= EXTRA_NAME_SHIFT;
336        pen.y += [progressString size].height + LINE_PADDING + BAR_HEIGHT;
337       
338        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
339       
340        if ([fDefaults boolForKey: @"UseAdvancedBar"])
341            [self buildAdvancedBar: barWidth point: pen];
342        else
343            [self buildSimpleBar: barWidth point: pen];
344
345        //status string
346        pen.x += EXTRA_NAME_SHIFT;
347        pen.y += LINE_PADDING;
348        NSAttributedString * statusString = [[fTorrent statusString]
349            attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
350        [statusString drawAtPoint: pen];
351    }
352    else //small size
353    {
354        //icon
355        NSImage * icon = ![fTorrent isError] ? [fTorrent iconSmall] : fErrorImage;
356        NSSize iconSize = [icon size];
357       
358        pen.x += PADDING;
359        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
360       
361        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
362                operation: NSCompositeSourceOver fraction: 1.0];
363
364        //name and status string
365        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
366       
367        NSString * realStatusString = !fStatusRegular && [fTorrent isActive] ? [fTorrent remainingTimeString]
368                                                                            : [fTorrent shortStatusString];
369       
370        NSAttributedString * statusString = [[[NSAttributedString alloc] initWithString: realStatusString
371                                                    attributes: statusAttributes] autorelease];
372        NSAttributedString * nameString = [[fTorrent name] attributedStringFittingInWidth:
373                                mainWidth - [statusString size].width - LINE_PADDING attributes: nameAttributes];
374                     
375        //place name string
376        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
377        pen.y = cellFrame.origin.y + LINE_PADDING;
378
379        [nameString drawAtPoint: pen];
380       
381        //place status string
382        pen.x = NSMaxX(cellFrame) - PADDING - [statusString size].width;
383        pen.y += ([nameString size].height - [statusString size].height) * 0.5;
384       
385        [statusString drawAtPoint: pen];
386       
387        //progress bar
388        pen.x = cellFrame.origin.x + iconSize.width + 2.0 * PADDING;
389        pen.y = cellFrame.origin.y + [nameString size].height + LINE_PADDING + PADDING + BAR_HEIGHT;
390       
391        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
392       
393        if ([fDefaults boolForKey: @"UseAdvancedBar"])
394            [self buildAdvancedBar: barWidth point: pen];
395        else
396            [self buildSimpleBar: barWidth point: pen];
397    }
398   
399    [nameAttributes release];
400    [statusAttributes release];
401}
402
403@end
Note: See TracBrowser for help on using the repository browser.