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

#include "gtlayout_global.h"

VOID __regargs
LTP_DrawObjectLabel(LayoutHandle *Handle,ObjectNode *Node)
{
	LTP_SetPens(&Handle -> RPort,Handle -> TextPen,Handle -> BackgroundPen,JAM2);

	switch(Node -> LabelPlace)
	{
		case PLACE_LEFT:

			LTP_PrintLabel(Handle,Node,Node -> Left,Node -> Top + (Node -> Height - Handle -> RPort . TxHeight) / 2);
			break;

		case PLACE_RIGHT:

			LTP_PrintLabel(Handle,Node,Node -> Left + Node -> Width + INTERWIDTH + INTERWIDTH + Node -> LabelWidth,Node -> Top + (Node -> Height - Handle -> RPort . TxHeight) / 2);
			break;

		case PLACE_ABOVE:

			LTP_PrintLabel(Handle,Node,Node -> Left + (Node -> Width - Node -> LabelWidth) / 2 + INTERWIDTH + Node -> LabelWidth,Node -> Top - (Handle -> RPort . TxHeight + INTERHEIGHT));
			break;

		case PLACE_BELOW:

			LTP_PrintLabel(Handle,Node,Node -> Left + (Node -> Width - Node -> LabelWidth) / 2 + INTERWIDTH + Node -> LabelWidth,Node -> Top + Node -> Height + INTERHEIGHT);
			break;
	}
}

