/*************************************************************************
 *           DISKFONT.C
 *
 * Programma di esempio sull' uso dei diskfonts da C.
 * Leggere l'articolo dedicato per dettagli sulle funzioni
 * e sulla tecnica di programmazione utilizzata.
 *
 ************************************************************************/

/* Inclusione delle definizioni di sistema necessarie */
#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>

/* Puntatori globali necessari per le librerie */ 
struct Library *DiskfontBase;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

/* Questa struttura verra' riempita con i dati del font desiderato */
struct TextAttr MyFont =
   {
   NULL,            /* Nome del font */
   0,               /* Altezza */
   FS_NORMAL,       /* Stile */
   FPF_DISKFONT     /* Preferenze */
   };

/* Questa e' una finestra che useremo per le prove dei fonts */
struct NewWindow NewWindow =
   {
   0,0,                         /* Angolo sup sinistro */
   640,90,                      /* Larghezza, altezza */
   0,1,                         /* Penne */
   NULL,                        /* IDCMP Flags */
   NOCAREREFRESH | WINDOWDEPTH, /*flags */
   NULL,                        /* User gadget */
   NULL,                        /* Checkmark */
   NULL,                        /* Titolo */
   NULL,                        /* Schermo */
   NULL,                        /* BitMap */
   0,0,                         /* Dimensioni minime */
   0,0,                         /* Dimensioni massime */
   WBENCHSCREEN                 /* tipo di schermo */
   };

/* Buffers per i comandi */
char nome[80];
char alt[80];
 
/**** ChangeFont() ****************************************************/

/*
 Questa funzione prende gli attributi di un font, cerca di aprirlo e,
 se c'e' riuscita, lo sostituisce al font corrente della RastPort
 Ritorna TRUE (tutto bene) o FALSE (nobbuono).
 */

BOOL ChangeFont (rp,nome,altezza,stile)
    struct RastPort *rp;
    char *nome;
    UWORD   altezza,stile;
{

    struct TextFont *tf;

    MyFont.ta_Name = nome;
    MyFont.ta_YSize = altezza;
    MyFont.ta_Style = stile;

    /* Cerca di aprire il font richiesto */
    tf = (struct TextFont *) OpenDiskFont (&MyFont);
    if (tf==NULL) return (FALSE);

    /*
     Abbiamo aperto con successo il font, ora
     possiamo sostituirlo a quello presente nella
     RastPort della Finestra.
     */

    CloseFont (rp->Font);
    SetFont (rp, tf);
    return (TRUE);

} /* end ChangeFont */

/**** Programma principale ******************************************/
main ()

{
    /* Un paio di flags booleani */
    BOOL finito = FALSE;
    BOOL ok;

    /* Altri puntatori locali */
    struct Window *Window;
    struct RastPort *rp;


    /* Apertura delle risorse utilizzate dal programma */

    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",0);
    if (IntuitionBase == NULL)
        {
        printf("Niente Intuition.library!\n");
        goto cleanup;
        }

    GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 0);
    if (GfxBase == NULL)
        {
        printf("Niente Graphics.library!\n");
        goto cleanup;
        }

    DiskfontBase = (struct Library *) OpenLibrary ("diskfont.library",0);
    if (DiskfontBase == NULL)
        {
        printf("Niente Diskfont.library!\n");
        goto cleanup;
        }

    Window = (struct Window *) OpenWindow (&NewWindow);
    if (Window == NULL)
        {
        printf("Niente finestra!\n");
        goto cleanup;
        }

    rp = Window->RPort;

    /* Messaggi di benvenuto.. */

    printf ("\nProgramma dimostrativo sull'uso dei diskfonts da C\n");
    printf ("Leggere l'articolo relativo per maggiori dettagli\n");
    printf ("Batti su due linee il nome del font \n");
    printf ("e l'altezza dei caratteri desiderati\n");
    printf ("\nEsempio:\nNome Font : sapphire.font\n");
    printf ("Altezza : 14\n");
    printf ("\nBatti 'fine' per terminare\n\n");

    /* Loop principale del programma */

    while (!finito)
        {
        /* Prompt per un comando */
        printf ("\nBatti il nome del font da caricare\n");
        printf ("Batti 'fine' per terminare.\n");
        printf ("\nNome Font : ");
        gets (nome);

        /* Vogliamo finire ? */
        if ((strcmp (nome,"fine"))==0)
            {
            printf ("ok..bye bye!\n\n");
            finito = TRUE;
            }

        /* Battuto il nome di un font */
        else    {
            /* ora chiedi l'altezza */
            printf ("Altezza : ");
            gets (alt);

            /* Cerca di aprire il font richiesto */
            ok = ChangeFont (rp, nome, atoi(alt), 0);
            if (!ok)
                {
                printf ("Non trovo %s %s\n",nome,alt);

                }

            /* Abbiamo sostituito il font. */
            else    {
                /* Pulisci la finestra */
                SetRast (rp,0);
                RefreshWindowFrame (Window);
                Move (rp,10,50);

                /* Messaggio di esempio */
                Text (rp,"Questo e' il font ",18);
                Text (rp, nome, strlen (nome));
                Text (rp, ", ",2);
                Text (rp, alt, strlen(alt));
                }

            } /* end else */
        } /* end while */

cleanup:
    /* Rimette a posto il font prima di uscire */
    ChangeFont (rp,"topaz.font",8,0);

    /* Chiude tutte le risorse aperte dal programma */
    if (Window) CloseWindow (Window);
    if (GfxBase) CloseLibrary (GfxBase);
    if (DiskfontBase) CloseLibrary (DiskfontBase);
    if (IntuitionBase) CloseLibrary (IntuitionBase);
    
}   /* end main */
