1 | /****************************************************************************** |
---|
2 | * $Id: FileNameCell.m 7005 2008-11-01 14:26:51Z 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 "FileNameCell.h" |
---|
26 | #import "FileOutlineView.h" |
---|
27 | #import "Torrent.h" |
---|
28 | #import "FileListNode.h" |
---|
29 | #import "NSApplicationAdditions.h" |
---|
30 | #import "NSStringAdditions.h" |
---|
31 | |
---|
32 | #define PADDING_HORIZONAL 2.0f |
---|
33 | #define IMAGE_FOLDER_SIZE 16.0f |
---|
34 | #define IMAGE_ICON_SIZE 32.0f |
---|
35 | #define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0f |
---|
36 | #define PADDING_ABOVE_TITLE_FILE 2.0f |
---|
37 | #define PADDING_BELOW_STATUS_FILE 2.0f |
---|
38 | #define PADDING_BETWEEN_NAME_AND_FOLDER_STATUS 4.0f |
---|
39 | |
---|
40 | @interface FileNameCell (Private) |
---|
41 | |
---|
42 | - (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds; |
---|
43 | - (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds; |
---|
44 | |
---|
45 | - (NSAttributedString *) attributedTitleWithColor: (NSColor *) color; |
---|
46 | - (NSAttributedString *) attributedStatusWithColor: (NSColor *) color; |
---|
47 | |
---|
48 | @end |
---|
49 | |
---|
50 | @implementation FileNameCell |
---|
51 | |
---|
52 | - (id) init |
---|
53 | { |
---|
54 | if ((self = [super init])) |
---|
55 | { |
---|
56 | NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
---|
57 | [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail]; |
---|
58 | |
---|
59 | fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
---|
60 | [NSFont messageFontOfSize: 12.0f], NSFontAttributeName, |
---|
61 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
62 | |
---|
63 | fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
---|
64 | [NSFont messageFontOfSize: 9.0f], NSFontAttributeName, |
---|
65 | paragraphStyle, NSParagraphStyleAttributeName, nil]; |
---|
66 | |
---|
67 | [paragraphStyle release]; |
---|
68 | } |
---|
69 | return self; |
---|
70 | } |
---|
71 | |
---|
72 | - (void) dealloc |
---|
73 | { |
---|
74 | [fTitleAttributes release]; |
---|
75 | [fStatusAttributes release]; |
---|
76 | |
---|
77 | [fFolderImage release]; |
---|
78 | |
---|
79 | [super dealloc]; |
---|
80 | } |
---|
81 | |
---|
82 | - (id) copyWithZone: (NSZone *) zone |
---|
83 | { |
---|
84 | FileNameCell * copy = [super copyWithZone: zone]; |
---|
85 | |
---|
86 | copy->fTitleAttributes = [fTitleAttributes retain]; |
---|
87 | copy->fStatusAttributes = [fStatusAttributes retain]; |
---|
88 | |
---|
89 | copy->fFolderImage = [fFolderImage retain]; |
---|
90 | |
---|
91 | return copy; |
---|
92 | } |
---|
93 | |
---|
94 | - (NSImage *) image |
---|
95 | { |
---|
96 | FileListNode * node = (FileListNode *)[self objectValue]; |
---|
97 | NSImage * image; |
---|
98 | if ([node isFolder]) |
---|
99 | { |
---|
100 | if (!fFolderImage) |
---|
101 | { |
---|
102 | fFolderImage = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy]; |
---|
103 | [fFolderImage setFlipped: YES]; |
---|
104 | } |
---|
105 | image = fFolderImage; |
---|
106 | } |
---|
107 | else |
---|
108 | image = [node icon]; |
---|
109 | |
---|
110 | return image; |
---|
111 | } |
---|
112 | |
---|
113 | - (NSRect) imageRectForBounds: (NSRect) bounds |
---|
114 | { |
---|
115 | NSRect result = bounds; |
---|
116 | |
---|
117 | result.origin.x += PADDING_HORIZONAL; |
---|
118 | |
---|
119 | const CGFloat IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE; |
---|
120 | result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5f; |
---|
121 | result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE); |
---|
122 | |
---|
123 | return result; |
---|
124 | } |
---|
125 | |
---|
126 | - (NSRect) titleRectForBounds: (NSRect) bounds |
---|
127 | { |
---|
128 | return [self rectForTitleWithString: [self attributedTitleWithColor: nil] inBounds: bounds]; |
---|
129 | } |
---|
130 | |
---|
131 | - (NSRect) statusRectForBounds: (NSRect) bounds |
---|
132 | { |
---|
133 | return [self rectForStatusWithString: [self attributedStatusWithColor: nil] |
---|
134 | withTitleRect: [(FileListNode *)[self objectValue] isFolder] ? [self titleRectForBounds: bounds] : NSZeroRect |
---|
135 | inBounds: bounds]; |
---|
136 | } |
---|
137 | |
---|
138 | - (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView |
---|
139 | { |
---|
140 | //icon |
---|
141 | [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; |
---|
142 | |
---|
143 | //title |
---|
144 | NSColor * specialColor = nil; |
---|
145 | if ([self isHighlighted] |
---|
146 | && [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]]) |
---|
147 | specialColor = [NSColor whiteColor]; |
---|
148 | else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState) |
---|
149 | specialColor = [NSColor disabledControlTextColor]; |
---|
150 | else; |
---|
151 | |
---|
152 | NSAttributedString * titleString = [self attributedTitleWithColor: specialColor ? specialColor : [NSColor controlTextColor]]; |
---|
153 | NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame]; |
---|
154 | [titleString drawInRect: titleRect]; |
---|
155 | |
---|
156 | //status |
---|
157 | NSAttributedString * statusString = [self attributedStatusWithColor: specialColor ? specialColor : [NSColor darkGrayColor]]; |
---|
158 | NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame]; |
---|
159 | [statusString drawInRect: statusRect]; |
---|
160 | } |
---|
161 | |
---|
162 | @end |
---|
163 | |
---|
164 | @implementation FileNameCell (Private) |
---|
165 | |
---|
166 | - (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds |
---|
167 | { |
---|
168 | NSSize titleSize = [string size]; |
---|
169 | |
---|
170 | NSRect result = bounds; |
---|
171 | |
---|
172 | if (![(FileListNode *)[self objectValue] isFolder]) |
---|
173 | { |
---|
174 | result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE; |
---|
175 | result.origin.y += PADDING_ABOVE_TITLE_FILE; |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | result.origin.x += PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE; |
---|
180 | result.origin.y += (result.size.height - titleSize.height) * 0.5f; |
---|
181 | } |
---|
182 | result.size = titleSize; |
---|
183 | result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); |
---|
184 | |
---|
185 | return result; |
---|
186 | } |
---|
187 | |
---|
188 | - (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds; |
---|
189 | { |
---|
190 | NSSize statusSize = [string size]; |
---|
191 | |
---|
192 | NSRect result; |
---|
193 | |
---|
194 | if (![(FileListNode *)[self objectValue] isFolder]) |
---|
195 | { |
---|
196 | result = bounds; |
---|
197 | result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE; |
---|
198 | result.origin.y += result.size.height - PADDING_BELOW_STATUS_FILE - statusSize.height; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | result.origin.x = NSMaxX(titleRect) + PADDING_BETWEEN_NAME_AND_FOLDER_STATUS; |
---|
203 | result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0f; |
---|
204 | } |
---|
205 | |
---|
206 | result.size = statusSize; |
---|
207 | result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); |
---|
208 | |
---|
209 | return result; |
---|
210 | } |
---|
211 | |
---|
212 | - (NSAttributedString *) attributedTitleWithColor: (NSColor *) color |
---|
213 | { |
---|
214 | if (color) |
---|
215 | [fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName]; |
---|
216 | |
---|
217 | NSString * title = [(FileListNode *)[self objectValue] name]; |
---|
218 | return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease]; |
---|
219 | } |
---|
220 | |
---|
221 | - (NSAttributedString *) attributedStatusWithColor: (NSColor *) color |
---|
222 | { |
---|
223 | if (color) |
---|
224 | [fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName]; |
---|
225 | |
---|
226 | Torrent * torrent = [(FileOutlineView *)[self controlView] torrent]; |
---|
227 | FileListNode * node = (FileListNode *)[self objectValue]; |
---|
228 | |
---|
229 | NSString * percentString; |
---|
230 | CGFloat progress = [torrent fileProgress: node]; |
---|
231 | percentString = progress == 1.0f ? @"100%" : [NSString localizedStringWithFormat: @"%.2f%%", progress * 100.0f]; |
---|
232 | |
---|
233 | |
---|
234 | NSString * status = [NSString localizedStringWithFormat: NSLocalizedString(@"%@ of %@", |
---|
235 | "Inspector -> Files tab -> file status string"), percentString, [NSString stringForFileSize: [node size]]]; |
---|
236 | |
---|
237 | return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease]; |
---|
238 | } |
---|
239 | |
---|
240 | @end |
---|