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

#include "gtlayout_global.h"

VOID __regargs
LTP_DrawMultiLineButton(struct RastPort *RPort,WORD Left,WORD Top,WORD Width,WORD Height,struct ImageInfo *ImageInfo,BOOL Bold)
{
	WORD Len,ThisTop,ThisLeft;
	STRPTR *Lines = ImageInfo -> Lines;
	WORD LineCount = ImageInfo -> LineCount;
	STRPTR KeyStroke = ImageInfo -> KeyStroke;
	STRPTR Label;
	ULONG OldStyle,StyleFlags;

	if(Bold)
	{
		StyleFlags = FSF_BOLD;

		OldStyle = SetSoftStyle(RPort,FSF_BOLD,AskSoftStyle(RPort));
	}
	else
		StyleFlags = NULL;

	if(!LineCount)
		LineCount = 1;

	ThisTop = Top + (Height - LineCount * RPort -> TxHeight + 1) / 2 + RPort -> TxBaseline;

	do
	{
		if(LineCount && Lines)
			Label = *Lines++;
		else
			Label = ImageInfo -> Label;

		Len = strlen(Label);

		ThisLeft = Left + (Width - TextLength(RPort,Label,Len)) / 2;

		Move(RPort,ThisLeft,ThisTop);
		Text(RPort,Label,Len);

		if(KeyStroke)
		{
			if(KeyStroke >= Label && KeyStroke < Label + Len)
			{
				ULONG OldStyle;

				if(KeyStroke != Label)
					ThisLeft += TextLength(RPort,Label,(ULONG)KeyStroke - (ULONG)(Label));

				OldStyle = SetSoftStyle(RPort,FSF_UNDERLINED | StyleFlags,AskSoftStyle(RPort));

				Move(RPort,ThisLeft,ThisTop);
				Text(RPort,KeyStroke,1);

				SetSoftStyle(RPort,StyleFlags | OldStyle & ~FSF_UNDERLINED,OldStyle);

				KeyStroke = NULL;
			}
		}

		ThisTop += RPort -> TxHeight;
	}
	while(--LineCount > 0);

	if(Bold)
		SetSoftStyle(RPort,OldStyle & ~FSF_BOLD,OldStyle);
}

VOID __regargs
LTP_DrawBox(struct RastPort *rp,struct DrawInfo *drawInfo,LONG left,LONG top,LONG width,LONG height,BOOLEAN selected,BOOLEAN ghosted,ImageInfo *imageInfo)
{
	UWORD		*pens = drawInfo -> dri_Pens;
	UWORD		 pen1,pen2,pen3,pen4;
	ImageTypes	 imageType = imageInfo -> ImageType;

	if(selected)
	{
		pen1 = SHADOWPEN;
		pen2 = SHINEPEN;
		pen3 = FILLPEN;
		pen4 = FILLTEXTPEN;
	}
	else
	{
		pen1 = SHINEPEN;
		pen2 = SHADOWPEN;
		pen3 = BACKGROUNDPEN;
		pen4 = TEXTPEN;
	}

	LTP_SetPens(rp,pens[pen1],0,JAM1);
	Move(rp,left + 1,top + 1);
	Draw(rp,left + 1,top + height - 2);
	Draw(rp,left,top + height - 1);
	Draw(rp,left,top);
	Draw(rp,left + width - 2,top);

	if(imageInfo -> Emboss)
	{
		Move(rp,left + 2,top + height - 3);
		Draw(rp,left + 2,top + 1);
		Draw(rp,left + width - 3,top + 1);

/*		Move(rp,left + 3,top + height - 4);*/
/*		Draw(rp,left + 3,top + 2);*/
/*		Draw(rp,left + width - 4,top + 2);*/
	}

	LTP_SetAPen(rp,pens[pen2]);
	Move(rp,left + width - 2,top + height - 2);
	Draw(rp,left + width - 2,top + 1);
	Draw(rp,left + width - 1,top);
	Draw(rp,left + width - 1,top + height - 1);
	Draw(rp,left + 1,top + height - 1);

	if(imageInfo -> Emboss)
	{
		Move(rp,left + width - 3,top + 2);
		Draw(rp,left + width - 3,top + height - 2);
		Draw(rp,left + 2,top + height - 2);

/*		Move(rp,left + width - 4,top + 3);*/
/*		Draw(rp,left + width - 4,top + height - 3);*/
/*		Draw(rp,left + 3,top + height - 3);*/
	}

	LTP_SetAPen(rp,pens[pen3]);

	if(imageInfo -> Emboss)
	{
		RectFill(rp,left + 3,top + 2,left + 2 + width - 6,top + 1 + height - 4);
/*		RectFill(rp,left + 4,top + 3,left + 2 + width - 7,top + 1 + height - 5);*/
	}
	else
		RectFill(rp,left + 2,top + 1,left + 2 + width - 5,top + 1 + height - 3);

	LTP_SetAPen(rp,pens[pen4]);

	switch(imageType)
	{
		case IMAGECLASS_PICKER:
		{
			LONG	Left	= left + 4,
				Top	= top + 2,
				Width	= width - 8,
				Height	= height - 4;

			LTP_DrawPicker(rp,FALSE,Left,Top,Width,Height);
			break;
		}

		case IMAGECLASS_MULTILINEBUTTON:

			if(imageInfo -> Emboss)
				LTP_DrawMultiLineButton(rp,left + 5,top + 3,width - 10,height - 6,imageInfo,TRUE);
			else
				LTP_DrawMultiLineButton(rp,left + 4,top + 2,width - 8,height - 4,imageInfo,FALSE);

			break;

		case IMAGECLASS_LEFTINCREMENTER:
		case IMAGECLASS_RIGHTINCREMENTER:

			LTP_DrawIncrementer(rp,imageType == IMAGECLASS_LEFTINCREMENTER,left + 2,top + 1,width - 4,height - 2);
			break;
#ifdef DO_TAPEDECK_KIND
		case IMAGECLASS_BACKWARD:
		case IMAGECLASS_FORWARD:
		case IMAGECLASS_PREVIOUS:
		case IMAGECLASS_NEXT:
		case IMAGECLASS_RECORD:
		case IMAGECLASS_PLAY:
		case IMAGECLASS_STOP:
		case IMAGECLASS_PAUSE:
		case IMAGECLASS_EJECT:
		case IMAGECLASS_REWIND:

			LTP_DrawTapeButton(rp,imageInfo,left,top,width,height,drawInfo -> dri_Resolution . X,drawInfo -> dri_Resolution . Y,pens[pen3]);
			break;
#endif	/* DO_TAPEDECK_KIND */
	}

	if(ghosted)
	{
		LTP_SetAPen(rp,pens[BACKGROUNDPEN]);

		SetAfPt(rp,(UWORD *)&checkPat,1);
		RectFill(rp,left,top,left + width - 1,top + height - 1);
		SetAfPt(rp,NULL,0);
	}
}
