source: trunk/macosx/FileBrowserCell.m @ 1644

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

add missing licences

File size: 4.6 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 SPACE 2.0
29
30@implementation FileBrowserCell
31
32- (void) awakeFromNib
33{
34    [self setLeaf: YES];
35}
36
37- (void) setImage: (NSImage *) image
38{
39    [image setFlipped: YES];
40    [image setScalesWhenResized: YES];
41    [super setImage: image];
42}
43
44- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
45{
46    NSMutableDictionary * item;
47    if (!(item = [self objectValue]))
48        return;
49   
50    //image
51    float imageHeight = cellFrame.size.height - 2.0;
52   
53    NSImage * image = [self image];
54    [image setSize: NSMakeSize(imageHeight, imageHeight)];
55    NSRect imageRect = NSMakeRect(cellFrame.origin.x + 2.0 * SPACE,
56                                    cellFrame.origin.y + (cellFrame.size.height - imageHeight) / 2.0,
57                                    imageHeight, imageHeight);
58   
59    [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
60   
61    //text
62    NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
63    [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
64   
65    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
66                                    isEqual: [NSColor alternateSelectedControlColor]];
67    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
68                    highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
69                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
70                    paragraphStyle, NSParagraphStyleAttributeName, nil];
71   
72    NSString * nameString = [item objectForKey: @"Name"];
73    NSRect nameTextRect = NSMakeRect(NSMaxX(imageRect) + SPACE, cellFrame.origin.y + 1.0,
74            NSMaxX(cellFrame) - NSMaxX(imageRect) - 2.0 * SPACE, [nameString sizeWithAttributes: nameAttributes].height);
75   
76    [nameString drawInRect: nameTextRect withAttributes: nameAttributes];
77    [nameAttributes release];
78   
79    //status text
80    if (![[item objectForKey: @"IsFolder"] boolValue])
81    {
82        NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
83                        highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
84                        [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
85                        paragraphStyle, NSParagraphStyleAttributeName, nil];
86       
87        NSString * statusString = [NSString stringWithFormat: NSLocalizedString(@"%.2f%% of %@",
88                                                                "Inspector -> Files tab -> file status string"),
89                                    100.0 * [[item objectForKey: @"Progress"] floatValue],
90                                    [NSString stringForFileSize: [[item objectForKey: @"Size"] unsignedLongLongValue]]];
91       
92        NSRect statusTextRect = nameTextRect;
93        statusTextRect.size.height = [statusString sizeWithAttributes: statusAttributes].height;
94        statusTextRect.origin.y += (cellFrame.size.height + nameTextRect.size.height - statusTextRect.size.height) * 0.5 - 1.0;
95       
96        [statusString drawInRect: statusTextRect withAttributes: statusAttributes];
97        [statusAttributes release];
98    }
99   
100    [paragraphStyle release];
101}
102
103@end
Note: See TracBrowser for help on using the repository browser.