/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or
 *	implied, as to its fitness for any particular use.
 *
 *
 *	EOModelBrowserCell
 *
 *	Inherits From:		NXBrowserCell
 *
 *	Conforms To:		None
 *
 *	Declared In:		EOModelBrowserCell.h
 *
 *------------------------------------------------------------------------*/

#import "EOModelBrowserCell.h"


#define IMAGEMARGIN		4.0


@implementation EOModelBrowserCell

/*--------------------------------------------------------------------------
 *	Initialization
 *------------------------------------------------------------------------*/

- initTextCell: (const char *) aString
{
	[super initTextCell:aString];
	[self setTextColor:NX_COLORBLACK];
	isRelationship = NO;
	isToManyRelationship = NO;
	return self;
}



/*--------------------------------------------------------------------------
 *	Drawing
 *------------------------------------------------------------------------*/

- drawInside: (const NXRect *) cellFrame
	inView: controlView;
{
    /*
	 * every CustomCell needs these
	 */
	static id
		arrowImage = nil,
		doubleArrowImage = nil,
		sharedTextCell = nil;
	id
		theImage = nil;
	NXRect
		rect = *cellFrame;
	NXPoint
		imageOrigin;
	NXSize
		imageSize;

	if (arrowImage == nil)
		{
		char
			myPath[MAXPATHLEN+1];
			
		[[NXBundle bundleForClass:[self class]] getPath:myPath forResource:"arrow" ofType:"tiff"];
		arrowImage = [[NXImage alloc] initFromFile:myPath];
		}
		
	if (doubleArrowImage == nil)
		{
		char
			myPath[MAXPATHLEN+1];
			
		[[NXBundle bundleForClass:[self class]] getPath:myPath forResource:"double_arrow" ofType:"tiff"];
		doubleArrowImage = [[NXImage alloc] initFromFile:myPath];
		}

	/*
	 * erase the cell
	 */
	PSsetgray ((cFlags1.state || cFlags1.highlighted) ? NX_WHITE : NX_LTGRAY);
	NXRectFill (cellFrame);

	/*
	 * draw the right image
	 */
	if (([self isLeaf] == NO) || isRelationship)
		{
		theImage = arrowImage;

		if (isToManyRelationship)
			{
			theImage = doubleArrowImage;
			}
		
		[theImage getSize:&imageSize];
		imageOrigin.x = NX_X (cellFrame) + NX_WIDTH (cellFrame) - IMAGEMARGIN - imageSize.width;
		imageOrigin.y = NX_Y (cellFrame) + NX_HEIGHT (cellFrame) - (NX_HEIGHT(cellFrame) - imageSize.height) / 2.0;
		[theImage composite:NX_SOVER toPoint:&imageOrigin];
		NX_WIDTH (&rect) -= (imageSize.width + IMAGEMARGIN * 2.0 - NX_X (&rect));
		}
	
	if (sharedTextCell == nil)
		{
		sharedTextCell = [[Cell alloc] init];
		[sharedTextCell setWrap:NO];
		}
	
	[sharedTextCell setFont:[self font]];
	[sharedTextCell setStringValue:[self stringValue]];
	[sharedTextCell setEnabled:[self isEnabled]];
	[sharedTextCell drawInside:&rect inView:controlView];
    
	/*
	 * all drawing from now on will be in dark gray
	 */
	PSsetgray (NX_DKGRAY);
    
	/*
	 * draw the two dark gray lines above and below the cell
	 */
	if (cFlags1.state || cFlags1.highlighted)
		{
		NXRect
			rectArray[2];
		
		/*
		 * draw 1-pixel tall rectangles instead of lines (this is faster than
		 * PSmoveto(); PSlineto()).
		 */
		NXSetRect (&(rectArray[0]), NX_X (cellFrame), NX_Y (cellFrame), NX_WIDTH (cellFrame), 1.0);
		NXSetRect (&(rectArray[1]), NX_X (cellFrame), NX_MAXY (cellFrame) - 1.0, NX_WIDTH(cellFrame), 1.0);
		
		/*
		 * using NXRectFillList is faster than separate calls to NXRectFill
		 */
		NXRectFillList (rectArray, 2);
		}

	return self;
}


- highlight: (const NXRect *) cellFrame
	inView: controlView
	lit: (BOOL) flag
{
	if (cFlags1.highlighted != flag)
		{
		cFlags1.highlighted = flag;
		[self drawInside:cellFrame inView:controlView];
		}
	
	return self;
}



/*--------------------------------------------------------------------------
 *	Accessors
 *------------------------------------------------------------------------*/

- (NXColor) textColor
{
	return textColor;
}


- setTextColor: (NXColor) aColor
{
	textColor = aColor;
	return self;
}


- setTag: (int) theTag
{
	tag = theTag;
	return self;
}


- (int) tag
{
	return tag;
}



/*--------------------------------------------------------------------------
 *	Setting Relationship Degree
 *------------------------------------------------------------------------*/

- setIsRelationship: (BOOL) flag
{
	isRelationship = flag;
	return self;
}


- setIsToManyRelationship: (BOOL) flag
{
	isToManyRelationship = flag;
	return self;
}



/*--------------------------------------------------------------------------
 *	Setting Text Attributes
 *------------------------------------------------------------------------*/

- setTextAttributes: aTextObject
{
	//  This method is called during every drawInside: inView call.
	//  It asks for the current Text object.  This determines how the 
	//  Cell's text will be displayed.  Subclasses of Cell override 
	//  this method to get unique characteristics. 
	
	[super setTextAttributes:aTextObject];
	[aTextObject setTextColor:textColor];
	return aTextObject;
}	

@end
