
#include	<functions.h>
#include	<exec/types.h>
#include 	<exec/nodes.h>
#include	<exec/libraries.h>
#include	<graphics/gfx.h>
#include	<graphics/rastport.h>
#include	<graphics/text.h>
#include	<intuition/intuition.h>
#include 	<stdio.h>

#define	CHARWID 8

/**************  LEACH'S MENU SUPPORT STRUCTURES  *******************/

struct IntuiText L_MenuText[] =
{
	{
		0, 1,			/* UBYTE	FrontPen, BackPen	*/
		JAM1,			/* UBYTE	DrawMode			*/
		0, 1,			/* short	LeftEdge, TopEdge	*/
		NULL,			/* struct 	TextAttr			*/
		"LEACH",		/* UBYTE	*IText				*/
		NULL			/* struct	IntuiText *NextText	*/
	},
	{
		0, 1,			/* UBYTE	FrontPen, BackPen	*/
		JAM1,			/* UBYTE	DrawMode			*/
		0, 1,			/* short	LeftEdge, TopEdge	*/
		NULL,			/* struct 	TextAttr			*/
		" Scr On",		/* UBYTE	*IText				*/
		NULL			/* struct	IntuiText *NextText	*/
	},
	{
		0, 1,			/* UBYTE	FrontPen, BackPen	*/
		JAM1,			/* UBYTE	DrawMode			*/
		0, 1,			/* short	LeftEdge, TopEdge	*/
		NULL,			/* struct 	TextAttr			*/
		" Scr Off",		/* UBYTE	*IText				*/
		NULL			/* struct	IntuiText *NextText	*/
	}
};

/*--------------------------------------------------------------------------*
 * The first structure in this array will be attached to the Host's first
 * menu. The rest of the structures are subitems attached to the item.
 *--------------------------------------------------------------------------*/

struct MenuItem	 L_MenuItems[] =
{
	{
		NULL,							/* Next item.				*/
		0, 0,							/* LeftEdge, TopEdge. Set	*/
										/* at run-time.				*/
		0, 12,							/* Width, Height			*/
		ITEMTEXT | ITEMENABLED |		/* Flags					*/
		HIGHCOMP,
		NULL,							/* Mutual exclude flags.	*/
		&L_MenuText[0],					/* Item text.				*/
		NULL,							/* SelectFill				*/
		0,								/* Shortcut charactor		*/
		&L_MenuItems[1],				/* Pointer to Sub items.	*/
		0
	},
	{
		&L_MenuItems[2],				/* Next (sub)item.			*/
		5 * CHARWID,  0,				/* LeftEdge, TopEdge		*/
		9 * CHARWID, 12,				/* Width, Height			*/
		ITEMTEXT | ITEMENABLED |		/* Flags					*/
		HIGHCOMP,
		0x02L,							/* Mutual exclude flags.	*/
		&L_MenuText[1],					/* Item text.				*/
		NULL,							/* SelectFill				*/
		0,								/* Shortcut charactor		*/
		NULL,
		0
	},
	{
		NULL,							/* Next item.				*/
		5 * CHARWID,  14,				/* LeftEdge, TopEdge		*/
		9 * CHARWID, 12,				/* Width, Height			*/
		ITEMTEXT | ITEMENABLED |		/* Flags					*/
		HIGHCOMP,
		0x01L,							/* Mutual exclude flags.	*/
		&L_MenuText[2],					/* Item text.				*/
		NULL,							/* SelectFill				*/
		0,								/* Shortcut charactor		*/
		NULL,
		0
	}
};


extern struct Menu		*HostMenuStrip;
extern struct Window	*HostWind;
extern UBYTE			L_menu_num;
extern UBYTE			L_menuitem_num;


/****************************************************************************
 * This function walks down the Host's menu list to the end where it attaches
 * Leach's own menu. 
 ****************************************************************************/

install_menu()
{

struct MenuItem	*item_ptr;

L_menu_num		= 0;
L_menuitem_num	= 0;
item_ptr		= NULL;

if (!HostMenuStrip)				/* If the Host has no menus... (unlikly)	*/
{
	puts("Leach can not attach itself to Host's with no menus.");
	cleanup(302);
}
else
{
	item_ptr = HostMenuStrip->FirstItem;

	if (! item_ptr)
	{
		puts("Host has no first menu item!");
		cleanup(304);
	}
	++L_menuitem_num;
	while(item_ptr->NextItem)	/* While not the last Host menu item...	*/
	{
		++L_menuitem_num;
		item_ptr = item_ptr->NextItem;
	}
	ClearMenuStrip(HostWind);

	L_MenuItems[0].LeftEdge = item_ptr->LeftEdge;
	L_MenuItems[0].Width	= item_ptr->Width;
	L_MenuItems[0].TopEdge	= item_ptr->TopEdge + item_ptr->Height + 2;

	L_MenuItems[1].TopEdge	= 0;
	L_MenuItems[1].LeftEdge	= L_MenuItems[0].LeftEdge + L_MenuItems[0].Width -7;

	L_MenuItems[2].TopEdge	= L_MenuItems[1].TopEdge + 14;
	L_MenuItems[2].LeftEdge	= L_MenuItems[1].LeftEdge;

	item_ptr->NextItem	= L_MenuItems;
	SetMenuStrip(HostWind, HostMenuStrip);
}

return;

} /*  End of install_menu()  */


/****************************************************************************
 * If this routine removes Leach's menu item from the Host's MenuStrip.
 ****************************************************************************/

remove_menu()
{

struct MenuItem		*item_ptr;

if (!HostMenuStrip)	return;						/* Shouldn't ever happen	*/
item_ptr = HostMenuStrip->FirstItem;

while ( (item_ptr->NextItem != L_MenuItems) && item_ptr)
	item_ptr = item_ptr->NextItem;

/* menu_ptr = NULL if there's no first item or Leach wasn't in first menu.	*/
if (item_ptr)
{
	ClearMenuStrip(HostWind);
	item_ptr->NextItem = NULL;
	SetMenuStrip(HostWind, HostMenuStrip);
}

return;

} /*  End of remove_menu()  */
