1 | /****************************************************************************** |
---|
2 | * $Id: TorrentTableView.m 532 2006-07-05 22:53:37Z 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 19.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 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
63 | } |
---|
64 | |
---|
65 | return self; |
---|
66 | } |
---|
67 | |
---|
68 | - (void) awakeFromNib |
---|
69 | { |
---|
70 | [fContextRow setTitle: @"Context"]; |
---|
71 | [fContextNoRow setTitle: @"Context"]; |
---|
72 | } |
---|
73 | |
---|
74 | - (void) setTorrents: (NSArray *) torrents |
---|
75 | { |
---|
76 | fTorrents = torrents; |
---|
77 | } |
---|
78 | |
---|
79 | - (void) mouseDown: (NSEvent *) event |
---|
80 | { |
---|
81 | fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil]; |
---|
82 | |
---|
83 | if ([event modifierFlags] & NSAlternateKeyMask) |
---|
84 | { |
---|
85 | [fController toggleAdvancedBar: self]; |
---|
86 | fClickPoint = NSZeroPoint; |
---|
87 | } |
---|
88 | else if (![self pointInPauseRect: fClickPoint] && ![self pointInRevealRect: fClickPoint]) |
---|
89 | [super mouseDown: event]; |
---|
90 | else; |
---|
91 | |
---|
92 | [self display]; |
---|
93 | } |
---|
94 | |
---|
95 | - (void) mouseUp: (NSEvent *) event |
---|
96 | { |
---|
97 | NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil]; |
---|
98 | int row = [self rowAtPoint: point]; |
---|
99 | BOOL sameRow = row == [self rowAtPoint: fClickPoint]; |
---|
100 | |
---|
101 | if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint]) |
---|
102 | { |
---|
103 | Torrent * torrent = [fTorrents objectAtIndex: row]; |
---|
104 | |
---|
105 | if ([torrent isPaused]) |
---|
106 | [fController resumeTorrentWithIndex: [NSIndexSet indexSetWithIndex: row]]; |
---|
107 | else if ([torrent isActive]) |
---|
108 | [fController stopTorrentWithIndex: [NSIndexSet indexSetWithIndex: row]]; |
---|
109 | else; |
---|
110 | } |
---|
111 | else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint]) |
---|
112 | [[fTorrents objectAtIndex: row] revealData]; |
---|
113 | else if ([event clickCount] == 2) |
---|
114 | { |
---|
115 | if ([self pointInIconRect: point]) |
---|
116 | [[fTorrents objectAtIndex: row] revealData]; |
---|
117 | else |
---|
118 | [fController showInfo: nil]; |
---|
119 | } |
---|
120 | else; |
---|
121 | |
---|
122 | [super mouseUp: event]; |
---|
123 | |
---|
124 | fClickPoint = NSZeroPoint; |
---|
125 | [self display]; |
---|
126 | } |
---|
127 | |
---|
128 | - (NSMenu *) menuForEvent: (NSEvent *) event |
---|
129 | { |
---|
130 | int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; |
---|
131 | |
---|
132 | if (row >= 0) |
---|
133 | { |
---|
134 | if (![self isRowSelected: row]) |
---|
135 | [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO]; |
---|
136 | |
---|
137 | return fContextRow; |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | [self deselectAll: self]; |
---|
142 | return fContextNoRow; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | - (void) drawRect: (NSRect) r |
---|
147 | { |
---|
148 | NSRect rect; |
---|
149 | Torrent * torrent; |
---|
150 | NSImage * image; |
---|
151 | |
---|
152 | [super drawRect: r]; |
---|
153 | |
---|
154 | int i; |
---|
155 | for (i = 0; i < [fTorrents count]; i++) |
---|
156 | { |
---|
157 | torrent = [fTorrents objectAtIndex: i]; |
---|
158 | rect = [self pauseRectForRow: i]; |
---|
159 | |
---|
160 | if ([torrent isPaused]) |
---|
161 | image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon; |
---|
162 | else if ([torrent isActive]) |
---|
163 | image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon; |
---|
164 | else |
---|
165 | image = nil; |
---|
166 | |
---|
167 | if (image) |
---|
168 | { |
---|
169 | [image setFlipped: YES]; |
---|
170 | [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH) |
---|
171 | operation: NSCompositeSourceOver fraction: 1.0]; |
---|
172 | } |
---|
173 | |
---|
174 | rect = [self revealRectForRow: i]; |
---|
175 | image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon; |
---|
176 | [image setFlipped: YES]; |
---|
177 | [image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH) |
---|
178 | operation: NSCompositeSourceOver fraction: 1.0]; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | @end |
---|
183 | |
---|
184 | @implementation TorrentTableView (Private) |
---|
185 | |
---|
186 | - (NSRect) pauseRectForRow: (int) row |
---|
187 | { |
---|
188 | NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row]; |
---|
189 | |
---|
190 | float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR; |
---|
191 | |
---|
192 | return NSMakeRect(cellRect.origin.x + cellRect.size.width - AREA_CENTER - DISTANCE_FROM_CENTER - BUTTON_WIDTH, |
---|
193 | cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH); |
---|
194 | } |
---|
195 | |
---|
196 | - (NSRect) revealRectForRow: (int) row |
---|
197 | { |
---|
198 | NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row]; |
---|
199 | |
---|
200 | float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR; |
---|
201 | |
---|
202 | return NSMakeRect(cellRect.origin.x + cellRect.size.width - AREA_CENTER + DISTANCE_FROM_CENTER, |
---|
203 | cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH); |
---|
204 | } |
---|
205 | |
---|
206 | - (BOOL) pointInIconRect: (NSPoint) point |
---|
207 | { |
---|
208 | int row = [self rowAtPoint: point]; |
---|
209 | if (row < 0) |
---|
210 | return NO; |
---|
211 | |
---|
212 | NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row]; |
---|
213 | NSSize iconSize = [fDefaults boolForKey: @"SmallView"] ? [[[fTorrents objectAtIndex: row] iconSmall] size] |
---|
214 | : [[[fTorrents objectAtIndex: row] iconFlipped] size]; |
---|
215 | |
---|
216 | NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y |
---|
217 | + (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height); |
---|
218 | |
---|
219 | return NSPointInRect(point, iconRect); |
---|
220 | } |
---|
221 | |
---|
222 | - (BOOL) pointInPauseRect: (NSPoint) point |
---|
223 | { |
---|
224 | return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]); |
---|
225 | } |
---|
226 | |
---|
227 | - (BOOL) pointInRevealRect: (NSPoint) point |
---|
228 | { |
---|
229 | return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]); |
---|
230 | } |
---|
231 | |
---|
232 | @end |
---|