Make info windows modular. Add support for versions pane in info window. Allow downlo...
[pithos-macos] / pithos-macos / FileSystemBrowserCell.m
1 /*
2      File: FileSystemBrowserCell.m
3  Abstract: A cell that can draw an image/icon and a label color.
4   Version: 1.1
5  
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12  
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28  
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34  
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43  
44  Copyright (C) 2011 Apple Inc. All Rights Reserved.
45  
46  */
47
48 #import "FileSystemBrowserCell.h"
49
50 @implementation FileSystemBrowserCell
51
52 #define ICON_SIZE               16.0    // Our Icons are ICON_SIZE x ICON_SIZE 
53 #define ICON_INSET_HORIZ        4.0     // Distance to inset the icon from the left edge. 
54 #define ICON_TEXT_SPACING       2.0     // Distance between the end of the icon and the text part 
55 #define ICON_INSET_VERT         2.0     // Distance from top/bottom of icon
56
57 - (id)init {
58     self = [super init];
59     [self setLineBreakMode:NSLineBreakByTruncatingTail];
60     return self;
61 }
62
63 - (id)copyWithZone:(NSZone *)zone {
64     FileSystemBrowserCell *result = [super copyWithZone:zone];
65     result->_image = nil;
66     result.image = self.image;
67     result->_labelColor = nil;
68     result.labelColor = self.labelColor;
69     return result;
70 }
71
72 - (void)dealloc {
73     [_image release];
74     [_labelColor release];
75     [super dealloc];
76 }
77
78 @synthesize image = _image;
79 @synthesize labelColor = _labelColor;
80
81 - (NSRect)imageRectForBounds:(NSRect)bounds {
82     bounds.origin.x += ICON_INSET_HORIZ;
83     bounds.size.width = ICON_SIZE;
84     bounds.origin.y += trunc((bounds.size.height - ICON_SIZE) / 2.0); 
85     bounds.size.height = ICON_SIZE;
86     return bounds;
87 }
88
89 - (NSRect)titleRectForBounds:(NSRect)bounds {
90     // Inset the title for the image
91     CGFloat inset = (ICON_INSET_HORIZ + ICON_SIZE + ICON_TEXT_SPACING);
92     bounds.origin.x += inset;
93     bounds.size.width -= inset;
94     return [super titleRectForBounds:bounds];
95 }
96
97 - (NSSize)cellSizeForBounds:(NSRect)aRect {
98     // Make our cells a bit higher than normal to give some additional space for the icon to fit.
99     NSSize theSize = [super cellSizeForBounds:aRect];
100     theSize.width += (ICON_INSET_HORIZ + ICON_SIZE + ICON_TEXT_SPACING);
101     theSize.height = ICON_INSET_VERT + ICON_SIZE + ICON_INSET_VERT;
102     return theSize;
103 }
104
105 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
106     // First draw a label background color
107     if (self.labelColor != nil) {
108         [[self.labelColor colorWithAlphaComponent:0.2] set];
109         NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver);
110     }
111     
112     NSRect imageRect = [self imageRectForBounds:cellFrame];
113     if (self.image) {
114         // Flip images that don't agree with our flipped state
115         BOOL flipped = [controlView isFlipped] != [self.image isFlipped];
116         if (flipped) {
117             [[NSGraphicsContext currentContext] saveGraphicsState];
118             NSAffineTransform *transform = [[NSAffineTransform alloc] init];
119             [transform translateXBy:0 yBy:cellFrame.origin.y + cellFrame.size.height];
120             [transform scaleXBy:1.0 yBy:-1.0];
121             [transform translateXBy:0 yBy:-cellFrame.origin.y];
122             [transform concat];
123             [transform release];
124         }
125         [self.image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
126         if (flipped) {
127             [[NSGraphicsContext currentContext] restoreGraphicsState];
128         }
129     }
130     CGFloat inset = (ICON_INSET_HORIZ + ICON_SIZE + ICON_TEXT_SPACING);
131     cellFrame.origin.x += inset;
132     cellFrame.size.width -= inset;
133     cellFrame.origin.y += 1; // Looks better
134     cellFrame.size.height -= 1;
135     [super drawInteriorWithFrame:cellFrame inView:controlView];
136 }
137
138 - (void)drawWithExpansionFrame:(NSRect)cellFrame inView:(NSView *)view {
139     // We want to exclude the icon from the expansion frame when you hover over the cell
140     [super drawInteriorWithFrame:cellFrame inView:view];
141 }
142
143 @end