/* Esempio uso gadgets */

#include <intuition/intuition.h>

#ifdef LATTICE
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/exec.h>

#else
#include <functions.h>
#endif

#define GNOM "graphics.library"
#define INOM "intuition.library"
#define BUFL 40

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Window *wind;

SHORT BorderVectors1[] = { 0,0,136,0,136,20, 0,20, 0,0 };
SHORT BorderVectors2[] = { 0,0, 52,0, 52,21, 0,21, 0,0 };
SHORT BorderVectors3[] = { 0,0,184,0,184, 9, 0, 9, 0,0 };
 
UBYTE stringadgetbuff[ BUFL ], stringadgetundobuff[ BUFL ];

struct PropInfo gadgetpropinfo = {
 AUTOKNOB+FREEHORIZ, -1,-1, 0,0,
 0,0, 0,0, 0,0 
};
struct Image Image1 = {
 0, 0, 176, 10, 1, NULL, 0x0000, 0x0000, NULL
};

struct Border Border1 = {
 -1, -1, 3, 0, JAM1, 5, BorderVectors1, NULL };

struct Border Border2 = {
 -1, -1, 3, 0, JAM1, 5, BorderVectors2, NULL };

struct Border Border3 = {
 -1, -1, 3, 0, JAM1, 5, BorderVectors3, NULL };

struct IntuiText IText1 = {
 3, 2, JAM2, 41, 7, NULL, (UBYTE *) "Annulla!", NULL };

struct IntuiText IText2 = {
 3, 2, JAM2, 14, 6, NULL, (UBYTE *) "OK!", NULL };

struct Gadget gadgetprop = {
 NULL, 88, 44, 184, 14, GADGHCOMP,
 RELVERIFY, PROPGADGET,
 (APTR) &Image1, NULL, NULL, NULL,
 (APTR) &gadgetpropinfo, NULL, NULL
};

struct Gadget gadgetannulla = {
 &gadgetprop, 195, 66, 135, 19, NULL,
 RELVERIFY, BOOLGADGET, (APTR) &Border1,
 NULL, &IText1, NULL, NULL, NULL, NULL
};

struct Gadget gadgetok = {
 &gadgetannulla, 14, 66, 51, 20, NULL,
 RELVERIFY, BOOLGADGET, (APTR) &Border2,
 NULL, &IText2, NULL, NULL, NULL, NULL
};

struct StringInfo stringadgetinfo = {
 &stringadgetbuff[ 0 ], &stringadgetundobuff[ 0 ],
 0, BUFL, 0, 0, 0, 0, 0, 0, 0, 0, NULL
};
struct Gadget gadgetstringa = {
 &gadgetok, 89, 19, 183, 14, NULL,
 RELVERIFY + STRINGCENTER,
 STRGADGET, (APTR) &Border3,
 NULL, NULL, NULL, (APTR) &stringadgetinfo,
 NULL, NULL
};

struct NewWindow NewWindow1 = {
 144,40, 346,91, 0,1,
 GADGETUP | CLOSEWINDOW | ACTIVEWINDOW,
 WINDOWCLOSE | ACTIVATE | NOCAREREFRESH,
 &gadgetstringa, NULL, (UBYTE *) "Gadgets",
 NULL, NULL, 0, 0, 0, 0, WBENCHSCREEN
};

void die( errore )
int errore;
{
 if ( wind ) CloseWindow( wind );
 if ( IntuitionBase ) CloseLibrary( IntuitionBase );
 if ( GfxBase ) CloseLibrary( GfxBase );
 _exit( errore );
}

void main()
{
 struct IntuiMessage *msg;
 APTR iadr; /* Indirizzo del gadget selezionato */
 ULONG tmes; /* Tipo di messaggio di IDCMP */
 UBYTE potas[6]; /* Buffer per valore propgadget */
 int l;  /* Lunghezza stringa convertita */

 IntuitionBase=(struct IntuitionBase *)OpenLibrary(INOM,33L);
 if ( IntuitionBase == NULL ) die( 10 );

 GfxBase = (struct GfxBase *) OpenLibrary( GNOM, 33L );
 if ( GfxBase == NULL ) die( 11 );

 NewWindow1.FirstGadget = &gadgetstringa;

 wind = (struct Window *) OpenWindow( &NewWindow1 );
 if ( wind == NULL ) die( 12 );
 ActivateGadget( &gadgetstringa, wind, NULL );
 for ( ;; )
   {
   Wait( 1L << wind -> UserPort -> mp_SigBit );
   while((msg=(struct IntuiMessage *)GetMsg(wind->UserPort)))
     {
     iadr = msg -> IAddress;
     tmes = msg -> Class;
     ReplyMsg( (struct Message *) msg );
     switch ( tmes )
       {
       case GADGETUP:
         if ( iadr == (APTR) &gadgetok )
           {
           printf( "Click sul gadget OK!\n" );
           printf( "Buffer = %s\n", stringadgetbuff );
           die( 0 );
           }
         else if ( iadr == (APTR) &gadgetannulla )
           {
           printf( "Click sul gadget annulla!\n" );
           printf( "Buffer = %s\n", stringadgetbuff );
           die( 0 );
           }
         else if ( iadr == (APTR) &gadgetprop )
           {
           l=sprintf( potas, "%-5d", gadgetpropinfo.HorizPot );
           Move( wind -> RPort, 155, 40 );
           SetDrMd( wind -> RPort, JAM2 | INVERSVID );
           Text( wind -> RPort, potas, l );
           break;
           }
         break;
       case CLOSEWINDOW:
       printf( "Chiusura finestra!\n" );
       die( 0 );
       break;
     } /* fine switch() */
  } /* fine while() */
 } /* fine for (;;) */
}
