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

#include "gtlayout_global.h"

#ifdef DO_MENUS

	/* LTP_ShrinkMenu(RootMenu *Root,ItemNode *First,ItemNode *Last,UWORD Mask):
	 *
	 *	Rethink the widths of all menu items between First and Last, including
	 *	both the first and the last entry.
	 */

VOID __regargs
LTP_ShrinkMenu(RootMenu *Root,ItemNode *First,ItemNode *Last,UWORD Mask)
{
	ItemNode	*Here;
	UWORD		 Width,MaxWidth = 0,CommandWidth = 0;

		// Determine the widths of all items, also calculate
		// the command widths

	for(Here = First ; Here -> Node . mln_Succ ; Here = (ItemNode *)Here -> Node . mln_Succ)
	{
		if((Here -> Flags & ITEMF_IsSub) == Mask)
		{
			if(!(Here -> Flags & ITEMF_IsBar))
			{
				struct IntuiText *IntuiText = (struct IntuiText *)Here -> Item . ItemFill;

				Width = 2 + TextLength(&Root -> RPort,IntuiText -> IText,strlen(IntuiText -> IText)) + 2;

				if(Here -> Item . Flags & CHECKIT)
					Width += 2 + Root -> CheckWidth;

				if(Width > MaxWidth)
					MaxWidth = Width;

				if((Width = LTP_GetCommandWidth(Root,Here)) > CommandWidth)
					CommandWidth = Width;
			}
		}

		if(Here == Last)
			break;
	}

		// Now adjust the widths of all objects

	for(Here = First ; Here -> Node . mln_Succ ; Here = (ItemNode *)Here -> Node . mln_Succ)
	{
		if((Here -> Flags & ITEMF_IsSub) == Mask)
		{
			if(Here -> Flags & ITEMF_IsBar)
			{
				struct Image *Image = (struct Image *)Here -> Item . ItemFill;

				Image -> Width = MaxWidth + CommandWidth - 4;
			}
			else
			{
					// Move over the submenu items if necessary

				if(Here -> Item . SubItem)
				{
					ItemNode *Sub = (ItemNode *)((ULONG)Here -> Item . SubItem - sizeof(struct MinNode));

					for(;;)
					{
						Sub -> Item . LeftEdge = MaxWidth + 2;

						if(Sub -> Item . NextItem)
							Sub = (ItemNode *)Sub -> Node . mln_Succ;
						else
							break;
					}
				}
			}

			Here -> Item . Width = MaxWidth + CommandWidth;
		}

		if(Here == Last)
			break;
	}
}

#endif	/* DO_MENUS */
