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

#include "gtlayout_global.h"

VOID __regargs
LTP_CreateGadgets(LayoutHandle *Handle,struct IBox *Bounds,LONG Left,LONG Top,LONG PlusWidth,LONG PlusHeight)
{
	if(!Handle -> Failed)
	{
		BOOLEAN	Fits;
		BOOLEAN	Cloned = FALSE;
		WORD	Attempt = 3;
/*
		if(!V39 && (Handle -> RPort . TxFlags & FPF_PROPORTIONAL))
		{
			LTP_Rescale(Handle,FALSE,FALSE);

			Handle -> Rescaled = FALSE;
		}
*/
		do
		{
			LTP_LayoutGroup(Handle,Handle -> TopGroup);
			LTP_PlaceGroups(Handle,Handle -> TopGroup,Left,Top);

			Fits = TRUE;

			if(Handle -> TopGroup -> Width + PlusWidth > Bounds -> Width)
			{
				if(Handle -> TopGroup -> Width + PlusWidth > Handle -> Screen -> Width)
				{
					Fits = FALSE;

					if(Handle -> TopGroup -> Height + PlusHeight > Bounds -> Height)
					{
						if(!LTP_Rescale(Handle,TRUE,TRUE))
							Handle -> Failed = TRUE;
					}
					else
					{
						if(!LTP_Rescale(Handle,TRUE,FALSE))
							Handle -> Failed = TRUE;
					}
				}
			}
			else
			{
				if(Handle -> TopGroup -> Height + PlusHeight > Bounds -> Height)
				{
					if(Handle -> TopGroup -> Height + PlusHeight > Handle -> Screen -> Height)
					{
						Fits = FALSE;

						if(!LTP_Rescale(Handle,FALSE,TRUE))
							Handle -> Failed = TRUE;
					}
				}
			}

#ifdef DO_CLONING
			DB(kprintf("1) failed %ld cloned %ld cloningpermitted %ld\n",Handle -> Failed,Cloned,Handle -> CloningPermitted));

			if(Handle -> Failed && !Cloned && Handle -> CloningPermitted)
			{
				if(LTP_PrepareCloning(Handle))
					Bounds = &Handle -> CloneExtra -> Bounds;

				Cloned = TRUE;
			}
#endif

			if(Attempt > 0 && Fits && !Handle -> Rescaled && !Cloned && !Handle -> Failed && (Handle -> GrowView || (Handle -> ResizeView && Handle -> Window)))
			{
				LONG		Width	= Handle -> TopGroup -> Width	+ PlusWidth,
						Height	= Handle -> TopGroup -> Height	+ PlusHeight;
				ObjectNode	*View;

				Attempt--;

				if(Handle -> GrowView)
					View = Handle -> GrowView;
				else
					View = Handle -> ResizeView;

 				if(Bounds -> Width != Width || Bounds -> Height != Height)
				{
					LONG	PlusChars,
						PlusLines;
					WORD	GlyphWidth,
						GlyphHeight;
					WORD	AvailableWidth,
						AvailableHeight;

					AvailableWidth	= Bounds -> Width	- Width;
					AvailableHeight	= Bounds -> Height	- Height;

					if(View -> Special . List . TextAttr)
					{
						GlyphWidth	= View -> Special . List . FixedGlyphWidth;
						GlyphHeight	= View -> Special . List . FixedGlyphHeight;
					}
					else
					{
						GlyphWidth	= Handle -> GlyphWidth;
						GlyphHeight	= Handle -> RPort . TxHeight;
					}

					if(View -> Special . List . MaxGrowX && Handle -> GrowView)
					{
						PlusChars = AvailableWidth / GlyphWidth;

						if(PlusChars > 0 && View -> Chars + PlusChars > View -> Special . List . MaxGrowX)
							PlusChars = View -> Special . List . MaxGrowX - View -> Chars;

						if(PlusChars < 0)
							PlusChars = 0;
					}
					else
					{
						if(View -> Special . List . ResizeX && !Handle -> GrowView)
						{
							if(AvailableWidth < 0)
								PlusChars = (AvailableWidth - (GlyphWidth + 1)) / GlyphWidth;
							else
								PlusChars = AvailableWidth / GlyphWidth;
						}
						else
							PlusChars = 0;
					}

					if(View -> Special . List . MaxGrowY && Handle -> GrowView)
					{
						PlusLines = AvailableHeight / GlyphHeight;

						if(PlusLines > 0 && View -> Lines + PlusLines > View -> Special . List . MaxGrowY)
							PlusLines = View -> Special . List . MaxGrowY - View -> Lines;

						if(PlusLines < 0)
							PlusLines = 0;
					}
					else
					{
						if(View -> Special . List . ResizeY && !Handle -> GrowView)
						{
							if(AvailableHeight < 0)
								PlusLines = (AvailableHeight - (GlyphHeight + 1)) / GlyphHeight;
							else
								PlusLines = AvailableHeight / GlyphHeight;
						}
						else
							PlusLines = 0;
					}

					if(PlusChars != 0 || PlusLines != 0)
					{
						View -> Chars += PlusChars;
						View -> Lines += PlusLines;

						LTP_ResetGroups(Handle -> TopGroup);
/*
						if(!V39 && (Handle -> RPort . TxFlags & FPF_PROPORTIONAL))
						{
							LTP_Rescale(Handle,FALSE,FALSE);

							Handle -> Rescaled = FALSE;
						}
*/
						Fits = FALSE;
					}
				}

				Handle -> GrowView = NULL;
			}
		}
		while(!Fits && !Handle -> Failed);

#ifdef DO_CLONING
		DB(kprintf("2) failed %ld cloned %ld\n",Handle -> Failed,Cloned));

		if(!Handle -> Failed && Cloned)
			LTP_CloneScreen(Handle,Handle -> TopGroup -> Width + PlusWidth,Handle -> TopGroup -> Height + PlusHeight);
#endif

		if(!Handle -> Failed)
		{
			if(Handle -> GadgetArray = LTP_Alloc(Handle,sizeof(struct Gadget *) * Handle -> Count))
			{
#ifdef DO_PICKSHORTCUTS
				LTP_SearchKeys(Handle,Handle -> TopGroup);
				LTP_SelectKeys(Handle,Handle -> TopGroup);
#endif
				LTP_LayoutGadgets(Handle,Handle -> TopGroup);

				if(Handle -> Failed)
					LTP_DisposeGadgets(Handle);
			}
		}
	}
}
