// Copyright (C) 1995 Jon Kutemeier
// Use is governed by the MiscKit license

/******************************************************************************
 *      $Log: DateSelectionCell.m,v $
# Revision 1.1  1993/10/08  22:04:14  jkute
# Checkpoint
#
# Revision 1.1  1993/04/13  18:02:34  gpopick
# done
#
# Revision 1.1  1993/01/29  19:45:21  jkute
# *** empty log message ***
#
# Revision 1.1  1992/11/17  20:48:22  jkute
# Initial revision
#
******************************************************************************/
static char *ident = "$Id: DateSelectionCell.m,v 1.1 1993/10/08 22:04:14 jkute Exp $";
static int __hack(int x){if(x)return x;else return __hack((int)*ident);}
/*****************************************************************************/

#import <strings.h>

#import <dpsclient/psops.h>
#import <appkit/graphics.h>
#import <appkit/color.h>
#import <appkit/Font.h>
#import <appkit/Text.h>

#import "DateSelectionCell.h"

#define COLOR_DKBLUE	NXConvertRGBToColor(34.0/255.0, 68.0/255.0, 119.0/255.0)

@implementation DateSelectionCell

- init
{
    id retCode;

    retCode = [super init];

    newCellFont = [Font newFont:"Helvetica" size:10 style:0
		   matrix:NX_FLIPPEDMATRIX];
    [Font setUserFont:newCellFont];

    [self setTextColor:NX_COLORWHITE];
    [self setHighlightTextColor:NX_COLORBLACK];
    [self setBackgroundColor:COLOR_DKBLUE];
    [self setHighlightColor:NX_COLORWHITE];

    [self setDrawRightEdge:NO];
    [self setDrawEntireBottomEdge:YES];

    return retCode;
}

- setTextColor:(NXColor)aColor
{
    textColor = aColor;

    return self;
}

- (NXColor)textColor
{
    return textColor;
}

- setHighlightTextColor:(NXColor)aColor
{
    highlightTextColor = aColor;

    return self;
}

- (NXColor)highlightTextColor
{
    return highlightTextColor;
}

- setBackgroundColor:(NXColor)aColor
{
    backgroundColor = aColor;

    return self;
}

- (NXColor)backgroundColor
{
    return backgroundColor;
}

- setHighlightColor:(NXColor)aColor
{
    highlightColor = aColor;

    return self;
}

- (NXColor)highlightColor
{
    return highlightColor;
}

- setDrawRightEdge:(BOOL)yn
{
    drawRightEdge = yn;

    return self;
}

- (BOOL)drawRightEdge
{
    return drawRightEdge;
}

- setDrawEntireBottomEdge:(BOOL)yn
{
    drawEntireBottomEdge = yn;

    return self;
}

- (BOOL)drawEntireBottomEdge
{
    return drawEntireBottomEdge;
}

- drawInside:(const NXRect *)cellFrame inView:controlView
{
    float	yPos, 
		xPos,
		width;
    const char	*stringValue;
    NXRect 	rectArray[2];
    int		numOfRects;

    stringValue = [self stringValue];

    /* erase the cell */

    NXSetColor((cFlags1.state || cFlags1.highlighted) ? [self highlightColor]
	       : [self backgroundColor]);
    NXRectFill(cellFrame);
    
    if (stringValue && *stringValue)
    {
	/* set the font according to our drawing status */

	if (NXDrawingStatus != NX_DRAWING)
	  [newCellFont set];
	else
	  [[newCellFont screenFont] set];
    
	/* draw the text */

	NXSetColor([self textColor]);

	if ( (cFlags1.state || cFlags1.highlighted) )
	  NXSetColor([self highlightTextColor]);

	yPos = NX_Y(cellFrame) + 10.0;// + lineHeight - descender;
	xPos = NX_X(cellFrame) + ((NX_WIDTH(cellFrame) - 
				   [newCellFont getWidthOf:stringValue])/2.0);

	PSmoveto( xPos, yPos );

	PSshow( stringValue );
    }

    NXSetColor(NX_COLORBLACK);

    numOfRects = 1;

    if (![self drawEntireBottomEdge])
    {
	xPos = NX_X(cellFrame) + 1.0;
	width = NX_WIDTH(cellFrame) - 1.0;
    }
    else
    {
	xPos = NX_X(cellFrame);
	width = NX_WIDTH(cellFrame);
    }

    NXSetRect(&(rectArray[0]), xPos, NX_MAXY(cellFrame), width, 1.0);

    if ([self drawRightEdge])
    {
	NXSetRect(&(rectArray[1]), NX_MAXX(cellFrame), 
		  NX_Y(cellFrame) + 1.0, 1.0, NX_HEIGHT(cellFrame));
	numOfRects++;
    }

    NXRectFillList(rectArray, numOfRects);

    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;
}

- read:(NXTypedStream *)aStream
{
    float	red,
		green,
		blue;

    [super read:aStream];

    NXReadTypes(aStream, "fff", &red, &green, &blue);
    textColor = NXConvertRGBToColor(red, green, blue);

    NXReadTypes(aStream, "fff", &red, &green, &blue);
    highlightTextColor = NXConvertRGBToColor(red, green, blue);

    NXReadTypes(aStream, "fff", &red, &green, &blue);
    backgroundColor = NXConvertRGBToColor(red, green, blue);

    NXReadTypes(aStream, "fff", &red, &green, &blue);
    highlightColor = NXConvertRGBToColor(red, green, blue);

    NXReadTypes(aStream, "@cc", &newCellFont, &drawEntireBottomEdge, 
		&drawRightEdge);

    return self;
}

- write:(NXTypedStream *)aStream
{
    float	red,
		green,
		blue;

    [super write:aStream];

    NXConvertColorToRGBA(textColor, &red, &green, &blue, 0);
    NXWriteTypes(aStream, "fff", &red, &green, &blue);

    NXConvertColorToRGBA(highlightTextColor, &red, &green, &blue, 0);
    NXWriteTypes(aStream, "fff", &red, &green, &blue);

    NXConvertColorToRGBA(backgroundColor, &red, &green, &blue, 0);
    NXWriteTypes(aStream, "fff", &red, &green, &blue);

    NXConvertColorToRGBA(highlightColor, &red, &green, &blue, 0);
    NXWriteTypes(aStream, "fff", &red, &green, &blue);

    NXWriteTypes(aStream, "@cc", &newCellFont, &drawEntireBottomEdge,
		 &drawRightEdge);

    return self;
}

@end

