
/* Generated by Interface Builder */
/* the current font size calculation is totally wrong and should be changed */
/* it should actually put a string into a certain font size, see how big it is compared to the space and
/* rescale the font to fit the space */


#import "BarGauge.h"
#import "strings.h"

#define HEIGHT_LABLES	0.30
#define HEIGHT_NOLABEL	0.10
#define WIDTH_LABLES	0.30
#define WIDTH_NOLABEL	0.10
#define SPACING		3.0	/* spacing between labels and drawing */
#define	TICBAR		4.0	/* thickness of a tic marker */

@implementation BarGauge

+ newFrame: (NXRect *) r
{
	self = [super newFrame:r];
	GaugeFlags.borderType = NX_BEZEL;
	[self refreshBackground:self];
	return self;
}

- refreshBackground:sender;
/* for some reason the vertical bars (height greater than width work as expect)
/* but the horizonatal bars seem to have a reverse coordinate system 
/* sometimes (always?) */
/* could it be that locking focus on a bitmap causes view to have inveted 
/* coordinate systems?  Maybe! */
{
	float	LabelHOff,LabelWOff;
	float	TicSpacing;
	int	i;
	NXRect	ticRect;	/* to do fancy tics (which are really boxes) */
	char	labelStr[20];	/* The could never be this big */
				/* (famous last words) */
	int	probTicSize;	/*prob. max tic str size based on labelTicPat*/
	id	ticFont;
	
	sprintf(labelStr,GaugeDat.labelTicPat,GAUGEMAX);
	probTicSize = strlen(labelStr);
	if (!GaugeDat.labelTicPat || !*GaugeDat.labelTicPat)
		GaugeDat.labelTicPat = "%1.1f";
	FaceRect = bounds;
	FaceRect.origin.x += SPACING;
	FaceRect.origin.y += SPACING;
	FaceRect.size.height -= 2*SPACING;
	FaceRect.size.width  -= 2*SPACING;
	if (GaugeFlags.labelTicsOn){
		LabelHOff = (HEIGHT_LABLES * NX_HEIGHT(&bounds)) + SPACING;
		LabelWOff = (WIDTH_LABLES * NX_WIDTH(&bounds)) + SPACING;
	} else {
		LabelHOff = (HEIGHT_NOLABEL * NX_HEIGHT(&bounds));
		LabelWOff = (WIDTH_NOLABEL * NX_WIDTH(&bounds));
	}
	[[GaugeDat.bm contentView] lockFocus];
	PSsetlinewidth(0.0);
	PSsetgray(GaugeDat.backGray);
	NXRectFill(&bounds);	/* do this in case no border */
	PSsetgray(GaugeDat.faceGray);
	NXRectFill(&FaceRect);	/* don't know if i should do this but */
	FancyBack(&bounds);
	if (bounds.size.width > bounds.size.height){
		/* do horizontal bar */
		if (GaugeFlags.ticsOn){
			FaceRect.origin.y += LabelHOff;
			FaceRect.size.height -= LabelHOff;
		}
		TicSpacing = (NX_WIDTH(&FaceRect)-TICBAR) / (GaugeDat.numMajor-1);
		ticFont = [Font newFont:GaugeDat.ticFontName
					size:(1.5*TicSpacing/probTicSize) /* 1.5 times is total kludge for now */
					style:0
					matrix:NX_IDENTITYMATRIX];
		[ticFont set];
		for (i = 0 ; i < GaugeDat.numMajor; i ++){
			NX_X(&ticRect) = NX_X(&FaceRect)+(i*TicSpacing);
			NX_WIDTH(&ticRect) = TICBAR;
			if (GaugeFlags.labelTicsOn)
				NX_Y(&ticRect)=NX_Y(&FaceRect) - SPACING;
			else	NX_Y(&ticRect)=NX_Y(&FaceRect) - LabelHOff;
			NX_HEIGHT(&ticRect)= NX_MAXY(&FaceRect)-NX_Y(&ticRect);
			if (GaugeFlags.labelTicsOn){
				PSsetgray(GaugeDat.textGray);
			   	sprintf(labelStr,GaugeDat.labelTicPat,
			   	  (i)*GaugeDat.ticSize + GaugeDat.startValue);
				PSmoveto(NX_X(&ticRect)- 
				  ((float)i/GaugeDat.numMajor)*
				 ([ticFont getWidthOf:labelStr] -  NX_WIDTH(&ticRect)),
				  NX_Y(&bounds)+SPACING);
				PSshow(labelStr);
			}
			PSsetgray(GaugeDat.ticGray);
			if (GaugeFlags.borderType == NX_NOBORDER)
				NXFrameRect(&ticRect);
			else {FancyRect(&ticRect)}
		}
		if ((!GaugeFlags.labelTicsOn) && GaugeFlags.ticsOn)
			FaceRect.origin.y -= (LabelHOff / 2.0);
	}else{
		/* do vertical bar */
		if (GaugeFlags.ticsOn){
			FaceRect.origin.x += LabelWOff;
			FaceRect.size.width -= LabelWOff;
		}
		TicSpacing = (NX_HEIGHT(&FaceRect)-TICBAR)/ (GaugeDat.numMajor-1);
		ticFont = [Font newFont:GaugeDat.ticFontName
					size:(1.5* TicSpacing/probTicSize) /* 1.5* is total kludge for now */
					style:0
					matrix:NX_IDENTITYMATRIX];
		[ticFont set];
		for (i=0; i < GaugeDat.numMajor; i++){
			NX_Y(&ticRect) = NX_Y(&FaceRect) +(i*TicSpacing);
			NX_HEIGHT(&ticRect) = TICBAR;
			if (GaugeFlags.labelTicsOn)
				NX_X(&ticRect) = NX_X(&FaceRect)-SPACING;
			else	NX_X(&ticRect) = NX_X(&FaceRect)-LabelWOff;
			NX_WIDTH(&ticRect)=NX_MAXX(&FaceRect)-NX_X(&ticRect);
			if (GaugeFlags.labelTicsOn){
				PSsetgray(GaugeDat.textGray);
			   	sprintf(labelStr,GaugeDat.labelTicPat,
					  i*GaugeDat.ticSize + GaugeDat.startValue);
				PSmoveto(NX_X(&bounds)+SPACING,NX_Y(&ticRect)-
					(((float)i/GaugeDat.numMajor)*
					([ticFont pointSize]- NX_HEIGHT(&ticRect))));
				PSshow(labelStr);
			}
			PSsetgray(GaugeDat.ticGray);
			if (GaugeFlags.borderType == NX_NOBORDER)
				NXFrameRect(&ticRect);
			else {FancyRect(&ticRect)}
		}
		if (!GaugeFlags.labelTicsOn && GaugeFlags.ticsOn)
			FaceRect.origin.x -= ( LabelWOff / 2.0);
	}
	if ((GaugeFlags.borderType == NX_NOBORDER) 
			|| (GaugeFlags.borderType == NX_LINE)){
		PSsetlinewidth(SPACING);	/* didn't affect it? */
		NXFrameRect(&bounds);
	}
	PSstroke();
	[[GaugeDat.bm contentView] unlockFocus];
	[self display];
	return self;
}

