source: trunk/macosx/TorrentTableView.m @ 3384

Last change on this file since 3384 was 3384, checked in by livings124, 15 years ago

trivial code cleanup

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