/* Example of GadTools Functions */

x = addlib("apig.library",0,-30,0)          /* make the lib avaiable */

call SET_APIG_GLOBALS()


scr          = LOCKPUBSCREEN("Workbench")   /* gonna put our window  */
                                            /* Workbench screen      */

scrvinfo     = GETVISUALINFO(scr)           /* NewGadgets need this  */

scrfont      = GETVALUE(scr,40,4,'p')       /* NewGadgets need this  */
                                            /* points to TextAttr    */

glistpointer = ALLOCVEC(4,MEMF_CLEAR)       /* a pointer for context */

conxgad      = CREATECONTEXT(glistpointer)  

previousgadget = conxgad

myid = 10


/* we now create and initialize a NewGadget structure */

newgadx = MAKENEWGADGET(scrvinfo,scrfont,65,32,145,42,"YOUR COLOR PALETTE",
                        ,PLACETEXT_ABOVE,myid,null())

/* we now create the gadget, a color PALETTE gadget */
/* remember we cannot have more than 15 parms       */

previousgadget = CREATEGADGET(PALETTE_KIND,previousgadget,newgadx,
                                 ,GTPA_DEPTH,3,
                                 ,GTPA_COLOR,3,
                                 ,GTPA_INDICATORWIDTH,20,
                                 ,GTPA_INDICATORHEIGHT,20,
                                 ,TAG_DONE,0)

wintitle = "APIG v3.1 GadTools PALETTE_KIND Gadget Example"
winidcmp = CLOSEWINDOW+GADGETUP+GADGETDOWN+MOUSEMOVE
winflags = WINDOWCLOSE+WINDOWDRAG+WINDOWSIZING+WINDOWDEPTH+GIMMEZEROZERO,
           +ACTIVATE

portname = "apiggadt4_port"

p = openport(portname)


/* open window, last parm in OPENWINDOW call points to our gadgets  */
w1  = OPENWINDOW(portname,0,30,640,200,2,4,winidcmp,winflags,wintitle,
                 ,scr,0,null(),null(),conxgad)

call GT_REFRESHWINDOW(w1,null())  /* need after opening window */

rpw1 = GETWINDOWRASTPORT(w1) 

exitme = 0

do while exitme = 0

     x = waitpkt(portname)
     do forever 
        msg = getpkt(portname)
        if msg = '0000 0000'x then leave

        msgclass  = getarg(msg,0)
        msgclass  = getarg(msg,0)
        msgcode   = getarg(msg,1)
        msgqual   = getarg(msg,2)
        msgmx     = getarg(msg,3)
        msgmy     = getarg(msg,4)
        msgsecs   = getarg(msg,5)
        msgmics   = getarg(msg,6)
        msgwinadr = getarg(msg,7)
        msggadadr = getarg(msg,8)
        msggadid  = getarg(msg,9)

        x = reply(msg,0)  
        /*
        say "class  =" msgclass
        say "code   =" msgcode 
        say "qual   =" msgqual 
        say "mx     =" msgmx   
        say "my     =" msgmy   
        say "secs   =" msgsecs 
        say "mics   =" msgmics 
        say "winadr =" c2x(msgwinadr)
        say "gadadr =" c2x(msggadadr)
        say "gadid  =" msggadid 
        */
        select
           when msgclass = CLOSEWINDOW then exitme = 1
           when msggadid = myid & msgclass = GADGETUP then 
                call pitext(rpw1,290,50,("You Selected Color" msgcode),1,2,JAM2,null())
           otherwise nop
        end
     end
end

call CLOSEWINDOW(w1)

call UNLOCKPUBSCREEN(null(),scr)

call FREEVISUALINFO(scrvinfo)

call FREEGADGETS(conxgad)

call FREETHIS(newgadx)

call FREEVEC(glistpointer)

exit

