/********************************************************************
 *                   B O O L E A N    G A D G E T S                 *
 *                                                                  *
 * a short and simple demonstration of gadgets                      *
 *   by Tom Krotchko                                                *
 *                                                                  * 
 * note to Lattice users - I've just gotten a Lattice C compiler,   *
 * and unfortunately, this will not compile under Lattice. Please   * 
 * be patient while I learn the nuances of Lattice.                 * 
 *                                                                  * 
 ********************************************************************/
#include "exec/types.h"
#include "exec/exec.h"
#include "intuition/intuition.h"
#include "graphics/gfxbase.h"

/* Set up our Intuition structure                                        */
struct IntuitionBase *IntuitionBase;

/* A GfxBase structure for screen stuff                                  */
struct GfxBase *GfxBase;

/* The IntuiMessage structure lets us get messages from Intuition through
   the IDCMP                                                             */
struct IntuiMessage *message;

/* IntuiText structure puts text in a format for Intuition to use        */
struct IntuiText IText[] = {
 {2,1,JAM2,5,1,NULL,"TRUE",NULL },
 {2,1,JAM2,5,1,NULL,"FALSE",NULL},
 {2,1,JAM2,5,1,NULL,"First  Gadget Selected",NULL},
 {2,1,JAM2,5,1,NULL,"Second Gadget Selected",NULL}
};

/* All gadgets for a particular screen are formed into a linked list     */

/* The first gadget will be a TOGGLESELECT type BOOLEAN gadget           */
struct Gadget MyGadget[] = {
{
  &MyGadget[1],                 /* Point to the next gadget in the list  */
  230,80,40,10,                 /* LeftEdge, TopEdge, Width, Height      */
  GADGHCOMP,                    /* Flags - this one says to Complement   */
  TOGGLESELECT | GADGIMMEDIATE, /* Activation Flags - gadget attributes  */
  BOOLGADGET,                   /* GadgetType - BOOLEAN (yes or no)      */
  NULL,                         /* GadgetRender - for an Image or Border */
  NULL,                         /* SelectRender - for an Image or Border */
  &IText[0],                    /* Point to text for gadget              */
  NULL,                         /* MutualExclusion - ignored by 1.2      */
  NULL,                         /* SpecialInfo - for proportional gadgets*/
  0,                            /* GadgetId - for programmer use         */
  NULL                          /* UserData - pointer for programmer use */
},

/* The second gadget is like the first, except it isn't a TOGGLESELECT   */
{NULL,290,80,50,10,GADGHCOMP,GADGIMMEDIATE, BOOLGADGET,
  NULL,NULL,&IText[1],NULL,NULL,0,NULL} };

/* This is what our window will look like - notice, no sizing gadget     */
struct NewWindow NewWindow = {
  20, 20,
  600, 172,
  1, 2,
  GADGETDOWN | CLOSEWINDOW,
  WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | ACTIVATE,
  &MyGadget[0],
  NULL,
  "Gadgets",
  NULL,
  NULL,
  20, 20,
  321, 123,
  WBENCHSCREEN   /* no custom screen this time - just use the standard   */
};               /* WORKBENCH SCREEN                                     */

/* This structure is RETURNED from the OpenWindow                        */
struct Window *Window;

main()
{
 ULONG Signals, MIClass, MICode;
 APTR MIAddress;

 if (!(IntuitionBase = (struct IntuitionBase *)
      OpenLibrary ("intuition.library",LIBRARY_VERSION))) {
  exit(FALSE); }

 if (!(GfxBase = (struct GfxBase *)
      OpenLibrary("graphics.library", LIBRARY_VERSION))) {
   exit (FALSE);  }

 if (!(Window = (struct Window *) OpenWindow(&NewWindow))) {
   exit(FALSE); }

 for (;;) {
     Signals = Wait (1<<Window->UserPort->mp_SigBit);

     while(message = GetMsg(Window->UserPort)) {
         MIClass = message->Class;
         MICode  = message->Code;
         MIAddress = message->IAddress;
         ReplyMsg(message);

         switch (MIClass) {
           case CLOSEWINDOW:
             CloseWindow(Window);
             CloseLibrary(GfxBase);
             CloseLibrary(IntuitionBase);
             exit(TRUE);
             break;
           /***************************************************************/
           /* This is the new stuff - notice that MIAddress will have the */
           /* address of the gadget that was selected.                    */

           /* If the IDCMP flags are set differently, we will receive the */
           /* message when the mouse button is let up (GADGETUP) instead  */
           /* of pressed down. This is strictly a matter of style.        */

           /* PrintIText is a way to print text in an Intuition Window    */
           /***************************************************************/
           case GADGETDOWN:
             if (MIAddress == &MyGadget[0])
                PrintIText(Window->RPort,&IText[2],195L,100L);
             if (MIAddress == &MyGadget[1])    
                PrintIText(Window->RPort,&IText[3],195L,100L);
             break;
           default:
           /* The program should never get in here. All messages received */
           /* from Intuition should be expected.                          */
             printf("Wacky message received.\n");
             break;
         } /* switch */
     } /* while  */
 } /* for */
} /* main */
