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

#include "gtlayout_global.h"

VOID __regargs
LTP_PrintLine(LayoutHandle *handle,UBYTE alignType,LONG left,LONG top,LONG space,STRPTR line,LONG len)
{
	struct RastPort 	*rp;
	LONG			 width;
	struct TextExtent	 extent;

	rp	= &handle -> RPort;
	len	= TextFit(rp,line,len,&extent,NULL,1,space,32767);
	width	= extent . te_Width;

	if(len)
	{
		LTP_SetPens(rp,handle -> TextPen,handle -> BackgroundPen,JAM2);

		if(alignType == ALIGNTEXT_CENTERED)
		{
			LTP_PlaceText(handle,line,len,left + (space - width) / 2,top + handle -> RPort . TxBaseline);

			if(width < space)
			{
				LONG fill;

				LTP_SetAPen(rp,handle -> BackgroundPen);

				if((fill = (space - width) / 2) > 0)
					RectFill(rp,left,top,left + fill - 1,top + handle -> RPort . TxHeight - 1);

				if((fill = space - ((space - width) / 2)) > 0)
					RectFill(rp,left + fill,top,left + space - 1,top + handle -> RPort . TxHeight - 1);
			}
		}
		else
		{
			if(alignType == ALIGNTEXT_LEFT)
			{
				LTP_PlaceText(handle,line,len,left,top + handle -> RPort.TxBaseline);

				if(left + width <= left + space - 1)
				{
					LTP_SetAPen(rp,handle -> BackgroundPen);

					RectFill(rp,left + width,top,left + space - 1,top + handle -> RPort . TxHeight - 1);
				}
			}
			else
			{
				LTP_PlaceText(handle,line,len,left + space - width,top + handle -> RPort . TxBaseline);

				if(width < space)
				{
					LTP_SetAPen(rp,handle -> BackgroundPen);
					RectFill(rp,left,top,left + space - width - 1,top + handle -> RPort . TxHeight - 1);
				}
			}
		}
	}
	else
	{
		LTP_SetAPen(rp,handle -> BackgroundPen);
		RectFill(rp,left,top,left + space - 1,top + handle -> RPort . TxHeight - 1);
	}
}


/*****************************************************************************/


VOID __regargs
LTP_PrintLinePadded(LayoutHandle *Handle,LONG Left,LONG Top,LONG Space,STRPTR Line,LONG Len)
{
	struct RastPort *RPort	= &Handle -> RPort;
	LONG		 Size	= 0;
	LONG		 Start	= 0;
	LONG		 Count	= 0;
	LONG		 i	= 0;
	LONG		 Width	= 0;

	while(i < Len && Line[i] == ' ')
	{
		Size++;
		i++;
	}

	while(i < Len)
	{
		while(i < Len && Line[i] != ' ')
		{
			Size++;
			i++;
		}

		Width += TextLength(RPort,&Line[Start],Size);

		Count++;

		while(i < Len && Line[i] == ' ')
			i++;

		Start	= i;
		Size	= 0;
	}

	if(Width)
	{
		LONG j;

		Space -= Width;

		j = Start = Size = 0;

		while(j < Len && Line[j] == ' ')
		{
			Size++;
			j++;
		}

		LTP_SetPens(RPort,Handle -> TextPen,Handle -> BackgroundPen,JAM2);
		Move(RPort,Left,Top + RPort -> TxBaseline);

		Count--;

		for(i = 0 ; i <= Count ; i++)
		{
			if(i)
			{
				LONG Delta;

				if(Delta = (Space * i) / Count - (Space * (i - 1)) / Count)
				{
					LTP_SetAPen(RPort,Handle -> BackgroundPen);
					RectFill(RPort,RPort -> cp_x,Top,RPort -> cp_x + Delta - 1,Top + RPort -> TxHeight - 1);

					LTP_SetAPen(RPort,Handle -> TextPen);
					Move(RPort,RPort -> cp_x + Delta,RPort -> cp_y);
				}
			}

			while(j < Len && Line[j] != ' ')
			{
				Size++;
				j++;
			}

			Text(RPort,&Line[Start],Size);

			while(j < Len && Line[j] == ' ')
				j++;

			Start = j;
			Size = 0;
		}
	}
	else
	{
		LTP_SetAPen(RPort,Handle -> BackgroundPen);
		RectFill(RPort,Left,Top,Left + Space - 1,Top + RPort -> TxHeight - 1);
	}
}


/*****************************************************************************/


VOID __regargs
LTP_PrintBoxLine(LayoutHandle *handle,ObjectNode *node,LONG index)
{
	if(node -> Special . Box . Lines && node -> Special . Box . Lines[index])
	{
		LONG len = strlen(node -> Special . Box . Lines[index]);

		if(node -> Special . Box . AlignText == ALIGNTEXT_PAD)
			LTP_PrintLinePadded(handle,node -> Left + 4,node -> Top + 2 + index * handle -> RPort . TxHeight,node -> Width - 8,node -> Special . Box . Lines[index],len);
		else
			LTP_PrintLine(handle,node -> Special . Box . AlignText,node -> Left + 4,node -> Top + 2 + index * handle -> RPort . TxHeight,node -> Width - 8,node -> Special . Box . Lines[index],len);
	}
}
