/*
 *  filename: c.fastfont
 *  purpose:  front end for my turbo font lister module
 */

#include "baricon.h"
#include "coords.h"
#include "dbox.h"
#include "event.h"
#include "flex.h"
#include "menu.h"
#include "msgs.h"
#include "os.h"
#include "res.h"
#include "saveas.h"
#include "template.h"
#include "visdelay.h"
#include "werr.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "xferrecv.h"
#include "xfersend.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

/* wimp sprite area */
#define wimp_sprites            1

/* iconbar menu items */
#define iconbar_menu_info       1
#define iconbar_menu_quit       2

/* version number and date */
#define about_version_icon      3
#define version                 "1.00 (24-Nov-91)"
#define cat_file_version        0

/* icons within main window */
#define main_fontpath           0
#define main_make               1
#define main_clear              2
#define main_current            3

/* filetypes */
#define filetype_module         0xffa

/* limits */
#define max_name_length         212
#define max_fontpath_length     256

/* masks to pass to wimp_poll */
#define poll_mask               wimp_EMNULL | wimp_EMPTRENTER | wimp_EMPTRLEAVE

static menu             iconbar_menu ;
static wimp_w           main_handle ;
static BOOL             main_open ;
static char             *fontpath ;

/*****************************************************************************/
/* set font$path                                                             */
static void set_fontpath(char *value)
{
  wimpt_noerr(os_swi6(0x24, (int) "Font$Path", (int) value, strlen(value), 0,
                                                                       0, 0)) ;
                      /* OS_SetVarVal */
}

/*****************************************************************************/
/* time to create and save the module                                        */
BOOL saveproc(char *filename, void *handle)
{
  char command[212 + 30],
       previous_fontpath[max_fontpath_length] ;
  BOOL ok ;

  handle = handle ;

  strcpy(command, "Run <FastFont$Dir>.MakeModule ") ;
  strcat(command, filename) ;

  os_read_var_val("Font$Path", previous_fontpath, max_fontpath_length) ;
  set_fontpath(fontpath) ;

  visdelay_begin() ;
  ok = (wimpt_complain(wimp_starttask(command)) == 0) ;
  visdelay_end() ;

  set_fontpath(previous_fontpath) ;

  return ok ;
}

/*****************************************************************************/
/* time to make the module                                                   */
static void make_module(void)
{
  if (fontpath[0] == 0)
  {
    werr(0, msgs_lookup("nfp")) ;
  }
  else
  {
    saveas(filetype_module, "TurbFntLst", 3000, saveproc, 0, 0, 0) ;
  }
}

/*****************************************************************************/
/* set the caret to the end of the fontpath string                           */
static void set_caret_pos(void)
{
  wimp_caretstr caretstr ;

  caretstr.w      = main_handle ;
  caretstr.i      = main_fontpath ;
  caretstr.index  = strlen(fontpath) ;
  caretstr.height = -1 ;

  wimpt_noerr(wimp_set_caret_pos(&caretstr)) ;
}

/*****************************************************************************/
/* redraw the fontpath icon                                                  */
static void redraw_fontpath(void)
{
  wimpt_noerr(wimp_set_icon_state(main_handle, main_fontpath, 0, 0)) ;
  set_caret_pos() ;
}

/*****************************************************************************/
/* update the fontpath as a result of a dataload message                     */
/* the filetype MUST have been checked already                               */
static void update_fontpath(void)
{
  char *filename ;

  xferrecv_checkinsert(&filename) ;

  if (fontpath[0] == 0)
  {
    /* no need to check length as length of name can be a maximum of 212
       and fontpath is 256 characters long                               */
    strcpy(fontpath, filename) ;
    strcat(fontpath, ".") ;
  }
  else
  {
    if ((strlen(fontpath) + 2 + strlen(filename)) < max_fontpath_length)
    {
      strcat(fontpath, ",") ;
      strcat(fontpath, filename) ;
      strcat(fontpath, ".") ;
    }
    else
    {
      werr(0, msgs_lookup("ftl")) ;
      return ;
    }
  }

  xferrecv_insertfileok() ;

  redraw_fontpath() ;
}

