source: trunk/macosx/FileNameCell.m @ 5904

Last change on this file since 5904 was 5904, checked in by livings124, 14 years ago

use a custom class instead of a dictionary for holding a torrent's file structure

  • 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 5904 2008-05-22 18:39:49Z 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.0
33#define IMAGE_FOLDER_SIZE 16.0
34#define IMAGE_ICON_SIZE 32.0
35#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
36#define PADDING_ABOVE_TITLE_FILE 2.0
37#define PADDING_BELOW_STATUS_FILE 2.0
38
39@interface FileNameCell (Private)
40
41- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
42- (NSRect) rectForStatusWithString: (NSAttributedString *) string 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.0], NSFontAttributeName,
60                            paragraphStyle, NSParagraphStyleAttributeName, nil];
61       
62        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
63                                [NSFont messageFontOfSize: 9.0], 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 float IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
119    result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5;
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] inBounds: bounds];
133}
134
135- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
136{
137    //icon
138    [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
139   
140    //title
141    NSColor * specialColor = nil;
142    if ([self isHighlighted]
143            && [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]])
144        specialColor = [NSColor whiteColor];
145    else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles:
146                    [(FileListNode *)[self objectValue] indexes]] == NSOffState)
147        specialColor = [NSColor disabledControlTextColor];
148    else;
149   
150    NSAttributedString * titleString = [self attributedTitleWithColor: specialColor ? specialColor : [NSColor controlTextColor]];
151    NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
152    [titleString drawInRect: titleRect];
153   
154    //status
155    if (![(FileListNode *)[self objectValue] isFolder])
156    {
157        NSAttributedString * statusString = [self attributedStatusWithColor: specialColor ? specialColor : [NSColor darkGrayColor]];
158        NSRect statusRect = [self rectForStatusWithString: statusString inBounds: cellFrame];
159        [statusString drawInRect: statusRect];
160    }
161}
162
163@end
164
165@implementation FileNameCell (Private)
166
167- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
168{
169    NSSize titleSize = [string size];
170   
171    NSRect result = bounds;
172   
173    if (![(FileListNode *)[self objectValue] isFolder])
174    {
175        result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
176        result.origin.y += PADDING_ABOVE_TITLE_FILE;
177    }
178    else
179    {
180        result.origin.x += PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
181        result.origin.y += (result.size.height - titleSize.height) * 0.5;
182    }
183    result.size = titleSize;
184    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
185   
186    return result;
187}
188
189- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
190{
191    if ([(FileListNode *)[self objectValue] isFolder])
192        return NSZeroRect;
193   
194    NSSize statusSize = [string size];
195   
196    NSRect result = bounds;
197   
198    result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
199    result.origin.y += result.size.height - PADDING_BELOW_STATUS_FILE - statusSize.height;
200   
201    result.size = statusSize;
202    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
203   
204    return result;
205}
206
207- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
208{
209    if (color)
210        [fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
211       
212    NSString * title = [(FileListNode *)[self objectValue] name];
213    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
214}
215
216- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color
217{
218    if (color)
219        [fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
220   
221    Torrent * torrent = [(FileOutlineView *)[self controlView] torrent];
222    FileListNode * node = (FileListNode *)[self objectValue];
223    float percent = [torrent fileProgress: [[node indexes] firstIndex]] * 100.0;
224   
225    NSString * status = [NSString localizedStringWithFormat: NSLocalizedString(@"%.2f%% of %@",
226                            "Inspector -> Files tab -> file status string"), percent, [NSString stringForFileSize: [node size]]];
227   
228    return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
229}
230
231@end
Note: See TracBrowser for help on using the repository browser.