source: trunk/macosx/TorrentCell.m @ 855

Last change on this file since 855 was 855, checked in by livings124, 16 years ago

Use tableView:objectValueForTableColumn:row: instead of tableView:willDisplayCell:...

This required passing in a dictionary of values to display, but should make it 10.5 compatible and potentially a little more efficient. Those using Leopard please report any issues, because I don't have it to test.

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