head     1.2;
branch   ;
access   ;
symbols  ;
locks    gtoal:1.2;
comment  @ * @;


1.2
date     91.03.02.13.59.17;  author gtoal;  state Exp;
branches ;
next     1.1;

1.1
date     91.03.02.13.55.51;  author gtoal;  state Exp;
branches ;
next     ;


desc
@Initial release
@


1.2
log
@IAY's upgrade
@
text
@/* main.c --- main of Preview.  */

#include "d2rd.h"
#include "menus.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "output.h"
#include "kernel.h"
#include "template.h"
#include "resspr.h"
#include "res.h"
#include "font.h"
#include "bbc.h"
#include "colourtran.h"
#include "flex.h"
#include "visdelay.h"

struct dvi_file *dvi_files = NULL;
struct display *windows;

os_error *err;
wimp_i iconbar_icon;
wimp_w proginfo, saveas;


/* The filetype of the file to be saved.  This is set according to the icon
   type of the user_drag from the saveas window.  */

int saveas_filetype;


/* The buffer where the input of the SAVEAS_FILENAME icon is put.  */

char *saveas_buffer;


/* The MY_REF field as put in the message header by the wimp when sending the
   DATASAVE message.  */

int msg_ref;


/* To keep things extremely simple, all windows display a sprite which has
   the same mode as the current screenmode.  Hence, on a mode-change every
   sprite needs to be redefined.  And we always need to know the current mode.  */

int vdu_mode;

/* Record where the menu was opened, so it can be used to influence the
   reopening of a window after choosing a new magnification.  */

wimp_mousestr menu_pos;

/* To have ourselves in the executable image.  */

char copyright_notice[] =
"This is !Preview\nCopyright 1990 Graham Toal and Pieter Schoenmakers\n";


/* This function is called at exit.  */

void
cleanup (void)
{
  if (loading_a_file)
    visdelay_end ();
  wimp_taskclose (wimpt_task ());
}



void
set_saveas_state (char *file_name, char *sprite_name)
{
  wimp_icreate ic;
  int i;

  /* Change the file icon.  */

  wimpt_noerr (wimp_get_icon_info (saveas, SAVEAS_FILEICON, &ic.i));
  ic.i.flags = wimp_ISPRITE | wimp_IHCENTRE | wimp_IVCENTRE | wimp_INDIRECT | (6 << 12);
  ic.i.data.indirectsprite.name = sprite_name;
  ic.i.data.indirectsprite.spritearea = (void *) 1;
  ic.i.data.indirectsprite.nameisname = TRUE;
  ic.w = saveas;
  wimpt_noerr (wimp_delete_icon (saveas, SAVEAS_FILEICON));
  wimpt_noerr (wimp_create_icon (&ic, &i));
  if (i != SAVEAS_FILEICON)
    tfatal ("xfer_send template mangled");

  /* Change the file name.  */

  wimpt_noerr (wimp_get_icon_info (saveas, SAVEAS_FILENAME, &ic.i));
  saveas_buffer = ic.i.data.indirecttext.buffer;
  strcpy (saveas_buffer, file_name);
}



