source: trunk/macosx/TorrentTableView.m @ 419

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

Now that sorting by stat sorts by progress, it should be resorted more often.

  • Property svn:keywords set to Date Rev Author Id
File size: 7.2 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 419 2006-06-20 19:20:36Z 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_WIDTH 14.0
30#define BUTTON_TO_TOP 33.5
31#define DISTANCE_FROM_CENTER 2.5
32//change BUTTONS_TOTAL_WIDTH when changing this
33#define AREA_CENTER 21.0
34
35@interface TorrentTableView (Private)
36
37- (NSRect) pauseRectForRow: (int) row;
38- (NSRect) revealRectForRow: (int) row;
39- (BOOL) pointInPauseRect: (NSPoint) point;
40- (BOOL) pointInRevealRect: (NSPoint) point;
41- (BOOL) pointInIconRect: (NSPoint) point;
42
43@end
44
45@implementation TorrentTableView
46
47- (id) initWithCoder: (NSCoder *) decoder
48{
49    if ((self = [super initWithCoder: decoder]))
50    {
51        fResumeOnIcon = [NSImage imageNamed: @"ResumeOn.png"];
52        fResumeOffIcon = [NSImage imageNamed: @"ResumeOff.png"];
53        fPauseOnIcon = [NSImage imageNamed: @"PauseOn.png"];
54        fPauseOffIcon = [NSImage imageNamed: @"PauseOff.png"];
55        fRevealOnIcon = [NSImage imageNamed: @"RevealOn.png"];
56        fRevealOffIcon = [NSImage imageNamed: @"RevealOff.png"];
57    }
58   
59    return self;
60}
61   
62- (void) awakeFromNib
63{
64    [fContextRow setTitle: @"Context"];
65    [fContextNoRow setTitle: @"Context"];
66}
67
68- (void) setTorrents: (NSArray *) torrents
69{
70    fTorrents = torrents;
71}
72
73- (void) mouseDown: (NSEvent *) e
74{
75    fClickPoint = [self convertPoint: [e locationInWindow] fromView: nil];
76    int row = [self rowAtPoint: fClickPoint];
77
78    if( [e modifierFlags] & NSAlternateKeyMask )
79    {
80        [fController advancedChanged: self];
81        fClickPoint = NSMakePoint( 0, 0 );
82    }
83    else if( ![self pointInPauseRect: fClickPoint] &&
84             ![self pointInRevealRect: fClickPoint] )
85        [super mouseDown: e];
86    else;
87
88    [self display];
89}
90
91- (void) mouseUp: (NSEvent *) e
92{
93    NSPoint point;
94    int row;
95    bool sameRow;
96    Torrent * torrent;
97
98    point = [self convertPoint: [e locationInWindow] fromView: nil];
99    row   = [self rowAtPoint: point];
100    sameRow = row == [self rowAtPoint: fClickPoint];
101   
102    if( sameRow && [self pointInPauseRect: point]
103            && [self pointInPauseRect: fClickPoint] )
104    {
105        torrent = [fTorrents objectAtIndex: row];
106
107                if( [torrent isPaused] )
108                        [fController resumeTorrentWithIndex: [NSIndexSet indexSetWithIndex: row]];
109                else if( [torrent isActive] )
110                        [fController stopTorrentWithIndex: [NSIndexSet indexSetWithIndex: row]];
111                else;
112    }
113    else if( sameRow && [self pointInRevealRect: point]
114                && [self pointInRevealRect: fClickPoint] )
115        [[fTorrents objectAtIndex: row] reveal];
116        else if ([e clickCount] == 2)
117    {
118        if ([self pointInIconRect: point])
119            [[fTorrents objectAtIndex: row] reveal];
120        else
121            [fController showInfo: nil];
122    }
123    else;
124   
125        [super mouseUp: e];
126
127    fClickPoint = NSMakePoint( 0, 0 );
128    [self display];
129}
130
131- (NSMenu *) menuForEvent: (NSEvent *) e
132{
133    int row = [self rowAtPoint: [self convertPoint: [e locationInWindow] fromView: nil]];
134   
135    if (row >= 0)
136    {
137        if (![self isRowSelected: row])
138            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row]
139                byExtendingSelection: NO];
140               
141        return fContextRow;
142    }
143    else
144    {
145        [self deselectAll: self];
146        return fContextNoRow;
147    }
148}
149
150- (void) drawRect: (NSRect) r
151{
152    unsigned i;
153    NSRect rect;
154    NSImage * image;
155    Torrent * torrent;
156
157    [super drawRect: r];
158
159    for( i = 0; i < [fTorrents count]; i++ )
160    {
161        torrent = [fTorrents objectAtIndex: i];
162        rect  = [self pauseRectForRow: i];
163
164        if ([torrent isPaused])
165            image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
166        else if ([torrent isActive])
167            image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
168        else
169            image = nil;
170
171        if (image)
172        {
173            [image setFlipped: YES];
174            [image drawAtPoint: rect.origin fromRect:
175                NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH) operation:
176                NSCompositeSourceOver fraction: 1.0];
177        }
178
179        rect = [self revealRectForRow: i];
180        image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
181        [image setFlipped: YES];
182        [image drawAtPoint: rect.origin fromRect:
183            NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH) operation:
184            NSCompositeSourceOver fraction: 1.0];
185    }
186}
187
188@end
189
190@implementation TorrentTableView (Private)
191
192- (NSRect) pauseRectForRow: (int) row
193{
194    NSRect cellRect = [self frameOfCellAtColumn:
195                [self columnWithIdentifier: @"Torrent"] row: row];
196               
197    return NSMakeRect(cellRect.origin.x + cellRect.size.width
198                        - AREA_CENTER - DISTANCE_FROM_CENTER - BUTTON_WIDTH,
199                        cellRect.origin.y + BUTTON_TO_TOP, BUTTON_WIDTH, BUTTON_WIDTH);
200}
201
202- (NSRect) revealRectForRow: (int) row
203{
204    NSRect cellRect = [self frameOfCellAtColumn:
205                [self columnWithIdentifier: @"Torrent"] row: row];
206   
207    return NSMakeRect(cellRect.origin.x + cellRect.size.width
208                        - AREA_CENTER + DISTANCE_FROM_CENTER,
209                        cellRect.origin.y + BUTTON_TO_TOP, BUTTON_WIDTH, BUTTON_WIDTH);
210}
211
212- (BOOL) pointInIconRect: (NSPoint) point
213{
214    int row = [self rowAtPoint: point];
215    if (row < 0)
216        return NO;
217   
218    NSRect cellRect = [self frameOfCellAtColumn:
219                [self columnWithIdentifier: @"Torrent"] row: row];
220    NSSize iconSize = [[[fTorrents objectAtIndex: row] iconFlipped] size];
221   
222    NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
223                        + (cellRect.size.height - iconSize.height) * 0.5,
224                        iconSize.width, iconSize.height);
225   
226    return NSPointInRect(point, iconRect);
227}
228
229- (BOOL) pointInPauseRect: (NSPoint) point
230{
231    return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
232}
233
234- (BOOL) pointInRevealRect: (NSPoint) point
235{
236    return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
237}
238
239@end
Note: See TracBrowser for help on using the repository browser.