/*
 *  guitest.c
 *
 *  Test program for portable gui library
 */


#include "gui.h"


#define MARGIN 16


const char *task_name = "guitest";

struct gui_ta *test_doc;
struct gui_ta_position test_pos;
struct gui_ta_selection test_sel;
unsigned int selection = 0;


void click(gui_window_id window_id, unsigned int x, unsigned int y, unsigned int z)
{
  switch (z)
  {
    case gui_CLICK:
    {
      gui_ta_position_xy(test_doc, &test_pos, x < MARGIN ? 0 : x - MARGIN, y < MARGIN ? 0 : y - MARGIN);
      gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
      if (selection)
      {
        selection = 0;
        gui_refresh_window(window_id);
      }
      break;
    }
    case gui_CLICK_RIGHT:
    {
      struct gui_ta_position pos;

      gui_ta_position_xy(test_doc, &pos, x - MARGIN, y - MARGIN);
      if (pos.content < test_pos.content ||
         (pos.content == test_pos.content && (pos.word < test_pos.word ||
                                             (pos.word == test_pos.word && pos.offset < test_pos.offset))))
      {
        test_sel.start = pos;
        test_sel.end = test_pos;
      }
      else
      {
        test_sel.start = test_pos;
        test_sel.end = pos;
      }

      selection = 1;
      gui_refresh_window(window_id);
      break;
    }
    case gui_CLICK_MENU:
    {
      gui_ta_export(test_doc, "export");
    }
  }
}


void redraw(const gui_window_id window_id, unsigned int invalid[])
{
  if (invalid[0] <= MARGIN) gui_fill(0xffffff, invalid[0], invalid[1], MARGIN, invalid[3]);
  if (invalid[1] <= MARGIN) gui_fill(0xffffff, invalid[0], invalid[1], invalid[2], MARGIN);
  if (invalid[2] >= test_doc->width + MARGIN)
    gui_fill(0xffffff, test_doc->width + MARGIN, invalid[1], invalid[2], invalid[3]);
  if (invalid[3] >= test_doc->height + MARGIN)
    gui_fill(0xffffff, invalid[0], test_doc->height + MARGIN, invalid[2], invalid[3]);

  if (!(invalid[2] < MARGIN || invalid[0] > test_doc->width + MARGIN ||
        invalid[3] < MARGIN || invalid[1] > test_doc->height + MARGIN))
    gui_ta_render(test_doc, MARGIN, MARGIN, invalid, selection ? &test_sel : NULL);
}


void resize(const gui_window_id window_id, unsigned int width, unsigned int height)
{
  if (width != test_doc->width + 2 * MARGIN)
  {
    gui_ta_reformat(test_doc, width - 2 * MARGIN);
    gui_window_extent(window_id, 0, test_doc->height + 2 * MARGIN);
    gui_ta_position_cwo(test_doc, &test_pos);
    gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
    gui_refresh_window(window_id);
  }
}


void input(const gui_window_id window_id, wchar_t key)
{
  const unsigned int height = test_doc->height;

  printf("input: key = %04x\n", key);

  if ((key > 31 || key == 8) && selection)
  {
    selection = 0;
    gui_ta_delete(test_doc, &test_sel.start, &test_sel.end, window_id, MARGIN, MARGIN);
    test_pos = test_sel.start;
  }

  if (key > 32 && key != 127)
  {
    gui_ta_insert_char(test_doc, &test_pos, key, window_id, MARGIN, MARGIN);
    test_pos.offset++;
  }
  else if (key == 32)
  {
    gui_ta_insert_split(test_doc, &test_pos, window_id, MARGIN, MARGIN);
    test_pos.word++;
    test_pos.offset = 0;
  }
  else if (key == 13)
  {
    gui_ta_insert_newline(test_doc, &test_pos, window_id, MARGIN, MARGIN);
    test_pos.content++;
    test_pos.word = test_pos.offset = 0;
/*    test_pos.word++;
    test_pos.offset = 0;*/
  }
  else if (key == 8 && !selection)
  {
    struct gui_ta_position start = test_pos;

    if (start.offset > 0)
      start.offset--;
    else if (start.word > 0)
    {
      start.word--;
      start.offset = wcslen((*(*test_doc->content)[start.content]->word)[start.word]->text);
    }
    else if (start.content > 0)
    {
      start.content--;
      start.word = (*test_doc->content)[start.content]->words - 1;
      start.offset = wcslen((*(*test_doc->content)[start.content]->word)[start.word]->text);
    }
    else
      return;

    gui_ta_delete(test_doc, &start, &test_pos, window_id, MARGIN, MARGIN);
    test_pos = start;
  }
  else if (key == 127 && !selection)
  {
    struct gui_ta_position end = test_pos;

    if (end.offset < wcslen((*(*test_doc->content)[end.content]->word)[end.word]->text))
      end.offset++;
    else if (end.word < (*test_doc->content)[end.content]->words - 1)
    {
      end.word++;
      end.offset = 0;
    }
    else if (end.content < test_doc->contents - 1)
    {
      end.content++;
      end.word = 0;
      end.offset = 0;
    }
    else
      return;

    gui_ta_delete(test_doc, &test_pos, &end, window_id, MARGIN, MARGIN);
  }

  gui_ta_position_cwo(test_doc, &test_pos);
  gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);

  if (height != test_doc->height)
    gui_window_extent(window_id, 2000, test_doc->height + 2 * MARGIN);
}


