source: trunk/macosx/TorrentTableView.m @ 920

Last change on this file since 920 was 920, checked in by joshe, 15 years ago

Merge nat-traversal branch to trunk.

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