source: trunk/macosx/TorrentTableView.m @ 2840

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

remove some warnings

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