//
//  ICeCoffEELabeledIconCell.m
//  ICeCoffEE
//
//  Created by Nicholas Riley on 6/8/07.
//  Copyright 2007 Nicholas Riley. All rights reserved.
//

#import "ICeCoffEELabeledIconCell.h"


@implementation ICeCoffEELabeledIconCell

const static float LEFT_PADDING = 3;
const static float PADDING = 5;
const static NSSize ICON_SIZE = {16, 16};

- (ICeCoffEELabeledIconCell *)justCopySuper;
{
    return [super copy];
}

+ (ICeCoffEELabeledIconCell *)copyFromTextFieldCell:(NSTextFieldCell *)cell;
{
    ((struct objc_object *)cell)->isa = self;
    ICeCoffEELabeledIconCell *copy = [(ICeCoffEELabeledIconCell *)cell justCopySuper];
    copy->iconRef = NULL;
    ((struct objc_object *)cell)->isa = [NSTextFieldCell class];
    return [copy autorelease];
}

- (void)dealloc;
{
    if (iconRef != NULL) {
	ReleaseIconRef(iconRef);
	iconRef = NULL;
    }
    [super dealloc];
}

- (id)copyWithZone:(NSZone *)zone;
{
    ICeCoffEELabeledIconCell *cell = [super copyWithZone: zone];
    cell->iconRef = iconRef;
    if (iconRef != NULL)
	AcquireIconRef(iconRef);
    return cell;
}

- (void)setIconRef:(IconRef)anIconRef;
{
    if (anIconRef == iconRef)
	return;
    if (iconRef != NULL)
	ReleaseIconRef(iconRef);
    if (anIconRef != NULL)
	AcquireIconRef(anIconRef);
    iconRef = anIconRef;
}

- (IconRef)iconRef;
{
    return iconRef;
}

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    return nil;
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent;
{
    NSRect textFrame, imageFrame;
    NSDivideRect(aRect, &imageFrame, &textFrame,
                 PADDING + ICON_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, PADDING + ICON_SIZE.width,
                 NSMinXEdge);
    [super selectWithFrame: textFrame inView: controlView editor:textObj delegate: anObject start: selStart length: selLength];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
    if (iconRef != NULL) {
        NSRect imageFrame;
	BOOL flipped = [controlView isFlipped];
	
        NSDivideRect(cellFrame, &imageFrame, &cellFrame, PADDING + ICON_SIZE.width, NSMinXEdge);
        if ([self drawsBackground]) {
            [[self backgroundColor] set];
            NSRectFill(imageFrame);
        }
        imageFrame.origin.x += LEFT_PADDING;
        imageFrame.size = ICON_SIZE;
	
        imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height + 2) / 2);

	NSRect imageRect = NSOffsetRect(imageFrame, -imageFrame.origin.x, -imageFrame.origin.y);
	CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
	CGContextTranslateCTM(context, imageFrame.origin.x, imageFrame.origin.y);
	if (flipped)
	    CGContextScaleCTM(context, 1, -1);
	PlotIconRefInContext(context, (CGRect *)&imageRect, kAlignNone, kTransformNone,
			     NULL /*inLabelColor*/, kPlotIconRefNormalFlags, iconRef);
	if (flipped)
	    CGContextScaleCTM(context, 1, -1);
	CGContextTranslateCTM(context, -imageFrame.origin.x, -imageFrame.origin.y);
    }
    [super drawWithFrame: cellFrame inView: controlView];
}

- (NSSize)cellSize;
{
    NSSize cellSize = [super cellSize];
    cellSize.width += (iconRef != NULL ? (ICON_SIZE.width + PADDING): 0);
    return cellSize;
}

@end
