#import "TabSelectionCell.h"

#define	TAB_IMAGE_WIDTH		14.0
#define	TAB_IMAGE_HEIGHT	17.0
#define	HALF_TAB_WIDTH		7.0

@implementation TabSelectionCell

/* Methods overridden from superclass */
- initTextCell:(const char *)aString
{
	/* Do the superclass' initTextCell: method */
	[super initTextCell:aString];
    
	/* Finish the initializing process */
	[self finishInitializing];

	return self;
}

- awake
{
	/* Do the superclass' awake method */
	[super awake];
	
	/* Finish the initializing process */
	[self finishInitializing];

	return self;
}

- setFont:fontObj
{
	/* Do the superclass' init method */
	[super setFont:fontObj];

	/* Grab the font information for the contents of the cell */
	NXTextFontInfo(fontObj,&ascender,&descender,&lineHeight);
    
	return self;
}    

- drawInside:(const NXRect *)cellFrame inView:controlView
{
	NXPoint	leftImageOrigin,rightImageOrigin;
	NXRect	textFrame,lowerBezelFrame;
	id		controlCellList = [controlView cellList];
	int		ourIndex = [controlCellList indexOf:self];

	/* If there are no contents */
	if (!contents)
	{
		/* Exit here! */
		return self;
	}

	/* Set the drawing to be totally opaque */
	PSsetalpha(1.0);

	/* Set up the lowerBezelFrame that will be used for drawing the bezel at
		our bottom */
	NXSetRect(&lowerBezelFrame,
				cellFrame->origin.x + HALF_TAB_WIDTH,
				NX_MAXY(cellFrame) - 1.0,
				cellFrame->size.width,
				1.0);

	/* Set textFrame to cellFrame */
	textFrame = *cellFrame;

	/* Set up the leftImageOrigin */
	leftImageOrigin.x = NX_X(cellFrame);
	leftImageOrigin.y = NX_MAXY(cellFrame);

	/* If ourIndex is 0 (we are the first cell) */
	if (ourIndex == 0)
	{
		/* If we are selected */
		if ([self isSelected])
		{
			/* Composite tabBeginSelectedImage to leftImageOrigin */
			[tabBeginSelectedImage composite:NX_SOVER
											toPoint:&leftImageOrigin];
		}
		else
		{
			/* Otherwise, composite tabBeginImage to leftImageOrigin */
			[tabBeginImage composite:NX_SOVER toPoint:&leftImageOrigin];
			
			/* Subtract a HALF_TAB_WIDTH from lowerBezelFrame's x */
			lowerBezelFrame.origin.x -= HALF_TAB_WIDTH;
		}

		/* Add a TAB_IMAGE_WIDTH to textFrame's x */
		textFrame.origin.x += TAB_IMAGE_WIDTH;

		/* Subtract a TAB_IMAGE_WIDTH plus HALF_TAB_WIDTH minus 1.0 from
			textFrame's width */
		textFrame.size.width -= TAB_IMAGE_WIDTH + HALF_TAB_WIDTH - 1.0;		
	}
	else
	{
		/* Subtract a HALF_TAB_WIDTH from leftImageOrigin's x */
		leftImageOrigin.x -= HALF_TAB_WIDTH;
		
		/* If the cell to left of us is selected */
		if ([[controlCellList objectAt:ourIndex-1] isSelected])
		{
			/* Composite tabBeginImage to leftImageOrigin */
			[tabBeginImage composite:NX_SOVER toPoint:&leftImageOrigin];

			/* Composite tabEndSelectedImage to leftImageOrigin */
			[tabEndSelectedImage composite:NX_SOVER toPoint:&leftImageOrigin];
		}
		else if ([self isSelected])
		{
			/* Otherwise, if we are selected */

			/* Composite tabEndImage to leftImageOrigin */
			[tabEndImage composite:NX_SOVER toPoint:&leftImageOrigin];

			/* Composite tabBeginSelectedImage to leftImageOrigin */
			[tabBeginSelectedImage composite:NX_SOVER
											toPoint:&leftImageOrigin];
		}
		else
		{
			/* Composite tabBeginImage to leftImageOrigin */
			[tabBeginImage composite:NX_SOVER toPoint:&leftImageOrigin];

			/* Composite tabEndImage to leftImageOrigin */
			[tabEndImage composite:NX_SOVER toPoint:&leftImageOrigin];

			/* Subtract a HALF_TAB_WIDTH from lowerBezelFrame's x */
			lowerBezelFrame.origin.x -= TAB_IMAGE_WIDTH;

			/* Add 1.0 to lowerBezelFrame's width */
			lowerBezelFrame.size.width += 1.0;
		}

		/* Add a HALF_TAB_WIDTH to textFrame's x */
		textFrame.origin.x += HALF_TAB_WIDTH;

		/* Subtract a TAB_IMAGE_WIDTH minus 1.0 from textFrame's width */
		textFrame.size.width -= TAB_IMAGE_WIDTH - 1.0;		
	}

	/* If ourIndex is less than controlCellList's count minus 1 (we are not
		the last cell) */
	if (ourIndex < [controlCellList count]-1)
	{
		/* Draw the cell to the right of us */
		[controlView drawCell:[controlCellList objectAt:ourIndex+1]];		
	}
	else
	{
		/* Set up the rightImageOrigin */
		rightImageOrigin.x = NX_X(cellFrame) + NX_WIDTH(cellFrame)
															- TAB_IMAGE_WIDTH;
		rightImageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame);

		/* If we are selected */
		if ([self isSelected])
		{
			/* Composite tabEndSelectedImage to rightImageOrigin */
			[tabEndSelectedImage composite:NX_SOVER
											toPoint:&rightImageOrigin];
		}
		else
		{
			/* Composite tabEndImage to rightImageOrigin */
			[tabEndImage composite:NX_SOVER toPoint:&rightImageOrigin];
		}

		/* Subtract a TAB_IMAGE_WIDTH from textFrame's width */
		textFrame.size.width -= HALF_TAB_WIDTH;		
		
		/* Add a HALF_TAB_WIDTH to lowerBezelFrame's width */
		lowerBezelFrame.size.width += HALF_TAB_WIDTH;
	}

	/* Erase the cell */
	
	/* If we are selected */
	if ([self isSelected])
	{
		/* Set our drawing color to NX_LTGRAY */		
		PSsetgray(NX_LTGRAY);
	}
	else
	{
		PSsetgray(.5);
	}
	
	/* Fill in textFrame */
	NXRectFill(&textFrame);

	/* Now draw the upper bezel */
	
	/* Set our drawing color to NX_LTGRAY */		
	PSsetgray(NX_LTGRAY);
	
	/* Set textFrame's height to 1.0 */
	textFrame.size.height = 1.0;

	/* Fill in textFrame */
	NXRectFill(&textFrame);

	/* Now draw the text */

	/* Set our drawing color to NX_BLACK */		
	PSsetgray(NX_BLACK);
	
	/* Draw the cell's contents */
	PSmoveto(NX_X(&textFrame),NX_Y(&textFrame)+lineHeight-descender+2.0);
	PSshow(contents);

	/* If we are not selected */
	if (![self isSelected])
	{
		/* Draw the lower bezel */

		/* Set our drawing color to NX_WHITE */		
		PSsetgray(NX_WHITE);

		/* Fill in lowerBezelFrame */
		NXRectFill(&lowerBezelFrame);
	}

	return self;
}

/* Other instance methods */
- finishInitializing
{
	/* Get the "tabBegin" image */
	if (!tabBeginImage)
	{
		tabBeginImage = [NXImage findImageNamed:"TabBegin"];
	}

	/* Get the "tabEnd" image */
	if (!tabEndImage)
	{
		tabEndImage = [NXImage findImageNamed:"TabEnd"];
	}

	/* Get the "tabBeginSelected" image */
	if (!tabBeginSelectedImage)
	{
		tabBeginSelectedImage = [NXImage findImageNamed:"TabBeginSelected"];
	}

	/* Get the "tabEndSelected" image */
	if (!tabEndSelectedImage)
	{
		tabEndSelectedImage = [NXImage findImageNamed:"TabEndSelected"];
	}

	/* All text displayed will be in Helvetica, 12 point */
	[self setFont:[Font newFont:"Helvetica" size:12.0]];

	return self;
}

- (BOOL)isSelected
{
	/* Return YES if either one of these flags are on */
	return (cFlags1.state || cFlags1.highlighted);
}

@end