source: trunk/macosx/FileNameCell.m @ 10711

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

#3216 Progress Percentage Should Round Down

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