

/*  Select.gadget test 2 (25.5.98)  */
/*  Written for SAS/C               */
/*  Compile: SC LINK SGCustomTest   */
/*  © 1998 Massimo Tantignone       */


#include "exec/types.h"
#include "dos/dos.h"
#include "intuition/intuition.h"
#include "intuition/gadgetclass.h"
#include "libraries/gadtools.h"
#include "proto/intuition.h"
#include "proto/gadtools.h"
#include "proto/exec.h"

#include <gadgets/select.h>
#include <clib/selectgadget_protos.h>
#include <pragmas/selectgadget_pragmas.h>


/* The library base for the "select.gadget" class library */

struct Library *SelectGadgetBase;


ULONG main(void)
{
   /* The usual stuff */

   struct Screen *scr;
   struct Window *win;
   struct IntuiMessage *imsg;
   struct DrawInfo *dri;
   APTR vi;
   ULONG class, code, fine = FALSE;
   ULONG width = 640, height = 200;
   ULONG one = FALSE, two = FALSE;
   struct NewGadget ng;
   struct Gadget *gad1, *gad2, *glist = NULL;
   struct Gadget *iaddress;
   STRPTR labels1[] = { "First option",
                        "Second option",
                        "Third option",
                        "Fourth option",
                        NULL };
   STRPTR labels2[] = { "This is a",
                        "GadTools gadget",
                        "which was made",
                        "pop-up",
                        "by the support",
                        "functions of",
                        "the select.gadget",
                        "library.",
                        NULL };

   /* Let's try to open the "select.gadget" library any way we can */

   SelectGadgetBase = OpenLibrary("select.gadget",40L);

   if (!SelectGadgetBase)
      SelectGadgetBase = OpenLibrary("Gadgets/select.gadget",40L);

   if (!SelectGadgetBase)
      SelectGadgetBase = OpenLibrary("Classes/Gadgets/select.gadget",40L);

   /* Really not found? Then quit (and complain a bit) */

   if (!SelectGadgetBase) return (RETURN_FAIL);

   /* Inquire about the real screen size */

   if (scr = LockPubScreen(NULL))
   {
      width = scr->Width;
      height = scr->Height;
      UnlockPubScreen(NULL,scr);
   }

   /* Open a window on the default public screen */

   if (win = OpenWindowTags(NULL,WA_Left,(width - 500) / 2,
                                 WA_Top,(height - 160) / 2,
                                 WA_Width,500,WA_Height,160,
                                 WA_MinWidth,100,WA_MinHeight,100,
                                 WA_MaxWidth,-1,WA_MaxHeight,-1,
                                 WA_CloseGadget,TRUE,
                                 WA_SizeGadget,TRUE,
                                 WA_DepthGadget,TRUE,
                                 WA_DragBar,TRUE,
                                 WA_SimpleRefresh,TRUE,
                                 WA_Activate,TRUE,
                                 WA_Title,"select.gadget custom gadget test",
                                 WA_IDCMP,IDCMP_CLOSEWINDOW |
                                          IDCMP_GADGETUP |
                                          IDCMP_REFRESHWINDOW,
                                 TAG_END))
   {
      /* Get the screen's DrawInfo, it will be useful... */

      if (dri = GetScreenDrawInfo(win->WScreen))
      {
         /* Same for the VisualInfo */

         if (vi = GetVisualInfoA(win->WScreen,NULL))
         {
            /* Create two gadgets, the GadTools way */

            glist = CreateContext(&glist);

            /* The width isn't very accurate, but this is just an example */

            ng.ng_LeftEdge = 40;
            ng.ng_TopEdge = win->BorderTop + 40;
            ng.ng_Width = win->WScreen->RastPort.Font->tf_XSize * 18 + 30;
            ng.ng_Height = win->WScreen->Font->ta_YSize + 6;
            ng.ng_GadgetText = "G_adTools 1";
            ng.ng_TextAttr = win->WScreen->Font;
            ng.ng_GadgetID = 1;
            ng.ng_Flags = 0L;
            ng.ng_VisualInfo = vi;

            gad1 = CreateGadget(GENERIC_KIND,glist,&ng,GT_Underscore,'_',TAG_END);

            ng.ng_LeftEdge = win->Width - 40 - ng.ng_Width;
            ng.ng_TopEdge += 40;
            ng.ng_GadgetText = "Ga_dTools 2";
            ng.ng_GadgetID = 2;

            gad2 = CreateGadget(GENERIC_KIND,gad1,&ng,GT_Underscore,'_',TAG_END);

            /* If all went ok, transform the gadgets and use them */

            if (gad2)
            {
               one = InitSelectGadget(gad1,0L,GA_DrawInfo,dri,
                                              SGA_TextPlace,PLACETEXT_RIGHT,
                                              SGA_Labels,labels1,
                                              SGA_DropShadow,TRUE,
                                              SGA_FollowMode,SGFM_KEEP,
                                              TAG_END);

               two = InitSelectGadget(gad2,0L,GA_DrawInfo,dri,
                                              SGA_TextPlace,PLACETEXT_LEFT,
                                              SGA_Labels,labels2,
                                              SGA_Active,3,
                                              SGA_ItemSpacing,2,
                                              SGA_PopUpPos,SGPOS_BELOW,
                                              SGA_SymbolWidth,-21,
                                              TAG_END);

               /* Add the gadgets to the window and display them */

               AddGList(win,glist,-1,-1,NULL);
               RefreshGList(glist,win,NULL,-1);
               GT_RefreshWindow(win,NULL);
            }

            /* Now let's handle the events until the window gets closed */

            while (!fine)
            {
               Wait(1 << win->UserPort->mp_SigBit);

               while (imsg = GT_GetIMsg(win->UserPort))
               {
                  class = imsg->Class;
                  code = imsg->Code;
                  iaddress = imsg->IAddress;
                  GT_ReplyIMsg(imsg);

                  if (class == IDCMP_CLOSEWINDOW) fine = TRUE;

                  if (class == IDCMP_GADGETUP) printf("Gadget: %ld, Item: %ld\n",
                                                      iaddress->GadgetID,
                                                      code);

                  if (class == IDCMP_REFRESHWINDOW)
                  {
                     GT_BeginRefresh(win);
                     GT_EndRefresh(win,TRUE);
                  }
               }
            }

            /* If the gadgets were added, remove them */

            if (gad2)
            {
               RemoveGList(win,glist,-1);
            }

            /* Strip the gadgets of additional "select" information */

            if (one) ClearSelectGadget(gad1);
            if (two) ClearSelectGadget(gad2);

            /* Dispose the gadgets; FreeGadgets() ignores NULL arguments */

            FreeGadgets(glist);

            /* Free the VisualInfo */

            FreeVisualInfo(vi);
         }

         /* Release the DrawInfo structure */

         FreeScreenDrawInfo(win->WScreen,dri);
      }

      /* Say good-bye to the window... */

      CloseWindow(win);
   }

   /* ... and to the library */

   CloseLibrary(SelectGadgetBase);

   /* We did our job, now let's go home :-) */

   return (RETURN_OK);
}


