source: trunk/macosx/FileNameCell.m @ 9723

Last change on this file since 9723 was 9723, checked in by livings124, 12 years ago

dead code removal

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 8.1 KB
Line 
1/******************************************************************************
2 * $Id: FileNameCell.m 9723 2009-12-12 03:11:48Z livings124 $
3 *
4 * Copyright (c) 2007-2009 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 *) attributedTitle;
46- (NSAttributedString *) attributedStatus;
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            fFolderImage = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
102        image = fFolderImage;
103    }
104    else
105        image = [node icon];
106   
107    return image;
108}
109
110- (NSRect) imageRectForBounds: (NSRect) bounds
111{
112    NSRect result = bounds;
113   
114    result.origin.x += PADDING_HORIZONAL;
115   
116    const CGFloat IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
117    result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5f;
118    result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
119   
120    return result;
121}
122
123- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
124{
125    //icon
126    if ([NSApp isOnSnowLeopardOrBetter])
127        [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0
128            respectFlipped: YES hints: nil];
129    else
130    {
131        NSImage * image = [self image];
132        [image setFlipped: YES];
133        [image drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
134    }
135   
136    NSColor * titleColor, * statusColor;
137    if ([self backgroundStyle] == NSBackgroundStyleDark)
138        titleColor = statusColor = [NSColor whiteColor];
139    else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState)
140        titleColor = statusColor = [NSColor disabledControlTextColor];
141    else
142    {
143        titleColor = [NSColor controlTextColor];
144        statusColor = [NSColor darkGrayColor];
145    }
146   
147    [fTitleAttributes setObject: titleColor forKey: NSForegroundColorAttributeName];
148    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
149   
150    //title
151    NSAttributedString * titleString = [self attributedTitle];
152    NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
153    [titleString drawInRect: titleRect];
154   
155    //status
156    NSAttributedString * statusString = [self attributedStatus];
157    NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame];
158    [statusString drawInRect: statusRect];
159}
160
161@end
162
163@implementation FileNameCell (Private)
164
165- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
166{
167    NSSize titleSize = [string size];
168   
169    NSRect result = bounds;
170   
171    if (![(FileListNode *)[self objectValue] isFolder])
172    {
173        result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
174        result.origin.y += PADDING_ABOVE_TITLE_FILE;
175    }
176    else
177    {
178        result.origin.x += PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
179        result.origin.y += (result.size.height - titleSize.height) * 0.5f;
180    }
181    result.size = titleSize;
182    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
183   
184    return result;
185}
186
187- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
188{
189    NSSize statusSize = [string size];
190   
191    NSRect result;
192    if (![(FileListNode *)[self objectValue] isFolder])
193    {
194        result.origin.x = bounds.origin.x + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
195        result.origin.y = NSMaxY(bounds) - PADDING_BELOW_STATUS_FILE - statusSize.height;
196    }
197    else
198    {
199        result.origin.x = NSMaxX(titleRect) + PADDING_BETWEEN_NAME_AND_FOLDER_STATUS;
200        result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0f;
201    }
202       
203    result.size = statusSize;
204    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
205   
206    return result;
207}
208
209- (NSAttributedString *) attributedTitle
210{
211    NSString * title = [(FileListNode *)[self objectValue] name];
212    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
213}
214
215- (NSAttributedString *) attributedStatus
216{
217    Torrent * torrent = [(FileOutlineView *)[self controlView] torrent];
218    FileListNode * node = (FileListNode *)[self objectValue];
219   
220    CGFloat progress = [torrent fileProgress: node];
221    NSString * percentString = progress == 1.0f ? @"100%" : [NSString localizedStringWithFormat: @"%.2f%%", progress * 100.0f];
222   
223   
224    NSString * status = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@",
225                            "Inspector -> Files tab -> file status string"), percentString, [NSString stringForFileSize: [node size]]];
226   
227    return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
228}
229
230@end
Note: See TracBrowser for help on using the repository browser.