source: trunk/macosx/FileNameCell.m @ 10206

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

remove temporary asserts

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 7.8 KB
Line 
1/******************************************************************************
2 * $Id: FileNameCell.m 10206 2010-02-15 14:56:14Z livings124 $
3 *
4 * Copyright (c) 2007-2010 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#define PADDING_BETWEEN_NAME_AND_FOLDER_STATUS 4.0
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.0], NSFontAttributeName,
61                            paragraphStyle, NSParagraphStyleAttributeName, nil];
62       
63        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
64                                [NSFont messageFontOfSize: 9.0], 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    [super dealloc];
78}
79
80- (id) copyWithZone: (NSZone *) zone
81{
82    FileNameCell * copy = [super copyWithZone: zone];
83   
84    copy->fTitleAttributes = [fTitleAttributes retain];
85    copy->fStatusAttributes = [fStatusAttributes retain];
86   
87    return copy;
88}
89
90- (NSImage *) image
91{
92    FileListNode * node = (FileListNode *)[self objectValue];
93    return [node icon];
94}
95
96- (NSRect) imageRectForBounds: (NSRect) bounds
97{
98    NSRect result = bounds;
99   
100    result.origin.x += PADDING_HORIZONAL;
101   
102    const CGFloat IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
103    result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5;
104    result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
105   
106    return result;
107}
108
109- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
110{
111    //icon
112    if ([NSApp isOnSnowLeopardOrBetter])
113        [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0
114            respectFlipped: YES hints: nil];
115    else
116    {
117        NSImage * image = [self image];
118        [image setFlipped: YES];
119        [image drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
120    }
121   
122    NSColor * titleColor, * statusColor;
123    if ([self backgroundStyle] == NSBackgroundStyleDark)
124        titleColor = statusColor = [NSColor whiteColor];
125    else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState)
126        titleColor = statusColor = [NSColor disabledControlTextColor];
127    else
128    {
129        titleColor = [NSColor controlTextColor];
130        statusColor = [NSColor darkGrayColor];
131    }
132   
133    [fTitleAttributes setObject: titleColor forKey: NSForegroundColorAttributeName];
134    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
135   
136    //title
137    NSAttributedString * titleString = [self attributedTitle];
138    NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
139    [titleString drawInRect: titleRect];
140   
141    //status
142    NSAttributedString * statusString = [self attributedStatus];
143    NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame];
144    [statusString drawInRect: statusRect];
145}
146
147@end
148
149@implementation FileNameCell (Private)
150
151- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
152{
153    const NSSize titleSize = [string size];
154   
155    NSRect result;
156    if (![(FileListNode *)[self objectValue] isFolder])
157    {
158        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
159        result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE_FILE;
160    }
161    else
162    {
163        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
164        result.origin.y = NSMidY(bounds) - titleSize.height * 0.5;
165    }
166    result.size.height = titleSize.height;
167    result.size.width = MIN(titleSize.width, NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONAL);
168   
169    return result;
170}
171
172- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
173{
174    const NSSize statusSize = [string size];
175   
176    NSRect result;
177    if (![(FileListNode *)[self objectValue] isFolder])
178    {
179        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
180        result.origin.y = NSMaxY(bounds) - PADDING_BELOW_STATUS_FILE - statusSize.height;
181    }
182    else
183    {
184        result.origin.x = NSMaxX(titleRect) + PADDING_BETWEEN_NAME_AND_FOLDER_STATUS;
185        result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0;
186    }
187       
188    result.size.height = statusSize.height;
189    result.size.width = NSMaxX(bounds) - NSMaxX(result) - PADDING_HORIZONAL;
190   
191    return result;
192}
193
194- (NSAttributedString *) attributedTitle
195{
196    NSString * title = [(FileListNode *)[self objectValue] name];
197    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
198}
199
200- (NSAttributedString *) attributedStatus
201{
202    Torrent * torrent = [(FileOutlineView *)[self controlView] torrent];
203    FileListNode * node = (FileListNode *)[self objectValue];
204   
205    const CGFloat progress = [torrent fileProgress: node];
206    NSString * percentString = progress == 1.0 ? @"100%" : [NSString localizedStringWithFormat: @"%.2f%%", progress * 100.0];
207   
208    NSString * status = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@",
209                            "Inspector -> Files tab -> file status string"), percentString, [NSString stringForFileSize: [node size]]];
210   
211    return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
212}
213
214@end
Note: See TracBrowser for help on using the repository browser.