source: trunk/macosx/TorrentTableView.m @ 2879

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

add a menu additions class

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