/*============================================================================*/
/*  Menus.c: by Kevin T. Seghetti					      */
/*  Simple paint program, main module					      */
/*============================================================================*/

#include "equates.h"
#include <functions.h>
#include <exec/types.h>
#include <intuition/intuition.h>

struct Remember *MenuRemember;
extern struct IntuiText *CreateIntuiText();

/*============================================================================*/
/*  CreateSmartMenuItems: create a list of menu items, with control	      */
/*  over left edge placement						      */
/*  Inputs:								      */
/*	    MenuText->array of pointers to text strings,		      */
/*	    MenuKeys->array of single chars, indicating hotkeys 	      */
/*		      (if null, then no hot key for that item)                */
/*	    LeftEdge = # of pixels from left edge text should begin	      */
/*============================================================================*/

struct MenuItem *
CreateSmartMenuItems(MenuText,MenuKeys,LeftEdge)
char *MenuText[];
char MenuKeys[];
SHORT LeftEdge;
{
    int i,Len,Entries;
    FLAG Key;
    struct MenuItem *FirstItem,*MenuItemPTR,*LastItemPTR;
    FirstItem =  NULL;

    Entries = 0;

    while(MenuText[Entries])
	++Entries;

    Key = FALSE;
    Len = 0;
    for(i=0;i<Entries;++i)
     {
	if(MenuKeys[i])
	    Key = TRUE;
	if(strlen(MenuText[i])*8 > Len)
	    Len = strlen(MenuText[i])*8;
     }
    Len += 8;

    if(Key)
	Len += COMMWIDTH;

    for(i=0;i<Entries;++i)
     {
	MenuItemPTR = (struct MenuItem *)AllocRemember(&MenuRemember,(long)sizeof(struct MenuItem),MEMF_CHIP);
	if(!MenuItemPTR)
	    cleanup("no memory for menus",ERR_NOMEM);
	if(!FirstItem)
	    FirstItem = MenuItemPTR;
	else
	    LastItemPTR->NextItem = MenuItemPTR;

	Key = (!!MenuKeys[i]);

	LastItemPTR = MenuItemPTR;

	MenuItemPTR->LeftEdge = LeftEdge;
	MenuItemPTR->Width = Len;
	MenuItemPTR->TopEdge = (i)*10;
	MenuItemPTR->Height = 10;
	MenuItemPTR->Flags = ITEMTEXT|ITEMENABLED|HIGHCOMP|((Key) ? COMMSEQ : NULL);
	MenuItemPTR->MutualExclude = 0;
	MenuItemPTR->ItemFill = (APTR)CreateIntuiText(MenuText[i],&MenuRemember);
	((struct IntuiText *)MenuItemPTR->ItemFill)->TopEdge = 1;
	MenuItemPTR->SelectFill = NULL;
	MenuItemPTR->Command = MenuKeys[i];
	MenuItemPTR->NextSelect = MENUNULL;
	MenuItemPTR->SubItem = NULL;
     }
     MenuItemPTR->NextItem = NULL;
    return(FirstItem);
}

/*============================================================================*/
/*  CreateMenuItems: create a list of menu items			      */
/*  Inputs:								      */
/*	    MenuText->array of pointers to text strings,		      */
/*	    MenuKeys->array of single chars, indicating hotkeys 	      */
/*		      (if null, then no hot key for that item)                */
/*============================================================================*/
struct MenuItem *
CreateMenuItems(MenuText,MenuKeys)
char *MenuText[];
char MenuKeys[];
{
    return(CreateSmartMenuItems(MenuText,MenuKeys,5));
}

/*============================================================================*/
/* CreateMenu: Create a menu structure					      */
/* Inputs: FirstItem-> first item of menuitem list(created by CreateMenuItems)*/
/*	   MenuName-> null terminated text string, naming menu		      */
/*  Bugs:								      */
/*	IRemember is hard-wired to MenuRemember 			      */
/*============================================================================*/

struct Menu *
CreateMenu(FirstItem,MenuName)
struct MenuItem *FirstItem;
char *MenuName;
{
    struct Menu *MenuPTR;
    MenuPTR = (struct Menu *)AllocRemember(&MenuRemember,(long)sizeof(struct Menu),MEMF_CHIP);
    if(!MenuPTR)
	cleanup("no memory for menus",ERR_NOMEM);
    MenuPTR->LeftEdge = 0;
    MenuPTR->TopEdge = 0;
    MenuPTR->Width = 3+strlen(MenuName)*8;
    MenuPTR->Height = 8;
    MenuPTR->Flags = MENUENABLED;
    MenuPTR->MenuName = (BYTE *)MenuName;
    MenuPTR->FirstItem = FirstItem;
    MenuPTR->NextMenu = NULL;
    return(MenuPTR);
}


