2 * Copyright (c) 2009 David Kocher. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Bug fixes, suggestions and comments should be sent to:
16 * dkocher@cyberduck.ch
19 #import "CDOutlineCell.h"
21 @implementation CDOutlineCell
24 if ((self = [super init])) {
25 [self setLineBreakMode:NSLineBreakByTruncatingTail];
26 [self setSelectable:YES];
31 - (void)setIcon:(NSImage *)aIcon
37 icon = [aIcon retain];
45 - (id)copyWithZone:(NSZone *)zone
47 CDOutlineCell *cell = (CDOutlineCell *)[super copyWithZone:zone];
50 [cell setIcon:[self icon]];
61 - (NSRect)imageRectForBounds:(NSRect)cellFrame {
64 result.size = NSMakeSize(16, 16);
65 result.origin = cellFrame.origin;
67 result.origin.y += ceil((cellFrame.size.height - result.size.height) / 2);
74 - (NSRect)titleRectForBounds:(NSRect)cellFrame {
77 CGFloat imageWidth = NSMakeSize(16, 16).width;
79 result.origin.x += (3 + imageWidth);
80 result.size.width -= (3 + imageWidth);
82 result = [super titleRectForBounds:cellFrame];
87 - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
88 [super editWithFrame:[self titleRectForBounds:aRect] inView:controlView editor:textObj delegate:anObject event:theEvent];
91 - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
92 [super selectWithFrame:[self titleRectForBounds:aRect] inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
95 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
97 NSRect imageFrame = [self imageRectForBounds:cellFrame];
98 [icon drawInRect:imageFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
99 NSInteger newX = NSMaxX(imageFrame) + 3;
100 cellFrame.size.width = NSMaxX(cellFrame) - newX;
101 cellFrame.origin.x = newX;
103 [super drawWithFrame:cellFrame inView:controlView];
107 NSSize cellSize = [super cellSize];
109 cellSize.width += NSMakeSize(16, 16).width;
115 - (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
116 NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
117 // If we have an image, we need to see if the user clicked on the image portion.
119 // This code closely mimics drawWithFrame:inView:
120 NSSize imageSize = NSMakeSize(16, 16);
122 NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
124 imageFrame.origin.x += 3;
125 imageFrame.size = imageSize;
126 // If the point is in the image rect, then it is a content hit
127 if (NSMouseInRect(point, imageFrame, [controlView isFlipped])) {
128 // We consider this just a content area. It is not trackable, nor it it editable text. If it was, we would or in the additional items.
129 // By returning the correct parts, we allow NSTableView to correctly begin an edit when the text portion is clicked on.
130 return NSCellHitContentArea;
133 // At this point, the cellFrame has been modified to exclude the portion for the image. Let the superclass handle the hit testing at this point.
134 return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];