#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "event.h"
#include "res.h"
#include "menu.h"
#include "template.h"
#include "werr.h"
#include "coords.h"
#include "bbc.h"
#include "os.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

/******************************** CONSTANTS ********************************/

#define X_SPC 32
#define Y_SPC 40

#define SPC 20

/******************************* GLOBAL DATA *******************************/

menu Alphabets_menu;

wimp_w Alphabets_window;

char msg[300];
char alphabet[][7] = {"BFont","Latin1","Latin2","Latin3","Latin4","Greek"};
char wimp_chars[32][8];

/******************************** FUNCTIONS ********************************/


void Alphabets_menuproc(void *handle, char *hit)
{
wimp_redrawstr r;
int i,j;

  handle = handle;

  sprintf(msg,"Alphabet %s", alphabet[*hit-1]);
  system(msg);
  for (i = 0; i < 32; i++)
  {
    bbc_vdu(23);
    bbc_vdu(i+128);
    for (j = 0; j < 8; j++)
      bbc_vdu(wimp_chars[i][j]);
  }

  win_settitle(Alphabets_window, alphabet[*hit-1]);

  r.w = Alphabets_window;
  wimpt_noerr(wimp_getwindowoutline(&r));
  r.w = -1;
  wimp_force_redraw(&r);
}

void Alphabets_redraw_window(wimp_w handle)
{
int more;
wimp_redrawstr r;

coords_cvtstr cvtstr;
coords_pointstr pointstr;

int x,y;
int x0,x1,y0,y1;
int c;

  r.w = handle;
  wimpt_noerr(wimp_redraw_wind(&r, &more));

  while (more)
  {
    cvtstr.box = r.box;
    cvtstr.scx = r.scx;
    cvtstr.scy = r.scy;
    coords_box_toworkarea(&r.g,&cvtstr);

    y0 = (int) ceil(r.g.y0 / Y_SPC);
    y1 = (int) ceil(r.g.y1 / Y_SPC);

    x0 = (int) floor(r.g.x0 / X_SPC);
    x1 = (int) floor(r.g.x1 / X_SPC + 1);

    pointstr.x=0;
    pointstr.y=0;
    coords_point_toscreen(&pointstr, &cvtstr);

    for(y = y0;y <= y1;y++)
      for(x = x0;x < x1;x++)
      {
        bbc_move(pointstr.x+x*X_SPC+8, pointstr.y+y*Y_SPC-1);
        c=(2-y)*16 + x;
        if((c > 31) & (c < 256) & (c != 127))
          bbc_vdu(c);
      }

    wimp_get_rectangle(&r, &more);
  }
}

void Alphabets_event_handler(wimp_eventstr *e, void *handle)
{
  handle = handle;

  switch (e->e)
  {
  case wimp_EREDRAW:
    Alphabets_redraw_window(Alphabets_window);
    break;

  case wimp_EOPEN:
    wimpt_noerr(wimp_open_wind(&e->data.o));
    break;

  case wimp_ECLOSE:
    exit(0);
    break;

  default:
    break;
  }
}

BOOL Alphabets_initialise(void)
{
wimp_wind *wind;
wimp_wstate state;

int i,j;
char d[9];

  wimpt_init("Alphabets");
  res_init("Alphabets");
  template_init();

  system("Alphabet Latin1");
  for (i = 0; i < 32; i++)
  {
     d[0] = i + 128;
     wimpt_noerr(os_word(10, (void *) d));
     for (j = 0; j < 8; j++)
       wimp_chars[i][j] = d[j+1];
  }

  wind = template_syshandle("MainWindow");
  if (wind == 0)
    return FALSE;
  if (wimpt_complain(wimp_create_wind(wind, &Alphabets_window)))
    return FALSE;

  if ((Alphabets_menu = menu_new("Alphabets", "BFont,Latin1,Latin2,Latin3,Latin4,Greek" )) == NULL)
    return FALSE;

  wimpt_noerr(wimp_get_wind_state(Alphabets_window, &state));
  state.o.behind = -1;
  wimpt_noerr(wimp_open_wind(&state.o));

  win_register_event_handler(Alphabets_window, Alphabets_event_handler, 0);
  event_attachmenu(Alphabets_window, Alphabets_menu, Alphabets_menuproc, 0);

  win_activeinc();

  return TRUE;
}

/****************************** MAIN PROGRAM *******************************/

int main()
{
  if (!Alphabets_initialise())
    return 1;

  while (TRUE)
    event_process();

  return 0;
}
