source: trunk/macosx/TorrentCell.m @ 839

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

Better compare of IP strings.

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