source: trunk/macosx/TorrentTableView.m @ 421

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

A lot of cleanup; remove cantfindaname once and for all.

  • Property svn:keywords set to Date Rev Author Id
File size: 7.0 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 421 2006-06-21 14:07:56Z 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 *) event
74{
75    fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];
76
77    if ([event modifierFlags] & NSAlternateKeyMask)
78    {
79        [fController advancedChanged: self];
80        fClickPoint = NSZeroPoint;
81    }
82    else if (![self pointInPauseRect: fClickPoint] && ![self pointInRevealRect: fClickPoint])
83        [super mouseDown: event];
84    else;
85
86    [self display];
87}
88
89- (void) mouseUp: (NSEvent *) event
90{
91    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
92    int row = [self rowAtPoint: point];
93    BOOL sameRow = row == [self rowAtPoint: fClickPoint];
94   
95    if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint])
96    {
97        Torrent * torrent = [fTorrents objectAtIndex: row];
98
99                if ([torrent isPaused])
100                        [fController resumeTorrentWithIndex: [NSIndexSet indexSetWithIndex: row]];
101                else if ([torrent isActive])
102                        [fController stopTorrentWithIndex: [NSIndexSet indexSetWithIndex: row]];
103                else;
104    }
105    else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint])
106        [[fTorrents objectAtIndex: row] reveal];
107        else if ([event clickCount] == 2)
108    {
109        if ([self pointInIconRect: point])
110            [[fTorrents objectAtIndex: row] reveal];
111        else
112            [fController showInfo: nil];
113    }
114    else;
115   
116        [super mouseUp: event];
117
118    fClickPoint = NSZeroPoint;
119    [self display];
120}
121
122- (NSMenu *) menuForEvent: (NSEvent *) event
123{
124    int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
125   
126    if (row >= 0)
127    {
128        if (![self isRowSelected: row])
129            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
130               
131        return fContextRow;
132    }
133    else
134    {
135        [self deselectAll: self];
136        return fContextNoRow;
137    }
138}
139
140- (void) drawRect: (NSRect) r
141{
142    NSRect rect;
143    Torrent * torrent;
144    NSImage * image;
145
146    [super drawRect: r];
147
148    int i;
149    for (i = 0; i < [fTorrents count]; i++)
150    {
151        torrent = [fTorrents objectAtIndex: i];
152        rect  = [self pauseRectForRow: i];
153
154        if ([torrent isPaused])
155            image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
156        else if ([torrent isActive])
157            image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
158        else
159            image = nil;
160
161        if (image)
162        {
163            [image setFlipped: YES];
164            [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
165                operation: NSCompositeSourceOver fraction: 1.0];
166        }
167
168        rect = [self revealRectForRow: i];
169        image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
170        [image setFlipped: YES];
171        [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
172            operation: NSCompositeSourceOver fraction: 1.0];
173    }
174}
175
176@end
177
178@implementation TorrentTableView (Private)
179
180- (NSRect) pauseRectForRow: (int) row
181{
182    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
183               
184    return NSMakeRect(cellRect.origin.x + cellRect.size.width
185                        - AREA_CENTER - DISTANCE_FROM_CENTER - BUTTON_WIDTH,
186                        cellRect.origin.y + BUTTON_TO_TOP, BUTTON_WIDTH, BUTTON_WIDTH);
187}
188
189- (NSRect) revealRectForRow: (int) row
190{
191    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
192   
193    return NSMakeRect(cellRect.origin.x + cellRect.size.width - AREA_CENTER + DISTANCE_FROM_CENTER,
194                        cellRect.origin.y + BUTTON_TO_TOP, BUTTON_WIDTH, BUTTON_WIDTH);
195}
196
197- (BOOL) pointInIconRect: (NSPoint) point
198{
199    int row = [self rowAtPoint: point];
200    if (row < 0)
201        return NO;
202   
203    NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
204    NSSize iconSize = [[[fTorrents objectAtIndex: row] iconFlipped] size];
205   
206    NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
207            + (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height);
208   
209    return NSPointInRect(point, iconRect);
210}
211
212- (BOOL) pointInPauseRect: (NSPoint) point
213{
214    return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
215}
216
217- (BOOL) pointInRevealRect: (NSPoint) point
218{
219    return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
220}
221
222@end
Note: See TracBrowser for help on using the repository browser.