1 | /****************************************************************************** |
---|
2 | * $Id: TorrentTableView.m 3702 2007-11-04 16:45:17Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-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 "TorrentTableView.h" |
---|
26 | #import "TorrentCell.h" |
---|
27 | #import "Controller.h" |
---|
28 | #import "Torrent.h" |
---|
29 | #import "NSApplicationAdditions.h" |
---|
30 | #import "NSMenuAdditions.h" |
---|
31 | |
---|
32 | #define PADDING 3.0 |
---|
33 | |
---|
34 | #define BUTTON_TO_TOP_REGULAR 33.0 |
---|
35 | #define BUTTON_TO_TOP_SMALL 20.0 |
---|
36 | |
---|
37 | #define ACTION_BUTTON_TO_TOP 47.0 |
---|
38 | |
---|
39 | #define ACTION_MENU_GLOBAL_TAG 101 |
---|
40 | #define ACTION_MENU_UNLIMITED_TAG 102 |
---|
41 | #define ACTION_MENU_LIMIT_TAG 103 |
---|
42 | |
---|
43 | @interface TorrentTableView (Private) |
---|
44 | |
---|
45 | - (NSRect) pauseRectForRow: (int) row; |
---|
46 | - (NSRect) revealRectForRow: (int) row; |
---|
47 | - (NSRect) actionRectForRow: (int) row; |
---|
48 | |
---|
49 | - (BOOL) pointInIconRect: (NSPoint) point; |
---|
50 | - (BOOL) pointInProgressRect: (NSPoint) point; |
---|
51 | - (BOOL) pointInMinimalStatusRect: (NSPoint) point; |
---|
52 | |
---|
53 | - (BOOL) pointInPauseRect: (NSPoint) point; |
---|
54 | - (BOOL) pointInRevealRect: (NSPoint) point; |
---|
55 | - (BOOL) pointInActionRect: (NSPoint) point; |
---|
56 | |
---|
57 | - (void) updateFileMenu: (NSMenu *) menu forFiles: (NSArray *) files; |
---|
58 | |
---|
59 | @end |
---|
60 | |
---|
61 | @implementation TorrentTableView |
---|
62 | |
---|
63 | - (id) initWithCoder: (NSCoder *) decoder |
---|
64 | { |
---|
65 | if ((self = [super initWithCoder: decoder])) |
---|
66 | { |
---|
67 | fClickPoint = NSZeroPoint; |
---|
68 | fClickIn = NO; |
---|
69 | |
---|
70 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
71 | |
---|
72 | [self setDelegate: self]; |
---|
73 | } |
---|
74 | |
---|
75 | return self; |
---|
76 | } |
---|
77 | |
---|
78 | - (void) dealloc |
---|
79 | { |
---|
80 | [fKeyStrokes release]; |
---|
81 | [fMenuTorrent release]; |
---|
82 | |
---|
83 | [super dealloc]; |
---|
84 | } |
---|
85 | |
---|
86 | - (void) setTorrents: (NSArray *) torrents |
---|
87 | { |
---|
88 | fTorrents = torrents; |
---|
89 | } |
---|
90 | |
---|
91 | - (void) tableView: (NSTableView *) tableView willDisplayCell: (id) cell |
---|
92 | forTableColumn: (NSTableColumn *) tableColumn row: (int) row |
---|
93 | { |
---|
94 | [cell setRepresentedObject: [fTorrents objectAtIndex: row]]; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | - (NSString *) tableView: (NSTableView *) tableView typeSelectStringForTableColumn: (NSTableColumn *) tableColumn row: (int) row |
---|
99 | { |
---|
100 | return [[fTorrents objectAtIndex: row] name]; |
---|
101 | } |
---|
102 | |
---|
103 | - (void) mouseDown: (NSEvent *) event |
---|
104 | { |
---|
105 | fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil]; |
---|
106 | |
---|
107 | if ([self pointInActionRect: fClickPoint]) |
---|
108 | { |
---|
109 | int row = [self rowAtPoint: fClickPoint]; |
---|
110 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; //ensure button is pushed down |
---|
111 | |
---|
112 | [self displayTorrentMenuForEvent: event]; |
---|
113 | |
---|
114 | fClickPoint = NSZeroPoint; |
---|
115 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; |
---|
116 | } |
---|
117 | else if ([self pointInPauseRect: fClickPoint] || [self pointInRevealRect: fClickPoint]) |
---|
118 | { |
---|
119 | fClickIn = YES; |
---|
120 | [self setNeedsDisplayInRect: [self rectOfRow: [self rowAtPoint: fClickPoint]]]; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | if ([self pointInMinimalStatusRect: fClickPoint]) |
---|
125 | { |
---|
126 | [fDefaults setBool: ![fDefaults boolForKey: @"DisplaySmallStatusRegular"] forKey: @"DisplaySmallStatusRegular"]; |
---|
127 | fClickPoint = NSZeroPoint; |
---|
128 | [self reloadData]; |
---|
129 | } |
---|
130 | |
---|
131 | [super mouseDown: event]; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | #warning not working on Leopard |
---|
136 | - (void) mouseUp: (NSEvent *) event |
---|
137 | { |
---|
138 | //NSLog(@"up"); |
---|
139 | NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil]; |
---|
140 | int row = [self rowAtPoint: point], oldRow = [self rowAtPoint: fClickPoint]; |
---|
141 | |
---|
142 | if (row == oldRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint]) |
---|
143 | { |
---|
144 | Torrent * torrent = [fTorrents objectAtIndex: row]; |
---|
145 | |
---|
146 | if ([torrent isActive]) |
---|
147 | [fController stopTorrents: [NSArray arrayWithObject: torrent]]; |
---|
148 | else |
---|
149 | { |
---|
150 | if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) |
---|
151 | [fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]]; |
---|
152 | else if ([torrent waitingToStart]) |
---|
153 | [fController stopTorrents: [NSArray arrayWithObject: torrent]]; |
---|
154 | else |
---|
155 | [fController resumeTorrents: [NSArray arrayWithObject: torrent]]; |
---|
156 | } |
---|
157 | } |
---|
158 | else if (row == oldRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint]) |
---|
159 | [[fTorrents objectAtIndex: row] revealData]; |
---|
160 | else if ([event clickCount] == 2 && !NSEqualPoints(fClickPoint, NSZeroPoint)) |
---|
161 | { |
---|
162 | if ([self pointInProgressRect: fClickPoint]) |
---|
163 | { |
---|
164 | [fDefaults setBool: ![fDefaults boolForKey: @"DisplayStatusProgressSelected"] forKey: @"DisplayStatusProgressSelected"]; |
---|
165 | [self reloadData]; |
---|
166 | } |
---|
167 | else if ([self pointInIconRect: point]) |
---|
168 | [[fTorrents objectAtIndex: row] revealData]; |
---|
169 | else if (![self pointInActionRect: point]) |
---|
170 | [fController showInfo: nil]; |
---|
171 | else; |
---|
172 | } |
---|
173 | else; |
---|
174 | |
---|
175 | [super mouseUp: event]; |
---|
176 | |
---|
177 | fClickPoint = NSZeroPoint; |
---|
178 | fClickIn = NO; |
---|
179 | [self setNeedsDisplayInRect: [self rectOfRow: oldRow]]; |
---|
180 | } |
---|
181 | |
---|
182 | - (void) mouseDragged: (NSEvent *) event |
---|
183 | { |
---|
184 | if (NSEqualPoints(fClickPoint, NSZeroPoint)) |
---|
185 | { |
---|
186 | [super mouseDragged: event]; |
---|
187 | return; |
---|
188 | } |
---|
189 | |
---|
190 | NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil]; |
---|
191 | int oldRow = [self rowAtPoint: fClickPoint]; |
---|
192 | |
---|
193 | BOOL inRect; |
---|
194 | if ([self pointInRevealRect: fClickPoint]) |
---|
195 | inRect = oldRow == [self rowAtPoint: point] && [self pointInRevealRect: point]; |
---|
196 | else if ([self pointInPauseRect: fClickPoint]) |
---|
197 | inRect = oldRow == [self rowAtPoint: point] && [self pointInPauseRect: point]; |
---|
198 | else |
---|
199 | { |
---|
200 | [super mouseDragged: event]; |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | if (inRect != fClickIn) |
---|
205 | { |
---|
206 | fClickIn = inRect; |
---|
207 | [self setNeedsDisplayInRect: [self rectOfRow: oldRow]]; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | - (NSMenu *) menuForEvent: (NSEvent *) event |
---|
212 | { |
---|
213 | int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; |
---|
214 | |
---|
215 | if (row >= 0) |
---|
216 | { |
---|
217 | if (![self isRowSelected: row]) |
---|
218 | [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO]; |
---|
219 | return fContextRow; |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | [self deselectAll: self]; |
---|
224 | return fContextNoRow; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | - (void) flagsChanged: (NSEvent *) event |
---|
229 | { |
---|
230 | [self display]; |
---|
231 | [super flagsChanged: event]; |
---|
232 | } |
---|
233 | |
---|
234 | - (void) keyDown: (NSEvent *) event |
---|
235 | { |
---|
236 | //this is handled by the delegate in Leopard |
---|
237 | if ([NSApp isOnLeopardOrBetter]) |
---|
238 | { |
---|
239 | [super keyDown: event]; |
---|
240 | return; |
---|
241 | } |
---|
242 | |
---|
243 | if (!fKeyStrokes) |
---|
244 | fKeyStrokes = [[NSMutableArray alloc] init]; |
---|
245 | |
---|
246 | unichar newChar = [[event characters] characterAtIndex: 0]; |
---|
247 | if (newChar == ' ' || [[NSCharacterSet alphanumericCharacterSet] characterIsMember: newChar] |
---|
248 | || [[NSCharacterSet symbolCharacterSet] characterIsMember: newChar] |
---|
249 | || [[NSCharacterSet punctuationCharacterSet] characterIsMember: newChar]) |
---|
250 | { |
---|
251 | if ([fKeyStrokes count] > 0 && [event timestamp] - [[fKeyStrokes lastObject] timestamp] > 1.0) |
---|
252 | [fKeyStrokes removeAllObjects]; |
---|
253 | [fKeyStrokes addObject: event]; |
---|
254 | |
---|
255 | [self interpretKeyEvents: fKeyStrokes]; |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | [fKeyStrokes removeAllObjects]; |
---|
260 | [super keyDown: event]; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | - (void) insertText: (NSString *) text |
---|
265 | { |
---|
266 | //this is handled by the delegate in Leopard |
---|
267 | if ([NSApp isOnLeopardOrBetter]) |
---|
268 | { |
---|
269 | [super insertText: text]; |
---|
270 | return; |
---|
271 | } |
---|
272 | |
---|
273 | //sort torrents by name before finding closest match |
---|
274 | NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES |
---|
275 | selector: @selector(caseInsensitiveCompare:)] autorelease]; |
---|
276 | NSArray * descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil]; |
---|
277 | |
---|
278 | NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors]; |
---|
279 | [descriptors release]; |
---|
280 | |
---|
281 | text = [text lowercaseString]; |
---|
282 | |
---|
283 | //select torrent closest to text that isn't before text alphabetically |
---|
284 | NSEnumerator * enumerator = [tempTorrents objectEnumerator]; |
---|
285 | Torrent * torrent; |
---|
286 | while ((torrent = [enumerator nextObject])) |
---|
287 | if ([[[torrent name] lowercaseString] hasPrefix: text]) |
---|
288 | { |
---|
289 | int row = [fTorrents indexOfObject: torrent]; |
---|
290 | [self selectRow: row byExtendingSelection: NO]; |
---|
291 | [self scrollRowToVisible: row]; |
---|
292 | return; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | - (void) displayTorrentMenuForEvent: (NSEvent *) event |
---|
297 | { |
---|
298 | int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; |
---|
299 | if (row < 0) |
---|
300 | return; |
---|
301 | |
---|
302 | //get and update file menu |
---|
303 | fMenuTorrent = [[fTorrents objectAtIndex: row] retain]; |
---|
304 | NSMenu * fileMenu = [fMenuTorrent fileMenu]; |
---|
305 | [self updateFileMenu: fileMenu forFiles: [fMenuTorrent fileList]]; |
---|
306 | |
---|
307 | //add file menu items to action menu |
---|
308 | NSRange range = NSMakeRange(0, [fileMenu numberOfItems]); |
---|
309 | [fActionMenu appendItemsFromMenu: fileMenu atIndexes: [NSIndexSet indexSetWithIndexesInRange: range]]; |
---|
310 | |
---|
311 | //place menu below button |
---|
312 | NSRect rect = [self actionRectForRow: row]; |
---|
313 | NSPoint location = rect.origin; |
---|
314 | location.y += rect.size.height + 5.0; |
---|
315 | location = [self convertPoint: location toView: nil]; |
---|
316 | |
---|
317 | NSEvent * newEvent = [NSEvent mouseEventWithType: [event type] location: location |
---|
318 | modifierFlags: [event modifierFlags] timestamp: [event timestamp] windowNumber: [event windowNumber] |
---|
319 | context: [event context] eventNumber: [event eventNumber] clickCount: [event clickCount] pressure: [event pressure]]; |
---|
320 | |
---|
321 | [NSMenu popUpContextMenu: fActionMenu withEvent: newEvent forView: self]; |
---|
322 | |
---|
323 | //move file menu items back to the torrent's file menu |
---|
324 | range.location = [fActionMenu numberOfItems] - range.length; |
---|
325 | [fileMenu appendItemsFromMenu: fActionMenu atIndexes: [NSIndexSet indexSetWithIndexesInRange: range]]; |
---|
326 | |
---|
327 | [fMenuTorrent release]; |
---|
328 | fMenuTorrent = nil; |
---|
329 | } |
---|
330 | |
---|
331 | - (void) menuNeedsUpdate: (NSMenu *) menu |
---|
332 | { |
---|
333 | //this method seems to be called when it shouldn't be |
---|
334 | if (!fMenuTorrent || ![menu supermenu]) |
---|
335 | return; |
---|
336 | |
---|
337 | if (menu == fUploadMenu || menu == fDownloadMenu) |
---|
338 | { |
---|
339 | BOOL upload = menu == fUploadMenu; |
---|
340 | int mode = [fMenuTorrent speedMode: upload]; |
---|
341 | |
---|
342 | NSMenuItem * item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG]; |
---|
343 | [item setState: mode == TR_SPEEDLIMIT_SINGLE ? NSOnState : NSOffState]; |
---|
344 | [item setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)", |
---|
345 | "torrent action context menu -> upload/download limit"), [fMenuTorrent speedLimit: upload]]]; |
---|
346 | |
---|
347 | item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG]; |
---|
348 | [item setState: mode == TR_SPEEDLIMIT_UNLIMITED ? NSOnState : NSOffState]; |
---|
349 | |
---|
350 | item = [menu itemWithTag: ACTION_MENU_GLOBAL_TAG]; |
---|
351 | [item setState: mode == TR_SPEEDLIMIT_GLOBAL ? NSOnState : NSOffState]; |
---|
352 | } |
---|
353 | else if (menu == fRatioMenu) |
---|
354 | { |
---|
355 | int mode = [fMenuTorrent ratioSetting]; |
---|
356 | |
---|
357 | NSMenuItem * item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG]; |
---|
358 | [item setState: mode == NSOnState ? NSOnState : NSOffState]; |
---|
359 | [item setTitle: [NSString stringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)", |
---|
360 | "torrent action context menu -> ratio stop"), [fMenuTorrent ratioLimit]]]; |
---|
361 | |
---|
362 | item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG]; |
---|
363 | [item setState: mode == NSOffState ? NSOnState : NSOffState]; |
---|
364 | |
---|
365 | item = [menu itemWithTag: ACTION_MENU_GLOBAL_TAG]; |
---|
366 | [item setState: mode == NSMixedState ? NSOnState : NSOffState]; |
---|
367 | } |
---|
368 | else if ([menu supermenu]) //assume the menu is part of the file list |
---|
369 | { |
---|
370 | NSMenu * supermenu = [menu supermenu]; |
---|
371 | [self updateFileMenu: menu forFiles: [[[supermenu itemAtIndex: [supermenu indexOfItemWithSubmenu: menu]] |
---|
372 | representedObject] objectForKey: @"Children"]]; |
---|
373 | } |
---|
374 | else; |
---|
375 | } |
---|
376 | |
---|
377 | - (void) setQuickLimitMode: (id) sender |
---|
378 | { |
---|
379 | int mode; |
---|
380 | switch ([sender tag]) |
---|
381 | { |
---|
382 | case ACTION_MENU_UNLIMITED_TAG: |
---|
383 | mode = TR_SPEEDLIMIT_UNLIMITED; |
---|
384 | break; |
---|
385 | case ACTION_MENU_LIMIT_TAG: |
---|
386 | mode = TR_SPEEDLIMIT_SINGLE; |
---|
387 | break; |
---|
388 | case ACTION_MENU_GLOBAL_TAG: |
---|
389 | mode = TR_SPEEDLIMIT_GLOBAL; |
---|
390 | break; |
---|
391 | default: |
---|
392 | return; |
---|
393 | } |
---|
394 | |
---|
395 | [fMenuTorrent setSpeedMode: mode upload: [sender menu] == fUploadMenu]; |
---|
396 | |
---|
397 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
398 | } |
---|
399 | |
---|
400 | - (void) setQuickLimit: (id) sender |
---|
401 | { |
---|
402 | BOOL upload = [sender menu] == fUploadMenu; |
---|
403 | [fMenuTorrent setSpeedMode: TR_SPEEDLIMIT_SINGLE upload: upload]; |
---|
404 | [fMenuTorrent setSpeedLimit: [[sender title] intValue] upload: upload]; |
---|
405 | |
---|
406 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
407 | } |
---|
408 | |
---|
409 | - (void) setQuickRatioMode: (id) sender |
---|
410 | { |
---|
411 | int mode; |
---|
412 | switch ([sender tag]) |
---|
413 | { |
---|
414 | case ACTION_MENU_UNLIMITED_TAG: |
---|
415 | mode = NSOffState; |
---|
416 | break; |
---|
417 | case ACTION_MENU_LIMIT_TAG: |
---|
418 | mode = NSOnState; |
---|
419 | break; |
---|
420 | case ACTION_MENU_GLOBAL_TAG: |
---|
421 | mode = NSMixedState; |
---|
422 | break; |
---|
423 | default: |
---|
424 | return; |
---|
425 | } |
---|
426 | |
---|
427 | [fMenuTorrent setRatioSetting: mode]; |
---|
428 | |
---|
429 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
430 | } |
---|
431 | |
---|
432 | - (void) setQuickRatio: (id) sender |
---|
433 | { |
---|
434 | [fMenuTorrent setRatioSetting: NSOnState]; |
---|
435 | [fMenuTorrent setRatioLimit: [[sender title] floatValue]]; |
---|
436 | |
---|
437 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
438 | } |
---|
439 | |
---|
440 | - (void) checkFile: (id) sender |
---|
441 | { |
---|
442 | NSIndexSet * indexSet = [[sender representedObject] objectForKey: @"Indexes"]; |
---|
443 | [fMenuTorrent setFileCheckState: [sender state] != NSOnState ? NSOnState : NSOffState forIndexes: indexSet]; |
---|
444 | |
---|
445 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil]; |
---|
446 | } |
---|
447 | |
---|
448 | - (void) drawRow: (int) row clipRect: (NSRect) rect |
---|
449 | { |
---|
450 | [super drawRow: row clipRect: rect]; |
---|
451 | |
---|
452 | Torrent * torrent = [fTorrents objectAtIndex: row]; |
---|
453 | |
---|
454 | //pause/resume icon |
---|
455 | NSImage * pauseImage = nil; |
---|
456 | NSRect pauseRect = [self pauseRectForRow: row]; |
---|
457 | if ([torrent isActive]) |
---|
458 | pauseImage = fClickIn && NSPointInRect(fClickPoint, pauseRect) ? [NSImage imageNamed: @"PauseOn.png"] |
---|
459 | : [NSImage imageNamed: @"PauseOff.png"]; |
---|
460 | else |
---|
461 | { |
---|
462 | BOOL inPauseRect = fClickIn && NSPointInRect(fClickPoint, pauseRect); |
---|
463 | if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask && [fDefaults boolForKey: @"Queue"]) |
---|
464 | pauseImage = inPauseRect ? [NSImage imageNamed: @"ResumeNoWaitOn.png"] : [NSImage imageNamed: @"ResumeNoWaitOff.png"]; |
---|
465 | else if ([torrent waitingToStart]) |
---|
466 | pauseImage = inPauseRect ? [NSImage imageNamed: @"PauseOn.png"] : [NSImage imageNamed: @"PauseOff.png"]; |
---|
467 | else |
---|
468 | pauseImage = inPauseRect ? [NSImage imageNamed: @"ResumeOn.png"] : [NSImage imageNamed: @"ResumeOff.png"]; |
---|
469 | } |
---|
470 | |
---|
471 | if (pauseImage) |
---|
472 | [pauseImage compositeToPoint: NSMakePoint(pauseRect.origin.x, NSMaxY(pauseRect)) operation: NSCompositeSourceOver]; |
---|
473 | |
---|
474 | //reveal icon |
---|
475 | NSRect revealRect = [self revealRectForRow: row]; |
---|
476 | NSImage * revealImage = fClickIn && NSPointInRect(fClickPoint, revealRect) ? [NSImage imageNamed: @"RevealOn.png"] |
---|
477 | : [NSImage imageNamed: @"RevealOff.png"]; |
---|
478 | [revealImage compositeToPoint: NSMakePoint(revealRect.origin.x, NSMaxY(revealRect)) operation: NSCompositeSourceOver]; |
---|
479 | |
---|
480 | //action icon |
---|
481 | if (![fDefaults boolForKey: @"SmallView"]) |
---|
482 | { |
---|
483 | NSRect actionRect = [self actionRectForRow: row]; |
---|
484 | NSImage * actionImage = NSPointInRect(fClickPoint, actionRect) ? [NSImage imageNamed: @"ActionOn.png"] |
---|
485 | : [NSImage imageNamed: @"ActionOff.png"]; |
---|
486 | [actionImage compositeToPoint: NSMakePoint(actionRect.origin.x, NSMaxY(actionRect)) operation: NSCompositeSourceOver]; |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | @end |
---|
491 | |
---|
492 | @implementation TorrentTableView (Private) |
---|
493 | |
---|
494 | - (NSRect) pauseRectForRow: (int) row |
---|
495 | { |
---|
496 | if (row < 0) |
---|
497 | return NSZeroRect; |
---|
498 | |
---|
499 | NSRect cellRect = [self frameOfCellAtColumn: 0 row: row]; |
---|
500 | |
---|
501 | float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR; |
---|
502 | |
---|
503 | return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH - PADDING - BUTTON_WIDTH, |
---|
504 | cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH); |
---|
505 | } |
---|
506 | |
---|
507 | - (NSRect) revealRectForRow: (int) row |
---|
508 | { |
---|
509 | if (row < 0) |
---|
510 | return NSZeroRect; |
---|
511 | |
---|
512 | NSRect cellRect = [self frameOfCellAtColumn: 0 row: row]; |
---|
513 | |
---|
514 | float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR; |
---|
515 | |
---|
516 | return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH, |
---|
517 | cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH); |
---|
518 | } |
---|
519 | |
---|
520 | - (NSRect) actionRectForRow: (int) row |
---|
521 | { |
---|
522 | if (row < 0) |
---|
523 | return NSZeroRect; |
---|
524 | |
---|
525 | TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell]; |
---|
526 | NSRect cellRect = [self frameOfCellAtColumn: 0 row: row], |
---|
527 | iconRect = [cell iconRectForBounds: cellRect]; |
---|
528 | |
---|
529 | if ([fDefaults boolForKey: @"SmallView"]) |
---|
530 | return iconRect; |
---|
531 | else |
---|
532 | return NSMakeRect(iconRect.origin.x + (iconRect.size.width - ACTION_BUTTON_WIDTH) * 0.5, |
---|
533 | cellRect.origin.y + ACTION_BUTTON_TO_TOP, ACTION_BUTTON_WIDTH, ACTION_BUTTON_HEIGHT); |
---|
534 | } |
---|
535 | |
---|
536 | - (BOOL) pointInIconRect: (NSPoint) point |
---|
537 | { |
---|
538 | int row = [self rowAtPoint: point]; |
---|
539 | if (row < 0) |
---|
540 | return NO; |
---|
541 | |
---|
542 | TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell]; |
---|
543 | return NSPointInRect(point, [cell iconRectForBounds: [self frameOfCellAtColumn: 0 row: row]]); |
---|
544 | } |
---|
545 | |
---|
546 | - (BOOL) pointInProgressRect: (NSPoint) point |
---|
547 | { |
---|
548 | int row = [self rowAtPoint: point]; |
---|
549 | if (row < 0 || [fDefaults boolForKey: @"SmallView"]) |
---|
550 | return NO; |
---|
551 | |
---|
552 | TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell]; |
---|
553 | [cell setRepresentedObject: [fTorrents objectAtIndex: row]]; |
---|
554 | return NSPointInRect(point, [cell progressRectForBounds: [self frameOfCellAtColumn: 0 row: row]]); |
---|
555 | } |
---|
556 | |
---|
557 | - (BOOL) pointInMinimalStatusRect: (NSPoint) point |
---|
558 | { |
---|
559 | int row = [self rowAtPoint: point]; |
---|
560 | if (row < 0 || ![fDefaults boolForKey: @"SmallView"]) |
---|
561 | return NO; |
---|
562 | |
---|
563 | TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell]; |
---|
564 | [cell setRepresentedObject: [fTorrents objectAtIndex: row]]; |
---|
565 | return NSPointInRect(point, [cell minimalStatusRectForBounds: [self frameOfCellAtColumn: 0 row: row]]); |
---|
566 | } |
---|
567 | |
---|
568 | - (BOOL) pointInPauseRect: (NSPoint) point |
---|
569 | { |
---|
570 | int row = [self rowAtPoint: point]; |
---|
571 | if (row < 0) |
---|
572 | return NO; |
---|
573 | |
---|
574 | return NSPointInRect(point, [self pauseRectForRow: row]); |
---|
575 | } |
---|
576 | |
---|
577 | - (BOOL) pointInRevealRect: (NSPoint) point |
---|
578 | { |
---|
579 | int row = [self rowAtPoint: point]; |
---|
580 | if (row < 0) |
---|
581 | return NO; |
---|
582 | |
---|
583 | return NSPointInRect(point, [self revealRectForRow: row]); |
---|
584 | } |
---|
585 | |
---|
586 | - (BOOL) pointInActionRect: (NSPoint) point |
---|
587 | { |
---|
588 | int row = [self rowAtPoint: point]; |
---|
589 | if (row < 0) |
---|
590 | return NO; |
---|
591 | |
---|
592 | return NSPointInRect(point, [self actionRectForRow: row]); |
---|
593 | } |
---|
594 | |
---|
595 | - (void) updateFileMenu: (NSMenu *) menu forFiles: (NSArray *) files |
---|
596 | { |
---|
597 | BOOL create = [menu numberOfItems] <= 0; |
---|
598 | |
---|
599 | NSEnumerator * enumerator = [files objectEnumerator]; |
---|
600 | NSDictionary * dict; |
---|
601 | NSMenuItem * item; |
---|
602 | while ((dict = [enumerator nextObject])) |
---|
603 | { |
---|
604 | NSString * name = [dict objectForKey: @"Name"]; |
---|
605 | |
---|
606 | if (create) |
---|
607 | { |
---|
608 | item = [[NSMenuItem alloc] initWithTitle: name action: NULL keyEquivalent: @""]; |
---|
609 | |
---|
610 | NSImage * icon; |
---|
611 | if (![[dict objectForKey: @"IsFolder"] boolValue]) |
---|
612 | { |
---|
613 | icon = [[dict objectForKey: @"Icon"] copy]; |
---|
614 | [icon setFlipped: NO]; |
---|
615 | } |
---|
616 | else |
---|
617 | { |
---|
618 | NSMenu * itemMenu = [[NSMenu alloc] initWithTitle: name]; |
---|
619 | [itemMenu setAutoenablesItems: NO]; |
---|
620 | [item setSubmenu: itemMenu]; |
---|
621 | [itemMenu setDelegate: self]; |
---|
622 | [itemMenu release]; |
---|
623 | |
---|
624 | icon = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy]; |
---|
625 | } |
---|
626 | |
---|
627 | [item setRepresentedObject: dict]; |
---|
628 | |
---|
629 | [icon setScalesWhenResized: YES]; |
---|
630 | [icon setSize: NSMakeSize(16.0, 16.0)]; |
---|
631 | [item setImage: icon]; |
---|
632 | [icon release]; |
---|
633 | |
---|
634 | [item setAction: @selector(checkFile:)]; |
---|
635 | |
---|
636 | [menu addItem: item]; |
---|
637 | [item release]; |
---|
638 | } |
---|
639 | else |
---|
640 | item = [menu itemWithTitle: name]; |
---|
641 | |
---|
642 | NSIndexSet * indexSet = [dict objectForKey: @"Indexes"]; |
---|
643 | [item setState: [fMenuTorrent checkForFiles: indexSet]]; |
---|
644 | [item setEnabled: [fMenuTorrent canChangeDownloadCheckForFiles: indexSet]]; |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | @end |
---|