1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ImageAndTextCell.m Tue May 12 14:38:30 2009 +0200
1.3 @@ -0,0 +1,175 @@
1.4 +/*
1.5 + ImageAndTextCell.m
1.6 + Copyright (c) 2001-2006, Apple Computer, Inc., all rights reserved.
1.7 + Author: Chuck Pisula
1.8 +
1.9 + Milestones:
1.10 + * 03-01-2001: Initial creation by Chuck Pisula
1.11 + * 11-04-2005: Added hitTestForEvent:inRect:ofView: for better NSOutlineView support by Corbin Dunn
1.12 +
1.13 + Subclass of NSTextFieldCell which can display text and an image simultaneously.
1.14 +*/
1.15 +
1.16 +/*
1.17 + IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
1.18 + consideration of your agreement to the following terms, and your use, installation,
1.19 + modification or redistribution of this Apple software constitutes acceptance of these
1.20 + terms. If you do not agree with these terms, please do not use, install, modify or
1.21 + redistribute this Apple software.
1.22 +
1.23 + In consideration of your agreement to abide by the following terms, and subject to these
1.24 + terms, Apple grants you a personal, non-exclusive license, under AppleÕs copyrights in
1.25 + this original Apple software (the "Apple Software"), to use, reproduce, modify and
1.26 + redistribute the Apple Software, with or without modifications, in source and/or binary
1.27 + forms; provided that if you redistribute the Apple Software in its entirety and without
1.28 + modifications, you must retain this notice and the following text and disclaimers in all
1.29 + such redistributions of the Apple Software. Neither the name, trademarks, service marks
1.30 + or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
1.31 + the Apple Software without specific prior written permission from Apple. Except as expressly
1.32 + stated in this notice, no other rights or licenses, express or implied, are granted by Apple
1.33 + herein, including but not limited to any patent rights that may be infringed by your
1.34 + derivative works or by other works in which the Apple Software may be incorporated.
1.35 +
1.36 + The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
1.37 + EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
1.38 + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
1.39 + USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
1.40 +
1.41 + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
1.42 + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
1.43 + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
1.44 + REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
1.45 + WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
1.46 + OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.47 +*/
1.48 +
1.49 +#import "ImageAndTextCell.h"
1.50 +#import <AppKit/NSCell.h>
1.51 +
1.52 +@implementation ImageAndTextCell
1.53 +
1.54 +- (id)init {
1.55 + self = [super init];
1.56 + if( self ) {
1.57 + [self setLineBreakMode:NSLineBreakByTruncatingTail];
1.58 + [self setSelectable:YES];
1.59 + }
1.60 + return self;
1.61 +}
1.62 +
1.63 +- (void)dealloc {
1.64 + [image release];
1.65 + [super dealloc];
1.66 +}
1.67 +
1.68 +- (id)copyWithZone:(NSZone *)zone {
1.69 + ImageAndTextCell *cell = (ImageAndTextCell *)[super copyWithZone:zone];
1.70 + // The image ivar will be directly copied; we need to retain or copy it.
1.71 + cell->image = [image retain];
1.72 + return cell;
1.73 +}
1.74 +
1.75 +- (void)setImage:(NSImage *)anImage {
1.76 + if (anImage != image) {
1.77 + [image release];
1.78 + image = [anImage retain];
1.79 + }
1.80 +}
1.81 +
1.82 +- (NSImage *)image {
1.83 + return image;
1.84 +}
1.85 +
1.86 +- (NSRect)imageRectForBounds:(NSRect)cellFrame {
1.87 + NSRect result;
1.88 + if (image != nil) {
1.89 + result.size = [image size];
1.90 + result.origin = cellFrame.origin;
1.91 + result.origin.x += 3;
1.92 + result.origin.y += ceil((cellFrame.size.height - result.size.height) / 2);
1.93 + } else {
1.94 + result = NSZeroRect;
1.95 + }
1.96 + return result;
1.97 +}
1.98 +
1.99 +// We could manually implement expansionFrameWithFrame:inView: and drawWithExpansionFrame:inView: or just properly implement titleRectForBounds to get expansion tooltips to automatically work for us
1.100 +- (NSRect)titleRectForBounds:(NSRect)cellFrame {
1.101 + NSRect result;
1.102 + if (image != nil) {
1.103 + CGFloat imageWidth = [image size].width;
1.104 + result = cellFrame;
1.105 + result.origin.x += (3 + imageWidth);
1.106 + result.size.width -= (3 + imageWidth);
1.107 + } else {
1.108 + result = NSZeroRect;
1.109 + }
1.110 + return result;
1.111 +}
1.112 +
1.113 +
1.114 +- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
1.115 + NSRect textFrame, imageFrame;
1.116 + NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge);
1.117 + [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent];
1.118 +}
1.119 +
1.120 +- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
1.121 + NSRect textFrame, imageFrame;
1.122 + NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge);
1.123 + [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
1.124 +}
1.125 +
1.126 +- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
1.127 + if (image != nil) {
1.128 + NSRect imageFrame;
1.129 + NSSize imageSize = [image size];
1.130 + NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
1.131 + if ([self drawsBackground]) {
1.132 + [[self backgroundColor] set];
1.133 + NSRectFill(imageFrame);
1.134 + }
1.135 + imageFrame.origin.x += 3;
1.136 + imageFrame.size = imageSize;
1.137 +
1.138 + if ([controlView isFlipped])
1.139 + imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
1.140 + else
1.141 + imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
1.142 +
1.143 + [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
1.144 + }
1.145 + [super drawWithFrame:cellFrame inView:controlView];
1.146 +}
1.147 +
1.148 +- (NSSize)cellSize {
1.149 + NSSize cellSize = [super cellSize];
1.150 + cellSize.width += (image ? [image size].width : 0) + 3;
1.151 + return cellSize;
1.152 +}
1.153 +
1.154 +- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
1.155 + NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
1.156 + // If we have an image, we need to see if the user clicked on the image portion.
1.157 + if (image != nil) {
1.158 + // This code closely mimics drawWithFrame:inView:
1.159 + NSSize imageSize = [image size];
1.160 + NSRect imageFrame;
1.161 + NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
1.162 +
1.163 + imageFrame.origin.x += 3;
1.164 + imageFrame.size = imageSize;
1.165 + // If the point is in the image rect, then it is a content hit
1.166 + if (NSMouseInRect(point, imageFrame, [controlView isFlipped])) {
1.167 + // 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.
1.168 + // By returning the correct parts, we allow NSTableView to correctly begin an edit when the text portion is clicked on.
1.169 + return NSCellHitContentArea;
1.170 + }
1.171 + }
1.172 + // 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.
1.173 + return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
1.174 +}
1.175 +
1.176 +
1.177 +@end
1.178 +