#include <portab.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <aes.h>
#include <vdi.h>
#include <tos.h>

#define SCREEN 1
#define MONO   1

#define DIALOG 0
#define WINDOW 1

int cdecl fontsel_input(int handle, int ncount, int ftype, char *text, int *id, int *size);

int work_in[103],work_out[57];
int aeshandle,vdihandle;
char alertstr[256];

typedef struct
{
   unsigned long  id;      /* UFSL ID (UFSL)       */
   unsigned int   version; /* version (BCD-Format) */
   int dialtyp;            /* 0=Dialog, 1=Fenster  */
   int cdecl (*fontsel_init)(void);
   int cdecl (*fontsel_input)(int vdihandle, /* klar */
                              int n_fonts,   /* Gesamtzahl der Fonts */
                              int ftype,     /* 1=nur monospaced, 0=egal */
                              char *text,    /* Benutzerdefinierter Text */
                              int *fretid,   /* eingestellte FontId */
                              int *fretsize  /* eingestellte Fontgr”že */
                              );
   OBJECT *helpbutton;                       /* Typ: BOXTEXT */
   int cdecl (*helpfunc)(void);              /* Benutzerdefinierte Helpfkt. */
   char *examplestr;                         /* Beispielstring */
} UFSL;

/* ----- Cookie Jar -------------------------------------------------------- */

typedef struct
{
   long  id,
      *ptr;
} COOKJAR;

/* ------------------------------------------------------------------------- */
/* ----- get_cookie -------------------------------------------------------- */
/* ------------------------------------------------------------------------- */

static long *get_cookie(long cookie)
{
   long  sav;
   COOKJAR  *cookiejar;
   int   i = 0;

   sav = Super((void *)1L);
   if(sav == 0L)
      sav = Super(0L);
   cookiejar = *((COOKJAR **)0x05a0l);
   if(sav != -1L)
      Super((void *)sav);
   if(cookiejar)
   {
      while(cookiejar[i].id)
      {
         if(cookiejar[i].id == cookie)
            return(cookiejar[i].ptr);
         i++;
      }
   }
   return(0l);
}

int open_work (int device)
{
   register int i;
   int handle;

   for (i = 0; i < 103; i++) work_in [i] = 1;
   work_in [0]  = device;                         /* device handle */
   work_in [10] = 2;                         /* Raster Koordinaten */

   if (device == SCREEN)
   {
     handle=aeshandle;
     v_opnvwk (work_in, &handle, work_out);     /* virtuell ”ffnen */
   }
   else                                        /* nicht Bildschirm */
   {
     v_opnwk (work_in, &handle, work_out);  /* physikalisch ”ffnen */
   }
   return (handle);
}

void close_work (int handle, int device)
{
  switch (device)
  {
    case SCREEN:
       v_clsvwk (handle);
       break;
    default:
       v_clswk (handle);
       break;
  }
}

int cdecl helpfunc(OBJECT *tree)
{
   form_alert(1,"[0][|Hilfstext][  OK  ]");
}

main()
{
   int ret,id,size,n_fonts,done=FALSE;
   UFSL *ufsl;

   appl_init();
   graf_mouse(ARROW,NULL);
   aeshandle=graf_handle(&ret,&ret,&ret,&ret);
   vdihandle=open_work(SCREEN);

   vq_extnd(vdihandle,0,work_out);

   if(vq_gdos())
      n_fonts=vst_load_fonts(vdihandle,0)+work_out[10];
   else
      n_fonts=work_out[10]; /* Nur 6x6 system font */
   ufsl=(UFSL *)get_cookie('UFSL');
   if(ufsl)
   {
      ufsl->fontsel_init();
      ufsl->dialtyp=DIALOG;
      ufsl->helpbutton->ob_width=7*(strlen("HILFE")+3);
      ufsl->helpbutton->ob_flags=SELECTABLE|EXIT;
      ufsl->helpbutton->ob_state=OUTLINED|SHADOWED;
      ufsl->helpbutton->ob_spec.tedinfo->te_font=SMALL;
      strcpy(ufsl->helpbutton->ob_spec.tedinfo->te_ptext,"HILFE");
      ufsl->helpfunc=helpfunc;
      do
      {
         switch(ufsl->fontsel_input(vdihandle,n_fonts,0,"Bitte Font ausw„hlen",&id,&size))
         {
            case TRUE:
               sprintf(alertstr,"[1][|FontId: %d, Fontgr”že: %dpt.][  OK  ]",id,size);
               form_alert(1,alertstr);
               vst_font(vdihandle,id);
               vst_point(vdihandle,size,&ret,&ret,&ret,&ret);
               break;
            case FALSE:
               form_alert(1,"[1][|Abbruch gew„hlt. ][  OK  ]");
               done=TRUE;
               break;
            case -1:
               form_alert(1,"[3][|Out of Memory! ][  OK  ]");
               done=TRUE;
               break;
            case -2:
               form_alert(1,"[3][|Mehrfachaufruf nicht m”glich! ][  OK  ]");
               done=TRUE;
               break;
         }
      }
      while(!done);
   }
   else
      form_alert(1,"[1][|Kein gltiger UFSL-Cookie! ][  OK  ]");
   if(vq_gdos())
      vst_unload_fonts(vdihandle,0);
   close_work(vdihandle,SCREEN);
   appl_exit();
}