VOID __regargs
LTP_DrawGroup(LayoutHandle *Handle,ObjectNode *Group)
{
	ObjectNode	*Node;
	LONG		 i;
	ULONG		 page;

	if(Group -> Label || Group -> Special . Group . Frame)
		LTP_DrawLabel(Handle,Group);

	SCANPAGE(Group,Node,page)
	{
		switch(Node -> Type)
		{
#ifdef OLD_STYLE_DEFAULT_KEY
			case BUTTON_KIND:

				if(Handle -> ReturnKey == Node)
					LTP_DrawBevelBox(Handle,Node);

				break;
#endif
#ifdef DO_GAUGE_KIND
			case GAUGE_KIND:
			{
				LONG Percent = Node -> Current;

				Node -> Current = 0;

				LTP_DrawGauge(Handle,Node,Percent,TRUE);

				break;
			}
#endif
			case PALETTE_KIND:

				if(Node -> Special . Palette . UsePicker)
					LTP_DrawPalette(Handle,Node);

				break;

			case GROUP_KIND:

				if(Node -> Label)
					LTP_DrawLabel(Handle,Node);

				LTP_DrawGroup(Handle,Node);

				break;

			case XBAR_KIND:
			{
				WORD Top = Node -> Top + ((Node -> Height + 1) / 2) - 1;

				LockLayerRom(Handle -> RPort . Layer);

				if(Node -> Special . Bar . FullSize)
				{
					LTP_SetAPen(&Handle -> RPort,Handle -> ShadowPen);
					Move(&Handle -> RPort,Handle -> Window -> BorderLeft,Top);
					Draw(&Handle -> RPort,Handle -> Window -> Width - (Handle -> Window -> BorderRight + 1),Top);

					LTP_SetAPen(&Handle -> RPort,Handle -> ShinePen);
					Move(&Handle -> RPort,Handle -> Window -> BorderLeft,Top + 1);
					Draw(&Handle -> RPort,Handle -> Window -> Width - (Handle -> Window -> BorderRight + 1),Top + 1);
				}
				else
				{
					if(Node -> Special . Bar . Parent -> Label)
					{
						LTP_SetAPen(&Handle -> RPort,Handle -> ShadowPen);
						Move(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + 2 * Handle -> GlyphWidth,Top + 1);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + 2 * Handle -> GlyphWidth,Top);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + Node -> Special . Bar . Parent -> Width - (2 * Handle -> GlyphWidth + 1) - 1,Top);

						LTP_SetAPen(&Handle -> RPort,Handle -> ShinePen);
						Move(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + 2 * Handle -> GlyphWidth + 1,Top + 1);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + Node -> Special . Bar . Parent -> Width - (2 * Handle -> GlyphWidth + 1),Top + 1);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + Node -> Special . Bar . Parent -> Width - (2 * Handle -> GlyphWidth + 1),Top);
					}
					else
					{
						LTP_SetAPen(&Handle -> RPort,Handle -> ShadowPen);
						Move(&Handle -> RPort,Node -> Special . Bar . Parent -> Left,Top + 1);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left,Top);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + Node -> Special . Bar . Parent -> Width - 1,Top);

						LTP_SetAPen(&Handle -> RPort,Handle -> ShinePen);
						Move(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + 1,Top + 1);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + Node -> Special . Bar . Parent -> Width - 1,Top + 1);
						Draw(&Handle -> RPort,Node -> Special . Bar . Parent -> Left + Node -> Special . Bar . Parent -> Width - 1,Top);
					}
				}

				UnlockLayerRom(Handle -> RPort . Layer);

				break;
			}

			case YBAR_KIND:
			{
				WORD Left = Node -> Left + ((Node -> Width + 1) / 2) - 1;

				LockLayerRom(Handle -> RPort . Layer);

				if(Node -> Special . Bar . Parent -> Label)
				{
					LTP_SetAPen(&Handle -> RPort,Handle -> ShadowPen);
					Move(&Handle -> RPort,Left + 1,Node -> Special . Bar . Parent -> Top + Handle -> RPort . TxHeight + Handle -> InterHeight);
					Draw(&Handle -> RPort,Left,Node -> Special . Bar . Parent -> Top + Handle -> RPort . TxHeight + Handle -> InterHeight);
					Draw(&Handle -> RPort,Left,Node -> Special . Bar . Parent -> Top + Node -> Special . Bar . Parent -> Height - (Handle -> RPort . TxHeight + Handle -> InterHeight + 1) - 1);

					LTP_SetAPen(&Handle -> RPort,Handle -> ShinePen);
					Move(&Handle -> RPort,Left + 1,Node -> Special . Bar . Parent -> Top + Handle -> RPort . TxHeight + Handle -> InterHeight + 1);
					Draw(&Handle -> RPort,Left + 1,Node -> Special . Bar . Parent -> Top + Node -> Special . Bar . Parent -> Height - (Handle -> RPort . TxHeight + Handle -> InterHeight + 1));
					Draw(&Handle -> RPort,Left,Node -> Special . Bar . Parent -> Top + Node -> Special . Bar . Parent -> Height - (Handle -> RPort . TxHeight + Handle -> InterHeight + 1));
				}
				else
				{
					LTP_SetAPen(&Handle -> RPort,Handle -> ShadowPen);
					Move(&Handle -> RPort,Left + 1,Node -> Special . Bar . Parent -> Top);
					Draw(&Handle -> RPort,Left,Node -> Special . Bar . Parent -> Top);
					Draw(&Handle -> RPort,Left,Node -> Special . Bar . Parent -> Top + Node -> Special . Bar . Parent -> Height - 1 - 1);

					LTP_SetAPen(&Handle -> RPort,Handle -> ShinePen);
					Move(&Handle -> RPort,Left + 1,Node -> Special . Bar . Parent -> Top + 1);
					Draw(&Handle -> RPort,Left + 1,Node -> Special . Bar . Parent -> Top + Node -> Special . Bar . Parent -> Height - 1);
					Draw(&Handle -> RPort,Left,Node -> Special . Bar . Parent -> Top + Node -> Special . Bar . Parent -> Height - 1);
				}

				UnlockLayerRom(Handle -> RPort . Layer);

				break;
			}

			case FRAME_KIND:

				if(Node -> Special . Frame . DrawBox)
					LTP_DrawBevelBox(Handle,Node);

				if(Node -> Special . Frame . RefreshHook)
				{
					RefreshMsg Message;

					Message . ID		= Node -> ID;
					Message . Left		= Node -> Left;
					Message . Top		= Node -> Top;
					Message . Width		= Node -> Width;
					Message . Height	= Node -> Height;

					if(Node -> Special . Frame . DrawBox)
					{
						Message . Left		+= 4;
						Message . Top		+= 2;
						Message . Width		-= 8;
						Message . Height	-= 4;
					}

					CallHookPkt(Node -> Special . Frame . RefreshHook,Handle,&Message);
				}

				// FALL THROUGH TO...

			case BOOPSI_KIND:

				// FALL THROUGH TO...

			case POPUP_KIND:

				// FALL THROUGH TO...

			case TAPEDECK_KIND:

				if(Node -> Label)
					LTP_DrawObjectLabel(Handle,Node);

				break;

			case MX_KIND:

				if(Node -> Label && !V39)
					LTP_PrintLabel(Handle,Node,Node -> Left,Node -> Top + (Node -> Height - Handle -> RPort . TxHeight) / 2 + 1);

				break;

			case LISTVIEW_KIND:

				if(Node -> Special . List . ExtraLabels)
				{
					LockLayerRom(Handle -> RPort . Layer);

					LTP_SetPens(&Handle -> RPort,Handle -> TextPen,Handle -> BackgroundPen,JAM2);

					for(i = 0 ; Node -> Special . List . ExtraLabels[i] ; i++)
					{
						LONG Len = strlen(Node -> Special . List . ExtraLabels[i]);

						LTP_PlaceText(Handle,Node -> Special . List . ExtraLabels[i],Len,Node -> Left - (TextLength(&Handle -> RPort,Node -> Special . List . ExtraLabels[i],Len) + INTERWIDTH),Node -> Top + 2 + i * Handle -> RPort . TxHeight + Handle -> RPort . TxBaseline);
					}

					UnlockLayerRom(Handle -> RPort . Layer);
				}

				if(Node -> Label)
				{
					if(Node -> LabelPlace == PLACE_ABOVE && Node -> Special . List . FlushLabelLeft)
					{
						struct TextFont *Font = NULL,*OldFont;

						if(Node -> Special . List . TextAttr)
						{
							if(Font = LTP_OpenFont(Node -> Special . List . TextAttr))
							{
								OldFont = Handle -> RPort . Font;

								SetFont(&Handle -> RPort,Font);
							}
							else
								break;
						}

						LTP_PrintLabel(Handle,Node,Node -> Left + 2 + INTERWIDTH + Node -> LabelWidth,Node -> Top - (Handle -> RPort . TxHeight + INTERHEIGHT));

						if(Font)
						{
							SetFont(&Handle -> RPort,OldFont);

							CloseFont(Font);
						}
					}
					else
					{
						if(Node -> Special . List . TextAttr && Node -> Label)
							LTP_DrawObjectLabel(Handle,Node);
					}
				}

				break;

			case BOX_KIND:

				Node -> Special . Box . Parent = Group;

				if(Node -> Special . Box . DrawBox)
					LTP_DrawBevelBox(Handle,Node);

				LockLayerRom(Handle -> RPort . Layer);

				for(i = 0 ; i < Node -> Lines ; i++)
				{
					if(Node -> Special . Box . Labels && Node -> Special . Box . Labels[i])
					{
						LONG Len = strlen(Node -> Special . Box . Labels[i]);

						LTP_SetPens(&Handle -> RPort,Handle -> TextPen,Handle -> BackgroundPen,JAM2);
						LTP_PlaceText(Handle,Node -> Special . Box . Labels[i],Len,Node -> Left - (TextLength(&Handle -> RPort,Node -> Special . Box . Labels[i],Len) + INTERWIDTH),Node -> Top + 2 + i * Handle -> RPort . TxHeight + Handle -> RPort . TxBaseline);
					}

					LTP_PrintBoxLine(Handle,Node,i);
				}

				UnlockLayerRom(Handle -> RPort . Layer);

				break;
		}
	}
}
