source: trunk/macosx/TorrentCell.m @ 836

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

Make error image smaller so resizing isn't necessary.

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