source: branches/nat-traversal/macosx/TorrentTableView.m @ 878

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

For waiting torrents, button in table will now be a pause button to remove from queue.

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