-drawSelf:(NXRect *)drawRects :(int)rectCount
{
	NXRect	dataRect;
	
	dataRect = FaceRect;
	PScomposite(0.0,0.0,bounds.size.width,bounds.size.height,
		GaugeDat.bmgState,0.0, 0.0,NX_COPY);
	/* now draw the bar */
	PSsetgray(GaugeDat.ptGray);
	if (NX_WIDTH(&bounds) > NX_HEIGHT(&bounds)){
		/* do horizontal bar */
		dataRect.size.width = ((GaugeDat.value - GaugeDat.startValue) 
				/ GAUGESIZE) 
				* NX_WIDTH(&dataRect);
	}else{
		/* do vertical bar */
		dataRect.size.height = ((GaugeDat.value - GaugeDat.startValue)
				/ GAUGESIZE)
				* NX_HEIGHT(&dataRect);

	}
	if (GaugeFlags.borderType == NX_NOBORDER) {
		NXRectFill(&dataRect);
		if (GaugeFlags.overFlow) NXHighlightRect(&bounds);
		return self; /* exit quick */
	}
	PSsetgray(GaugeDat.ticGray);
	FancyRect(&dataRect);
	if (GaugeFlags.overFlow) NXHighlightRect(&bounds);
	NXPing();
	return self;
}

@end
