ImageAndTextCell.m
author Jens Alfke <jens@mooseyard.com>
Sun May 03 10:13:31 2009 -0700 (2009-05-03)
changeset 27 256370e8935a
permissions -rw-r--r--
* MYTask: Added -commandLine, overhauled logging.
* MYDirectoryWatcher: Overhauled logging.
* MYErrorUtils: Disabled use of Security API (so it'll build without linking against Security.framework.)
     1 /*
     2     ImageAndTextCell.m
     3     Copyright (c) 2001-2006, Apple Computer, Inc., all rights reserved.
     4     Author: Chuck Pisula
     5 
     6     Milestones:
     7     * 03-01-2001: Initial creation by Chuck Pisula
     8     * 11-04-2005: Added hitTestForEvent:inRect:ofView: for better NSOutlineView support by Corbin Dunn
     9 
    10     Subclass of NSTextFieldCell which can display text and an image simultaneously.
    11 */
    12 
    13 /*
    14  IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
    15  consideration of your agreement to the following terms, and your use, installation, 
    16  modification or redistribution of this Apple software constitutes acceptance of these 
    17  terms.  If you do not agree with these terms, please do not use, install, modify or 
    18  redistribute this Apple software.
    19  
    20  In consideration of your agreement to abide by the following terms, and subject to these 
    21  terms, Apple grants you a personal, non-exclusive license, under AppleÕs copyrights in 
    22  this original Apple software (the "Apple Software"), to use, reproduce, modify and 
    23  redistribute the Apple Software, with or without modifications, in source and/or binary 
    24  forms; provided that if you redistribute the Apple Software in its entirety and without 
    25  modifications, you must retain this notice and the following text and disclaimers in all 
    26  such redistributions of the Apple Software.  Neither the name, trademarks, service marks 
    27  or logos of Apple Computer, Inc. may be used to endorse or promote products derived from 
    28  the Apple Software without specific prior written permission from Apple. Except as expressly
    29  stated in this notice, no other rights or licenses, express or implied, are granted by Apple
    30  herein, including but not limited to any patent rights that may be infringed by your 
    31  derivative works or by other works in which the Apple Software may be incorporated.
    32  
    33  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES, 
    34  EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, 
    35  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS 
    36  USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
    37  
    38  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 
    39  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
    40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
    41  REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND 
    42  WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR 
    43  OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    44 */
    45 
    46 #import "ImageAndTextCell.h"
    47 #import <AppKit/NSCell.h>
    48 
    49 @implementation ImageAndTextCell
    50 
    51 - (id)init {
    52     self = [super init];
    53     if( self ) {
    54         [self setLineBreakMode:NSLineBreakByTruncatingTail];
    55         [self setSelectable:YES];
    56     }
    57     return self;
    58 }
    59 
    60 - (void)dealloc {
    61     [image release];
    62     [super dealloc];
    63 }
    64 
    65 - (id)copyWithZone:(NSZone *)zone {
    66     ImageAndTextCell *cell = (ImageAndTextCell *)[super copyWithZone:zone];
    67     // The image ivar will be directly copied; we need to retain or copy it.
    68     cell->image = [image retain];
    69     return cell;
    70 }
    71 
    72 - (void)setImage:(NSImage *)anImage {
    73     if (anImage != image) {
    74         [image release];
    75         image = [anImage retain];
    76     }
    77 }
    78 
    79 - (NSImage *)image {
    80     return image;
    81 }
    82 
    83 - (NSRect)imageRectForBounds:(NSRect)cellFrame {
    84     NSRect result;
    85     if (image != nil) {
    86         result.size = [image size];
    87         result.origin = cellFrame.origin;
    88         result.origin.x += 3;
    89         result.origin.y += ceil((cellFrame.size.height - result.size.height) / 2);
    90     } else {
    91         result = NSZeroRect;
    92     }
    93     return result;
    94 }
    95 
    96 // We could manually implement expansionFrameWithFrame:inView: and drawWithExpansionFrame:inView: or just properly implement titleRectForBounds to get expansion tooltips to automatically work for us
    97 - (NSRect)titleRectForBounds:(NSRect)cellFrame {
    98     NSRect result;
    99     if (image != nil) {
   100         CGFloat imageWidth = [image size].width;
   101         result = cellFrame;
   102         result.origin.x += (3 + imageWidth);
   103         result.size.width -= (3 + imageWidth);
   104     } else {
   105         result = NSZeroRect;
   106     }
   107     return result;
   108 }
   109 
   110 
   111 - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
   112     NSRect textFrame, imageFrame;
   113     NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge);
   114     [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent];
   115 }
   116 
   117 - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
   118     NSRect textFrame, imageFrame;
   119     NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge);
   120     [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
   121 }
   122 
   123 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
   124     if (image != nil) {
   125         NSRect	imageFrame;
   126         NSSize imageSize = [image size];
   127         NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
   128         if ([self drawsBackground]) {
   129             [[self backgroundColor] set];
   130             NSRectFill(imageFrame);
   131         }
   132         imageFrame.origin.x += 3;
   133         imageFrame.size = imageSize;
   134 
   135         if ([controlView isFlipped])
   136             imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
   137         else
   138             imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
   139 
   140         [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
   141     }
   142     [super drawWithFrame:cellFrame inView:controlView];
   143 }
   144 
   145 - (NSSize)cellSize {
   146     NSSize cellSize = [super cellSize];
   147     cellSize.width += (image ? [image size].width : 0) + 3;
   148     return cellSize;
   149 }
   150 
   151 - (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
   152     NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
   153     // If we have an image, we need to see if the user clicked on the image portion.
   154     if (image != nil) {
   155         // This code closely mimics drawWithFrame:inView:
   156         NSSize imageSize = [image size];
   157         NSRect imageFrame;
   158         NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
   159         
   160         imageFrame.origin.x += 3;
   161         imageFrame.size = imageSize;
   162         // If the point is in the image rect, then it is a content hit
   163         if (NSMouseInRect(point, imageFrame, [controlView isFlipped])) {
   164             // 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.
   165             // By returning the correct parts, we allow NSTableView to correctly begin an edit when the text portion is clicked on.
   166             return NSCellHitContentArea;
   167         }        
   168     }
   169     // 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.
   170     return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];    
   171 }
   172 
   173 
   174 @end
   175