/*  GadTools layout toolkit
**
**  Copyright © 1993-1995 by Olaf `Olsen' Barthel
**  Freely distributable.
*/

#include "gtlayout_global.h"

#ifdef DO_GAUGE_KIND
VOID __regargs
LTP_DrawTick(LayoutHandle *Handle,ObjectNode *Node,WORD Left,WORD Top)
{
	struct RastPort *rp = &Handle -> RPort;

	Left += Node -> Left;

	LTP_SetAPen(rp,Handle -> ShadowPen);
	Move(rp,Left,Top);
	Draw(rp,Left,Top + 1);

	LTP_SetAPen(rp,Handle -> ShinePen);
	Move(rp,Left + 1,Top);
	Draw(rp,Left + 1,Top + 1);
}

VOID __regargs
LTP_DrawGauge(LayoutHandle *handle,ObjectNode *node,LONG percent,BOOLEAN fullRefresh)
{
	struct RastPort *rp = &handle -> RPort;

	if(fullRefresh)
	{
		EraseRect(rp,node -> Left,node -> Top,node -> Left + node -> Width - 1,node -> Top + node -> Height - 1);

		if(node -> Special . Gauge . NoTicks)
		{
			DrawBevelBox(rp,node -> Left,node -> Top,node -> Width,node -> Height,
				GT_VisualInfo,	handle -> VisualInfo,
				GTBB_Recessed,	TRUE,
			TAG_DONE);
		}
		else
		{
			LONG top = node -> Top + node -> Height - (handle -> RPort . TxHeight + 2);

			DrawBevelBox(rp,node -> Left,node -> Top,node -> Width,node -> Height - (handle -> InterHeight + handle -> RPort . TxHeight + 2),
				GT_VisualInfo,	handle -> VisualInfo,
				GTBB_Recessed,	TRUE,
			TAG_DONE);

			LTP_SetPens(rp,handle -> TextPen,0,JAM1);

			LTP_PlaceText(handle,"0%",2,node -> Left,top + handle -> RPort . TxBaseline);
			LTP_PlaceText(handle,"100%",4,node -> Left + node -> Width - TextLength(rp,"100%",4),top + handle -> RPort . TxBaseline);

			if(node -> Width >= TextLength(rp,"0%50%100%",9) + 4 * handle -> GlyphWidth)
			{
				LONG i,left,total = node -> Width - 2;

				LTP_PlaceText(handle,"50%",3,node -> Left + (node -> Width - TextLength(rp,"50%",3)) / 2,top + handle -> RPort . TxBaseline);

				top -= handle -> InterHeight;

				for (i = 0 ; i < 5 ; i++)
				{
					left = (total * i) / 4;

					LTP_DrawTick(handle,node,left,top);
				}
			}
			else
			{
				LONG i,left,total = node -> Width - 2;

				top -= handle -> InterHeight;

				for (i = 0; i < 3; i++)
				{
					left = (total * i) / 2;

					LTP_DrawTick(handle,node,left,top);
				}
			}
		}
	}

	if(node -> Special . Gauge . InfoLength && node -> Special . Gauge . InfoText[0])
	{
		LONG			height	= node -> Height - (1 + 1);
		LONG			width	= node -> Width - (2 + 2);
		LONG			left	= 0;
		LONG			right	= (width * percent) / 100;
		struct TextExtent	Extent;
		LONG			len;

		if(!node -> Special . Gauge . NoTicks)
			height -= handle -> InterHeight + handle -> RPort . TxHeight + 2;

		if(right)
		{
			LTP_SetAPen(rp,handle -> DrawInfo -> dri_Pens[FILLPEN]);

			RectFill(rp,node -> Left + 2 + left,node -> Top + 1,node -> Left + 2 + right - 1,node -> Top + 1 + height - 1);
		}

		if(right < width)
		{
			left	= right;
			right	= width;

			LTP_SetAPen(rp,handle -> BackgroundPen);
			RectFill(rp,node -> Left + 2 + left,node -> Top + 1,node -> Left + 2 + right - 1,node -> Top + 1 + height - 1);
		}

		if(handle -> DrawInfo -> dri_Pens[FILLTEXTPEN] == handle -> DrawInfo -> dri_Pens[BACKGROUNDPEN])
		{
			if(handle -> DrawInfo -> dri_Pens[TEXTPEN] == handle -> DrawInfo -> dri_Pens[FILLPEN])
				LTP_SetPens(rp,handle -> DrawInfo -> dri_Pens[TEXTPEN],handle -> BackgroundPen,JAM1 | COMPLEMENT);
			else
				LTP_SetPens(rp,handle -> DrawInfo -> dri_Pens[TEXTPEN],handle -> BackgroundPen,JAM1);
		}
		else
		{
			if(handle -> DrawInfo -> dri_Pens[FILLTEXTPEN] == handle -> DrawInfo -> dri_Pens[FILLPEN])
				LTP_SetPens(rp,handle -> DrawInfo -> dri_Pens[FILLTEXTPEN],handle -> BackgroundPen,JAM1 | COMPLEMENT);
			else
				LTP_SetPens(rp,handle -> DrawInfo -> dri_Pens[FILLTEXTPEN],handle -> BackgroundPen,JAM1);
		}

		len = TextFit(rp,node -> Special . Gauge . InfoText,strlen(node -> Special . Gauge . InfoText),&Extent,NULL,1,width,32767);

		LTP_PlaceText(handle,node -> Special . Gauge . InfoText,len,node -> Left + 2 + (width - Extent . te_Width) / 2,node -> Top + 1 + (height - rp -> Font -> tf_YSize) / 2 + rp -> Font -> tf_Baseline);

		LTP_SetPens(rp,handle -> TextPen,handle->BackgroundPen,JAM1);
	}
	else
	{
		LONG height	= node -> Height - (1 + 1);
		LONG width	= node -> Width - (2 + 2);

		if(!node -> Special . Gauge . NoTicks)
			height -= handle -> InterHeight + handle -> RPort . TxHeight + 2;

		if(node -> Current < percent)
		{
			LONG left	= (width * node -> Current) / 100;
			LONG right	= (width * percent) / 100;

			if(right)
			{
				LTP_SetAPen(rp,handle -> DrawInfo -> dri_Pens[FILLPEN]);
				RectFill(rp,node -> Left + 2 + left,node -> Top + 1,node -> Left + 2 + right - 1,node -> Top + 1 + height - 1);
			}
		}
		else
		{
			if(node -> Current > percent)
			{
				LONG left	= (width * percent) / 100;
				LONG right	= (width * node -> Current) / 100;

				if(right)
				{
					LTP_SetAPen(rp,handle -> BackgroundPen);
					RectFill(rp,node -> Left + 2 + left,node -> Top + 1,node -> Left + 2 + right - 1,node -> Top + 1 + height - 1);
				}
			}
		}
	}

	node -> Current = percent;

	LTP_PutStorage(node);
}
#endif
