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