1 | /****************************************************************************** |
---|
2 | * $Id: FileOutlineView.m 6270 2008-06-30 19:56:29Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007-2008 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 "FileOutlineView.h" |
---|
26 | #import "FileNameCell.h" |
---|
27 | #import "FilePriorityCell.h" |
---|
28 | #import "Torrent.h" |
---|
29 | #import "FileListNode.h" |
---|
30 | #import "QuickLookController.h" |
---|
31 | #import "CTGradient.h" |
---|
32 | |
---|
33 | @implementation FileOutlineView |
---|
34 | |
---|
35 | - (void) awakeFromNib |
---|
36 | { |
---|
37 | FileNameCell * nameCell = [[FileNameCell alloc] init]; |
---|
38 | [[self tableColumnWithIdentifier: @"Name"] setDataCell: nameCell]; |
---|
39 | [nameCell release]; |
---|
40 | |
---|
41 | FilePriorityCell * priorityCell = [[FilePriorityCell alloc] init]; |
---|
42 | [[self tableColumnWithIdentifier: @"Priority"] setDataCell: priorityCell]; |
---|
43 | [priorityCell release]; |
---|
44 | |
---|
45 | [self setAutoresizesOutlineColumn: NO]; |
---|
46 | [self setIndentationPerLevel: 14.0]; |
---|
47 | |
---|
48 | NSColor * endingColor = [NSColor colorWithCalibratedRed: 217.0/255.0 green: 250.0/255.0 blue: 211.0/255.0 alpha: 1.0]; |
---|
49 | NSColor * beginningColor = [endingColor blendedColorWithFraction: 0.3 ofColor: [NSColor whiteColor]]; |
---|
50 | fHighPriorityGradient = [[CTGradient gradientWithBeginningColor: beginningColor endingColor: endingColor] retain]; |
---|
51 | |
---|
52 | endingColor = [NSColor colorWithCalibratedRed: 255.0/255.0 green: 243.0/255.0 blue: 206.0/255.0 alpha: 1.0]; |
---|
53 | beginningColor = [endingColor blendedColorWithFraction: 0.3 ofColor: [NSColor whiteColor]]; |
---|
54 | fLowPriorityGradient = [[CTGradient gradientWithBeginningColor: beginningColor endingColor: endingColor] retain]; |
---|
55 | |
---|
56 | endingColor = [NSColor colorWithCalibratedRed: 225.0/255.0 green: 218.0/255.0 blue: 255.0/255.0 alpha: 1.0]; |
---|
57 | beginningColor = [endingColor blendedColorWithFraction: 0.3 ofColor: [NSColor whiteColor]]; |
---|
58 | fMixedPriorityGradient = [[CTGradient gradientWithBeginningColor: beginningColor endingColor: endingColor] retain]; |
---|
59 | |
---|
60 | fMouseRow = -1; |
---|
61 | } |
---|
62 | |
---|
63 | - (void) dealloc |
---|
64 | { |
---|
65 | [fHighPriorityGradient release]; |
---|
66 | [fLowPriorityGradient release]; |
---|
67 | [fMixedPriorityGradient release]; |
---|
68 | |
---|
69 | [super dealloc]; |
---|
70 | } |
---|
71 | |
---|
72 | - (void) setTorrent: (Torrent *) torrent |
---|
73 | { |
---|
74 | fTorrent = torrent; |
---|
75 | } |
---|
76 | |
---|
77 | - (Torrent *) torrent |
---|
78 | { |
---|
79 | return fTorrent; |
---|
80 | } |
---|
81 | |
---|
82 | - (void) mouseDown: (NSEvent *) event |
---|
83 | { |
---|
84 | [[self window] makeKeyWindow]; |
---|
85 | [super mouseDown: event]; |
---|
86 | } |
---|
87 | |
---|
88 | - (void) keyDown: (NSEvent *) event |
---|
89 | { |
---|
90 | unichar firstChar = [[event charactersIgnoringModifiers] characterAtIndex: 0]; |
---|
91 | if (firstChar == ' ') |
---|
92 | [[QuickLookController quickLook] toggleQuickLook]; |
---|
93 | else if (firstChar == NSRightArrowFunctionKey) |
---|
94 | [[QuickLookController quickLook] pressRight]; |
---|
95 | else if (firstChar == NSLeftArrowFunctionKey) |
---|
96 | [[QuickLookController quickLook] pressLeft]; |
---|
97 | else; |
---|
98 | |
---|
99 | [super keyDown: event]; |
---|
100 | } |
---|
101 | |
---|
102 | - (NSMenu *) menuForEvent: (NSEvent *) event |
---|
103 | { |
---|
104 | int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; |
---|
105 | |
---|
106 | if (row >= 0) |
---|
107 | { |
---|
108 | if (![self isRowSelected: row]) |
---|
109 | [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO]; |
---|
110 | } |
---|
111 | else |
---|
112 | [self deselectAll: self]; |
---|
113 | |
---|
114 | return [self menu]; |
---|
115 | } |
---|
116 | |
---|
117 | - (NSRect) iconRectForRow: (int) row |
---|
118 | { |
---|
119 | FileNameCell * cell = (FileNameCell *)[self preparedCellAtColumn: [self columnWithIdentifier: @"Name"] row: row]; |
---|
120 | NSRect iconRect = [cell imageRectForBounds: [self rectOfRow: row]]; |
---|
121 | |
---|
122 | iconRect.origin.x += [self indentationPerLevel] * (float)([self levelForRow: row] + 1); |
---|
123 | return iconRect; |
---|
124 | } |
---|
125 | |
---|
126 | - (void) updateTrackingAreas |
---|
127 | { |
---|
128 | [super updateTrackingAreas]; |
---|
129 | |
---|
130 | NSEnumerator * enumerator = [[self trackingAreas] objectEnumerator]; |
---|
131 | NSTrackingArea * area; |
---|
132 | while ((area = [enumerator nextObject])) |
---|
133 | { |
---|
134 | if ([area owner] == self && [[area userInfo] objectForKey: @"Row"]) |
---|
135 | [self removeTrackingArea: area]; |
---|
136 | } |
---|
137 | |
---|
138 | NSRange visibleRows = [self rowsInRect: [self visibleRect]]; |
---|
139 | if (visibleRows.length == 0) |
---|
140 | return; |
---|
141 | |
---|
142 | int col = [self columnWithIdentifier: @"Priority"]; |
---|
143 | NSPoint mouseLocation = [self convertPoint: [[self window] convertScreenToBase: [NSEvent mouseLocation]] fromView: nil]; |
---|
144 | |
---|
145 | int row; |
---|
146 | for (row = visibleRows.location; row < NSMaxRange(visibleRows); row++) |
---|
147 | { |
---|
148 | FilePriorityCell * cell = (FilePriorityCell *)[self preparedCellAtColumn: col row: row]; |
---|
149 | |
---|
150 | NSDictionary * userInfo = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: row] forKey: @"Row"]; |
---|
151 | [cell addTrackingAreasForView: self inRect: [self frameOfCellAtColumn: col row: row] withUserInfo: userInfo |
---|
152 | mouseLocation: mouseLocation]; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | - (int) hoveredRow |
---|
157 | { |
---|
158 | return fMouseRow; |
---|
159 | } |
---|
160 | |
---|
161 | - (void) mouseEntered: (NSEvent *) event |
---|
162 | { |
---|
163 | NSNumber * row; |
---|
164 | if ((row = [(NSDictionary *)[event userData] objectForKey: @"Row"])) |
---|
165 | { |
---|
166 | fMouseRow = [row intValue]; |
---|
167 | [self setNeedsDisplayInRect: [self frameOfCellAtColumn: [self columnWithIdentifier: @"Priority"] row: fMouseRow]]; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | - (void) mouseExited: (NSEvent *) event |
---|
172 | { |
---|
173 | NSNumber * row; |
---|
174 | if ((row = [(NSDictionary *)[event userData] objectForKey: @"Row"])) |
---|
175 | { |
---|
176 | [self setNeedsDisplayInRect: [self frameOfCellAtColumn: [self columnWithIdentifier: @"Priority"] row: [row intValue]]]; |
---|
177 | fMouseRow = -1; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | - (void) drawRow: (int) row clipRect: (NSRect) clipRect |
---|
182 | { |
---|
183 | if (![self isRowSelected: row]) |
---|
184 | { |
---|
185 | NSDictionary * item = [self itemAtRow: row]; |
---|
186 | NSIndexSet * indexes = [(FileListNode *)item indexes]; |
---|
187 | |
---|
188 | if ([fTorrent checkForFiles: indexes] != NSOffState) |
---|
189 | { |
---|
190 | CTGradient * gradient = nil; |
---|
191 | |
---|
192 | NSSet * priorities = [fTorrent filePrioritiesForIndexes: indexes]; |
---|
193 | int count = [priorities count]; |
---|
194 | if (count == 1) |
---|
195 | { |
---|
196 | switch ([[priorities anyObject] intValue]) |
---|
197 | { |
---|
198 | case TR_PRI_LOW: |
---|
199 | gradient = fLowPriorityGradient; |
---|
200 | break; |
---|
201 | case TR_PRI_HIGH: |
---|
202 | gradient = fHighPriorityGradient; |
---|
203 | break; |
---|
204 | } |
---|
205 | } |
---|
206 | else if (count > 1) |
---|
207 | gradient = fMixedPriorityGradient; |
---|
208 | else; |
---|
209 | |
---|
210 | if (gradient) |
---|
211 | { |
---|
212 | NSRect rect = [self rectOfRow: row]; |
---|
213 | rect.size.height -= 1.0; |
---|
214 | [gradient fillRect: rect angle: 90]; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | [super drawRow: row clipRect: clipRect]; |
---|
220 | } |
---|
221 | |
---|
222 | @end |
---|