/* TestMenu.c V1.0 (14-oct-90) - by Hans Jansen
 *
 * Program to exercise the output from MakeMenu V1.0, enabling you to
 * quickly prototype a menu for your own application program.
 *
 * This program was adapted from the RKM example Menus.c (from chapter
 * Intuition:Menus, page 125-133; also published on Fish Disk #344, the
 * "Libs&DevsCompanion"). The original program carried the following
 * copyright statement:
 */
 
/* Copyright (c) 1990 Commodore-Amiga, Inc.
 *
 * This example is provided in electronic form by Commodore-Amiga, Inc. for
 * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
 * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
 * information on the correct usage of the techniques and operating system
 * functions presented in this example.  The source and executable code of
 * this example may only be distributed in free electronic form, via bulletin
 * board or as part of a fully non-commercial and freely redistributable
 * diskette.  Both the source and executable code (including comments) must
 * be included, without modification, in any copy.  This example may not be
 * published in printed form or distributed with any commercial product.
 * However, the programming techniques and support routines set forth in
 * this example may be used in the development of original executable
 * software products for Commodore Amiga computers.
 * All other rights reserved.
 * This example is provided "as-is" and is subject to change; no warranties
 * are made.  All use is at your own risk.  No liability or responsibility
 * is assumed.
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <libraries/dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Use lowest non-obsolete version that supplies the functions you need. */
#define LIB_REV 33

/* prototypes */
UBYTE    handleIDCMP(struct Window *);
VOID     OpenAll(VOID);
VOID     cleanExit(int);

/* Globals */
struct   IntuitionBase *IntuitionBase = NULL;
struct   GfxBase       *GfxBase = NULL; 
struct   Window *window = NULL;

struct IntuiText WinText =
       {3, 0, JAM2, 20, 20, NULL, "Activate MenuItem or CloseGadget",NULL};

struct NewWindow NewWindow = {
    0, 10, 640, 100, 2, 1, MENUPICK|CLOSEWINDOW,
    WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|ACTIVATE|NOCAREREFRESH,
    NULL, NULL, "TestMenu V1.0 (12-oct-90) - by Hans Jansen",
    NULL, NULL, 0, 0, -1, -1, WBENCHSCREEN
    };

extern struct Menu *MyMenu;

VOID main (int argc, char *argv[])
{
/* Declare variables here */
ULONG  signalmask, signals;
UBYTE  done = 0;

OpenAll();

/* Set up the signals that you want to hear about ... */
signalmask = 1L << window->UserPort->mp_SigBit;

/* And wait to hear from your signals */      
while( !done )
   {
   signals = Wait(signalmask);    
   if(signals & signalmask)
      done = handleIDCMP(window);
   };

/* Exit the program */
cleanExit(RETURN_OK);
}


/* Handle the IDCMP messages */
UBYTE handleIDCMP( struct Window *win )
{
UBYTE  flag = 0;
USHORT code, selection, flags;
struct IntuiMessage *message = NULL;
ULONG  class, menuNum, itemNum, subNum;

/* Examine pending messages */
while(message = (struct IntuiMessage *)GetMsg(win->UserPort))
   {
   class = message->Class;
   code = message->Code;
 
/* When we're through with a message, reply */
   ReplyMsg((struct Message *)message);
   
/* See what events occurred */
   switch( class )
      {
      case CLOSEWINDOW:
         flag = 1;
         break;
      case MENUPICK:
         selection = code;
         while(selection != -1) /* MENUNULL does not work for PDC V3.33... */
            {
/*	(uncomment this to see why...)
	    printf ("selection = %x, MENUNULL = %x\n", selection, MENUNULL);
*/
            menuNum = MENUNUM(selection);
            itemNum = ITEMNUM(selection);
            subNum  = SUBNUM(selection);
            flags = ((struct MenuItem *)
                    ItemAddress(MyMenu,(LONG)selection))->Flags;
	    /* Show the selection (in the invoking CLI window!) */
            printf("Selected menu %d, item %d", menuNum, itemNum);
	    if (subNum != NOSUB) printf (", subitem %d", subNum);
            if(flags&CHECKED) printf(" (Checked) ");
	    printf ("\n");
	    /* See if there are more selections */
            selection = ((struct MenuItem *)ItemAddress
              (MyMenu,(LONG)selection))->NextSelect;
         } /* end while */
         break; /* case of MENUPICK */
      default:
         break;
      } /* end switch */
   } /* end while */
return(flag);
}


/* Open the needed libraries, windows, etc. */
VOID OpenAll (VOID)
{
/* Open the Intuition Library */
IntuitionBase = (struct IntuitionBase *)
    OpenLibrary( "intuition.library",LIB_REV);
if(IntuitionBase == NULL)
    cleanExit(RETURN_WARN);

/* Open the Graphics Library */
GfxBase = (struct GfxBase *)
    OpenLibrary("graphics.library", LIB_REV);
if(GfxBase == NULL)
    cleanExit(RETURN_WARN);

/* Open the window */
window = OpenWindow(&NewWindow);
if(window == NULL)
    cleanExit(RETURN_WARN);

/* Give a brief explanation of the program */
PrintIText(window->RPort,&WinText,0,0);

/* attach the menu to the window */
SetMenuStrip(window, MyMenu);
}


/* Free up all the resources that we where using */
VOID cleanExit (int returnValue)
{
if(window)
   {
/* If there is a menu strip, then remove it */
   if(window->MenuStrip)
      ClearMenuStrip(window);

/* Close the window */
   CloseWindow(window);
   }

/* Close the library, and then exit */
if(GfxBase)
   CloseLibrary((struct Library *)GfxBase);

if(IntuitionBase)
   CloseLibrary((struct Library *)IntuitionBase);
exit(returnValue);
}
