source: trunk/macosx/TorrentCell.m @ 261

Last change on this file since 261 was 261, checked in by titer, 16 years ago

Updated svn:keywords

  • Property svn:keywords set to Date Rev Author Id
File size: 10.3 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 261 2006-05-29 21:27:31Z titer $
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 "StringAdditions.h"
27
28#define BAR_HEIGHT 12
29
30@implementation TorrentCell
31
32/***********************************************************************
33 * Static tables
34 ***********************************************************************
35 * We use these tables to optimize the drawing. They contain packed
36 * RGBA pixels for every color we might need.
37 **********************************************************************/
38
39static uint32_t kBorder[] =
40    { 0x00000005, 0x00000010, 0x00000015, 0x00000015,
41      0x00000015, 0x00000015, 0x00000015, 0x00000015,
42      0x00000015, 0x00000015, 0x00000010, 0x00000005 };
43
44static uint32_t kBack[] =
45    { 0xB4B4B4FF, 0xE3E3E3FF, 0xE8E8E8FF, 0xDEDEDEFF,
46      0xDBDBDBFF, 0xE5E5E5FF, 0xE7E7E7FF, 0xF5F5F5FF,
47      0xFAFAFAFF, 0xDEDEDEFF, 0x0000003F, 0x00000015 };
48
49/* Coefficients for the "3D effect":
50   0.59, 0.91, 0.97, 0.92, 0.79, 0.76, 0.85, 0.93, 1.00, 0.99 */
51
52/* 255, 100, 80 */
53static uint32_t kRed[] =
54    { 0x963A2FFF, 0xE85B48FF, 0xF7614DFF, 0xEA5C49FF,
55      0xC94F3FFF, 0xC14C3CFF, 0xD85544FF, 0xED5D4AFF,
56      0xFF6450FF, 0xFC634FFF, 0x0000003F, 0x00000015 };
57
58/* 160, 220, 255 */
59static uint32_t kBlue1[] =
60    { 0x5E8196FF, 0x91C8E8FF, 0x9BD5F7FF, 0x93CAEAFF,
61      0x7EADC9FF, 0x79A7C1FF, 0x88BBD8FF, 0x94CCEDFF,
62      0xA0DCFFFF, 0x9ED9FCFF, 0x0000003F, 0x00000015 };
63
64/* 120, 190, 255 */
65static uint32_t kBlue2[] =
66    { 0x467096FF, 0x6DACE8FF, 0x74B8F7FF, 0x6EAEEAFF,
67      0x5E96C9FF, 0x5B90C1FF, 0x66A1D8FF, 0x6FB0EDFF,
68      0x78BEFFFF, 0x76BCFCFF, 0x0000003F, 0x00000015 };
69
70/* 80, 160, 255 */
71static uint32_t kBlue3[] =
72    { 0x2F5E96FF, 0x4891E8FF, 0x4D9BF7FF, 0x4993EAFF,
73      0x3F7EC9FF, 0x3C79C1FF, 0x4488D8FF, 0x4A94EDFF,
74      0x50A0FFFF, 0x4F9EFCFF, 0x0000003F, 0x00000015 };
75
76/* 30, 70, 180 */
77static uint32_t kBlue4[] =
78    { 0x11296AFF, 0x1B3FA3FF, 0x1D43AEFF, 0x1B40A5FF,
79      0x17378EFF, 0x163588FF, 0x193B99FF, 0x1B41A7FF,
80      0x1E46B4FF, 0x1D45B2FF, 0x0000003F, 0x00000015 };
81
82/* 130, 130, 130 */
83static uint32_t kGray[] =
84    { 0x4C4C4CFF, 0x767676FF, 0x7E7E7EFF, 0x777777FF,
85      0x666666FF, 0x626262FF, 0x6E6E6EFF, 0x787878FF,
86      0x828282FF, 0x808080FF, 0x0000003F, 0x00000015 };
87
88/* 0, 255, 0 */
89static uint32_t kGreen[] =
90    { 0x009600FF, 0x00E800FF, 0x00F700FF, 0x00EA00FF,
91      0x00C900FF, 0x00C100FF, 0x00D800FF, 0x00ED00FF,
92      0x00FF00FF, 0x00FC00FF, 0x0000003F, 0x00000015 };
93
94- (void) setTorrent: (Torrent *) torrent
95{
96    fTorrent = torrent;
97}
98
99- (void) setTextColor: (NSColor *) color
100{
101    fTextColor = color;
102}
103
104/***********************************************************************
105 * init
106 ***********************************************************************
107 * Prepares the NSBitmapImageReps we are going to need in order to
108 * draw.
109 **********************************************************************/
110- (id) init
111{
112    self = [super init];
113
114    return self;
115}
116
117/***********************************************************************
118 * buildSimpleBar
119 **********************************************************************/
120- (void) buildSimpleBar
121{
122    int        h, w, end, pixelsPerRow;
123    uint32_t * p;
124    uint32_t * colors;
125
126    pixelsPerRow = [fBitmap bytesPerRow] / 4;
127
128    p   = (uint32_t *) [fBitmap bitmapData] + 1;
129    end = lrintf( floor( [fTorrent progress] * ( fWidth - 2 ) ) );
130
131    if( [fTorrent isSeeding] )
132        colors = kGreen;
133    else if( [fTorrent isActive] )
134        colors = kBlue2;
135    else
136        colors = kGray;
137
138    for( h = 0; h < BAR_HEIGHT; h++ )
139    {
140        for( w = 0; w < end; w++ )
141        {
142            p[w] = htonl( colors[h] );
143        }
144        for( w = end; w < fWidth - 2; w++ )
145        {
146            p[w] = htonl( kBack[h] );
147        }
148        p += pixelsPerRow;
149    }
150}
151
152/***********************************************************************
153 * buildAdvancedBar
154 **********************************************************************/
155- (void) buildAdvancedBar
156{
157    int      h, w, end, pixelsPerRow;
158    uint32_t * p, * colors;
159    uint8_t  * bitmapData  = [fBitmap bitmapData];
160    int        bytesPerRow = [fBitmap bytesPerRow];
161
162    fPieces = malloc( fWidth );
163    [fTorrent getAvailability: fPieces size: fWidth];
164
165    if( [fTorrent isSeeding] )
166    {
167        /* All green, same as the simple bar */
168        [self buildSimpleBar];
169        return;
170    }
171
172    pixelsPerRow = [fBitmap bytesPerRow] / 4;
173
174    /* First two lines: dark blue to show progression */
175    end  = lrintf( floor( [fTorrent progress] * ( fWidth - 2 ) ) );
176    for( h = 0; h < 2; h++ )
177    {
178        p = (uint32_t *) ( bitmapData + h * bytesPerRow ) + 1;
179        for( w = 0; w < end; w++ )
180        {
181            p[w] = htonl( kBlue4[h] );
182        }
183        for( w = end; w < fWidth - 2; w++ )
184        {
185            p[w] = htonl( kBack[h] );
186        }
187    }
188
189    /* Lines 2 to 14: blue or grey depending on whether
190       we have the piece or not */
191    for( w = 0; w < fWidth - 2; w++ )
192    {
193        /* Point to pixel ( 2 + w, 2 ). We will then draw
194           "vertically" */
195        p  = (uint32_t *) ( bitmapData + 2 * bytesPerRow ) + 1 + w;
196
197        if( fPieces[w] < 0 )
198        {
199            colors = kGray;
200        }
201        else if( fPieces[w] < 1 )
202        {
203            colors = kRed;
204        }
205        else if( fPieces[w] < 2 )
206        {
207            colors = kBlue1;
208        }
209        else if( fPieces[w] < 3 )
210        {
211            colors = kBlue2;
212        }
213        else
214        {
215            colors = kBlue3;
216        }
217
218        for( h = 2; h < BAR_HEIGHT; h++ )
219        {
220            p[0] = htonl( colors[h] );
221            p    = (uint32_t *) ( (uint8_t *) p + bytesPerRow );
222        }
223    }
224
225    free( fPieces );
226}
227
228- (void) buildBar
229{
230    int h;
231    uint32_t * p;
232
233    /* Left and right borders */
234    p = (uint32_t *) [fBitmap bitmapData];
235    for( h = 0; h < BAR_HEIGHT; h++ )
236    {
237        p[0]          = htonl( kBorder[h] );
238        p[fWidth - 1] = htonl( kBorder[h] );
239        p += [fBitmap bytesPerRow] / 4;
240    }
241
242    /* ...and redraw the progress bar on the top of it */
243    if( [[NSUserDefaults standardUserDefaults]
244            boolForKey:@"UseAdvancedBar"])
245    {
246        [self buildAdvancedBar];
247    }
248    else
249    {
250        [self buildSimpleBar];
251    }
252}
253
254/***********************************************************************
255 * drawWithFrame
256 ***********************************************************************
257 * We have the strings, we have the bitmap. Let's just draw them where
258 * they belong.
259 **********************************************************************/
260- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
261{
262    if( ![view lockFocusIfCanDraw] )
263    {
264        return;
265    }
266
267    NSMutableDictionary * attributes;
268    attributes = [NSMutableDictionary dictionaryWithCapacity: 2];
269    [attributes setObject: fTextColor
270        forKey: NSForegroundColorAttributeName];
271
272    NSPoint pen = cellFrame.origin;
273
274    /* Draw the icon */
275    pen.x += 5; pen.y += 10;
276    NSImage * icon = [fTorrent icon];
277    [icon drawAtPoint: pen fromRect:
278        NSMakeRect( 0, 0, [icon size].width, [icon size].height )
279        operation: NSCompositeSourceOver fraction: 1.0];
280
281    NSString * string;
282    fWidth  = NSWidth( cellFrame ) - [icon size].width - 15;
283
284    /* Draw file or folder name */
285    pen.x += [icon size].width + 5; pen.y -= 7;
286    [attributes setObject: [NSFont messageFontOfSize: 11]
287        forKey: NSFontAttributeName];
288    NSString * sizeString = [NSString stringWithFormat: @" (%@)",
289        [NSString stringForFileSize: [fTorrent size]]];
290    string = [[[fTorrent name] stringFittingInWidth: fWidth - 50 -
291        [sizeString sizeWithAttributes: attributes].width
292        withAttributes: attributes] stringByAppendingString: sizeString];
293    [string drawAtPoint: pen withAttributes: attributes];
294
295    /* Draw the progress bar */
296    pen.y += 17;
297    fBitmap = [[NSBitmapImageRep alloc]
298        initWithBitmapDataPlanes: nil pixelsWide: fWidth
299        pixelsHigh: BAR_HEIGHT bitsPerSample: 8 samplesPerPixel: 4
300        hasAlpha: YES isPlanar: NO colorSpaceName:
301        NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
302    NSImage * img = [[NSImage alloc] initWithSize: [fBitmap size]];
303    [img addRepresentation: fBitmap];
304    [img setFlipped: YES];
305    [self buildBar];
306    [img drawAtPoint: pen fromRect: NSMakeRect( 0, 0,
307            [img size].width, [img size].height )
308        operation: NSCompositeSourceOver fraction: 1.0];
309    [img release];
310    [fBitmap release];
311
312    /* Status strings */
313    [attributes setObject: [NSFont messageFontOfSize: 9]
314            forKey: NSFontAttributeName];
315    pen.y += BAR_HEIGHT + 2;
316    [[fTorrent statusString] drawAtPoint: pen withAttributes: attributes];
317    pen.y += 13;
318    string = [[fTorrent infoString] stringFittingInWidth:
319        ( fWidth - 80 ) withAttributes: attributes];
320    [string drawAtPoint: pen withAttributes: attributes];
321
322    /* Rates */
323    pen.x += fWidth - 70; pen.y -= 13;
324    [[fTorrent downloadString] drawAtPoint: pen withAttributes: attributes];
325    pen.y += 13;
326    [[fTorrent uploadString] drawAtPoint: pen withAttributes: attributes];
327
328    [view unlockFocus];
329}
330
331@end
Note: See TracBrowser for help on using the repository browser.