/*****************************************************************************/
/* main window handler                                                       */
static void main_handler(wimp_eventstr *e, void *handle)
{
  handle = handle ;

  switch (e->e)
  {
   case wimp_EOPEN:
    wimpt_noerr(wimp_open_wind(&e->data.o)) ;
    break ;
   case wimp_ECLOSE:
    wimpt_noerr(wimp_close_wind(e->data.o.w)) ;
    win_register_event_handler(main_handle, 0, 0) ;
    main_open = FALSE ;
    break ;
   case wimp_EBUT:
    switch (e->data.but.m.i)
    {
     case main_make:
      make_module() ;
      break ;
     case main_clear:
      fontpath[0] = 0 ;
      redraw_fontpath() ;
      break ;
     case main_current:
      os_read_var_val("Font$Path", fontpath, max_fontpath_length) ;
      if (fontpath[0] == 0)
      {
        werr(0, msgs_lookup("fpu")) ;
      }
      redraw_fontpath() ;
      break ;
    }
    break ;
   case wimp_EKEY:
    if (e->data.key.chcode == 13)               /* return */
    {
      make_module() ;
    }
    else
    {
      wimp_processkey(e->data.key.chcode) ;
    }
    break ;
   case wimp_ESEND:
   case wimp_ESENDWANTACK:
    switch (e->data.msg.hdr.action)
    {
     case wimp_MDATALOAD:
      if ((e->data.msg.data.dataload.type <  0x1000) ||
          (e->data.msg.data.dataload.type == 0x3000))
      {
        werr(0, msgs_lookup("daa")) ;
      }
      else
      {
        update_fontpath() ;
      }
    }
    break ;
  }
}

/*****************************************************************************/
/* a click has occured on our iconbar icon                                   */
static void iconbar_click(wimp_i icon)
{
  icon = icon ;

  if (!main_open)
  {
    wimp_openstr openstr ;
    wimp_wind    *wind ;
    wimp_icon    icon ;

    wind = template_syshandle("main") ;

    if (wimpt_complain(wimp_create_wind(wind, &main_handle)))
    {
      return ;
    }                                     /* too many windows */

    openstr.w      = main_handle ;
    openstr.box    = wind->box ;
    openstr.x      = wind->scx ;
    openstr.y      = wind->scy ;
    openstr.behind = -1 ;

    wimpt_noerr(wimp_open_wind(&openstr)) ;

    win_register_event_handler(main_handle, main_handler, 0) ;

    wimp_get_icon_info(main_handle, main_fontpath, &icon) ;
    fontpath    = icon.data.indirecttext.buffer ;
    fontpath[0] = 0 ;

    main_open = TRUE ;
  }
  else
  {
    wimp_wstate wstate ;

    wimp_get_wind_state(main_handle, &wstate) ;

    wstate.o.behind = -1 ;

    wimpt_noerr(wimp_open_wind(&wstate.o)) ;
  }
  set_caret_pos() ;
}

/*****************************************************************************/
/* open our info window                                                      */
static void open_info(void)
{
  dbox d ;                              /* dialogue box handle */

  if (d = dbox_new("about"), d)
  {
    dbox_setfield(d, about_version_icon, version) ;
    dbox_show(d) ;
    dbox_fillin(d) ;
    dbox_dispose(&d) ;
  }
}

/*****************************************************************************/
/* an iconbar menu entry has been selected. process it                       */
static void iconbar_menuproc(void *handle, char *hit)
{
  handle = handle ;                     /* stop compiler warning */

  switch (hit[0])
  {
   case iconbar_menu_info:
    open_info() ;
    break ;
   case iconbar_menu_quit:
    exit(0) ;                           /* leave the program for good */
  }
}

/*****************************************************************************/
/* initialisation                                                            */
static BOOL initialise(void)
{
  wimpt_init("Fast Font") ;             /* main wimp initialisation */
  res_init("FastFont") ;                /* resources */
  template_init() ;                     /* templates */
  dbox_init() ;                         /* dialogue boxes */
  flex_init() ;                         /* dynamic memory allocation */
  msgs_init() ;                         /* messages */
  visdelay_init() ;                     /* visible delay */

  if (iconbar_menu = menu_new("Fast Font", msgs_lookup("BarMen")),
                                                          iconbar_menu == NULL)
  {
    return FALSE ;                      /* menu creation failed */
  }

  baricon("!FastFont", wimp_sprites, iconbar_click) ;

  if (!event_attachmenu(win_ICONBAR, iconbar_menu, iconbar_menuproc, 0))
  {
    return FALSE ;                      /* unable to attach menu */
  }

  event_setmask(poll_mask) ;            /* mask out messages we are not */
                                        /* interested in */
  return TRUE ;                         /* all went ok */
}

/*****************************************************************************/
/* main loop                                                                 */
int main()
{
  if (initialise())
  {
    while (TRUE)
    {
      event_process() ;
    }
  }
  return(0) ;
}
