
/*
	these methods are adopted from Bill Edney,
	slightly modified by Heinrich Giesen 1996
*/

//#import "DarkGrayView.h"
#import "TabMatrix.h"

@implementation DarkGrayView

- drawSelf:(const NXRect *)rects :(int)count
{
	PSsetgray(NX_DKGRAY);	/* Set our drawing color to NX_DKGRAY */
	NXRectFill(&bounds);	/* Fill in our bounds */
	
	return self;
}

@end


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

static	id	tabBeginImage = nil;			/* The "tabBegin" image */
static	id	tabEndImage = nil;				/* The "tabEnd" image */
static	id	tabBeginSelectedImage = nil;	/* The "tabBeginSelected" image */
static	id	tabEndSelectedImage = nil;		/* The "tabEndSelected" image */

@implementation TabSelectionCell

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

	return self;
}


- awake
{

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

	return self;
}


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

	/* 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];
	NXCoord	delta;

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

	delta = NX_HEIGHT(cellFrame) - TAB_IMAGE_HEIGHT;

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

	/*	Set up the lowerBezelFrame that will be used for drawing the bezel at
		our bottom */
	NXSetRect(&lowerBezelFrame,
				NX_X(cellFrame) + HALF_TAB_WIDTH,
				NX_MAXY(cellFrame) - 1.0 - delta,
				NX_WIDTH(cellFrame),
				1.0);

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

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

	if (ourIndex == 0) {	/* If ourIndex is 0 (we are the first cell) */
		if ([self isSelected]) {	/* If we are selected */
			/* 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 {	//€we are the last cell
		/* Set up the rightImageOrigin */
		rightImageOrigin.x = NX_X(cellFrame) + NX_WIDTH(cellFrame)
								 - TAB_IMAGE_WIDTH ;
		rightImageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame) - delta;

		/* 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 */
	
	PSsetgray( [self isSelected] ? NX_LTGRAY:0.5 );	// drawing color

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

	/* Now draw the upper bezel */

	PSsetgray(NX_LTGRAY);			/* Set our drawing color to NX_LTGRAY */
	textFrame.size.height = 1.0;	/* Set textFrame's height to 1.0 */
	NXRectFill(&textFrame);			/* Fill in textFrame */

	/* Now draw the text */
	PSsetgray(NX_BLACK);	/* Set our drawing color to NX_BLACK */
	
	/* Draw the cell's contents */
	PSmoveto(NX_X(&textFrame)+3.0,NX_Y(&textFrame)+lineHeight-descender+1.0);
	[[self font] set];	// else PS error: font missing
	PSshow(contents);

	if (![self isSelected]) {	/* If we are not selected */
		/* Draw the lower bezel */
		PSsetgray(NX_WHITE);			/* Set our drawing color to NX_WHITE */
		NXRectFill(&lowerBezelFrame);	/* Fill in lowerBezelFrame */
	}

	return self;
}

/* Other instance methods */
- finishInitializing
{
	if (tabBeginImage) return self;	// images are loaded

	tabBeginImage = [NXImage findImageNamed:"Images/TabBegin"];
	tabEndImage = [NXImage findImageNamed:"Images/TabEnd"];
	tabBeginSelectedImage = [NXImage findImageNamed:"Images/TabBeginSelected"];
	tabEndSelectedImage = [NXImage findImageNamed:"Images/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
