source: trunk/macosx/TorrentCell.m @ 843

Last change on this file since 843 was 843, checked in by livings124, 15 years ago

temp fix for crashing

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