ImageAndTextCell.m (5359B)
1 /* 2 ImageAndTextCell.m 3 Copyright (c) 2001-2004, Apple Computer, Inc., all rights reserved. 4 Author: Chuck Pisula 5 6 Milestones: 7 Initially created 3/1/01 8 9 Subclass of NSTextFieldCell which can display text and an image simultaneously. 10 */ 11 12 /* 13 IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in 14 consideration of your agreement to the following terms, and your use, installation, 15 modification or redistribution of this Apple software constitutes acceptance of these 16 terms. If you do not agree with these terms, please do not use, install, modify or 17 redistribute this Apple software. 18 19 In consideration of your agreement to abide by the following terms, and subject to these 20 terms, Apple grants you a personal, non-exclusive license, under AppleÕs copyrights in 21 this original Apple software (the "Apple Software"), to use, reproduce, modify and 22 redistribute the Apple Software, with or without modifications, in source and/or binary 23 forms; provided that if you redistribute the Apple Software in its entirety and without 24 modifications, you must retain this notice and the following text and disclaimers in all 25 such redistributions of the Apple Software. Neither the name, trademarks, service marks 26 or logos of Apple Computer, Inc. may be used to endorse or promote products derived from 27 the Apple Software without specific prior written permission from Apple. Except as expressly 28 stated in this notice, no other rights or licenses, express or implied, are granted by Apple 29 herein, including but not limited to any patent rights that may be infringed by your 30 derivative works or by other works in which the Apple Software may be incorporated. 31 32 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, 33 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, 34 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS 35 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 36 37 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 38 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 39 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 40 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND 41 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR 42 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 43 */ 44 45 #import "ImageAndTextCell.h" 46 47 @implementation ImageAndTextCell 48 49 - (void)dealloc { 50 [image release]; 51 image = nil; 52 [super dealloc]; 53 } 54 55 - copyWithZone:(NSZone *)zone { 56 ImageAndTextCell *cell = (ImageAndTextCell *)[super copyWithZone:zone]; 57 cell->image = [image retain]; 58 return cell; 59 } 60 61 - (void)setImage:(NSImage *)anImage { 62 if (anImage != image) { 63 [image release]; 64 image = [anImage retain]; 65 } 66 } 67 68 - (NSImage *)image { 69 return image; 70 } 71 72 - (NSRect)imageFrameForCellFrame:(NSRect)cellFrame { 73 if (image != nil) { 74 NSRect imageFrame; 75 imageFrame.size = [image size]; 76 imageFrame.origin = cellFrame.origin; 77 imageFrame.origin.x += 3; 78 imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2); 79 return imageFrame; 80 } 81 else 82 return NSZeroRect; 83 } 84 85 - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent { 86 NSRect textFrame, imageFrame; 87 NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge); 88 [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent]; 89 } 90 91 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5 92 typedef int NSInteger; 93 #endif 94 - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength { 95 NSRect textFrame, imageFrame; 96 NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge); 97 [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength]; 98 } 99 100 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 101 if (image != nil) { 102 NSSize imageSize; 103 NSRect imageFrame; 104 105 imageSize = [image size]; 106 NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge); 107 if ([self drawsBackground]) { 108 [[self backgroundColor] set]; 109 NSRectFill(imageFrame); 110 } 111 imageFrame.origin.x += 3; 112 imageFrame.size = imageSize; 113 114 if ([controlView isFlipped]) 115 imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2); 116 else 117 imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2); 118 119 [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver]; 120 } 121 [super drawWithFrame:cellFrame inView:controlView]; 122 } 123 124 - (NSSize)cellSize { 125 NSSize cellSize = [super cellSize]; 126 cellSize.width += (image ? [image size].width : 0) + 3; 127 return cellSize; 128 } 129 130 @end