/* menu test driver program */

#include "exec/types.h"
#include "intuition/intuition.h"
#include "graphics/display.h"
#include "libraries/dos.h"
#include "stdio.h"

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

extern struct Menu *MyMenu;

struct NewScreen NewScreen =
  {
  0,0,640,200,         /* Left, Top, Width, Height */
  2,0,1,               /* Depth, detail Pen, Block Pen*/
  HIRES,               /* Hi resolution screen */
  CUSTOMSCREEN,        /* type */
  NULL,                /* Font */
  "Custom Services"    /* Screen Title */
  };

struct NewWindow NewWindow =
  {
  0, 0, 640, 200,       /* Left edge, Top edge, width, height */
  0, 1,                   /* Width, Height */
  CLOSEWINDOW | MENUPICK, /* IDCMP message flags */
  WINDOWCLOSE | WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH |
  SMART_REFRESH | ACTIVATE | NOCAREREFRESH,
  NULL,                    /* First Gadget */
  NULL,                    /* default to checkmark */
  "Menu Test Window",      /* Title of window      */
  NULL,                    /* Standard Screen      */
  NULL,                    /* Bit Map              */
  100,  25,                /* Min Width, Min height*/
  640, 200,                /* Max width, Height    */
  CUSTOMSCREEN             /* Screen type          */
  };

  int Flgs[26];          /* one flag for each letter */

main(argc, argv)
  int argc;
  char **argv;
  {
  unsigned char str[30]; /* window message structure */
  unsigned int i;        /* loop counter */
  unsigned M0, I0, S0;   /* Menu control index */
  struct Screen *Screen; /* ptr to screen*/
  struct Window *Window; /* ptr to window*/
  struct IntuiMessage *message; /*message pointer */

  Do_Arguments(argc,argv);
  Delay(150);

/* open libraries, screen, and window */

  IntuitionBase = (struct IntuitionBase *)
          OpenLibrary("intuition.library",0);
  if( IntuitionBase == NULL )
    {
    printf("Unable to Open intuition.library");
    exit(10);
    };

  GfxBase = (struct GfxBase *)
          OpenLibrary("graphics.library",0);
  if( GfxBase == NULL )
    {
    printf("Unable to Open graphics.library");
    exit(10);
    };

  if( (Screen = (struct Screen *)OpenScreen(&NewScreen))==NULL)
    {
    printf("Unable to open Screen");
    exit(10);
    };

  NewWindow.Screen = Screen;

  if( (Window = (struct Window *)OpenWindow(&NewWindow))==NULL)
    {
    CloseScreen(Screen);
    printf("Unable to open window");
    exit(10);
    }

  SetMenuStrip(Window, MyMenu);

  Move(Window->RPort,20,20);         /* Move text pointer in window */
  Text(Window->RPort,"Hello Sir",9); /* set hello in the window */
  for (;;)  /* loop untill a Close Gadget is struck */
    {
    WaitPort(Window->UserPort);  /* wait for a message */
    message = (struct IntuiMessage *)GetMsg(Window->UserPort);
    if( (message->Class) == MENUPICK ) /* is it a menu message?*/
      {
      for (i=0; i < 30; str[i++] = ' ');
      if( (message->Code) != MENUNULL)
        {
        M0 = MENUNUM(message->Code);
        I0 = ITEMNUM(message->Code);
        S0 = SUBNUM(message->Code);
        if( Flgs[3] )
          {
          sprintf(str,"Menu %x, Item %x, Sitem  %x ",
                          M0, I0, S0);
          Move(Window->RPort, 5, 20);
          Text(Window->RPort, str, 24);
          }
        else
          {
          Process_Item(M0, I0, S0);
          };
        }
      else if ( Flgs[3] )
        {
        sprintf(str,"No Item Selected.       ");
        Move(Window->RPort, 5, 20);
        Text(Window->RPort, str, 24);
        };
      ReplyMsg(message);
      }
    else
      {
      if ( !Flgs[3] )
        {
        M0 = -1;
        I0 = 0;
        S0 = 0;
        Process_Item(M0, I0, S0);
        };
      break; /* Not Menu selection, must be close gadget */
      };
    };

  ReplyMsg(message);         /* Reply to message */
  ClearMenuStrip(Window);    /* Remove Menu */
  CloseWindow(Window);       /* close window */
  CloseScreen(Screen);       /* remove the screen */
  exit(TRUE);                /* Good Bye */
  }

Do_Arguments(count,strings)
  int count;
  char *strings[];
  {
  register int loop;
  register char *ptr;
  int  c;

  if ( count == 2 && *strings[1] == '?' )
    {
    printf(" Usage is: %s [-option ... -option]\n",strings[0]);
    }
  else
    {
    for(loop=0; loop < 26; loop++) Flgs[loop] = FALSE;
    while ( --count > 0 )                       
      {
      ptr = *++strings;
      if ( *ptr++ == '-' )
        {
        c = toupper(*ptr) - 'A' ;  /* get an index 0 to 25 */
        if ( c >= 0 && c <= 25 )
          {
          Flgs[c] = TRUE;
          }
        else
          {
          printf(" %s has an invalid option: %s\n",strings[0],ptr);
          };
        }
      else
        {
        printf(" irregular switch[%d] value:%s\n",count,ptr);
        };
      };
    };
  }
