/* :ts=8 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*                                                              */
/*      SampleEvnt.c                                            */
/*                                                              */
/*      Event Handler for AmigaPLUS article about Intuition    */
/*      Written by Michael G. Lehman                            */
/*      of Intuitive Technologies                               */
/*                                                              */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/


/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*      Complete Include file for Amiga programs                */
/*      Derived from examples supplied by Commodore Amiga       */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/


#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <exec/ports.h>
#include <exec/tasks.h>
#include <exec/libraries.h>
#include <exec/devices.h>
#include <exec/io.h>
#include <exec/devices.h>

#include <libraries/dos.h>

#include <graphics/gfx.h> /* ALWAYS INCLUDE GFX.H before other includes */
#include <graphics/display.h>
#include <graphics/clip.h>
#include <graphics/rastport.h>
#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <graphics/regions.h>
#include <graphics/copper.h>
#include <graphics/gels.h>

#include <devices/inputevent.h>
#include <devices/gameport.h>
#include <devices/console.h>
#include <devices/keymap.h>

#include <intuition/intuition.h>

struct XMenuItem                /* our extended menu structure */
{
        struct MenuItem *NextItem;
        SHORT LeftEdge, TopEdge;
        SHORT Width, Height;
        USHORT Flags;
        LONG MutualExclude;
        APTR ItemFill;
        APTR SelectFill;
        BYTE Command;
        struct MenuItem *SubItem;
        USHORT NextSelect;
        int (*ItemHndlr)();     /* all above is standard and we add this */
};

extern UBYTE * AllocMem();

#define SECONDS io_Actual
#define MICROSECONDS io_Length

#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif

#define IEQUALIFIER_MBUTTON IEQUALIFIER_LEFTBUTTON

#define PUBCLEAR MEMF_PUBLIC | MEMF_CLEAR
SHORT textmode;

char *Address;          /*general purpose address variable */

extern struct RastPort *rp;

extern SHORT class;

SHORT last_class = -1;
SHORT save_class;
extern SHORT code;
extern USHORT mousex,mousey;
extern SHORT qualifier;
extern struct Window *window;
extern SHORT mode;
SHORT submode;

SHORT gadgetid;
LONG quitcode;          /* set by the QUIT menu item handler */

SHORT prevMenu = -1;
long (*prevMenuHandler)() = NULL;

NoEvent()
{
  last_class = save_class;
  code = -1;
  class = -1;
  qualifier = -1;
}


HandleEvent()
{

  struct RastPort *rp;
  struct IntuiMessage *message;
  extern struct MenuItem *ItemAddress();

  long save_code;

  quitcode = FALSE;
  switch(class)
  {

  case MENUPICK:

more_menu_items:

    if (MENUNUM(code) != NOMENU && ITEMNUM(code) != NOITEM)
    {
      Address = ItemAddress(window -> MenuStrip,SHIFTMENU(MENUNUM(code)) |                                                  SHIFTITEM(ITEMNUM(code)));
      submode = SUBNUM(code);
      save_code = code;
      (*((struct XMenuItem *)Address)-> ItemHndlr)(NULL);
      code = save_code;
      Address = ItemAddress(window -> MenuStrip,code);
      if (Address && (code=Address -> NextSelect) != MENUNULL &&
          code != save_code)
       goto more_menu_items;
    }
    NoEvent();
    break;

  }

  return(quitcode);
}