void
initialise (void)
{
  char fname[1024];   /* YECH! */
  wimp_icreate ic;
  wimp_i dum;

  wimpt_init (PROGRAM_NAME);
  atexit (cleanup);

  res_init ("Preview");
  resspr_init ();
  res_findname ("Templates", fname);
  template_init ();
  template_readfile (fname);
  visdelay_init ();
#ifdef USE_FLEX
  flex_init ();
#endif

  vdu_mode = (_kernel_osbyte (135, 0, 0) >> 8) & 0xff;

  /* prefetch the aboutbox and saveas windows.  */

  wimpt_noerr (wimp_create_wind (template_syshandle ("progInfo"), &proginfo));
  wimpt_noerr (wimp_create_wind (template_syshandle ("xfer_send"), &saveas));

  /* Record where the saveas window puts the filename.  */

  wimpt_noerr (wimp_get_icon_info (saveas, SAVEAS_FILENAME, &ic.i));
  saveas_buffer = ic.i.data.indirecttext.buffer;

  /* Our IconBar icon. */

  ic.w = -1;
  ic.i.box.x0 = 0;
  ic.i.box.y0 = 0;
  ic.i.box.x1 = 68;
  ic.i.box.y1 = 68;
  ic.i.flags = wimp_ISPRITE | wimp_INDIRECT | (3 << 12);
  ic.i.data.indirectsprite.name = "!Preview";
  ic.i.data.indirectsprite.spritearea = resspr_area ();
  ic.i.data.indirectsprite.nameisname = 5;
  wimpt_noerr (wimp_create_icon (&ic, &iconbar_icon));

  /* Put the VERSION_STRING information in the aboutbox.  */

  ic.w = proginfo;
  ic.i.box.x0 = 154;
  ic.i.box.y0 = -208;
  ic.i.box.x1 = 630;
  ic.i.box.y1 = -160;
  ic.i.flags = wimp_ITEXT | wimp_INDIRECT | wimp_IBORDER | wimp_IFILLED | wimp_IHCENTRE | wimp_IVCENTRE | (7 << 24);
  ic.i.data.indirecttext.buffer = version_string;
  ic.i.data.indirecttext.validstring = "";
  ic.i.data.indirecttext.bufflen = strlen (version_string) + 1;
  wimpt_noerr (wimp_create_icon (&ic, &dum));

  iconbar_menu = menu_syshandle (menu_new (PROGRAM_NAME, ">Info,Quit"));
  mag_menu = menu_new ("", "\\magstep0,\\magstephalf,\\magstep1,\\magstep2,\\magstep3,\\magstep4,\\magstep5,75dpi tweak");
  saveas_menu = menu_new ("", ">Sprite,>Drawfile");
  goto_page = menu_new ("", "42424242");
  menu_make_writeable (goto_page, 1, goto_page_buffer, 12, "a0-9");
  window_menu = menu_new (PROGRAM_NAME, "Next,Previous,Goto page,Magnification,New view|Save|Close,Quit");
  menu_submenu (window_menu, 1 + MAINMENU_GOTO_PAGE, goto_page);
  menu_submenu (window_menu, 1 + MAINMENU_MAGNIFICATION, mag_menu);
  menu_submenu (window_menu, 1 + MAINMENU_SAVEAS, saveas_menu);

  x_os_scale = 1 << bbc_vduvar (bbc_XEigFactor);
  y_os_scale = 1 << bbc_vduvar (bbc_YEigFactor);
} /* initialise */



void
redraw_window(struct display *w)
{
  int more;
  double xs, ys;
  int x, y;
  wimp_redrawstr r;

  r.w = w->w_handle;
  wimp_redraw_wind (&r, &more);

  factors (w, &xs, &ys);
  font_converttoos( (int)(w->hor_offset * xs), 0, &x, &y );
  while (more)
    {
      wimpt_noerr (sprite_put_given (w->area, &w->sid, 0,
                                     r.box.x0 - r.scx + x,
                                     r.box.y1 - r.scy + y));
      wimp_get_rectangle (&r, &more);
    }
}


void
close_window (wimp_w wh)
{
  struct display *w;

  for (w = windows; w; w = w->next)
    if (w->w_handle == wh)
      break;

  if (w == NULL)
    return;

  if (! w->file->complete)
    {
      fatal ("Cannot delete a file while still loading it (sorry).");
      return;
    }

  wimp_close_wind (wh);
  wimp_delete_wind (wh);

  do_a_poll ();

  visdelay_begin ();
  delete_display (wh);
  visdelay_end ();
}


int
main (int argc, char *argv[])
{
  int i;

  initialise ();

  for (i = 1; i < argc; i++)
    read_a_file (argv[i]);

  for (;;)
    do_a_poll ();

  exit (0);        /* Calls cleanup ().  */
  return (0);      /* To keep stupid compiler happy.  */
} /* main */


