1 | /****************************************************************************** |
---|
2 | * $Id: TorrentCell.m 1645 2007-04-03 02:22:25Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2007 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 | //also defined in Torrent.m |
---|
30 | #define BAR_HEIGHT 12.0 |
---|
31 | |
---|
32 | @interface TorrentCell (Private) |
---|
33 | |
---|
34 | - (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point; |
---|
35 | - (void) buildSimpleBar: (float) width point: (NSPoint) point; |
---|
36 | - (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point; |
---|
37 | |
---|
38 | @end |
---|
39 | |
---|
40 | @implementation TorrentCell |
---|
41 | |
---|
42 | - (id) init |
---|
43 | { |
---|
44 | if ((self = [super init])) |
---|
45 | { |
---|
46 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
47 | |
---|
48 | fStatusRegular = [fDefaults boolForKey: @"SmallStatusRegular"]; |
---|
49 | |
---|
50 | NSSize startSize = NSMakeSize(100.0, BAR_HEIGHT); |
---|
51 | |
---|
52 | fProgressWhite = [NSImage imageNamed: @"ProgressBarWhite.png"]; |
---|
53 | [fProgressWhite setScalesWhenResized: YES]; |
---|
54 | |
---|
55 | fProgressBlue = [NSImage imageNamed: @"ProgressBarBlue.png"]; |
---|
56 | [fProgressBlue setScalesWhenResized: YES]; |
---|
57 | [fProgressBlue setSize: startSize]; |
---|
58 | |
---|
59 | fProgressGray = [NSImage imageNamed: @"ProgressBarGray.png"]; |
---|
60 | [fProgressGray setScalesWhenResized: YES]; |
---|
61 | [fProgressGray setSize: startSize]; |
---|
62 | |
---|
63 | fProgressGreen = [NSImage imageNamed: @"ProgressBarGreen.png"]; |
---|
64 | [fProgressGreen setScalesWhenResized: YES]; |
---|
65 | |
---|
66 | fProgressLightGreen = [NSImage imageNamed: @"ProgressBarLightGreen.png"]; |
---|
67 | [fProgressLightGreen setScalesWhenResized: YES]; |
---|
68 | |
---|
69 | fProgressAdvanced = [NSImage imageNamed: @"ProgressBarAdvanced.png"]; |
---|
70 | [fProgressAdvanced setScalesWhenResized: YES]; |
---|
71 | |
---|
72 | fProgressEndWhite = [NSImage imageNamed: @"ProgressBarEndWhite.png"]; |
---|
73 | fProgressEndBlue = [NSImage imageNamed: @"ProgressBarEndBlue.png"]; |
---|
74 | fProgressEndGray = [NSImage imageNamed: @"ProgressBarEndGray.png"]; |
---|
75 | fProgressEndGreen = [NSImage imageNamed: @"ProgressBarEndGreen.png"]; |
---|
76 | fProgressEndAdvanced = [NSImage imageNamed: @"ProgressBarEndAdvanced.png"]; |
---|
77 | |
---|
78 | fErrorImage = [[NSImage imageNamed: @"Error.tiff"] copy]; //no need to release for some reason |
---|
79 | [fErrorImage setFlipped: YES]; |
---|
80 | } |
---|
81 | return self; |
---|
82 | } |
---|
83 | |
---|
84 | - (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point |
---|
85 | { |
---|
86 | if (width <= 0.0) |
---|
87 | return; |
---|
88 | |
---|
89 | if ([barImage size].width < width) |
---|
90 | [barImage setSize: NSMakeSize(width * 2.0, BAR_HEIGHT)]; |
---|
91 | |
---|
92 | [barImage compositeToPoint: point fromRect: NSMakeRect(0, 0, width, BAR_HEIGHT) operation: NSCompositeSourceOver]; |
---|
93 | } |
---|
94 | |
---|
95 | - (void) buildSimpleBar: (float) width point: (NSPoint) point |
---|
96 | { |
---|
97 | NSDictionary * info = [self objectValue]; |
---|
98 | |
---|
99 | width -= 2.0; |
---|
100 | |
---|
101 | BOOL seeding = [[info objectForKey: @"Seeding"] boolValue], |
---|
102 | isActive = [[info objectForKey: @"Active"] boolValue]; |
---|
103 | float completedWidth, remainingWidth; |
---|
104 | |
---|
105 | //bar images and widths |
---|
106 | NSImage * barLeftEnd, * barRightEnd, * barComplete, * barRemaining; |
---|
107 | if (seeding) |
---|
108 | { |
---|
109 | float stopRatio, ratio; |
---|
110 | if ((stopRatio = [[info objectForKey: @"StopRatio"] floatValue]) != INVALID |
---|
111 | && (ratio = [[info objectForKey: @"Ratio"] floatValue]) < stopRatio) |
---|
112 | completedWidth = width * ratio / stopRatio; |
---|
113 | else |
---|
114 | completedWidth = width; |
---|
115 | remainingWidth = width - completedWidth; |
---|
116 | |
---|
117 | barLeftEnd = fProgressEndGreen; |
---|
118 | barRightEnd = fProgressEndGreen; |
---|
119 | barComplete = fProgressGreen; |
---|
120 | barRemaining = fProgressLightGreen; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | completedWidth = [[info objectForKey: @"Progress"] floatValue] * width; |
---|
125 | remainingWidth = width - completedWidth; |
---|
126 | |
---|
127 | if (remainingWidth == width) |
---|
128 | barLeftEnd = fProgressEndWhite; |
---|
129 | else if (isActive) |
---|
130 | barLeftEnd = fProgressEndBlue; |
---|
131 | else |
---|
132 | barLeftEnd = fProgressEndGray; |
---|
133 | |
---|
134 | if (completedWidth < width) |
---|
135 | barRightEnd = fProgressEndWhite; |
---|
136 | else if (isActive) |
---|
137 | barRightEnd = fProgressEndBlue; |
---|
138 | else |
---|
139 | barRightEnd = fProgressEndGray; |
---|
140 | |
---|
141 | barComplete = isActive ? fProgressBlue : fProgressGray; |
---|
142 | barRemaining = fProgressWhite; |
---|
143 | } |
---|
144 | |
---|
145 | //place bar |
---|
146 | [barLeftEnd compositeToPoint: point operation: NSCompositeSourceOver]; |
---|
147 | |
---|
148 | point.x += 1.0; |
---|
149 | [self placeBar: barComplete width: completedWidth point: point]; |
---|
150 | |
---|
151 | point.x += completedWidth; |
---|
152 | [self placeBar: barRemaining width: remainingWidth point: point]; |
---|
153 | |
---|
154 | point.x += remainingWidth; |
---|
155 | [barRightEnd compositeToPoint: point operation: NSCompositeSourceOver]; |
---|
156 | } |
---|
157 | |
---|
158 | - (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point |
---|
159 | { |
---|
160 | NSDictionary * info = [self objectValue]; |
---|
161 | |
---|
162 | //draw overlay over advanced bar |
---|
163 | [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver]; |
---|
164 | |
---|
165 | widthFloat -= 2.0; |
---|
166 | point.x += 1.0; |
---|
167 | |
---|
168 | //place actual advanced bar |
---|
169 | NSImage * image = [info objectForKey: @"AdvancedBar"]; |
---|
170 | [image setSize: NSMakeSize(widthFloat, BAR_HEIGHT)]; |
---|
171 | [image compositeToPoint: point operation: NSCompositeSourceOver]; |
---|
172 | |
---|
173 | [self placeBar: fProgressAdvanced width: widthFloat point: point]; |
---|
174 | |
---|
175 | point.x += widthFloat; |
---|
176 | [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver]; |
---|
177 | } |
---|
178 | |
---|
179 | - (void) toggleMinimalStatus |
---|
180 | { |
---|
181 | fStatusRegular = !fStatusRegular; |
---|
182 | [fDefaults setBool: fStatusRegular forKey: @"SmallStatusRegular"]; |
---|
183 | } |
---|
184 | |
---|
185 | - (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view |
---|
186 | { |
---|
187 | BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view] |
---|
188 | isEqual: [NSColor alternateSelectedControlColor]]; |
---|
189 | |
---|
190 | NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
---|
191 | [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail]; |
---|
192 | |
---|
193 | NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
194 | highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName, |
---|
195 | [NSFont messageFontOfSize: 12.0], NSFontAttributeName, |
---|
196 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
197 | NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
198 | highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName, |
---|
199 | [NSFont messageFontOfSize: 9.0], NSFontAttributeName, |
---|
200 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
201 | [paragraphStyle release]; |
---|
202 | |
---|
203 | NSPoint pen = cellFrame.origin; |
---|
204 | const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0; |
---|
205 | |
---|
206 | NSDictionary * info = [self objectValue]; |
---|
207 | |
---|
208 | if (![fDefaults boolForKey: @"SmallView"]) //regular size |
---|
209 | { |
---|
210 | //icon |
---|
211 | NSImage * icon = [info objectForKey: @"Icon"]; |
---|
212 | NSSize iconSize = [icon size]; |
---|
213 | |
---|
214 | pen.x += PADDING; |
---|
215 | pen.y += (cellFrame.size.height - iconSize.height) * 0.5; |
---|
216 | |
---|
217 | [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height) |
---|
218 | operation: NSCompositeSourceOver fraction: 1.0]; |
---|
219 | |
---|
220 | //error badge |
---|
221 | if ([[info objectForKey: @"Error"] boolValue]) |
---|
222 | { |
---|
223 | NSSize errorIconSize = [fErrorImage size]; |
---|
224 | [fErrorImage drawAtPoint: NSMakePoint(pen.x + iconSize.width - errorIconSize.width, |
---|
225 | pen.y + iconSize.height - errorIconSize.height) |
---|
226 | fromRect: NSMakeRect(0, 0, errorIconSize.width, errorIconSize.height) |
---|
227 | operation: NSCompositeSourceOver fraction: 1.0]; |
---|
228 | } |
---|
229 | |
---|
230 | float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT; |
---|
231 | |
---|
232 | //name string |
---|
233 | pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT; |
---|
234 | pen.y = cellFrame.origin.y + PADDING; |
---|
235 | |
---|
236 | NSString * nameString = [info objectForKey: @"Name"]; |
---|
237 | NSSize nameSize = [nameString sizeWithAttributes: nameAttributes]; |
---|
238 | [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, nameSize.height) withAttributes: nameAttributes]; |
---|
239 | |
---|
240 | //progress string |
---|
241 | pen.y += nameSize.height + LINE_PADDING - 1.0; |
---|
242 | |
---|
243 | NSString * progressString = [info objectForKey: @"ProgressString"]; |
---|
244 | NSSize progressSize = [progressString sizeWithAttributes: statusAttributes]; |
---|
245 | [progressString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, progressSize.height) withAttributes: statusAttributes]; |
---|
246 | |
---|
247 | //progress bar |
---|
248 | pen.x -= EXTRA_NAME_SHIFT; |
---|
249 | pen.y += progressSize.height + LINE_PADDING + BAR_HEIGHT; |
---|
250 | |
---|
251 | float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING; |
---|
252 | |
---|
253 | if ([fDefaults boolForKey: @"UseAdvancedBar"]) |
---|
254 | [self buildAdvancedBar: barWidth point: pen]; |
---|
255 | else |
---|
256 | [self buildSimpleBar: barWidth point: pen]; |
---|
257 | |
---|
258 | //status string |
---|
259 | pen.x += EXTRA_NAME_SHIFT; |
---|
260 | pen.y += LINE_PADDING; |
---|
261 | |
---|
262 | NSString * statusString = [info objectForKey: @"StatusString"]; |
---|
263 | NSSize statusSize = [statusString sizeWithAttributes: statusAttributes]; |
---|
264 | [statusString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, statusSize.height) withAttributes: statusAttributes]; |
---|
265 | } |
---|
266 | else //small size |
---|
267 | { |
---|
268 | //icon |
---|
269 | NSImage * icon = ![[info objectForKey: @"Error"] boolValue] ? [info objectForKey: @"Icon"] : fErrorImage; |
---|
270 | NSSize iconSize = [icon size]; |
---|
271 | |
---|
272 | pen.x += PADDING; |
---|
273 | pen.y += (cellFrame.size.height - iconSize.height) * 0.5; |
---|
274 | |
---|
275 | [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height) |
---|
276 | operation: NSCompositeSourceOver fraction: 1.0]; |
---|
277 | |
---|
278 | //name and status string |
---|
279 | float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT; |
---|
280 | |
---|
281 | //place name string |
---|
282 | pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT; |
---|
283 | pen.y = cellFrame.origin.y + LINE_PADDING; |
---|
284 | |
---|
285 | NSString * nameString = [info objectForKey: @"Name"]; |
---|
286 | NSSize nameSize = [nameString sizeWithAttributes: nameAttributes]; |
---|
287 | |
---|
288 | NSString * statusString = !fStatusRegular && [[info objectForKey: @"Active"] boolValue] |
---|
289 | ? [info objectForKey: @"RemainingTimeString"] |
---|
290 | : [info objectForKey: @"ShortStatusString"]; |
---|
291 | NSSize statusSize = [statusString sizeWithAttributes: statusAttributes]; |
---|
292 | |
---|
293 | [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth - statusSize.width - 2.0 * LINE_PADDING, nameSize.height) |
---|
294 | withAttributes: nameAttributes]; |
---|
295 | |
---|
296 | //place status string |
---|
297 | pen.x = NSMaxX(cellFrame) - PADDING - statusSize.width; |
---|
298 | pen.y += (nameSize.height - statusSize.height) * 0.5; |
---|
299 | |
---|
300 | [statusString drawInRect: NSMakeRect(pen.x, pen.y, statusSize.width, statusSize.height) |
---|
301 | withAttributes: statusAttributes]; |
---|
302 | |
---|
303 | //progress bar |
---|
304 | pen.x = cellFrame.origin.x + iconSize.width + 2.0 * PADDING; |
---|
305 | pen.y = cellFrame.origin.y + nameSize.height + LINE_PADDING + PADDING + BAR_HEIGHT; |
---|
306 | |
---|
307 | float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING; |
---|
308 | |
---|
309 | if ([fDefaults boolForKey: @"UseAdvancedBar"]) |
---|
310 | [self buildAdvancedBar: barWidth point: pen]; |
---|
311 | else |
---|
312 | [self buildSimpleBar: barWidth point: pen]; |
---|
313 | } |
---|
314 | |
---|
315 | [nameAttributes release]; |
---|
316 | [statusAttributes release]; |
---|
317 | } |
---|
318 | |
---|
319 | @end |
---|