/*
	ImageAndTextCell.m
	Copyright (c) 2001-2002, Apple Computer, Inc., all rights reserved.
	Author: Chuck Pisula

	Milestones:
	Initially created 3/1/01

        Subclass of NSTextFieldCell which can display text and an image simultaneously.
*/

/*
 IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
 consideration of your agreement to the following terms, and your use, installation, 
 modification or redistribution of this Apple software constitutes acceptance of these 
 terms.  If you do not agree with these terms, please do not use, install, modify or 
 redistribute this Apple software.
 
 In consideration of your agreement to abide by the following terms, and subject to these 
 terms, Apple grants you a personal, non-exclusive license, under AppleÕs copyrights in 
 this original Apple software (the "Apple Software"), to use, reproduce, modify and 
 redistribute the Apple Software, with or without modifications, in source and/or binary 
 forms; provided that if you redistribute the Apple Software in its entirety and without 
 modifications, you must retain this notice and the following text and disclaimers in all 
 such redistributions of the Apple Software.  Neither the name, trademarks, service marks 
 or logos of Apple Computer, Inc. may be used to endorse or promote products derived from 
 the Apple Software without specific prior written permission from Apple. Except as expressly
 stated in this notice, no other rights or licenses, express or implied, are granted by Apple
 herein, including but not limited to any patent rights that may be infringed by your 
 derivative works or by other works in which the Apple Software may be incorporated.
 
 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES, 
 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, 
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS 
 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND 
 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR 
 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "ImageAndTextCell.h"

/*!
 * Displays an icon next to the text, similar to Finder's list view. Because
 * ImageAndTextCell is a subclass of NSTextFieldCell, it retains all of the
 * text formatting abilities of its superclass. Also supports setting an
 * alternate icon for when the cell is highlighted. This class was created
 * by Apple as part of the DragNDropOutlineView example, although it was
 * modified slightly by Belkadan Software for use in Dockyard Manager.
 *
 * @author Chuck Pisula
 */
@implementation ImageAndTextCell

- (void)dealloc {
    [image release];
    image = nil;
    [super dealloc];
}

- copyWithZone:(NSZone *)zone {
    ImageAndTextCell *cell = (ImageAndTextCell *)[super copyWithZone:zone];
    cell->image = [image retain];
    return cell;
}

/*!
 * Sets the cell's regular image to <code>anImage</code>. If <code>anImage</code>
 * is <code>nil</code>, clears the regular image.
 *
 * @see #image
 * @see #setAlternateImage:
 */
- (void)setImage:(NSImage *)anImage {
    if (anImage != image) {
        [image release];
        image = [anImage retain];
    }
}

/*!
 * Returns the cell's regular image. This image is displayed when the cell
 * is not highlighted, or always if there is no alternate image.
 *
 * @see #alternateImage
 * @see #currentImage
 * @see #setImage:
 */
- (NSImage *)image {
    return image;
}

/*!
 * Sets the cell's alternate image to <code>anImage</code>. If <code>anImage</code>
 * is <code>nil</code>, clears the alternate image.
 *
 * @see #alternateImage
 * @see #setImage:
 */
- (void)setAlternateImage:(NSImage *)anImage {
    if (anImage != altImage) {
        [altImage release];
        altImage = [anImage retain];
    }
}

/*!
 * Returns the cell's alternate image. This image is displayed when the cell
 * is highlighted (if it exists).
 *
 * @see #image
 * @see #currentImage
 * @see #setAlternateImage:
 */
- (NSImage *)alternateImage {
    return altImage;
}

/*!
 * Returns the image currently being displayed. If the cell is highlighted
 * and an alternate image has been set, the image returned is the alternate
 * image. Otherwise, it is the regular image.
 *
 * @see #image
 * @see #alternateImage
 */
- (NSImage *)currentImage {
	return ([self isHighlighted] && altImage) ? altImage : image;
}

- (NSRect)imageRectForBounds:(NSRect)cellFrame {
	NSImage *currentImage = [self currentImage];
    if (currentImage != nil) {
        NSRect imageFrame;
        imageFrame.size = [currentImage size];
        imageFrame.origin = cellFrame.origin;
        imageFrame.origin.x += 3;
        imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2) + 1;
        return imageFrame;
    }
    else
        return NSZeroRect;
}

- (NSRect)titleRectForBounds:(NSRect)cellFrame {
	NSImage *currentImage = [self currentImage];
    if (currentImage != nil) {
        NSRect titleFrame;
        titleFrame.size.height = cellFrame.size.height;
		titleFrame.size.width = cellFrame.size.width - [currentImage size].width - 5;
        titleFrame.origin = cellFrame.origin;
        titleFrame.origin.x += 5 + [currentImage size].width;
 //       titleFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2) + 1;
        return titleFrame;
    }
    else
        return cellFrame;
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
    NSRect textFrame, imageFrame;
    NSDivideRect (aRect, &imageFrame, &textFrame, 6 + [[self currentImage] size].width, NSMinXEdge);
    [super editWithFrame:textFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength {
    NSRect textFrame, imageFrame;
    NSDivideRect (aRect, &imageFrame, &textFrame, 6 + [[self currentImage] size].width, NSMinXEdge);
    [super selectWithFrame:textFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	NSImage *currentImage = [self currentImage];
    if (currentImage != nil) {
        NSSize	imageSize;
        NSRect	imageFrame, titleFrame;
		
/*		id currentValue = [[self objectValue] retain];
		[self setObjectValue:nil];
		[super drawWithFrame:cellFrame inView:controlView];
		[self setObjectValue:currentValue];
		[currentValue release]; */

		imageSize = [currentImage size];
        NSDivideRect(cellFrame, &imageFrame, &titleFrame, 3 + imageSize.width, NSMinXEdge);
/*      if ([self drawsBackground]) {
            [[self backgroundColor] set];
            NSRectFill(imageFrame);
        } */
        imageFrame.origin.x += 3;
        imageFrame.size = imageSize;

        if ([controlView isFlipped])
            imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
        else
            imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);

        [currentImage compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
		[super drawInteriorWithFrame:titleFrame inView:controlView];
		
    } else
		[super drawInteriorWithFrame:cellFrame inView:controlView];
}

- (NSSize)cellSize {
    NSSize cellSize = [super cellSize];
	NSImage *currentImage = [self currentImage];
    cellSize.width += (currentImage ? [currentImage size].width : 0) + 10;
    return cellSize;
}

@end