source: trunk/macosx/FileBrowserCell.m @ 2712

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

store the folder image in browser cell

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