source: trunk/macosx/FileNameCell.m @ 3476

Last change on this file since 3476 was 3476, checked in by livings124, 15 years ago

another if-to-switch

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