source: branches/2.0x/macosx/FileNameCell.m @ 11028

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

(2.0x) fix backport error from r10981 that caused errors when adding torrents

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 8.2 KB
Line 
1/******************************************************************************
2 * $Id: FileNameCell.m 11028 2010-07-21 03:59:08Z 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#import "transmission.h" // required by utils.h
33#import "utils.h"
34
35#define PADDING_HORIZONAL 2.0
36#define IMAGE_FOLDER_SIZE 16.0
37#define IMAGE_ICON_SIZE 32.0
38#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
39#define PADDING_ABOVE_TITLE_FILE 2.0
40#define PADDING_BELOW_STATUS_FILE 2.0
41#define PADDING_BETWEEN_NAME_AND_FOLDER_STATUS 4.0
42
43@interface FileNameCell (Private)
44
45- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
46- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
47
48- (NSAttributedString *) attributedTitle;
49- (NSAttributedString *) attributedStatus;
50
51@end
52
53@implementation FileNameCell
54
55- (id) init
56{
57    if ((self = [super init]))
58    {
59        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
60        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingMiddle];
61       
62        fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
63                            [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
64                            paragraphStyle, NSParagraphStyleAttributeName, nil];
65       
66        NSMutableParagraphStyle * statusParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
67        [statusParagraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
68       
69        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
70                                [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
71                                statusParagraphStyle, NSParagraphStyleAttributeName,  nil];
72       
73        [paragraphStyle release];
74        [statusParagraphStyle release];
75    }
76    return self;
77}
78
79- (void) dealloc
80{
81    [fTitleAttributes release];
82    [fStatusAttributes release];
83   
84    [super dealloc];
85}
86
87- (id) copyWithZone: (NSZone *) zone
88{
89    FileNameCell * copy = [super copyWithZone: zone];
90   
91    copy->fTitleAttributes = [fTitleAttributes retain];
92    copy->fStatusAttributes = [fStatusAttributes retain];
93   
94    return copy;
95}
96
97- (NSImage *) image
98{
99    FileListNode * node = (FileListNode *)[self objectValue];
100    return [node icon];
101}
102
103- (NSRect) imageRectForBounds: (NSRect) bounds
104{
105    NSRect result = bounds;
106   
107    result.origin.x += PADDING_HORIZONAL;
108   
109    const CGFloat IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
110    result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5;
111    result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
112   
113    return result;
114}
115
116- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
117{
118    //icon
119    if ([NSApp isOnSnowLeopardOrBetter])
120        [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0
121            respectFlipped: YES hints: nil];
122    else
123    {
124        NSImage * image = [self image];
125        [image setFlipped: YES];
126        [image drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
127    }
128   
129    NSColor * titleColor, * statusColor;
130    if ([self backgroundStyle] == NSBackgroundStyleDark)
131        titleColor = statusColor = [NSColor whiteColor];
132    else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState)
133        titleColor = statusColor = [NSColor disabledControlTextColor];
134    else
135    {
136        titleColor = [NSColor controlTextColor];
137        statusColor = [NSColor darkGrayColor];
138    }
139   
140    [fTitleAttributes setObject: titleColor forKey: NSForegroundColorAttributeName];
141    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
142   
143    //title
144    NSAttributedString * titleString = [self attributedTitle];
145    NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
146    [titleString drawInRect: titleRect];
147   
148    //status
149    NSAttributedString * statusString = [self attributedStatus];
150    NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame];
151    [statusString drawInRect: statusRect];
152}
153
154@end
155
156@implementation FileNameCell (Private)
157
158- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
159{
160    const NSSize titleSize = [string size];
161   
162    NSRect result;
163    if (![(FileListNode *)[self objectValue] isFolder])
164    {
165        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
166        result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE_FILE;
167        result.size.width = NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONAL;
168    }
169    else
170    {
171        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
172        result.origin.y = NSMidY(bounds) - titleSize.height * 0.5;
173        result.size.width = MIN(titleSize.width, NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONAL);
174    }
175    result.size.height = titleSize.height;
176   
177    return result;
178}
179
180- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
181{
182    const NSSize statusSize = [string size];
183   
184    NSRect result;
185    if (![(FileListNode *)[self objectValue] isFolder])
186    {
187        result.origin.x = NSMinX(titleRect);
188        result.origin.y = NSMaxY(bounds) - PADDING_BELOW_STATUS_FILE - statusSize.height;
189        result.size.width = NSWidth(titleRect);
190    }
191    else
192    {
193        result.origin.x = NSMaxX(titleRect) + PADDING_BETWEEN_NAME_AND_FOLDER_STATUS;
194        result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0;
195        result.size.width = NSMaxX(bounds) - NSMaxX(titleRect) - PADDING_HORIZONAL;
196    }
197    result.size.height = statusSize.height;
198   
199    return result;
200}
201
202- (NSAttributedString *) attributedTitle
203{
204    NSString * title = [(FileListNode *)[self objectValue] name];
205    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
206}
207
208- (NSAttributedString *) attributedStatus
209{
210    Torrent * torrent = [(FileOutlineView *)[self controlView] torrent];
211    FileListNode * node = (FileListNode *)[self objectValue];
212   
213    const CGFloat progress = [torrent fileProgress: node];
214    NSString * percentString = progress == 1.0 ? @"100%"
215                                : [NSString localizedStringWithFormat: @"%.2f%%", tr_truncd(progress * 100.0, 2)];
216   
217    NSString * status = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@",
218                            "Inspector -> Files tab -> file status string"), percentString, [NSString stringForFileSize: [node size]]];
219   
220    return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
221}
222
223@end
Note: See TracBrowser for help on using the repository browser.