void key(const gui_window_id window_id, unsigned int key)
{
  const unsigned int height = test_doc->height;

  if (key == KEY_LEFT)
  {
    if (test_pos.offset > 0)
      test_pos.offset--;
    else if (test_pos.word > 0)
    {
      test_pos.word--;
      test_pos.offset = wcslen((*(*test_doc->content)[test_pos.content]->word)[test_pos.word]->text);
    }
    else if (test_pos.content > 0)
    {
      test_pos.content--;
      test_pos.word = (*test_doc->content)[test_pos.content]->words - 1;
      test_pos.offset = wcslen((*(*test_doc->content)[test_pos.content]->word)[test_pos.word]->text);
    }
    else
      return;
  }
  else if (key == KEY_RIGHT)
  {
    if (test_pos.offset < wcslen((*(*test_doc->content)[test_pos.content]->word)[test_pos.word]->text))
      test_pos.offset++;
    else if (test_pos.word < (*test_doc->content)[test_pos.content]->words - 1)
    {
      test_pos.word++;
      test_pos.offset = 0;
    }
    else if (test_pos.content < test_doc->contents - 1)
    {
      test_pos.content++;
      test_pos.word = 0;
      test_pos.offset = 0;
    }
    else
      return;
  }
  else if (key == KEY_DOWN)
  {
    gui_ta_position_xy(test_doc, &test_pos, test_pos.x, test_pos.y + 1);
    gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
    return;
  }
  else if (key == KEY_UP)
  {
    gui_ta_position_xy(test_doc, &test_pos, test_pos.x,
                       test_pos.y - (*test_doc->content)[test_pos.content]->leading);
    gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
    return;
  }

  gui_ta_position_cwo(test_doc, &test_pos);
  gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);

  if (height != test_doc->height)
    gui_window_extent(window_id, 2000, test_doc->height + 2 * MARGIN);
}


void menuf(const gui_window_id window_id, signed int items[])
{
  static char length[40];
  gui_window_id info;
  unsigned int c, words = 0;

  switch (items[0])
  {
    case 0:
      switch (items[1])
      {
        case 0:
          for (c = 0; c < test_doc->contents; c++)
            words += (*test_doc->content)[c]->words;
          sprintf(length, gui_message[35], test_doc->contents, words);
          gui_start_dialog(gui_message[32], 600, 1, "help!");
          gui_create_display_box(gui_message[33], "Untitled", gui_message[39]);
          gui_create_display_box(gui_message[34], length, gui_message[40]);
          gui_create_display_box(gui_message[36], "Unicode text", gui_message[41]);
          gui_create_display_box(gui_message[37], "22:25:01 27 Mar 2001",
                gui_message[42]);
          info = gui_create_dialog();
          break;
      }
      break;

    case 2:
      switch (items[1])
      {
        case 3:
          gui_ta_align(test_doc, test_pos.content, ALIGN_LEFT, window_id, MARGIN, MARGIN);
          gui_ta_position_cwo(test_doc, &test_pos);
          gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
          break;
        case 4:
          gui_ta_align(test_doc, test_pos.content, ALIGN_CENTRE, window_id, MARGIN, MARGIN);
          gui_ta_position_cwo(test_doc, &test_pos);
          gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
          break;
        case 5:
          gui_ta_align(test_doc, test_pos.content, ALIGN_RIGHT, window_id, MARGIN, MARGIN);
          gui_ta_position_cwo(test_doc, &test_pos);
          gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
          break;
        case 6:
          gui_ta_align(test_doc, test_pos.content, ALIGN_FULL, window_id, MARGIN, MARGIN);
          gui_ta_position_cwo(test_doc, &test_pos);
          gui_ta_caret(window_id, test_doc, &test_pos, MARGIN, MARGIN);
          break;
      }
      break;
  }
}


