source: trunk/macosx/TorrentTableView.m @ 682

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

Ignore case when sorting.

  • Property svn:keywords set to Date Rev Author Id
File size: 9.4 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 682 2006-07-23 21:29:24Z 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        || [[NSCharacterSet symbolCharacterSet] characterIsMember: newChar]
159        || [[NSCharacterSet punctuationCharacterSet] characterIsMember: newChar])
160    {
161        if ([fKeyStrokes count] > 0 && [event timestamp] - [[fKeyStrokes lastObject] timestamp] > 1.0)
162            [fKeyStrokes removeAllObjects];
163        [fKeyStrokes addObject: event];
164   
165        [self interpretKeyEvents: fKeyStrokes];
166    }
167    else
168    {
169        if ([fKeyStrokes count] > 0)
170            [fKeyStrokes removeAllObjects];
171       
172        [super keyDown: event];
173    }
174}
175
176- (void) insertText: (NSString *) text
177{
178    //sort torrents by name before finding closest match
179    NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES
180                                            selector: @selector(caseInsensitiveCompare:)] autorelease];
181    NSArray * descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
182
183    NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
184    [descriptors release];
185   
186    //select torrent closest to text that isn't before text alphabetically
187    NSEnumerator * enumerator = [tempTorrents objectEnumerator];
188    Torrent * torrent;
189    while ((torrent = [enumerator nextObject]))
190    {NSLog([[torrent name] lowercaseString]);
191        if ([[torrent name] caseInsensitiveCompare: text] != NSOrderedAscending)
192        {
193            [self selectRow: [fTorrents indexOfObject: torrent] byExtendingSelection: NO];
194            break;
195        }
196    }
197   
198    //select last torrent alphabetically if no match found
199    if (!torrent)
200        [self selectRow: [fTorrents indexOfObject: [tempTorrents lastObject]] byExtendingSelection: NO];
201}
202
203- (void) drawRect: (NSRect) r
204{
205    NSRect rect;
206    Torrent * torrent;
207    NSImage * image;
208
209    [super drawRect: r];
210
211    int i;
212    for (i = 0; i < [fTorrents count]; i++)
213    {
214        torrent = [fTorrents objectAtIndex: i];
215        rect  = [self pauseRectForRow: i];
216
217        if ([torrent isPaused])
218            image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
219        else if ([torrent isActive])
220            image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
221        else
222            image = nil;
223
224        if (image)
225        {
226            [image setFlipped: YES];
227            [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
228                operation: NSCompositeSourceOver fraction: 1.0];
229        }
230
231        rect = [self revealRectForRow: i];
232        image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
233        [image setFlipped: YES];
234        [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
235            operation: NSCompositeSourceOver fraction: 1.0];
236    }
237}
238
239@end
240
241@implementation TorrentTableView (Private)
242
243- (NSRect) pauseRectForRow: (int) row
244{
245    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
246   
247    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
248   
249    return NSMakeRect(NSMaxX(cellRect) - AREA_CENTER - DISTANCE_FROM_CENTER - BUTTON_WIDTH,
250                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
251}
252
253- (NSRect) revealRectForRow: (int) row
254{
255    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
256   
257    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
258   
259    return NSMakeRect(NSMaxX(cellRect) - AREA_CENTER + DISTANCE_FROM_CENTER,
260                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
261}
262
263- (BOOL) pointInIconRect: (NSPoint) point
264{
265    int row = [self rowAtPoint: point];
266    if (row < 0)
267        return NO;
268   
269    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
270    NSSize iconSize = [fDefaults boolForKey: @"SmallView"] ? [[[fTorrents objectAtIndex: row] iconSmall] size]
271                                                        : [[[fTorrents objectAtIndex: row] iconFlipped] size];
272   
273    NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
274            + (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height);
275   
276    return NSPointInRect(point, iconRect);
277}
278
279- (BOOL) pointInPauseRect: (NSPoint) point
280{
281    return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
282}
283
284- (BOOL) pointInRevealRect: (NSPoint) point
285{
286    return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
287}
288
289@end
Note: See TracBrowser for help on using the repository browser.