source: trunk/macosx/TorrentTableView.m @ 1645

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

update licenses to 2007

  • Property svn:keywords set to Date Rev Author Id
File size: 11.9 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 1645 2007-04-03 02:22:25Z 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 BUTTON_TO_TOP_REGULAR 33.0
31#define BUTTON_TO_TOP_SMALL 20.0
32
33//button layout (from end of bar) is: padding, button, padding, button, padding
34//change BUTTONS_TOTAL_WIDTH in .h when changing these values, add 2.0 to that value
35#define BUTTON_WIDTH 14.0
36#define PADDING 3.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            [fDefaults setBool: ![fDefaults boolForKey: @"UseAdvancedBar"] forKey: @"UseAdvancedBar"];
104            fClickPoint = NSZeroPoint;
105        }
106        else
107        {
108            if ([self pointInMinimalStatusRect: fClickPoint])
109            {
110                [(TorrentCell *)[[self tableColumnWithIdentifier: @"Torrent"] dataCell] toggleMinimalStatus];
111                fClickPoint = NSZeroPoint;
112            }
113
114            [super mouseDown: event];
115        }
116    }
117    else;
118
119    [self display];
120}
121
122- (void) mouseUp: (NSEvent *) event
123{
124    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
125    int row = [self rowAtPoint: point];
126    BOOL sameRow = row == [self rowAtPoint: fClickPoint];
127   
128    if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint])
129    {
130        Torrent * torrent = [fTorrents objectAtIndex: row];
131
132        if ([torrent isActive])
133            [fController stopTorrents: [NSArray arrayWithObject: torrent]];
134        else if ([torrent isPaused])
135        {
136            if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
137                [fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]];
138            else if ([torrent waitingToStart])
139                [fController stopTorrents: [NSArray arrayWithObject: torrent]];
140            else
141                [fController resumeTorrents: [NSArray arrayWithObject: torrent]];
142        }
143        else;
144    }
145    else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint])
146        [[fTorrents objectAtIndex: row] revealData];
147    else if ([event clickCount] == 2 && !NSEqualPoints(fClickPoint, NSZeroPoint))
148    {
149        if ([self pointInIconRect: point])
150            [[fTorrents objectAtIndex: row] revealData];
151        else
152            [fController showInfo: nil];
153    }
154    else;
155   
156    [super mouseUp: event];
157
158    fClickPoint = NSZeroPoint;
159    [self display];
160}
161
162- (NSMenu *) menuForEvent: (NSEvent *) event
163{
164    int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
165   
166    if (row >= 0)
167    {
168        if (![self isRowSelected: row])
169            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
170               
171        return fContextRow;
172    }
173    else
174    {
175        [self deselectAll: self];
176        return fContextNoRow;
177    }
178}
179
180- (void) keyDown: (NSEvent *) event
181{
182    unichar newChar = [[event characters] characterAtIndex: 0];
183    if (newChar == ' ' || [[NSCharacterSet alphanumericCharacterSet] characterIsMember: newChar]
184        || [[NSCharacterSet symbolCharacterSet] characterIsMember: newChar]
185        || [[NSCharacterSet punctuationCharacterSet] characterIsMember: newChar])
186    {
187        if ([fKeyStrokes count] > 0 && [event timestamp] - [[fKeyStrokes lastObject] timestamp] > 1.0)
188            [fKeyStrokes removeAllObjects];
189        [fKeyStrokes addObject: event];
190   
191        [self interpretKeyEvents: fKeyStrokes];
192    }
193    else
194    {
195        if ([fKeyStrokes count] > 0)
196            [fKeyStrokes removeAllObjects];
197       
198        [super keyDown: event];
199    }
200}
201
202- (void) flagsChanged: (NSEvent *) event
203{
204    [self display];
205    [super flagsChanged: event];
206}
207
208- (void) insertText: (NSString *) text
209{
210    //sort torrents by name before finding closest match
211    NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES
212                                            selector: @selector(caseInsensitiveCompare:)] autorelease];
213    NSArray * descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
214
215    NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
216    [descriptors release];
217   
218    //select torrent closest to text that isn't before text alphabetically
219    NSEnumerator * enumerator = [tempTorrents objectEnumerator];
220    Torrent * torrent;
221    while ((torrent = [enumerator nextObject]))
222        if ([[torrent name] caseInsensitiveCompare: text] != NSOrderedAscending)
223        {
224            [self selectRow: [fTorrents indexOfObject: torrent] byExtendingSelection: NO];
225            break;
226        }
227   
228    //select last torrent alphabetically if no match found
229    if (!torrent)
230        [self selectRow: [fTorrents indexOfObject: [tempTorrents lastObject]] byExtendingSelection: NO];
231}
232
233- (void) drawRect: (NSRect) r
234{
235    NSRect rect;
236    Torrent * torrent;
237    NSImage * image;
238    NSRect buttonRect = NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH);
239
240    [super drawRect: r];
241
242    int i;
243    for (i = 0; i < [fTorrents count]; i++)
244    {
245        torrent = [fTorrents objectAtIndex: i];
246        rect  = [self pauseRectForRow: i];
247
248        if ([torrent isActive])
249            image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
250        else if ([torrent isPaused])
251        {
252            if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask && [fDefaults boolForKey: @"Queue"])
253                image = NSPointInRect(fClickPoint, rect) ? fResumeNoWaitOnIcon : fResumeNoWaitOffIcon;
254            else if ([torrent waitingToStart])
255                image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
256            else
257                image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
258        }
259        else
260            image = nil;
261
262        if (image)
263            [image compositeToPoint: NSMakePoint(rect.origin.x, NSMaxY(rect)) fromRect: buttonRect
264                    operation: NSCompositeSourceOver];
265
266        rect = [self revealRectForRow: i];
267        image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
268        [image compositeToPoint: NSMakePoint(rect.origin.x, NSMaxY(rect)) fromRect: buttonRect
269                    operation: NSCompositeSourceOver];
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) - PADDING - BUTTON_WIDTH - PADDING - 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) - PADDING - BUTTON_WIDTH,
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.