source: branches/new_api/macosx/TorrentCell.m @ 152

Last change on this file since 152 was 152, checked in by titer, 11 years ago

Fixed MacIntel? display bug

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