source: trunk/macosx/TorrentTableView.m @ 680

Last change on this file since 680 was 680, checked in by livings124, 16 years ago

Type-ahead in the table view. Works similar to the way typing in the finder works.

  • Property svn:keywords set to Date Rev Author Id
File size: 9.2 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 680 2006-07-23 20:53:31Z livings124 $
3 *
4 * Copyright (c) 2005-2006 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 "Torrent.h"
28
29#define BUTTON_TO_TOP_REGULAR 33.5
30#define BUTTON_TO_TOP_SMALL 20.0
31
32#define BUTTON_WIDTH 14.0
33#define DISTANCE_FROM_CENTER 2.5
34//change BUTTONS_TOTAL_WIDTH when changing this
35#define AREA_CENTER 21.0
36
37@interface TorrentTableView (Private)
38
39- (NSRect) pauseRectForRow: (int) row;
40- (NSRect) revealRectForRow: (int) row;
41- (BOOL) pointInPauseRect: (NSPoint) point;
42- (BOOL) pointInRevealRect: (NSPoint) point;
43- (BOOL) pointInIconRect: (NSPoint) point;
44
45@end
46
47@implementation TorrentTableView
48
49- (id) initWithCoder: (NSCoder *) decoder
50{
51    if ((self = [super initWithCoder: decoder]))
52    {
53        fResumeOnIcon = [NSImage imageNamed: @"ResumeOn.png"];
54        fResumeOffIcon = [NSImage imageNamed: @"ResumeOff.png"];
55        fPauseOnIcon = [NSImage imageNamed: @"PauseOn.png"];
56        fPauseOffIcon = [NSImage imageNamed: @"PauseOff.png"];
57        fRevealOnIcon = [NSImage imageNamed: @"RevealOn.png"];
58        fRevealOffIcon = [NSImage imageNamed: @"RevealOff.png"];
59       
60        fClickPoint = NSZeroPoint;
61       
62        fKeyStrokes = [[NSMutableArray alloc] init];
63       
64        fDefaults = [NSUserDefaults standardUserDefaults];
65    }
66   
67    return self;
68}
69   
70- (void) awakeFromNib
71{
72    [fContextRow setTitle: @"Context"];
73    [fContextNoRow setTitle: @"Context"];
74}
75
76- (void) dealloc
77{
78    [fKeyStrokes release];
79    [super dealloc];
80}
81
82- (void) setTorrents: (NSArray *) torrents
83{
84    fTorrents = torrents;
85}
86
87- (void) mouseDown: (NSEvent *) event
88{
89    fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];
90
91    if ([event modifierFlags] & NSAlternateKeyMask)
92    {
93        [fController toggleAdvancedBar: self];
94        fClickPoint = NSZeroPoint;
95    }
96    else if (![self pointInPauseRect: fClickPoint] && ![self pointInRevealRect: fClickPoint])
97        [super mouseDown: event];
98    else;
99
100    [self display];
101}
102
103- (void) mouseUp: (NSEvent *) event
104{
105    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
106    int row = [self rowAtPoint: point];
107    BOOL sameRow = row == [self rowAtPoint: fClickPoint];
108   
109    if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint])
110    {
111        Torrent * torrent = [fTorrents objectAtIndex: row];
112
113                if ([torrent isPaused])
114                        [fController resumeTorrents: [NSArray arrayWithObject: torrent]];
115                else if ([torrent isActive])
116            [fController stopTorrents: [NSArray arrayWithObject: torrent]];
117                else;
118    }
119    else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint])
120        [[fTorrents objectAtIndex: row] revealData];
121        else if ([event clickCount] == 2)
122    {
123        if ([self pointInIconRect: point])
124            [[fTorrents objectAtIndex: row] revealData];
125        else
126            [fController showInfo: nil];
127    }
128    else;
129   
130        [super mouseUp: event];
131
132    fClickPoint = NSZeroPoint;
133    [self display];
134}
135
136- (NSMenu *) menuForEvent: (NSEvent *) event
137{
138    int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
139   
140    if (row >= 0)
141    {
142        if (![self isRowSelected: row])
143            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
144               
145        return fContextRow;
146    }
147    else
148    {
149        [self deselectAll: self];
150        return fContextNoRow;
151    }
152}
153
154- (void) keyDown: (NSEvent *) event
155{
156    unichar newChar = [[event characters] characterAtIndex: 0];
157    if (newChar == ' ' || [[NSCharacterSet alphanumericCharacterSet] characterIsMember: newChar])
158    {
159        if ([fKeyStrokes count] > 0 && [event timestamp] - [[fKeyStrokes lastObject] timestamp] > 1.0)
160            [fKeyStrokes removeAllObjects];
161        [fKeyStrokes addObject: event];
162   
163        [self interpretKeyEvents: fKeyStrokes];
164    }
165    else
166    {
167        if ([fKeyStrokes count] > 0)
168            [fKeyStrokes removeAllObjects];
169       
170        [super keyDown: event];
171    }
172}
173
174- (void) insertText: (NSString *) text
175{
176    NSLog(text);
177   
178    //sort torrents by name before finding closest match
179    NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES] autorelease];
180    NSArray * descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
181
182    NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
183    [descriptors release];
184   
185    //select torrent closest to text that isn't before text alphabetically
186    NSEnumerator * enumerator = [tempTorrents objectEnumerator];
187    Torrent * torrent;
188    while ((torrent = [enumerator nextObject]))
189    {
190        if ([[torrent name] caseInsensitiveCompare: text] != NSOrderedAscending)
191        {
192            [self selectRow: [fTorrents indexOfObject: torrent] byExtendingSelection: NO];
193            break;
194        }
195    }
196   
197    //select last torrent alphabetically if no match found
198    if (!torrent)
199        [self selectRow: [fTorrents indexOfObject: [tempTorrents lastObject]] byExtendingSelection: NO];
200}
201
202- (void) drawRect: (NSRect) r
203{
204    NSRect rect;
205    Torrent * torrent;
206    NSImage * image;
207
208    [super drawRect: r];
209
210    int i;
211    for (i = 0; i < [fTorrents count]; i++)
212    {
213        torrent = [fTorrents objectAtIndex: i];
214        rect  = [self pauseRectForRow: i];
215
216        if ([torrent isPaused])
217            image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
218        else if ([torrent isActive])
219            image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
220        else
221            image = nil;
222
223        if (image)
224        {
225            [image setFlipped: YES];
226            [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
227                operation: NSCompositeSourceOver fraction: 1.0];
228        }
229
230        rect = [self revealRectForRow: i];
231        image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
232        [image setFlipped: YES];
233        [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
234            operation: NSCompositeSourceOver fraction: 1.0];
235    }
236}
237
238@end
239
240@implementation TorrentTableView (Private)
241
242- (NSRect) pauseRectForRow: (int) row
243{
244    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
245   
246    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
247   
248    return NSMakeRect(NSMaxX(cellRect) - AREA_CENTER - DISTANCE_FROM_CENTER - BUTTON_WIDTH,
249                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
250}
251
252- (NSRect) revealRectForRow: (int) row
253{
254    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
255   
256    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
257   
258    return NSMakeRect(NSMaxX(cellRect) - AREA_CENTER + DISTANCE_FROM_CENTER,
259                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
260}
261
262- (BOOL) pointInIconRect: (NSPoint) point
263{
264    int row = [self rowAtPoint: point];
265    if (row < 0)
266        return NO;
267   
268    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
269    NSSize iconSize = [fDefaults boolForKey: @"SmallView"] ? [[[fTorrents objectAtIndex: row] iconSmall] size]
270                                                        : [[[fTorrents objectAtIndex: row] iconFlipped] size];
271   
272    NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
273            + (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height);
274   
275    return NSPointInRect(point, iconRect);
276}
277
278- (BOOL) pointInPauseRect: (NSPoint) point
279{
280    return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
281}
282
283- (BOOL) pointInRevealRect: (NSPoint) point
284{
285    return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
286}
287
288@end
Note: See TracBrowser for help on using the repository browser.