source: trunk/macosx/FileNameCell.m

Last change on this file was 14665, checked in by mikedld, 6 years ago

#6050: Get rid of calls to deprecated convertScreenToBase: API (patch by mattrajca)

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 8.9 KB
Line 
1/******************************************************************************
2 * $Id: FileNameCell.m 14665 2016-01-07 17:21:12Z mikedld $
3 *
4 * Copyright (c) 2007-2012 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 "NSStringAdditions.h"
30
31#import "transmission.h" // required by utils.h
32#import "utils.h"
33
34#define PADDING_HORIZONAL 2.0
35#define IMAGE_FOLDER_SIZE 16.0
36#define IMAGE_ICON_SIZE 32.0
37#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
38#define PADDING_ABOVE_TITLE_FILE 2.0
39#define PADDING_BELOW_STATUS_FILE 2.0
40#define PADDING_BETWEEN_NAME_AND_FOLDER_STATUS 4.0
41#define PADDING_EXPANSION_FRAME 2.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    [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0 respectFlipped: YES hints: nil];
120   
121    NSColor * titleColor, * statusColor;
122    if ([self backgroundStyle] == NSBackgroundStyleDark)
123        titleColor = statusColor = [NSColor whiteColor];
124    else if ([[(FileListNode *)[self objectValue] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState)
125        titleColor = statusColor = [NSColor disabledControlTextColor];
126    else
127    {
128        titleColor = [NSColor controlTextColor];
129        statusColor = [NSColor darkGrayColor];
130    }
131   
132    [fTitleAttributes setObject: titleColor forKey: NSForegroundColorAttributeName];
133    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
134   
135    //title
136    NSAttributedString * titleString = [self attributedTitle];
137    NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
138    [titleString drawInRect: titleRect];
139   
140    //status
141    NSAttributedString * statusString = [self attributedStatus];
142    NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame];
143    [statusString drawInRect: statusRect];
144}
145
146- (NSRect) expansionFrameWithFrame: (NSRect) cellFrame inView: (NSView *) view
147{
148    NSAttributedString * titleString = [self attributedTitle];
149    NSRect realRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
150   
151    if ([titleString size].width > NSWidth(realRect)
152        && NSMouseInRect([view convertPoint: [[view window] mouseLocationOutsideOfEventStream] fromView: nil], realRect, [view isFlipped]))
153    {
154        realRect.size.width = [titleString size].width;
155        return NSInsetRect(realRect, -PADDING_EXPANSION_FRAME, -PADDING_EXPANSION_FRAME);
156    }
157   
158    return NSZeroRect;
159}
160
161- (void) drawWithExpansionFrame: (NSRect) cellFrame inView: (NSView *)view
162{
163    cellFrame.origin.x += PADDING_EXPANSION_FRAME;
164    cellFrame.origin.y += PADDING_EXPANSION_FRAME;
165   
166    [fTitleAttributes setObject: [NSColor controlTextColor] forKey: NSForegroundColorAttributeName];
167    NSAttributedString * titleString = [self attributedTitle];
168    [titleString drawInRect: cellFrame];
169}
170
171@end
172
173@implementation FileNameCell (Private)
174
175- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
176{
177    const NSSize titleSize = [string size];
178   
179    //no right padding, so that there's not too much space between this and the priority image
180    NSRect result;
181    if (![(FileListNode *)[self objectValue] isFolder])
182    {
183        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
184        result.origin.y = NSMinY(bounds) + PADDING_ABOVE_TITLE_FILE;
185        result.size.width = NSMaxX(bounds) - NSMinX(result);
186    }
187    else
188    {
189        result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
190        result.origin.y = NSMidY(bounds) - titleSize.height * 0.5;
191        result.size.width = MIN(titleSize.width, NSMaxX(bounds) - NSMinX(result));
192    }
193    result.size.height = titleSize.height;
194   
195    return result;
196}
197
198- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
199{
200    const NSSize statusSize = [string size];
201   
202    NSRect result;
203    if (![(FileListNode *)[self objectValue] isFolder])
204    {
205        result.origin.x = NSMinX(titleRect);
206        result.origin.y = NSMaxY(bounds) - PADDING_BELOW_STATUS_FILE - statusSize.height;
207        result.size.width = NSWidth(titleRect);
208    }
209    else
210    {
211        result.origin.x = NSMaxX(titleRect) + PADDING_BETWEEN_NAME_AND_FOLDER_STATUS;
212        result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0;
213        result.size.width = NSMaxX(bounds) - NSMaxX(titleRect);
214    }
215    result.size.height = statusSize.height;
216   
217    return result;
218}
219
220- (NSAttributedString *) attributedTitle
221{
222    NSString * title = [(FileListNode *)[self objectValue] name];
223    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
224}
225
226- (NSAttributedString *) attributedStatus
227{
228    FileListNode * node = (FileListNode *)[self objectValue];
229    Torrent * torrent = [node torrent];
230   
231    const CGFloat progress = [torrent fileProgress: node];
232    NSString * percentString = [NSString percentString: progress longDecimals: YES];
233   
234    NSString * status = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@",
235                            "Inspector -> Files tab -> file status string"), percentString, [NSString stringForFileSize: [node size]]];
236   
237    return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
238}
239
240@end
Note: See TracBrowser for help on using the repository browser.