/*  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;
	LONG			height	= node -> Height - (1 + 1);
	LONG			width	= node -> Width - (2 + 2);
	BOOLEAN			continuous;

	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 . Discrete)
	{
		if(width < 60)
			continuous = TRUE;
		else
			continuous = FALSE;
	}
	else
		continuous = TRUE;

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

	if(node -> Special . Gauge . InfoLength && node -> Special . Gauge . InfoText[0])
	{
		struct TextExtent	Extent;
		LONG			len;
		LONG			left;
		LONG			right;

		left = 0;

		if(continuous)
		{
			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
		{
			LONG count;

			right = 0;

			if(count = (10 * percent) / 100)
			{
				LONG chunk = width / 10,i;
				UWORD fillPen = handle -> DrawInfo -> dri_Pens[FILLPEN];
				UWORD backPen = handle -> BackgroundPen;

				for(i = 0 ; i < count ; i++)
				{
					LTP_SetAPen(rp,fillPen);
					RectFill(rp,node -> Left + 2 + left,node -> Top + 1,node -> Left + 2 + left + (chunk - 1) - 1,node -> Top + 1 + height - 1);

					left += chunk;
					right += chunk;

					LTP_SetAPen(rp,backPen);
					RectFill(rp,node -> Left + 2 + right - 1,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 left,right;

		if(continuous)
		{
			if(node -> Current < percent)
			{
				left	= (width * node -> Current) / 100;
				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)
				{
					left	= (width * percent) / 100;
					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);
					}
				}
			}
		}
		else
		{
			LONG count;

			count = (10 * percent) / 100;

			if(!fullRefresh && count == node -> Special . Gauge . LastPercentage)
			{
				node -> Current = percent;

				LTP_PutStorage(node);

				return;
			}

			node -> Special . Gauge . LastPercentage = count;

			right = 0;

			if(count)
			{
				LONG chunk = width / 10,i;
				UWORD fillPen = handle -> DrawInfo -> dri_Pens[FILLPEN];
				UWORD backPen = handle -> BackgroundPen;

				left = 0;

				for(i = 0 ; i < count ; i++)
				{
					LTP_SetAPen(rp,fillPen);
					RectFill(rp,node -> Left + 2 + left,node -> Top + 1,node -> Left + 2 + left + (chunk - 1) - 1,node -> Top + 1 + height - 1);

					left += chunk;
					right += chunk;

					LTP_SetAPen(rp,backPen);
					RectFill(rp,node -> Left + 2 + right - 1,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);
			}
		}
	}

	node -> Current = percent;

	LTP_PutStorage(node);
}
#endif