void task_initialise(void)
{
  unsigned int helvetica, times;
  wchar_t par1[] = { 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0020,
                     0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0020,
                     0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0020,
                     0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0020,
                     0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0020,
                     0x00a0, 0x00b0, 0x00c0, 0x00d0, 0x00e0, 0x00f0, 0x0020,
                     0x00a8, 0x00b8, 0x00c8, 0x00d8, 0x00e8, 0x00f8, 0x0020,
                     0x0100, 0x0110, 0x0120, 0x0130, 0x0140, 0x0150, 0x0160, 0x0170, 0x0020,
                     0x0108, 0x0118, 0x0128, 0x0138, 0x0148, 0x0158, 0x0168, 0x0178, 0x0020,
                     0x02bc, 0x0399, 0x03b1, 0x03c8, 0x0414, 0x0423, 0x043f, 0x044a, 0x0020,
                     0x2010, 0x2020, 0x2030, 0x2122, 0x2212, 0x2251, 0xfb01, 0xfb02,
                     ' ','C','u','r','a','b','i','t','u','r',' ','s','e','m','p','e','r',' ','v','e','l','i','t','.',' ','S','e','d',' ','u','t',' ','p','e','d','e',' ','s','e','d',' ','e','r','a','t',' ','c','o','n','s','e','c','t','e','t','u','e','r',' ','p','e','l','l','e','n','t','e','s','q','u','e','.',' ','C','u','r','a','b','i','t','u','r',' ','m','a','u','r','i','s','.',' ','D','o','n','e','c',' ','a',' ','j','u','s','t','o',' ','e','u',' ','l','o','r','e','m',' ','t','i','n','c','i','d','u','n','t',' ','n','o','n','u','m','m','y','.',' ','P','r','a','e','s','e','n','t',' ','c','o','n','s','e','q','u','a','t',' ','e','s','t',' ','n','o','n',' ','l','a','c','u','s','.',' ','C','u','r','a','b','i','t','u','r',' ','c','u','r','s','u','s','.',' ','M','o','r','b','i',' ','u','l','t','r','i','c','e','s',',',' ','v','e','l','i','t',' ','i','n',' ','l','u','c','t','u','s',' ','s','c','e','l','e','r','i','s','q','u','e',',',' ','n','u','l','l','a',' ','t','u','r','p','i','s',' ','v','i','v','e','r','r','a',' ','m','a','s','s','a',',',' ','a',' ','i','n','t','e','r','d','u','m',' ','p','e','d','e',' ','t','o','r','t','o','r',' ','v','e','l',' ','l','a','c','u','s','.',' ','Q','u','i','s','q','u','e',' ','p','o','r','t','a',' ','l','i','b','e','r','o',' ','e','t',' ','d','o','l','o','r','.',' ','M','a','u','r','i','s',' ','t','e','m','p','o','r','.',' ','S','e','d',' ','a','n','t','e','.',' ','S','e','d',' ','u','l','t','r','i','c','e','s',',',' ','p','e','d','e',' ','a','t',' ','s','o','d','a','l','e','s',' ','p','o','r','t','t','i','t','o','r',',',' ','i','p','s','u','m',' ','s','a','p','i','e','n',' ','f','e','r','m','e','n','t','u','m',' ','l','e','o',',',' ','e','u',' ','c','o','n','s','e','q','u','a','t',' ','a','r','c','u',' ','q','u','a','m',' ','e','u',' ','l','a','c','u','s','.',' ','P','h','a','s','e','l','l','u','s',' ','b','l','a','n','d','i','t','.',' ','M','o','r','b','i',' ','s','i','t',' ','a','m','e','t',' ','l','e','c','t','u','s',' ','a','c',' ','p','u','r','u','s',' ','v','e','n','e','n','a','t','i','s',' ','l','o','b','o','r','t','i','s','.',' ','F','u','s','c','e',' ','v','e','l',' ','e','r','o','s','.',' ','F','u','s','c','e',' ','a','d','i','p','i','s','c','i','n','g','.',' ','A','e','n','e','a','n',' ','s','i','t',' ','a','m','e','t',' ','l','e','c','t','u','s',' ','n','o','n',' ','e','s','t',' ','c','o','n','s','e','c','t','e','t','u','e','r',' ','f','e','u','g','i','a','t','.',' ','N','u','n','c',' ','n','e','c',' ','a','n','t','e',' ','v','i','t','a','e',' ','l','e','c','t','u','s',' ','m','a','l','e','s','u','a','d','a',' ','v','e','h','i','c','u','l','a','.',' ','E','t','i','a','m',' ','a','u','c','t','o','r','.',' ','I','n','t','e','g','e','r',' ','s','e','m','p','e','r',' ','p','h','a','r','e','t','r','a',' ','m','a','s','s','a','.',' ','V','e','s','t','i','b','u','l','u','m',' ','a','n','t','e',' ','i','p','s','u','m',' ','p','r','i','m','i','s',' ','i','n',' ','f','a','u','c','i','b','u','s',' ','o','r','c','i',' ','l','u','c','t','u','s',' ','e','t',' ','u','l','t','r','i','c','e','s',' ','p','o','s','u','e','r','e',' ','c','u','b','i','l','i','a',' ','C','u','r','a','e',';',' ','V','e','s','t','i','b','u','l','u','m',' ','n','e','c',' ','l','e','c','t','u','s',' ','e','g','e','t',' ','l','a','c','u','s',' ','m','o','l','l','i','s',' ','v','e','s','t','i','b','u','l','u','m','.',' ','F','u','s','c','e',' ','a','u','g','u','e','.', 0 };
  wchar_t par2[] = { 0x00b0, 0x00c0, 0x00d0, 0x00e0, 0x00f0, 0x0020,
                     0x00a8, 0x00b8, 0x00c8, 0x00d8, 0x00e8, 0x00f8, 0x0020,
                     0x0100, 0x0110, 0x0120, 0x0130, 0x0140, 0x0150, 0x0160, 0x0170, 0x0020,
                     0x0108, 0x0118, 0x0128, 0x0138, 0x0148, 0x0158, 0x0168, 0x0178, 0x0020,
                     0x02bc, 0x0399, 0x03b1, 0x03c8, 0x0414, 0x0423, 0x043f, 0x044a, 0x0020,
                     0x2010, 0x2020, 0x2030, 0x2122, 0x2212, 0x2251, 0xfb01, 0xfb02, 0x0020,
                     0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0020,
                     0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0020,
                     0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0020,
                     0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0020,
                     0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036,
                     ' ','C','u','r','a','b','i','t','u','r',' ','s','e','m','p','e','r',' ','v','e','l','i','t','.',' ','S','e','d',' ','u','t',' ','p','e','d','e',' ','s','e','d',' ','e','r','a','t',' ','c','o','n','s','e','c','t','e','t','u','e','r',' ','p','e','l','l','e','n','t','e','s','q','u','e','.',' ','C','u','r','a','b','i','t','u','r',' ','m','a','u','r','i','s','.',' ','D','o','n','e','c',' ','a',' ','j','u','s','t','o',' ','e','u',' ','l','o','r','e','m',' ','t','i','n','c','i','d','u','n','t',' ','n','o','n','u','m','m','y','.',' ','P','r','a','e','s','e','n','t',' ','c','o','n','s','e','q','u','a','t',' ','e','s','t',' ','n','o','n',' ','l','a','c','u','s','.',' ','C','u','r','a','b','i','t','u','r',' ','c','u','r','s','u','s','.',' ','M','o','r','b','i',' ','u','l','t','r','i','c','e','s',',',' ','v','e','l','i','t',' ','i','n',' ','l','u','c','t','u','s',' ','s','c','e','l','e','r','i','s','q','u','e',',',' ','n','u','l','l','a',' ','t','u','r','p','i','s',' ','v','i','v','e','r','r','a',' ','m','a','s','s','a',',',' ','a',' ','i','n','t','e','r','d','u','m',' ','p','e','d','e',' ','t','o','r','t','o','r',' ','v','e','l',' ','l','a','c','u','s','.',' ','Q','u','i','s','q','u','e',' ','p','o','r','t','a',' ','l','i','b','e','r','o',' ','e','t',' ','d','o','l','o','r','.',' ','M','a','u','r','i','s',' ','t','e','m','p','o','r','.',' ','S','e','d',' ','a','n','t','e','.',' ','S','e','d',' ','u','l','t','r','i','c','e','s',',',' ','p','e','d','e',' ','a','t',' ','s','o','d','a','l','e','s',' ','p','o','r','t','t','i','t','o','r',',',' ','i','p','s','u','m',' ','s','a','p','i','e','n',' ','f','e','r','m','e','n','t','u','m',' ','l','e','o',',',' ','e','u',' ','c','o','n','s','e','q','u','a','t',' ','a','r','c','u',' ','q','u','a','m',' ','e','u',' ','l','a','c','u','s','.',' ','P','h','a','s','e','l','l','u','s',' ','b','l','a','n','d','i','t','.',' ','M','o','r','b','i',' ','s','i','t',' ','a','m','e','t',' ','l','e','c','t','u','s',' ','a','c',' ','p','u','r','u','s',' ','v','e','n','e','n','a','t','i','s',' ','l','o','b','o','r','t','i','s','.',' ','F','u','s','c','e',' ','v','e','l',' ','e','r','o','s','.',' ','F','u','s','c','e',' ','a','d','i','p','i','s','c','i','n','g','.',' ','A','e','n','e','a','n',' ','s','i','t',' ','a','m','e','t',' ','l','e','c','t','u','s',' ','n','o','n',' ','e','s','t',' ','c','o','n','s','e','c','t','e','t','u','e','r',' ','f','e','u','g','i','a','t','.',' ','N','u','n','c',' ','n','e','c',' ','a','n','t','e',' ','v','i','t','a','e',' ','l','e','c','t','u','s',' ','m','a','l','e','s','u','a','d','a',' ','v','e','h','i','c','u','l','a','.',' ','E','t','i','a','m',' ','a','u','c','t','o','r','.',' ','I','n','t','e','g','e','r',' ','s','e','m','p','e','r',' ','p','h','a','r','e','t','r','a',' ','m','a','s','s','a','.',' ','V','e','s','t','i','b','u','l','u','m',' ','a','n','t','e',' ','i','p','s','u','m',' ','p','r','i','m','i','s',' ','i','n',' ','f','a','u','c','i','b','u','s',' ','o','r','c','i',' ','l','u','c','t','u','s',' ','e','t',' ','u','l','t','r','i','c','e','s',' ','p','o','s','u','e','r','e',' ','c','u','b','i','l','i','a',' ','C','u','r','a','e',';',' ','V','e','s','t','i','b','u','l','u','m',' ','n','e','c',' ','l','e','c','t','u','s',' ','e','g','e','t',' ','l','a','c','u','s',' ','m','o','l','l','i','s',' ','v','e','s','t','i','b','u','l','u','m','.',' ','F','u','s','c','e',' ','a','u','g','u','e','.',0 };
  gui_window_id window, dialog;
  gui_gadget_id magnitude, direction;
  gui_menu_id menu;

  test_doc = gui_ta(1400);

  times = gui_ta_get_style(test_doc, 300, 300, 0x000000, 0xffffff, "Times");
  helvetica = gui_ta_get_style(test_doc, 300, 300, 0xffffff, 0x0000ff, "Helvetica");

  gui_ta_add_paragraph(test_doc, 60, 15, par1, times, ALIGN_LEFT);
/*   gui_ta_add_space(test_doc, 100); */
  gui_ta_add_paragraph(test_doc, 60, 15, par2, times, ALIGN_CENTRE);
  gui_ta_add_paragraph(test_doc, 60, 15, par1, helvetica, ALIGN_RIGHT);
/*   gui_ta_add_space(test_doc, 50); */
  gui_ta_add_paragraph(test_doc, 60, 15, par2, helvetica, ALIGN_FULL);

  menu = gui_menu(2, 4);
  gui_menu_attach(menu, 0, gui_menu(8, 3));
  gui_menu_attach(menu, 1, gui_menu(13, 5));
  gui_menu_attach(menu, 2, gui_menu(20, 7));
  gui_menu_attach(menu, 3, gui_menu(29, 1));

  window = gui_create_window(gui_V_SCROLL_BAR, gui_message[0], test_doc->width + 2 * MARGIN,
                             test_doc->height + 2 * MARGIN, NULL, redraw, click, resize, key, input,
                             menuf, menu, NULL, "hi");

/*  gui_start_dialog("guitest dialog");
  gui_create_label("Resolve vector");
  magnitude = gui_create_text_box("Magnitude", "0-9.");
  direction = gui_create_text_box("Direction", "0-9.");
  gui_create_button("Resolve!", NULL);
  gui_create_button("Close", NULL);
  dialog = gui_create_dialog();*/
}


void task_finalise(void)
{
  gui_ta_remove(test_doc);
}
