/**************************************************************************
 *  $FILE:      ICTEST.C
 *  $DATE:      20 Giugno 1993
 *  $AUTHOR:    Andy Zanna
 * 
 * Questo programma e' un semplice test dei meccanismi di interconnessione
 * forniti dai Gadgets BOOPSI. Vengono creati un Gadget stringa ed un
 * Gadget proporzionale, che puo' aggiornare automaticamente il primo
 * grazie agli attributi ICA_TARGET e ICA_MAP. L'attributo PGA_TOP del
 * cursore e' mappato all'attributo STRINGA_LongVal del Gadget stringa.
 * 
 * Osservazioni:
 *  e' stata utilizzata, tanto per cambiare una tecnica di apertura
 *  della finestra e collegamento dei Gadgets diversa da quella finora
 *  utilizzata: I Gadgets sono stati passati direttamente a OpenWindowTags
 *  anziche' essere aggiunti in un secondo tempo, e la finestra e'
 *  stata aperta specificando le dimensioni INTERNE.
 *
 ***************************************************************************/

#include <exec/types.h>

#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/cghooks.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <intuition/icclass.h>

#include <utility/tagitem.h>

#include <clib/macros.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>


#if defined (__SASC)

#include <pragmas/exec_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/utility_pragmas.h>
#include <pragmas/dos_pragmas.h>

#endif

#include <string.h>



struct Library *IntuitionBase, *GfxBase, *UtilityBase, *DOSBase;



/* posizione Gadgets */
#define XPOS   40
#define YPOS   30
#define XOFF   80

#define GAD_W  60
#define GAD_H  12

/* posizione e dimensione finestra */
#define WIN_X  10
#define WIN_Y  10
#define WIN_W 320
#define WIN_H  60


struct Window   *win;
struct Gadget   *propgad, *string;
struct Gadget   *gad_list;


/*
 * Questa e' la mappa di interconnessione tra gli attributi
 * del gadget stringa e di quello proporzionale.
 */
struct TagItem prop2string[] =
{
    {PGA_Top, STRINGA_LongVal},
    {TAG_END,}
};


/*
 * Attende e gestisce un click sulla finestra.
 *
 * Ritorna  1 se si e' avuto un click normale
 * Ritorna -1 se si e' stato clickato il Gadget di chiusura.
 */

int wait_event (struct Window *w)
{
    struct MsgPort      *port;
    struct IntuiMessage *msg;
    int                  done;

    done = 0;
    port = w->UserPort;


    while (done == 0)
    {
        WaitPort (port);

        while (msg = (struct IntuiMessage *)GetMsg (port))
        {
            if (done == 0)
            {
                if (msg->Class == IDCMP_CLOSEWINDOW)
                    done = -1;
            }
            ReplyMsg ((struct Message *) msg);
        }
    }
    return done;
}



void main (void)
{

    /* Apertura librerie */
    IntuitionBase = OpenLibrary ("intuition.library", 37);
    if (IntuitionBase == NULL)
        goto cleanup;

    GfxBase = OpenLibrary ("graphics.library", 37);
    if (GfxBase == NULL)
        goto cleanup;

    UtilityBase = OpenLibrary ("utility.library", 37);
    if (UtilityBase == NULL)
        goto cleanup;


    string = (struct Gadget *)
        NewObject (NULL,
                   "strgclass",
                   GA_Top,         YPOS,
                   GA_Left,        XPOS,
                   GA_Width,       GAD_W,
                   GA_Height,      GAD_H,
                   GA_Previous,    &gad_list,

                   STRINGA_MaxChars, 15,
                   STRINGA_LongVal,  50,

                   TAG_END);

    if (string == NULL)
        goto cleanup;


    propgad = (struct Gadget *)
        NewObject (NULL,
                   "propgclass",

                   GA_Top,         YPOS,
                   GA_Left,        XPOS + XOFF,
                   GA_Width,       2 * GAD_W,
                   GA_Height,      GAD_H,

                   GA_Previous,    &gad_list,

                   PGA_FREEDOM,    FREEHORIZ,
                   PGA_Total,      100,
                   PGA_Visible,     25,
                   PGA_NewLook,     TRUE,

                   ICA_TARGET,     string,
                   ICA_MAP,        prop2string,
         
                   TAG_END);

    if (propgad == NULL)
        goto cleanup;


    /* Creazione finestra */
    win = OpenWindowTags(NULL,
                         WA_Left,       WIN_X,
                         WA_Top,        WIN_Y,
                         WA_InnerWidth, WIN_W,
                         WA_InnerHeight,WIN_H,
                         WA_AutoAdjust, TRUE,
                         WA_Title,      "Boopsi Test",
                         WA_DragBar,    TRUE,
                         WA_DepthGadget,TRUE,
                         WA_CloseGadget,TRUE,
                         WA_Gadgets,    gad_list,
                         WA_IDCMP,      IDCMP_GADGETUP | IDCMP_CLOSEWINDOW,
                         TAG_DONE);
    if (win == NULL)
        goto cleanup;


    /* Attende chiusura finestra */
    wait_event (win);


    /* Rilascia tutte le risorse utilizzate */
cleanup:

    if (win)
        CloseWindow (win);

    if (string)
        DisposeObject (string);

    if (propgad)
        DisposeObject (propgad);

    if (UtilityBase)
        CloseLibrary (UtilityBase);

    if (GfxBase)
        CloseLibrary (GfxBase);

    if (IntuitionBase)
        CloseLibrary (IntuitionBase);
}