/* Handle any pending events.  If LOADING_A_FILE is TRUE, return when the
   NULL event is encountered, otherwise never return.  */

void
do_a_poll (void)
{
  wimp_eventstr b;
  wimp_msgstr msg;
  wimp_mousestr mstr;
  struct display *w;
  int event;

  do
    {
      if (loading_a_file)
        wimpt_noerr (wimp_poll (0, &b));
      else
        wimpt_noerr (wimp_pollidle (0, &b, 0));

      event = b.e;

      switch (event)
        {
        case wimp_EREDRAW:
          for (w = windows; w; w = w->next)
            if (w->w_handle == b.data.o.w)
              break;
          if (w)
            redraw_window (w);

          break;

        case wimp_EOPEN:
          wimp_open_wind (&b.data.o);
          break;

        case wimp_ECLOSE:
          close_window (b.data.o.w);
          break;

        case wimp_EBUT:
          handle_buttons (&b.data);
          break;

        case wimp_EUSERDRAG:
          {
            char *s;
            _kernel_swi_regs r;

            wimpt_noerr (wimp_get_point_info (&mstr));
            msg.hdr.size = sizeof (wimp_msgstr);
            msg.hdr.your_ref = 0;
            msg.hdr.action = wimp_MDATASAVE;
            msg.data.datasave.w = mstr.w;
            msg.data.datasave.i = mstr.i;
            msg.data.datasave.x = mstr.x;
            msg.data.datasave.y = mstr.y;
            msg.data.datasave.estsize = (saveas_filetype == 0xff9 ? 4 + flex_size ((flex_ptr) &which_menu->area)
                                                                  : (54 * which_menu->page->text_max + 68 * which_menu->page->rule_max
                                                                     + which_menu->page->char_max + 40));
            msg.data.datasave.type = saveas_filetype;
            s = strrchr (saveas_buffer, '.');
            if (s == NULL)
              s = saveas_buffer;

            if (strlen (s) > 11)
              {
                fatal ("Bad filename");
                break;
              }
            strcpy (&msg.data.datasave.leaf[0], s);

            r.r[0] = wimp_ESENDWANTACK;
            r.r[1] = (int) &msg;
            r.r[2] = mstr.w;
            r.r[3] = mstr.i;
            _kernel_swi (0x400e7, &r, &r);

            msg_ref = msg.hdr.my_ref;
#if 0       /* Do not delete the menu yet!  */
            wimp_create_menu ((wimp_menustr *) -1, 0, 0);
#endif
          }

          break;

        case wimp_EMENU:
          if (which_menu == NULL)
            {
              /* Something was selected from the iconbar menu.  */

              switch (b.data.menu[0])
                {
                case ICONBARMENU_QUIT:
                  exit (0);
                default:
                  break;
                }
            }
          else
            {
              wimp_redrawstr r;

              w = which_menu;
              switch (b.data.menu[0])
                {
                case MAINMENU_NEXT:
                  do_a_poll ();

                  w->page = w->page->next;
                  if (w->page == NULL)
                    w->page = w->file->pages;

                  set_window_title (w);
                  fill_sprite (w);

                  r.w = w->w_handle;
                  r.box.x0 = 0;
                  r.box.y0 = 0;
                  r.box.x1 = 1000000000;
                  r.box.y1 = 1000000000;
                  wimp_force_redraw (&r);

                  break;

                case MAINMENU_PREVIOUS:
                  {
                    struct page *p = w->file->pages;

                    do_a_poll ();

                    while (p->next && p->next != w->page)
                      p = p->next;
                    w->page = p;

                    set_window_title (w);
                    fill_sprite (w);

                    r.w = w->w_handle;
                    r.box.x0 = 0;
                    r.box.y0 = 0;
                    r.box.x1 = 1000000000;
                    r.box.y1 = 1000000000;
                    wimp_force_redraw (&r);
                  }

                  break;

                case MAINMENU_GOTO_PAGE:
                  {
                    int i;
                    struct page *p;

                    if (b.data.menu[1] == -1)
                      break;

                    i = atoi (goto_page_buffer);
                    if (i == 0)
                      break;

                    for (p = w->file->pages; p; p = p->next)
                      if (p->dvi_page == i)
                        break;

                    if (p == NULL || p->complete == FALSE)
                      {
                        if (loading_a_file && current_dvi_file == w->file)
                          fatal ("Page does not exist or has not been loaded yet.");
                        else
                          fatal ("Page does not exist.");
                        break;
                      }
                    if (p == w->page)
                      {
                        fatal ("Page is already being displayed.");
                        break;
                      }

                    w->page = p;
                    set_window_title (w);

                    fill_sprite (w);

                    r.w = w->w_handle;
                    r.box.x0 = 0;
                    r.box.y0 = 0;
                    r.box.x1 = 1000000000;
                    r.box.y1 = 1000000000;
                    wimp_force_redraw (&r);

                    break;
                  }
                case MAINMENU_MAGNIFICATION:
                  {
                    wimp_winfo wi;

                    if (b.data.menu[1] == -1 || b.data.menu[1] == w->magstep)
                      break;

                    wi.w = w->w_handle;
                    wimp_get_wind_info (&wi);

                    do_a_poll ();

                    if (b.data.menu[1] == 7 /* 75dpi tweak */) {
                       if (w->mag_tweak == 1.0) w->mag_tweak = 75.0/90.0;
                                           else w->mag_tweak = 1.0;
                    } else /* magstep selection */ {
                       w->magstep = b.data.menu[1];
                    }

#ifdef USE_FLEX
                    flex_free ((flex_ptr) &w->area);
#else
                    free (w->area);
                    w->area = 0;
#endif
                    define_sprite (w);
                    fill_sprite (w);
                    adjust_window_size (w);

                    break;
                  }

                case MAINMENU_CLONE:
                  build_a_window (which_menu->file, which_menu->page);
                  break;

                case MAINMENU_CLOSE:
                  close_window (w->w_handle);
                  break;

                case MAINMENU_QUIT:
                  exit (0);
                  break;

                default:
                  break;
                }
            }
          break;

        case wimp_ESCROLL:
          if (ABS (b.data.scroll.x) == 2)
            b.data.scroll.o.x += SGN (b.data.scroll.x) * (b.data.scroll.o.box.x1 - b.data.scroll.o.box.x0);
          else if (ABS (b.data.scroll.x) == 1)
            b.data.scroll.o.x += SGN (b.data.scroll.x) * 32;

          if (ABS (b.data.scroll.y) == 2)
            b.data.scroll.o.y += SGN (b.data.scroll.y) * (b.data.scroll.o.box.y1 - b.data.scroll.o.box.y0);
          else if (ABS (b.data.scroll.y) == 1)
            b.data.scroll.o.y += SGN (b.data.scroll.y) * 32;

          wimp_open_wind (&b.data.scroll.o);
          break;

        case wimp_ESEND:
        case wimp_ESENDWANTACK:
          switch (b.data.msg.hdr.action)
            {
            case wimp_MCLOSEDOWN:
              exit (0);
              break;

            case wimp_MDATASAVEOK:
              if (b.data.msg.hdr.your_ref == msg_ref)
                {
                  output_saveas (b.data.msg.data.datasaveok.name);
                  wimp_create_menu ((wimp_menustr *) -1, 0, 0);

                  msg.hdr.size = sizeof (wimp_msgstr);
                  msg.hdr.your_ref = 0;
                  msg.hdr.action = wimp_MDATALOAD;
                  msg.data.dataload.w = mstr.w;
                  msg.data.dataload.i = mstr.i;
                  msg.data.dataload.x = mstr.x;
                  msg.data.dataload.y = mstr.y;
                  msg.data.dataload.size = 0;
                  msg.data.dataload.type = saveas_filetype;
                  strcpy (msg.data.dataload.name, b.data.msg.data.datasaveok.name);
                  wimpt_noerr (wimp_sendmessage (wimp_ESENDWANTACK, &msg, (wimp_t) mstr.w));
                }
              break;

            case wimp_MMENUWARN:
              if (which_menu == 0)
                {
                  if (b.data.msg.data.words[3] == 0)
                    wimpt_complain (wimp_create_submenu ((wimp_menustr *) proginfo, b.data.msg.data.words[1], b.data.msg.data.words[2]));
                }
              else if (b.data.msg.data.words[3] == MAINMENU_SAVEAS)
                {
                  if (b.data.msg.data.words[4] == SAVEASMENU_PAINT)
                    {
                      saveas_filetype = 0xff9;
                      set_saveas_state ("Sprite", "file_ff9");
                    }
                  else
                    {
                      saveas_filetype = 0xaff;
                      set_saveas_state ("DrawFile", "file_aff");
                    }
                  wimpt_complain (wimp_create_submenu ((wimp_menustr *) saveas, b.data.msg.data.words[1], b.data.msg.data.words[2]));
                }
                
              break;

            case wimp_MMODECHANGE:
              {
                int i = (_kernel_osbyte (135, 0, 0) >> 8) & 0xff;
                struct display *w;

                if (vdu_mode == i)
                  break;

                vdu_mode = i;

                for (w = windows; w; w = w->next)
                  {
#ifdef USE_FLEX
                    flex_free ((flex_ptr) &w->area);
#else
                    free (w->area);
                    w->area = 0;
#endif
                    define_sprite (w);
                    fill_sprite (w);
                    adjust_window_size (w);
                  }
              }
              break;

            case wimp_MDATALOAD:
              if (b.data.msg.data.dataload.w == -2 && b.data.msg.data.dataload.i == iconbar_icon)
                {
                  read_a_file (b.data.msg.data.dataload.name);

                  b.data.msg.hdr.your_ref = b.data.msg.hdr.my_ref;
                  b.data.msg.hdr.action = wimp_MDATALOADOK;
                  wimp_sendmessage (wimp_ESEND, &b.data.msg, b.data.msg.hdr.task);
                }
              break;

            case wimp_MDATAOPEN:
              if (b.data.msg.data.dataopen.type == FILE_TYPE_DVI)
                {
                  b.data.msg.hdr.your_ref = b.data.msg.hdr.my_ref;
                  wimp_sendmessage (wimp_EACK, &b.data.msg, b.data.msg.hdr.task);
                  read_a_file (b.data.msg.data.dataopen.name);
                }
              break;

            case wimp_MRAMFETCH:
              wimp_create_menu ((wimp_menustr *) -1, 0, 0);
              output_send (&b.data.msg);
              break;

            default:
              break;
            }
          break;

        case wimp_EACK:
#if 0
          fatal ("/usr/msg/ack");
#endif
          break;

        default:
          break;
        }
    }
  while (event);
} /* do_a_poll */
@


1.1
log
@Initial revision
@
text
@a106 1
  wimp_icon iff;
d161 1
a161 1
  mag_menu = menu_new ("", "\\magstep0,\\magstephalf,\\magstep1,\\magstep2,\\magstep3,\\magstep4,\\magstep5");
d179 3
a181 1
  int more, x, y;
d187 2
a188 2
  factors (w, &x, &y);

d192 2
a193 2
                                     r.box.x0 - r.scx + (int) ((double) w->hor_offset * (double) x / MAGNIFICATION_DIVISOR),
                                     r.box.y1 - r.scy + (int) ((double) w->ver_offset * (double) y / MAGNIFICATION_DIVISOR)));
a440 2
                    wimp_openstr wo;
                    int i = tex_magstep[w->magstep];
d450 7
a456 1
                    w->magstep = b.data.menu[1];
@
