source: trunk/macosx/TorrentTableView.m @ 10398

Last change on this file since 10398 was 10398, checked in by livings124, 12 years ago

first attempt at a single-row, more compact Compact (Minimal) View

  • Property svn:keywords set to Date Rev Author Id
File size: 29.3 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 10398 2010-03-20 03:27:17Z livings124 $
3 *
4 * Copyright (c) 2005-2010 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 "Controller.h"
27#import "FileListNode.h"
28#import "NSApplicationAdditions.h"
29#import "Torrent.h"
30#import "TorrentCell.h"
31#import "TorrentGroup.h"
32
33#define MAX_GROUP 999999
34
35#define ACTION_MENU_GLOBAL_TAG 101
36#define ACTION_MENU_UNLIMITED_TAG 102
37#define ACTION_MENU_LIMIT_TAG 103
38
39#define ACTION_MENU_PRIORITY_HIGH_TAG 101
40#define ACTION_MENU_PRIORITY_NORMAL_TAG 102
41#define ACTION_MENU_PRIORITY_LOW_TAG 103
42
43#define TOGGLE_PROGRESS_SECONDS 0.175
44
45@interface TorrentTableView (Private)
46
47- (BOOL) pointInGroupStatusRect: (NSPoint) point;
48
49- (void) setGroupStatusColumns;
50
51@end
52
53@implementation TorrentTableView
54
55- (id) initWithCoder: (NSCoder *) decoder
56{
57    if ((self = [super initWithCoder: decoder]))
58    {
59        fDefaults = [NSUserDefaults standardUserDefaults];
60       
61        fTorrentCell = [[TorrentCell alloc] init];
62       
63        NSData * groupData = [fDefaults dataForKey: @"CollapsedGroups"];
64        if (groupData)
65            fCollapsedGroups = [[NSUnarchiver unarchiveObjectWithData: groupData] mutableCopy];
66        else
67            fCollapsedGroups = [[NSMutableIndexSet alloc] init];
68       
69        fMouseRow = -1;
70        fMouseControlRow = -1;
71        fMouseRevealRow = -1;
72        fMouseActionRow = -1;
73        fActionPushedRow = -1;
74       
75        [self setDelegate: self];
76       
77        fPiecesBarPercent = [fDefaults boolForKey: @"PiecesBar"] ? 1.0 : 0.0;
78    }
79   
80    return self;
81}
82
83- (void) dealloc
84{
85    [fCollapsedGroups release];
86   
87    [fPiecesBarAnimation release];
88    [fMenuTorrent release];
89   
90    [fSelectedValues release];
91   
92    [fTorrentCell release];
93   
94    [super dealloc];
95}
96
97- (void) awakeFromNib
98{
99    //set group columns to show ratio, needs to be in awakeFromNib to size columns correctly
100    [self setGroupStatusColumns];
101}
102
103- (BOOL) isGroupCollapsed: (NSInteger) value
104{
105    if (value == -1)
106        value = MAX_GROUP;
107   
108    return [fCollapsedGroups containsIndex: value];
109}
110
111- (void) removeCollapsedGroup: (NSInteger) value
112{
113    if (value == -1)
114        value = MAX_GROUP;
115   
116    [fCollapsedGroups removeIndex: value];
117}
118
119- (void) removeAllCollapsedGroups
120{
121    [fCollapsedGroups removeAllIndexes];
122}
123
124- (void) saveCollapsedGroups
125{
126    [fDefaults setObject: [NSArchiver archivedDataWithRootObject: fCollapsedGroups] forKey: @"CollapsedGroups"];
127}
128
129- (BOOL) outlineView: (NSOutlineView *) outlineView isGroupItem: (id) item
130{
131    return ![item isKindOfClass: [Torrent class]];
132}
133
134- (CGFloat) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item
135{
136    return [item isKindOfClass: [Torrent class]] ? [self rowHeight] : GROUP_SEPARATOR_HEIGHT;
137}
138
139- (NSCell *) outlineView: (NSOutlineView *) outlineView dataCellForTableColumn: (NSTableColumn *) tableColumn item: (id) item
140{
141    const BOOL group = ![item isKindOfClass: [Torrent class]];
142    if (!tableColumn)
143        return !group ? fTorrentCell : nil;
144    else
145        return group ? [tableColumn dataCellForRow: [self rowForItem: item]] : nil;
146}
147
148- (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell forTableColumn: (NSTableColumn *) tableColumn
149    item: (id) item
150{
151    if ([item isKindOfClass: [Torrent class]])
152    {
153        [cell setRepresentedObject: item];
154       
155        const NSInteger row = [self rowForItem: item];
156        [cell setHover: row == fMouseRow];
157        [cell setControlHover: row == fMouseControlRow];
158        [cell setRevealHover: row == fMouseRevealRow];
159        [cell setActionHover: row == fMouseActionRow];
160        [cell setActionPushed: row == fActionPushedRow];
161    }
162    else
163    {
164        NSString * ident = [tableColumn identifier];
165        if ([ident isEqualToString: @"UL Image"] || [ident isEqualToString: @"DL Image"])
166        {
167            //ensure arrows are white only when selected
168            if ([NSApp isOnSnowLeopardOrBetter])
169                [[cell image] setTemplate: [cell backgroundStyle] == NSBackgroundStyleLowered];
170            else
171            {
172                NSImage * image = [cell image];
173                const BOOL template = [cell backgroundStyle] == NSBackgroundStyleLowered;
174                if ([image isTemplate] != template)
175                {
176                    [image setTemplate: template];
177                    [cell setImage: nil];
178                    [cell setImage: image];
179                }
180            }
181        }
182    }
183}
184
185- (NSRect) frameOfCellAtColumn: (NSInteger) column row: (NSInteger) row
186{
187    if (column == -1)
188        return [self rectOfRow: row];
189    else
190    {
191        NSRect rect = [super frameOfCellAtColumn: column row: row];
192       
193        //adjust placement for proper vertical alignment
194        if (column == [self columnWithIdentifier: @"Group"])
195            rect.size.height -= 1.0f;
196       
197        return rect;
198    }
199}
200
201- (NSString *) outlineView: (NSOutlineView *) outlineView typeSelectStringForTableColumn: (NSTableColumn *) tableColumn item: (id) item
202{
203    return [item isKindOfClass: [Torrent class]] ? [item name]
204            : [[self preparedCellAtColumn: [self columnWithIdentifier: @"Group"] row: [self rowForItem: item]] stringValue];
205}
206
207- (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
208                tableColumn: (NSTableColumn *) column item: (id) item mouseLocation: (NSPoint) mouseLocation
209{
210    NSString * ident = [column identifier];
211    if ([ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"])
212        return NSLocalizedString(@"Download speed", "Torrent table -> group row -> tooltip");
213    else if ([ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"])
214        return [fDefaults boolForKey: @"DisplayGroupRowRatio"] ? NSLocalizedString(@"Ratio", "Torrent table -> group row -> tooltip")
215                : NSLocalizedString(@"Upload speed", "Torrent table -> group row -> tooltip");
216    else if (ident)
217    {
218        NSInteger count = [[item torrents] count];
219        if (count == 1)
220            return NSLocalizedString(@"1 transfer", "Torrent table -> group row -> tooltip");
221        else
222            return [NSString stringWithFormat: NSLocalizedString(@"%d transfers", "Torrent table -> group row -> tooltip"), count];
223    }
224    else
225        return nil;
226}
227
228- (void) updateTrackingAreas
229{
230    [super updateTrackingAreas];
231    [self removeTrackingAreas];
232   
233    NSRange rows = [self rowsInRect: [self visibleRect]];
234    if (rows.length == 0)
235        return;
236   
237    NSPoint mouseLocation = [self convertPoint: [[self window] convertScreenToBase: [NSEvent mouseLocation]] fromView: nil];
238    for (NSUInteger row = rows.location; row < NSMaxRange(rows); row++)
239    {
240        if (![[self itemAtRow: row] isKindOfClass: [Torrent class]])
241            continue;
242       
243        NSDictionary * userInfo = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger: row] forKey: @"Row"];
244        TorrentCell * cell = (TorrentCell *)[self preparedCellAtColumn: -1 row: row];
245        [cell addTrackingAreasForView: self inRect: [self rectOfRow: row] withUserInfo: userInfo mouseLocation: mouseLocation];
246    }
247}
248
249- (void) removeTrackingAreas
250{
251    fMouseRow = -1;
252    fMouseControlRow = -1;
253    fMouseRevealRow = -1;
254    fMouseActionRow = -1;
255   
256    for (NSTrackingArea * area in [self trackingAreas])
257    {
258        if ([area owner] == self && [[area userInfo] objectForKey: @"Row"])
259            [self removeTrackingArea: area];
260    }
261}
262
263- (void) setRowHover: (NSInteger) row
264{
265    fMouseRow = row;
266    if (row >= 0 && [fDefaults boolForKey: @"SmallView"])
267        [self setNeedsDisplayInRect: [self rectOfRow: row]];
268}
269
270- (void) setControlButtonHover: (NSInteger) row
271{
272    fMouseControlRow = row;
273    if (row >= 0)
274        [self setNeedsDisplayInRect: [self rectOfRow: row]];
275}
276
277- (void) setRevealButtonHover: (NSInteger) row
278{
279    fMouseRevealRow = row;
280    if (row >= 0)
281        [self setNeedsDisplayInRect: [self rectOfRow: row]];
282}
283
284- (void) setActionButtonHover: (NSInteger) row
285{
286    fMouseActionRow = row;
287    if (row >= 0)
288        [self setNeedsDisplayInRect: [self rectOfRow: row]];
289}
290
291- (void) mouseEntered: (NSEvent *) event
292{
293    NSDictionary * dict = (NSDictionary *)[event userData];
294   
295    NSNumber * row;
296    if ((row = [dict objectForKey: @"Row"]))
297    {
298        NSInteger rowVal = [row integerValue];
299        NSString * type = [dict objectForKey: @"Type"];
300        if ([type isEqualToString: @"Action"])
301            fMouseActionRow = rowVal;
302        else if ([type isEqualToString: @"Control"])
303            fMouseControlRow = rowVal;
304        else if ([type isEqualToString: @"Reveal"])
305            fMouseRevealRow = rowVal;
306        else
307        {
308            fMouseRow = rowVal;
309            if (![fDefaults boolForKey: @"SmallView"])
310                return;
311        }
312       
313        [self setNeedsDisplayInRect: [self rectOfRow: rowVal]];
314    }
315}
316
317- (void) mouseExited: (NSEvent *) event
318{
319    NSDictionary * dict = (NSDictionary *)[event userData];
320   
321    NSNumber * row;
322    if ((row = [dict objectForKey: @"Row"]))
323    {
324        NSString * type = [dict objectForKey: @"Type"];
325        if ([type isEqualToString: @"Action"])
326            fMouseActionRow = -1;
327        else if ([type isEqualToString: @"Control"])
328            fMouseControlRow = -1;
329        else if ([type isEqualToString: @"Reveal"])
330            fMouseRevealRow = -1;
331        else
332        {
333            fMouseRow = -1;
334            if (![fDefaults boolForKey: @"SmallView"])
335                return;
336        }
337       
338        [self setNeedsDisplayInRect: [self rectOfRow: [row integerValue]]];
339    }
340}
341
342- (void) outlineViewSelectionIsChanging: (NSNotification *) notification
343{
344    if (fSelectedValues)
345        [self selectValues: fSelectedValues];
346}
347
348- (void) outlineViewItemDidExpand: (NSNotification *) notification
349{
350    NSInteger value = [[[notification userInfo] objectForKey: @"NSObject"] groupIndex];
351    if (value < 0)
352        value = MAX_GROUP;
353   
354    if ([fCollapsedGroups containsIndex: value])
355    {
356        [fCollapsedGroups removeIndex: value];
357        [[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self];
358    }
359}
360
361- (void) outlineViewItemDidCollapse: (NSNotification *) notification
362{
363    NSInteger value = [[[notification userInfo] objectForKey: @"NSObject"] groupIndex];
364    if (value < 0)
365        value = MAX_GROUP;
366   
367    [fCollapsedGroups addIndex: value];
368    [[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self];
369}
370
371- (void) mouseDown: (NSEvent *) event
372{
373    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
374    const NSInteger row = [self rowAtPoint: point];
375   
376    //check to toggle group status before anything else
377    if ([self pointInGroupStatusRect: point])
378    {
379        [fDefaults setBool: ![fDefaults boolForKey: @"DisplayGroupRowRatio"] forKey: @"DisplayGroupRowRatio"];
380        [self setGroupStatusColumns];
381       
382        return;
383    }
384   
385    const BOOL pushed = row != -1 && (fMouseActionRow == row || fMouseRevealRow == row || fMouseControlRow == row);
386   
387    //if pushing a button, don't change the selected rows
388    if (pushed)
389        fSelectedValues = [[self selectedValues] retain];
390   
391    [super mouseDown: event];
392   
393    [fSelectedValues release];
394    fSelectedValues = nil;
395   
396    //avoid weird behavior when showing menu by doing this after mouse down
397    if (row != -1 && fMouseActionRow == row)
398    {
399        fActionPushedRow = row;
400        [self setNeedsDisplayInRect: [self rectOfRow: row]]; //ensure button is pushed down
401       
402        [self displayTorrentMenuForEvent: event];
403       
404        fActionPushedRow = -1;
405        [self setNeedsDisplayInRect: [self rectOfRow: row]];
406    }
407    else if (!pushed && [event clickCount] == 2) //double click
408    {
409        id item = nil;
410        if (row != -1)
411            item = [self itemAtRow: row];
412       
413        if (!item || [item isKindOfClass: [Torrent class]])
414            [fController showInfo: nil];
415        else
416        {
417            if ([self isItemExpanded: item])
418                [self collapseItem: item];
419            else
420                [self expandItem: item];
421        }
422    }
423    else;
424}
425
426- (void) selectValues: (NSArray *) values
427{
428    NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
429   
430    for (id item in values)
431    {
432        if ([item isKindOfClass: [Torrent class]])
433        {
434            const NSInteger index = [self rowForItem: item];
435            if (index != -1)
436                [indexSet addIndex: index];
437        }
438        else
439        {
440            const NSInteger group = [item groupIndex];
441            for (NSInteger i = 0; i < [self numberOfRows]; i++)
442            {
443                id tableItem = [self itemAtRow: i];
444                if ([tableItem isKindOfClass: [TorrentGroup class]] && group == [tableItem groupIndex])
445                {
446                    [indexSet addIndex: i];
447                    break;
448                }
449            }
450        }
451    }
452   
453    [self selectRowIndexes: indexSet byExtendingSelection: NO];
454}
455
456- (NSArray *) selectedValues
457{
458    NSIndexSet * selectedIndexes = [self selectedRowIndexes];
459    NSMutableArray * values = [NSMutableArray arrayWithCapacity: [selectedIndexes count]];
460   
461    for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i])
462        [values addObject: [self itemAtRow: i]];
463   
464    return values;
465}
466
467- (NSArray *) selectedTorrents
468{
469    NSIndexSet * selectedIndexes = [self selectedRowIndexes];
470    NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [selectedIndexes count]]; //take a shot at guessing capacity
471   
472    for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i])
473    {
474        id item = [self itemAtRow: i];
475        if ([item isKindOfClass: [Torrent class]])
476            [torrents addObject: item];
477        else
478        {
479            NSArray * groupTorrents = [item torrents];
480            [torrents addObjectsFromArray: groupTorrents];
481            if ([self isItemExpanded: item])
482                i +=[groupTorrents count];
483        }
484    }
485   
486    return torrents;
487}
488
489- (NSMenu *) menuForEvent: (NSEvent *) event
490{
491    NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
492    if (row >= 0)
493    {
494        if (![self isRowSelected: row])
495            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
496        return fContextRow;
497    }
498    else
499    {
500        [self deselectAll: self];
501        return fContextNoRow;
502    }
503}
504
505//make sure that the pause buttons become orange when holding down the option key
506- (void) flagsChanged: (NSEvent *) event
507{
508    [self display];
509    [super flagsChanged: event];
510}
511
512//option-command-f will focus the filter bar's search field
513- (void) keyDown: (NSEvent *) event
514{
515    const unichar firstChar = [[event charactersIgnoringModifiers] characterAtIndex: 0];
516   
517    if (firstChar == 'f' && [event modifierFlags] & NSAlternateKeyMask && [event modifierFlags] & NSCommandKeyMask)
518        [fController focusFilterField];
519    else if (firstChar == ' ')
520        [fController toggleQuickLook: nil];
521    else
522        [super keyDown: event];
523}
524
525- (NSRect) iconRectForRow: (NSInteger) row
526{
527    return [fTorrentCell iconRectForBounds: [self rectOfRow: row]];
528}
529
530#warning catch string urls?
531- (void) paste: (id) sender
532{
533    NSURL * url;
534    if ((url = [NSURL URLFromPasteboard: [NSPasteboard generalPasteboard]]))
535        [fController openURL: [url absoluteString]];
536}
537
538- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
539{
540    SEL action = [menuItem action];
541   
542    if (action == @selector(paste:))
543        return [[[NSPasteboard generalPasteboard] types] containsObject: NSURLPboardType];
544   
545    return YES;
546}
547
548- (void) toggleControlForTorrent: (Torrent *) torrent
549{
550    if ([torrent isActive])
551        [fController stopTorrents: [NSArray arrayWithObject: torrent]];
552    else
553    {
554        if (([NSApp isOnSnowLeopardOrBetter] ? [NSEvent modifierFlags] : [[NSApp currentEvent] modifierFlags]) & NSAlternateKeyMask)
555            [fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]];
556        else if ([torrent waitingToStart])
557            [fController stopTorrents: [NSArray arrayWithObject: torrent]];
558        else
559            [fController resumeTorrents: [NSArray arrayWithObject: torrent]];
560    }
561}
562
563- (void) displayTorrentMenuForEvent: (NSEvent *) event
564{
565    const NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
566    if (row < 0)
567        return;
568   
569    //update file action menu
570    fMenuTorrent = [[self itemAtRow: row] retain];
571   
572    //update global limit check
573    [fGlobalLimitItem setState: [fMenuTorrent usesGlobalSpeedLimit] ? NSOnState : NSOffState];
574   
575    //place menu below button
576    NSRect rect = [fTorrentCell iconRectForBounds: [self rectOfRow: row]];
577    NSPoint location = rect.origin;
578    location.y += rect.size.height + 5.0;
579   
580    if ([NSApp isOnSnowLeopardOrBetter])
581    {
582        location = [self convertPoint: location toView: self];
583        [fActionMenu popUpMenuPositioningItem: nil atLocation: location inView: self];
584    }
585    else
586    {
587        location = [self convertPoint: location toView: nil];
588        NSEvent * newEvent = [NSEvent mouseEventWithType: [event type] location: location
589            modifierFlags: [event modifierFlags] timestamp: [event timestamp] windowNumber: [event windowNumber]
590            context: [event context] eventNumber: [event eventNumber] clickCount: [event clickCount] pressure: [event pressure]];
591       
592        [NSMenu popUpContextMenu: fActionMenu withEvent: newEvent forView: self];
593    }
594   
595    [fMenuTorrent release];
596    fMenuTorrent = nil;
597}
598
599- (void) menuNeedsUpdate: (NSMenu *) menu
600{
601    //this method seems to be called when it shouldn't be
602    if (!fMenuTorrent || ![menu supermenu])
603        return;
604   
605    if (menu == fUploadMenu || menu == fDownloadMenu)
606    {
607        NSMenuItem * item;
608        if ([menu numberOfItems] == 3)
609        {
610            const NSInteger speedLimitActionValue[] = { 0, 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500,
611                                                        750, 1000, 1500, 2000, -1 };
612           
613            for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++)
614            {
615                item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s",
616                        "Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setQuickLimit:)
617                        keyEquivalent: @""];
618                [item setTarget: self];
619                [item setRepresentedObject: [NSNumber numberWithInt: speedLimitActionValue[i]]];
620                [menu addItem: item];
621                [item release];
622            }
623        }
624       
625        const BOOL upload = menu == fUploadMenu;
626        const BOOL limit = [fMenuTorrent usesSpeedLimit: upload];
627       
628        item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG];
629        [item setState: limit ? NSOnState : NSOffState];
630        [item setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
631                            "torrent action menu -> upload/download limit"), [fMenuTorrent speedLimit: upload]]];
632       
633        item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG];
634        [item setState: !limit ? NSOnState : NSOffState];
635    }
636    else if (menu == fRatioMenu)
637    {
638        NSMenuItem * item;
639        if ([menu numberOfItems] == 4)
640        {
641            const CGFloat ratioLimitActionValue[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, -1.0 };
642           
643            for (NSInteger i = 0; ratioLimitActionValue[i] != -1.0; i++)
644            {
645                item = [[NSMenuItem alloc] initWithTitle: [NSString localizedStringWithFormat: @"%.2f", ratioLimitActionValue[i]]
646                        action: @selector(setQuickRatio:) keyEquivalent: @""];
647                [item setTarget: self];
648                [item setRepresentedObject: [NSNumber numberWithFloat: ratioLimitActionValue[i]]];
649                [menu addItem: item];
650                [item release];
651            }
652        }
653       
654        const tr_ratiolimit mode = [fMenuTorrent ratioSetting];
655       
656        item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG];
657        [item setState: mode == TR_RATIOLIMIT_SINGLE ? NSOnState : NSOffState];
658        [item setTitle: [NSString localizedStringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)",
659            "torrent action menu -> ratio stop"), [fMenuTorrent ratioLimit]]];
660       
661        item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG];
662        [item setState: mode == TR_RATIOLIMIT_UNLIMITED ? NSOnState : NSOffState];
663       
664        item = [menu itemWithTag: ACTION_MENU_GLOBAL_TAG];
665        [item setState: mode == TR_RATIOLIMIT_GLOBAL ? NSOnState : NSOffState];
666    }
667    else if (menu == fPriorityMenu)
668    {
669        const tr_priority_t priority = [fMenuTorrent priority];
670       
671        NSMenuItem * item = [menu itemWithTag: ACTION_MENU_PRIORITY_HIGH_TAG];
672        [item setState: priority == TR_PRI_HIGH ? NSOnState : NSOffState];
673       
674        item = [menu itemWithTag: ACTION_MENU_PRIORITY_NORMAL_TAG];
675        [item setState: priority == TR_PRI_NORMAL ? NSOnState : NSOffState];
676       
677        item = [menu itemWithTag: ACTION_MENU_PRIORITY_LOW_TAG];
678        [item setState: priority == TR_PRI_LOW ? NSOnState : NSOffState];
679    }
680}
681
682//alternating rows - first row after group row is white
683- (void) highlightSelectionInClipRect: (NSRect) clipRect
684{
685    NSRect visibleRect = clipRect;
686    NSRange rows = [self rowsInRect: visibleRect];
687    BOOL start = YES;
688   
689    const CGFloat totalRowHeight = [self rowHeight] + [self intercellSpacing].height;
690   
691    NSRect gridRects[(NSInteger)(ceil(visibleRect.size.height / totalRowHeight / 2.0)) + 1]; //add one if partial rows at top and bottom
692    NSInteger rectNum = 0;
693   
694    if (rows.length > 0)
695    {
696        //determine what the first row color should be
697        if ([[self itemAtRow: rows.location] isKindOfClass: [Torrent class]])
698        {
699            for (NSInteger i = rows.location-1; i>=0; i--)
700            {
701                if (![[self itemAtRow: i] isKindOfClass: [Torrent class]])
702                    break;
703                start = !start;
704            }
705        }
706        else
707        {
708            rows.location++;
709            rows.length--;
710        }
711       
712        NSInteger i;
713        for (i = rows.location; i < NSMaxRange(rows); i++)
714        {
715            if (![[self itemAtRow: i] isKindOfClass: [Torrent class]])
716            {
717                start = YES;
718                continue;
719            }
720           
721            if (!start && ![self isRowSelected: i])
722                gridRects[rectNum++] = [self rectOfRow: i];
723           
724            start = !start;
725        }
726       
727        const CGFloat newY = NSMaxY([self rectOfRow: i-1]);
728        visibleRect.size.height -= newY - visibleRect.origin.y;
729        visibleRect.origin.y = newY;
730    }
731   
732    const NSInteger numberBlankRows = ceil(visibleRect.size.height / totalRowHeight);
733   
734    //remaining visible rows continue alternating
735    visibleRect.size.height = totalRowHeight;
736    if (start)
737        visibleRect.origin.y += totalRowHeight;
738   
739    for (NSInteger i = start ? 1 : 0; i < numberBlankRows; i += 2)
740    {
741        gridRects[rectNum++] = visibleRect;
742        visibleRect.origin.y += 2.0 * totalRowHeight;
743    }
744   
745    NSAssert([[NSColor controlAlternatingRowBackgroundColors] count] >= 2, @"There should be 2 alternating row colors");
746   
747    [[[NSColor controlAlternatingRowBackgroundColors] objectAtIndex: 1] set];
748    NSRectFillList(gridRects, rectNum);
749   
750    [super highlightSelectionInClipRect: clipRect];
751}
752
753- (void) setQuickLimitMode: (id) sender
754{
755    const BOOL limit = [sender tag] == ACTION_MENU_LIMIT_TAG;
756    [fMenuTorrent setUseSpeedLimit: limit upload: [sender menu] == fUploadMenu];
757   
758    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
759}
760
761- (void) setQuickLimit: (id) sender
762{
763    const BOOL upload = [sender menu] == fUploadMenu;
764    [fMenuTorrent setUseSpeedLimit: YES upload: upload];
765    [fMenuTorrent setSpeedLimit: [[sender representedObject] intValue] upload: upload];
766   
767    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
768}
769
770- (void) setGlobalLimit: (id) sender
771{
772    [fMenuTorrent setUseGlobalSpeedLimit: [sender state] != NSOnState];
773   
774    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
775}
776
777- (void) setQuickRatioMode: (id) sender
778{
779    tr_ratiolimit mode;
780    switch ([sender tag])
781    {
782        case ACTION_MENU_UNLIMITED_TAG:
783            mode = TR_RATIOLIMIT_UNLIMITED;
784            break;
785        case ACTION_MENU_LIMIT_TAG:
786            mode = TR_RATIOLIMIT_SINGLE;
787            break;
788        case ACTION_MENU_GLOBAL_TAG:
789            mode = TR_RATIOLIMIT_GLOBAL;
790            break;
791        default:
792            return;
793    }
794   
795    [fMenuTorrent setRatioSetting: mode];
796   
797    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
798}
799
800- (void) setQuickRatio: (id) sender
801{
802    [fMenuTorrent setRatioSetting: TR_RATIOLIMIT_SINGLE];
803    [fMenuTorrent setRatioLimit: [[sender representedObject] floatValue]];
804   
805    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
806}
807
808- (void) setPriority: (id) sender
809{
810    tr_priority_t priority;
811    switch ([sender tag])
812    {
813        case ACTION_MENU_PRIORITY_HIGH_TAG:
814            priority = TR_PRI_HIGH;
815            break;
816        case ACTION_MENU_PRIORITY_NORMAL_TAG:
817            priority = TR_PRI_NORMAL;
818            break;
819        case ACTION_MENU_PRIORITY_LOW_TAG:
820            priority = TR_PRI_LOW;
821            break;
822        default:
823            NSAssert1(NO, @"Unknown priority: %d", [sender tag]);
824    }
825   
826    [fMenuTorrent setPriority: priority];
827   
828    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
829}
830
831- (void) checkFile: (id) sender
832{
833    NSIndexSet * indexSet = [(FileListNode *)[sender representedObject] indexes];
834    [fMenuTorrent setFileCheckState: [sender state] != NSOnState ? NSOnState : NSOffState forIndexes: indexSet];
835   
836    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
837}
838
839- (void) togglePiecesBar
840{
841    //stop previous animation
842    if (fPiecesBarAnimation)
843        [fPiecesBarAnimation release];
844   
845    NSMutableArray * progressMarks = [NSMutableArray arrayWithCapacity: 16];
846    for (NSAnimationProgress i = 0.0625; i <= 1.0; i += 0.0625)
847        [progressMarks addObject: [NSNumber numberWithFloat: i]];
848   
849    fPiecesBarAnimation = [[NSAnimation alloc] initWithDuration: TOGGLE_PROGRESS_SECONDS animationCurve: NSAnimationEaseIn];
850    [fPiecesBarAnimation setAnimationBlockingMode: NSAnimationNonblocking];
851    [fPiecesBarAnimation setProgressMarks: progressMarks];
852    [fPiecesBarAnimation setDelegate: self];
853   
854    [fPiecesBarAnimation startAnimation];
855}
856
857- (void) animationDidEnd: (NSAnimation *) animation
858{
859    if (animation == fPiecesBarAnimation)
860    {
861        [fPiecesBarAnimation release];
862        fPiecesBarAnimation = nil;
863    }
864}
865
866- (void) animation: (NSAnimation *) animation didReachProgressMark: (NSAnimationProgress) progress
867{
868    if (animation == fPiecesBarAnimation)
869    {
870        if ([fDefaults boolForKey: @"PiecesBar"])
871            fPiecesBarPercent = progress;
872        else
873            fPiecesBarPercent = 1.0 - progress;
874       
875        [self reloadData];
876    }
877}
878
879- (CGFloat) piecesBarPercent
880{
881    return fPiecesBarPercent;
882}
883
884@end
885
886@implementation TorrentTableView (Private)
887
888- (BOOL) pointInGroupStatusRect: (NSPoint) point
889{
890    NSInteger row = [self rowAtPoint: point];
891    if (row < 0 || [[self itemAtRow: row] isKindOfClass: [Torrent class]])
892        return NO;
893   
894    NSString * ident = [[[self tableColumns] objectAtIndex: [self columnAtPoint: point]] identifier];
895    return [ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"]
896            || [ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"];
897}
898
899- (void) setGroupStatusColumns
900{
901    const BOOL ratio = [fDefaults boolForKey: @"DisplayGroupRowRatio"];
902   
903    [[self tableColumnWithIdentifier: @"DL"] setHidden: ratio];
904    [[self tableColumnWithIdentifier: @"DL Image"] setHidden: ratio];
905}
906
907@end
Note: See TracBrowser for help on using the repository browser.