source: trunk/macosx/TorrentCell.m @ 344

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

This should make the borders of the new progress bar a bit more consistent.

  • Property svn:keywords set to Date Rev Author Id
File size: 11.4 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 344 2006-06-12 23:36: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: (int) width point: (NSPoint) point;
35- (void) buildAdvancedBar: (int) width point: (NSPoint) point;
36
37@end
38
39@implementation TorrentCell
40
41static NSImage * fProgressWhite, * fProgressBlue, * fProgressGray, * fProgressGreen,
42                * fProgressAdvanced, * fProgressEndWhite, * fProgressEndBlue,
43                * fProgressEndGray, * fProgressEndGreen, * fProgressEndAdvanced;
44
45// Used to optimize drawing. They contain packed RGBA pixels for every color needed.
46static uint32_t kBorder[] =
47    { 0x00000005, 0x00000010, 0x00000015, 0x00000015,
48      0x00000015, 0x00000015, 0x00000015, 0x00000015,
49      0x00000015, 0x00000015, 0x00000010, 0x00000005 };
50
51static uint32_t kBack[] = { 0xB4B4B4FF, 0xE3E3E3FF };
52
53static uint32_t kRed = 0xFF6450FF, //255, 100, 80
54                kBlue1 = 0xA0DCFFFF, //160, 220, 255
55                kBlue2 = 0x78BEFFFF, //120, 190, 255
56                kBlue3 = 0x50A0FFFF, //80, 160, 255
57                kBlue4 = 0x1E46B4FF, //30, 70, 180
58                kGray = 0x828282FF, //130, 130, 130
59                kGreen = 0x00FF00FF; //0, 255, 0
60
61- (id) init
62{
63    if ((self = [super init]))
64    {
65        NSSize startSize = NSMakeSize(100.0, BAR_HEIGHT);
66        if (!fProgressWhite)
67        {
68            fProgressWhite = [NSImage imageNamed: @"ProgressBarWhite.png"];
69            [fProgressWhite setScalesWhenResized: YES];
70            [fProgressWhite setSize: startSize];
71        }
72        if (!fProgressBlue)
73        {
74            fProgressBlue = [NSImage imageNamed: @"ProgressBarBlue.png"];
75            [fProgressBlue setScalesWhenResized: YES];
76            [fProgressBlue setSize: startSize];
77        }
78        if (!fProgressGray)
79        {
80            fProgressGray = [NSImage imageNamed: @"ProgressBarGray.png"];
81            [fProgressGray setScalesWhenResized: YES];
82            [fProgressGray setSize: startSize];
83        }
84        if (!fProgressGreen)
85        {
86            fProgressGreen = [NSImage imageNamed: @"ProgressBarGreen.png"];
87            [fProgressGreen setScalesWhenResized: YES];
88            [fProgressGreen setSize: startSize];
89        }
90        if (!fProgressAdvanced)
91        {
92            fProgressAdvanced = [NSImage imageNamed: @"ProgressBarAdvanced.png"];
93            [fProgressAdvanced setScalesWhenResized: YES];
94            [fProgressAdvanced setSize: startSize];
95        }
96       
97        if (!fProgressEndWhite)
98            fProgressEndWhite = [NSImage imageNamed: @"ProgressBarEndWhite.png"];
99        if (!fProgressEndBlue)
100            fProgressEndBlue = [NSImage imageNamed: @"ProgressBarEndBlue.png"];
101        if (!fProgressEndGray)
102            fProgressEndGray = [NSImage imageNamed: @"ProgressBarEndGray.png"];
103        if (!fProgressEndGreen)
104            fProgressEndGreen = [NSImage imageNamed: @"ProgressBarEndGreen.png"];
105        if (!fProgressEndAdvanced)
106            fProgressEndAdvanced = [NSImage imageNamed: @"ProgressBarEndAdvanced.png"];
107    }
108    return self;
109}
110
111- (void) setTorrent: (Torrent *) torrent
112{
113    fTorrent = torrent;
114}
115
116- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point
117{
118    if ([barImage size].width < width)
119        [barImage setSize: NSMakeSize(width * 1.5, BAR_HEIGHT)];
120
121    [barImage compositeToPoint: point fromRect: NSMakeRect(0, 0, width, BAR_HEIGHT)
122                        operation: NSCompositeSourceOver];
123}
124
125- (void) buildSimpleBar: (int) width point: (NSPoint) point
126{
127    width -= 2.0;
128    if ([fTorrent isSeeding])
129    {
130        [fProgressEndGreen compositeToPoint: point operation: NSCompositeSourceOver];
131       
132        point.x += 1.0;
133        [self placeBar: fProgressGreen width: width point: point];
134       
135        point.x += width;
136        [fProgressEndGreen compositeToPoint: point operation: NSCompositeSourceOver];
137    }
138    else
139    {
140        float completedWidth = [fTorrent progress] * width,
141                remainingWidth = width - completedWidth;
142   
143        NSImage * barActiveEnd, * barActive;
144        if ([fTorrent isActive])
145        {
146            barActiveEnd = fProgressEndBlue;
147            barActive = fProgressBlue;
148        }
149        else
150        {
151            barActiveEnd = fProgressEndGray;
152            barActive = fProgressGray;
153        }
154        if (completedWidth < 1.0)
155            barActiveEnd = fProgressEndWhite;
156   
157        [barActiveEnd compositeToPoint: point operation: NSCompositeSourceOver];
158       
159        point.x += 1.0;
160        [self placeBar: barActive width: completedWidth point: point];
161       
162        point.x += completedWidth;
163        [self placeBar: fProgressWhite width: remainingWidth point: point];
164       
165        point.x += remainingWidth;
166        [[fTorrent progress] < 1.0 ? fProgressEndWhite : fProgressEndGray
167                    compositeToPoint: point operation: NSCompositeSourceOver];
168    }
169}
170
171- (void) buildAdvancedBar: (int) width point: (NSPoint) point
172{
173    //if seeding, there's no need for the advanced bar
174    if ([fTorrent isSeeding])
175    {
176        [self buildSimpleBar: width point: point];
177        return;
178    }
179
180    NSBitmapImageRep * bitmap = [[NSBitmapImageRep alloc]
181        initWithBitmapDataPlanes: nil pixelsWide: width
182        pixelsHigh: BAR_HEIGHT bitsPerSample: 8 samplesPerPixel: 4
183        hasAlpha: YES isPlanar: NO colorSpaceName:
184        NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
185
186    int h, w;
187    uint32_t * p;
188    uint8_t * bitmapData = [bitmap bitmapData];
189    int bytesPerRow = [bitmap bytesPerRow];
190
191    /* Left and right borders */
192    p = (uint32_t *) bitmapData;
193    for( h = 0; h < BAR_HEIGHT; h++ )
194    {
195        p[0] = htonl( kBorder[h] );
196        p[width - 1] = htonl( kBorder[h] );
197        p += bytesPerRow / 4;
198    }
199
200    int8_t * pieces = malloc( width );
201    [fTorrent getAvailability: pieces size: width];
202
203    /* First two lines: dark blue to show progression */
204    int end  = lrintf( floor( [fTorrent progress] * ( width - 2 ) ) );
205    for( h = 0; h < 2; h++ )
206    {
207        p = (uint32_t *) ( bitmapData + h * bytesPerRow ) + 1;
208        for( w = 0; w < end; w++ )
209            p[w] = htonl( kBlue4 );
210        for( w = end; w < width - 2; w++ )
211            p[w] = htonl( kBack[h] );
212    }
213
214    /* Lines 2 to 14: blue or grey depending on whether
215       we have the piece or not */
216    uint32_t color;
217    for( w = 0; w < width - 2; w++ )
218    {
219        /* Point to pixel ( 2 + w, 2 ). We will then draw
220           "vertically" */
221        p = (uint32_t *) ( bitmapData + 2 * bytesPerRow ) + 1 + w;
222
223        if (pieces[w] < 0)
224            color = kGray;
225        else if (pieces[w] == 0)
226            color = kRed;
227        else if (pieces[w] == 1)
228            color = kBlue1;
229        else if (pieces[w] == 2)
230            color = kBlue2;
231        else
232            color = kBlue3;
233
234        for( h = 2; h < BAR_HEIGHT; h++ )
235        {
236            p[0] = htonl( color );
237            p = (uint32_t *) ( (uint8_t *) p + bytesPerRow );
238        }
239    }
240
241    free( pieces );
242   
243    //actually draw image
244    NSImage * img = [[NSImage alloc] initWithSize: [bitmap size]];
245    [img addRepresentation: bitmap];
246    [img compositeToPoint: point operation: NSCompositeSourceOver];
247    [img release];
248    [bitmap release];
249   
250    //draw overlay over advanced bar
251    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
252   
253    point.x += 1.0;
254    [self placeBar: fProgressAdvanced width: width - 2.0 point: point];
255   
256    point.x += width - 2.0;
257    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
258}
259
260- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
261{
262    BOOL highlighted = [self isHighlighted] && [[view window] isKeyWindow];
263    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
264                    highlighted ? [NSColor whiteColor] : [NSColor blackColor],
265                    NSForegroundColorAttributeName,
266                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName, nil];
267    NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
268                    highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor],
269                    NSForegroundColorAttributeName,
270                    [NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
271
272    NSPoint pen = cellFrame.origin;
273    float padding = 3.0, linePadding = 2.0;
274
275    //icon
276    NSImage * icon = [fTorrent iconFlipped];
277    NSSize iconSize = [icon size];
278   
279    pen.x += padding;
280    pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
281   
282    [icon drawAtPoint: pen fromRect: NSMakeRect( 0, 0, iconSize.width, iconSize.height )
283            operation: NSCompositeSourceOver fraction: 1.0];
284
285    float extraNameShift = 1.0,
286        mainWidth = cellFrame.size.width - iconSize.width - 3.0 * padding - extraNameShift;
287
288    //name string
289    pen.x += iconSize.width + padding + extraNameShift;
290    pen.y = cellFrame.origin.y + padding;
291    NSAttributedString * nameString = [[fTorrent name] attributedStringFittingInWidth: mainWidth
292                                attributes: nameAttributes];
293    [nameString drawAtPoint: pen];
294   
295    //progress string
296    pen.y += [nameString size].height + linePadding - 1.0;
297   
298    NSAttributedString * progressString = [[fTorrent progressString]
299        attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
300    [progressString drawAtPoint: pen];
301
302    //progress bar
303    pen.x -= extraNameShift;
304    pen.y += [progressString size].height + linePadding + BAR_HEIGHT;
305   
306    float barWidth = mainWidth + extraNameShift - BUTTONS_TOTAL_WIDTH + padding;
307   
308    if ([[NSUserDefaults standardUserDefaults] boolForKey: @"UseAdvancedBar"])
309        [self buildAdvancedBar: barWidth point: pen];
310    else
311        [self buildSimpleBar: barWidth point: pen];
312
313    //status strings
314    pen.x += extraNameShift;
315    pen.y += linePadding;
316    NSAttributedString * statusString = [[fTorrent statusString]
317        attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
318    [statusString drawAtPoint: pen];
319   
320    [nameAttributes release];
321    [statusAttributes release];
322}
323
324@end
Note: See TracBrowser for help on using the repository browser.