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

#include "gtlayout_global.h"

struct Menu * __stdargs
LT_LayoutMenus(LayoutHandle *handle,struct NewMenu *menuTemplate,...)
{
	struct Menu	*Result;
	va_list 	 VarArgs;

	va_start(VarArgs,menuTemplate);
	Result = LT_LayoutMenusA(handle,menuTemplate,(struct TagItem *)VarArgs);
	va_end(VarArgs);

	return(Result);
}


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


struct Menu * LIBENT
LT_LayoutMenusA(REG(a0) LayoutHandle *handle,REG(a1) struct NewMenu *menuTemplate,REG(a2) struct TagItem *TagParams)
{
	struct Menu *menu;

	menu = NULL;

	if(handle)
	{
		struct TagItem	*tag,*List = TagParams;
		LONG		 label,last;
		LONG		*Table = NULL;
		ULONG		 allocSize;
		BOOLEAN 	 foundFirst;
		BOOLEAN 	 foundLast;

		foundFirst	= FALSE;
		foundLast	= FALSE;

		while(tag = NextTagItem(&List))
		{
			switch(tag -> ti_Tag)
			{
				case LA_LabelTable:

					Table = (LONG *)tag -> ti_Data;
					break;

				case LA_FirstLabel:

					foundFirst = TRUE;

					label = tag -> ti_Data;
					break;

				case LA_LastLabel:

					foundLast = TRUE;

					last = tag -> ti_Data;
					break;
			}
		}

		if(foundFirst == foundLast || Table)
		{
			struct NewMenu *localTemplate = NULL;

			if(Table)
			{
				foundFirst = FALSE;

				if(handle -> LocaleHook)
				{
					LONG count;

					count = 0;

					while(menuTemplate[count] . nm_Type != NM_END)
						count++;

					count++;

					if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
					{
						LONG i;

						CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);

						for(i = 0 ; *Table != -1 && i < count ; i++)
						{
							if(localTemplate[i] . nm_Label != NM_BARLABEL && (localTemplate[i] . nm_Type == NM_TITLE || localTemplate[i] . nm_Type == NM_ITEM || localTemplate[i] . nm_Type == NM_SUB))
								localTemplate[i] . nm_Label = (STRPTR)CallHookPkt(handle -> LocaleHook,handle,(APTR)(*Table++));
						}
					}
				}

				menuTemplate = localTemplate;
			}

			if(foundFirst)
			{
				if(handle -> LocaleHook)
				{
					LONG count;

					count = 0;

					while(menuTemplate[count] . nm_Type != NM_END)
						count++;

					count++;

					if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
					{
						LONG i;

						CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);

						if(foundLast)
							count = last - label + 1;

						for(i = 0 ; i < count ; i++)
						{
							if(localTemplate[i] . nm_Label != NM_BARLABEL && (localTemplate[i] . nm_Type == NM_TITLE || localTemplate[i] . nm_Type == NM_ITEM || localTemplate[i] . nm_Type == NM_SUB))
							{
								localTemplate[i] . nm_Label = (STRPTR)CallHookPkt(handle -> LocaleHook,handle,(APTR)label);

								label++;
							}
						}
					}
				}

				menuTemplate = localTemplate;
			}

			if(menuTemplate)
			{
				if(menu = CreateMenusA(menuTemplate,NULL))
				{
					if(!LayoutMenus(menu,handle -> VisualInfo,
						GTMN_NewLookMenus,	TRUE,

						handle -> AmigaGlyph ? GTMN_AmigaKey :	TAG_IGNORE,handle -> AmigaGlyph,
						handle -> CheckGlyph ? GTMN_Checkmark : TAG_IGNORE,handle -> CheckGlyph,
					TAG_DONE))
					{
						FreeMenus(menu);

						menu = NULL;
					}
				}
			}

			LTP_Free(handle,localTemplate,allocSize);
		}
	}

	return(menu);
}
