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

#include "gtlayout_global.h"

BOOL LIBENT
LT_RebuildTagList(REG(a0) LayoutHandle *handle,REG(d0) BOOL clear,REG(a1) struct TagItem *TagParams)
{
	struct TagItem	*item,
			*list;

	struct IBox	*bounds = NULL;

	list = TagParams;

	while(item = NextTagItem(&list))
	{
		switch(item -> ti_Tag)
		{
			case LAWN_Bounds:

				bounds = (struct IBox *)item -> ti_Data;
				break;
		}
	}

	if(handle)
	{
		struct Gadget	*gadget;
		ObjectNode	*node;
		LONG		 left,top;
		struct IBox	 newBounds;
		LONG		 i;

		if(!handle -> SizeVerified)
			RemoveGList(handle -> Window,handle -> List,(UWORD)-1);

		gadget = handle -> List;

		while(gadget)
		{
			if(GETOBJECT(gadget,node))
			{
				LTP_PutStorage(node);

				switch(node -> Type)
				{
					case PICKER_KIND:

						if(node -> Special . Picker . Image)
						{
							DisposeObject(node -> Special . Picker . Image);

							node -> Special . Picker . Image = NULL;
						}

						break;

					case TAPEDECK_KIND:

						if(node -> Special . TapeDeck . ButtonImage)
						{
							DisposeObject(node -> Special . TapeDeck . ButtonImage);

							node -> Special . TapeDeck . ButtonImage = NULL;
						}

						break;

					case INCREMENTER_KIND:

						if(node -> Special . Incrementer . Image)
						{
							DisposeObject(node -> Special . Incrementer . Image);

							node -> Special . Incrementer . Image = NULL;
						}

						break;

					case STRING_KIND:

						if(!node -> Special . String . Backup)
							node -> Special . String . Backup = (STRPTR)LTP_Alloc(handle,node -> Special . String . MaxChars + 1);

						if(node -> Special . String . Backup)
						{
							strcpy(node -> Special . String . Backup,((struct StringInfo *)gadget -> SpecialInfo) -> Buffer);

							node -> Special . String . String = node -> Special . String . Backup;
						}

						node -> Special . String . Picker = NULL;

						break;

					case TEXT_KIND:

						node -> Special . Text . Picker = NULL;

						break;

					case INTEGER_KIND:

						node -> Special . Integer . LeftIncrementer = NULL;
						node -> Special . Integer . RightIncrementer = NULL;

						break;
#ifdef DO_LEVEL_KIND
					case LEVEL_KIND:

						if(node -> Special . Level . LevelImage)
						{
							DisposeObject(node -> Special . Level . LevelImage);

							node -> Special . Level . LevelImage = NULL;
						}

						break;
#endif	/* DO_LEVEL_KIND */
				}

				node -> Host = NULL;
			}

			gadget = gadget -> NextGadget;
		}

		FreeGadgets(handle -> List);

		handle -> List = NULL;

		LTP_Free(handle,handle -> GadgetArray,sizeof(struct Gadget *) * handle -> Count);

		handle -> GadgetArray = NULL;

		handle -> Count = handle -> Index = 0;

		LTP_ResetGroups(handle -> TopGroup);

		if(!bounds)
		{
			LTP_GetDisplayClip(handle -> Screen,&newBounds . Left,&newBounds . Top,&newBounds . Width,&newBounds . Height);

			bounds = &newBounds;
		}

		left	= handle -> Window -> BorderLeft + handle -> InterWidth;
		top	= handle -> Window -> BorderTop + handle -> InterHeight;

//		LTP_CreateGadgets(handle,bounds,left,top,left + handle -> InterWidth + handle -> Window -> BorderRight,top + handle -> InterHeight + handle -> Window -> BorderBottom);
		LTP_CreateGadgets(handle,bounds,left,top,left + handle -> InterWidth + handle -> Screen -> WBorRight,top + handle -> InterHeight + handle -> Screen -> WBorBottom);

		if(handle -> Failed)
		{
			handle -> SizeVerified = FALSE;

			EraseRect(&handle -> RPort,handle -> Window -> BorderLeft,handle -> Window -> BorderTop,handle -> Window -> Width - (handle -> Window -> BorderRight + 1),handle -> Window -> Height - (handle -> Window -> BorderBottom + 1));

			RefreshWindowFrame(handle -> Window);

			return(FALSE);
		}

		if(clear)
		{
			EraseRect(&handle -> RPort,handle -> Window -> BorderLeft,handle -> Window -> BorderTop,handle -> Window -> Width - (handle -> Window -> BorderRight + 1),handle -> Window -> Height - (handle -> Window -> BorderBottom + 1));

			if(!handle -> SizeVerified)
				RefreshWindowFrame(handle -> Window);
		}

		handle -> SizeVerified = FALSE;

		LTP_AddGadgets(handle);

		handle -> Previous = NULL;

		for(i = 0 ; i < handle -> Count ; i++)
		{
			if(handle -> GadgetArray[i])
			{
				if(GETOBJECT(handle -> GadgetArray[i],node))
				{
					if(LIKE_STRING_KIND(node) || (node -> Type == INTEGER_KIND))
					{
						if(!handle -> Previous)
							handle -> Previous = handle -> GadgetArray[i];
					}
				}
			}
		}

		return(TRUE);
	}
	else
		return(FALSE);
}

BOOL __stdargs
LT_RebuildTags(LayoutHandle *Handle,BOOL Clear,...)
{
	va_list VarArgs;
	BOOL	Result;

	va_start(VarArgs,Clear);
	Result = LT_RebuildTagList(Handle,Clear,(struct TagItem *)VarArgs);
	va_end(VarArgs);

	return(Result);
}

BOOL LIBENT
LT_Rebuild(REG(a0) LayoutHandle *handle,REG(a1) struct IBox *bounds,REG(a2) LONG extraWidth,REG(d0) LONG extraHeight,REG(d1) BOOL clear)
{
	return(LT_RebuildTags(handle,clear,
		LAWN_Bounds,bounds,
	TAG_DONE));
}
