Fix bugs
[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
73 @synthesize image = _image;
74 @synthesize labelColor = _labelColor;
75
76 - (NSRect)imageRectForBounds:(NSRect)bounds {
77     bounds.origin.x += ICON_INSET_HORIZ;
78     bounds.size.width = ICON_SIZE;
79     bounds.origin.y += trunc((bounds.size.height - ICON_SIZE) / 2.0); 
80     bounds.size.height = ICON_SIZE;
81     return bounds;
82 }
83
84 - (NSRect)titleRectForBounds:(NSRect)bounds {
85     // Inset the title for the image
86     CGFloat inset = (ICON_INSET_HORIZ + ICON_SIZE + ICON_TEXT_SPACING);
87     bounds.origin.x += inset;
88     bounds.size.width -= inset;
89     return [super titleRectForBounds:bounds];
90 }
91
92 - (NSSize)cellSizeForBounds:(NSRect)aRect {
93     // Make our cells a bit higher than normal to give some additional space for the icon to fit.
94     NSSize theSize = [super cellSizeForBounds:aRect];
95     theSize.width += (ICON_INSET_HORIZ + ICON_SIZE + ICON_TEXT_SPACING);
96     theSize.height = ICON_INSET_VERT + ICON_SIZE + ICON_INSET_VERT;
97     return theSize;
98 }
99
100 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
101     // First draw a label background color
102     if (self.labelColor != nil) {
103         [[self.labelColor colorWithAlphaComponent:0.2] set];
104         NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver);
105     }
106     
107     NSRect imageRect = [self imageRectForBounds:cellFrame];
108     if (self.image) {
109         // Flip images that don't agree with our flipped state
110         BOOL flipped = [controlView isFlipped] != [self.image isFlipped];
111         if (flipped) {
112             [[NSGraphicsContext currentContext] saveGraphicsState];
113             NSAffineTransform *transform = [[NSAffineTransform alloc] init];
114             [transform translateXBy:0 yBy:cellFrame.origin.y + cellFrame.size.height];
115             [transform scaleXBy:1.0 yBy:-1.0];
116             [transform translateXBy:0 yBy:-cellFrame.origin.y];
117             [transform concat];
118         }
119         [self.image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
120         if (flipped) {
121             [[NSGraphicsContext currentContext] restoreGraphicsState];
122         }
123     }
124     CGFloat inset = (ICON_INSET_HORIZ + ICON_SIZE + ICON_TEXT_SPACING);
125     cellFrame.origin.x += inset;
126     cellFrame.size.width -= inset;
127     cellFrame.origin.y += 1; // Looks better
128     cellFrame.size.height -= 1;
129     [super drawInteriorWithFrame:cellFrame inView:controlView];
130 }
131
132 - (void)drawWithExpansionFrame:(NSRect)cellFrame inView:(NSView *)view {
133     // We want to exclude the icon from the expansion frame when you hover over the cell
134     [super drawInteriorWithFrame:cellFrame inView:view];
135 }
136
137 @end