source: trunk/macosx/FileNameCell.m @ 7568

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

clean up the table text coloring code; trivial cleanup

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 8.5 KB
Line 
1/******************************************************************************
2 * $Id: FileNameCell.m 7568 2009-01-02 01:10:54Z 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 "NSStringAdditions.h"
30
31#define PADDING_HORIZONAL 2.0f
32#define IMAGE_FOLDER_SIZE 16.0f
33#define IMAGE_ICON_SIZE 32.0f
34#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0f
35#define PADDING_ABOVE_TITLE_FILE 2.0f
36#define PADDING_BELOW_STATUS_FILE 2.0f
37#define PADDING_BETWEEN_NAME_AND_FOLDER_STATUS 4.0f
38
39@interface FileNameCell (Private)
40
41- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
42- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
43
44- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
45- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color;
46
47@end
48
49@implementation FileNameCell
50
51- (id) init
52{
53    if ((self = [super init]))
54    {
55        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
56        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
57       
58        fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
59                            [NSFont messageFontOfSize: 12.0f], NSFontAttributeName,
60                            paragraphStyle, NSParagraphStyleAttributeName, nil];
61       
62        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
63                                [NSFont messageFontOfSize: 9.0f], NSFontAttributeName,
64                                paragraphStyle, NSParagraphStyleAttributeName, nil];
65       
66        [paragraphStyle release];
67    }
68    return self;
69}
70
71- (void) dealloc
72{
73    [fTitleAttributes release];
74    [fStatusAttributes release];
75   
76    [fFolderImage release];
77   
78    [super dealloc];
79}
80
81- (id) copyWithZone: (NSZone *) zone
82{
83    FileNameCell * copy = [super copyWithZone: zone];
84   
85    copy->fTitleAttributes = [fTitleAttributes retain];
86    copy->fStatusAttributes = [fStatusAttributes retain];
87   
88    copy->fFolderImage = [fFolderImage retain];
89   
90    return copy;
91}
92
93- (NSImage *) image
94{
95    FileListNode * node = (FileListNode *)[self objectValue];
96    NSImage * image;
97    if ([node isFolder])
98    {
99        if (!fFolderImage)
100        {
101            fFolderImage = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
102            [fFolderImage setFlipped: YES];
103        }
104        image = fFolderImage;
105    }
106    else
107        image = [node icon];
108   
109    return image;
110}
111
112- (NSRect) imageRectForBounds: (NSRect) bounds
113{
114    NSRect result = bounds;
115   
116    result.origin.x += PADDING_HORIZONAL;
117   
118    const CGFloat IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
119    result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5f;
120    result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
121   
122    return result;
123}
124
125- (NSRect) titleRectForBounds: (NSRect) bounds
126{
127    return [self rectForTitleWithString: [self attributedTitleWithColor: nil] inBounds: bounds];
128}
129
130- (NSRect) statusRectForBounds: (NSRect) bounds
131{
132    return [self rectForStatusWithString: [self attributedStatusWithColor: nil]
133            withTitleRect: [(FileListNode *)[self objectValue] isFolder] ? [self titleRectForBounds: bounds] : NSZeroRect
134            inBounds: bounds];
135}
136
137- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
138{
139    //icon
140    [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
141   
142    NSColor * titleColor, * statusColor;
143    if ([self backgroundStyle] == NSBackgroundStyleDark)
144        titleColor = statusColor = [NSColor whiteColor];
145    else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState)
146        titleColor = statusColor = [NSColor disabledControlTextColor];
147    else
148    {
149        titleColor = [NSColor controlTextColor];
150        statusColor = [NSColor darkGrayColor];
151    }
152   
153    //title
154    NSAttributedString * titleString = [self attributedTitleWithColor: titleColor];
155    NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
156    [titleString drawInRect: titleRect];
157   
158    //status
159    NSAttributedString * statusString = [self attributedStatusWithColor: statusColor];
160    NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame];
161    [statusString drawInRect: statusRect];
162}
163
164@end
165
166@implementation FileNameCell (Private)
167
168- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
169{
170    NSSize titleSize = [string size];
171   
172    NSRect result = bounds;
173   
174    if (![(FileListNode *)[self objectValue] isFolder])
175    {
176        result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
177        result.origin.y += PADDING_ABOVE_TITLE_FILE;
178    }
179    else
180    {
181        result.origin.x += PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
182        result.origin.y += (result.size.height - titleSize.height) * 0.5f;
183    }
184    result.size = titleSize;
185    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
186   
187    return result;
188}
189
190- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
191{
192    NSSize statusSize = [string size];
193   
194    NSRect result;
195    if (![(FileListNode *)[self objectValue] isFolder])
196    {
197        result.origin.x = bounds.origin.x + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
198        result.origin.y = NSMaxY(bounds) - 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
Note: See TracBrowser for help on using the repository browser.