source: trunk/macosx/TorrentTableView.m @ 1125

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

bindings for minimal view and advanced bar

  • Property svn:keywords set to Date Rev Author Id
File size: 11.8 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 1044 2006-10-31 19:16:15Z 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.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                [(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    NSRect buttonRect = NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH);
236
237    [super drawRect: r];
238
239    int i;
240    for (i = 0; i < [fTorrents count]; i++)
241    {
242        torrent = [fTorrents objectAtIndex: i];
243        rect  = [self pauseRectForRow: i];
244
245        if ([torrent isActive])
246            image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
247        else if ([torrent isPaused])
248        {
249            if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask && [fDefaults boolForKey: @"Queue"])
250                image = NSPointInRect(fClickPoint, rect) ? fResumeNoWaitOnIcon : fResumeNoWaitOffIcon;
251            else if ([torrent waitingToStart])
252                image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
253            else
254                image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
255        }
256        else
257            image = nil;
258
259        if (image)
260            [image compositeToPoint: NSMakePoint(rect.origin.x, NSMaxY(rect)) fromRect: buttonRect
261                    operation: NSCompositeSourceOver];
262
263        rect = [self revealRectForRow: i];
264        image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
265        [image compositeToPoint: NSMakePoint(rect.origin.x, NSMaxY(rect)) fromRect: buttonRect
266                    operation: NSCompositeSourceOver];
267    }
268}
269
270@end
271
272@implementation TorrentTableView (Private)
273
274- (NSRect) pauseRectForRow: (int) row
275{
276    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
277   
278    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
279   
280    return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH - PADDING - BUTTON_WIDTH,
281                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
282}
283
284- (NSRect) revealRectForRow: (int) row
285{
286    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
287   
288    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
289   
290    return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH,
291                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
292}
293
294- (BOOL) pointInIconRect: (NSPoint) point
295{
296    int row = [self rowAtPoint: point];
297    if (row < 0)
298        return NO;
299
300    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
301    NSSize iconSize = [fDefaults boolForKey: @"SmallView"] ? [[[fTorrents objectAtIndex: row] iconSmall] size]
302                                                        : [[[fTorrents objectAtIndex: row] iconFlipped] size];
303   
304    NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
305            + (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height);
306   
307    return NSPointInRect(point, iconRect);
308}
309
310- (BOOL) pointInMinimalStatusRect: (NSPoint) point
311{
312    int row = [self rowAtPoint: point];
313    if (row < 0 || ![fDefaults boolForKey: @"SmallView"])
314        return NO;
315
316    Torrent * torrent = [fTorrents objectAtIndex: row];
317    NSString * statusString = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive]
318                                    ? [torrent remainingTimeString] : [torrent shortStatusString];
319   
320    float statusWidth = [statusString sizeWithAttributes: fSmallStatusAttributes].width + 3.0;
321   
322    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
323    NSRect statusRect = NSMakeRect(NSMaxX(cellRect) - statusWidth, cellRect.origin.y,
324                                    statusWidth, cellRect.size.height - BUTTON_WIDTH);
325   
326    return NSPointInRect(point, statusRect);
327}
328
329- (BOOL) pointInPauseRect: (NSPoint) point
330{
331    return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
332}
333
334- (BOOL) pointInRevealRect: (NSPoint) point
335{
336    return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
337}
338
339@end
Note: See TracBrowser for help on using the repository browser.