/*
 *  textarea_riscos.c -- text area, RISC OS
 */

#include "gui.h"
#include "ufont.h"


/*
 *  gui_ta_text_width -- return width of text in specified style
 */

unsigned long gui_ta_text_width(struct gui_ta *doc, const wchar_t *text,
      unsigned long s)
{
  struct gui_ta_style *style;
/*   font_f font; */
  int width;

  style = (*doc->style)[s];

/*   gui_error = xfont_find_font(style->font, style->xsize, style->ysize, 0, 0, &font, NULL, NULL); */
/*   gui_check_error(); */
  gui_error = xufont_scan_string(style->handle, text, font_GIVEN_FONT | font_KERN, 0x7fffffff, 0x7fffffff,
                   NULL, NULL, 0, NULL, &width, NULL, NULL);
  gui_check_error();
/*   gui_error = xfont_lose_font(font); */
/*   gui_check_error(); */

  return width / 400;
}


/*
 *  gui_ta_render_text -- render a word to the window
 */

void gui_ta_render_text(struct gui_ta *doc, wchar_t *text, unsigned long s,
      unsigned long x, unsigned long y, unsigned long box_x0,
      unsigned long box_y0, unsigned long box_x1, unsigned long box_y1,
      bool sel)
{
  struct gui_ta_style *style;
/*   font_f font; */
  font_paint_block box;

  style = (*doc->style)[s];

  box.space.x = box.space.y = box.letter.x = box.letter.y = 0;
  box.rubout.x0 = (gui_window_origin_x + box_x0) * 400;
  box.rubout.y0 = (gui_window_origin_y - box_y1) * 400;
  box.rubout.x1 = (gui_window_origin_x + box_x1) * 400;
  box.rubout.y1 = (gui_window_origin_y - box_y0) * 400;

/*   gui_error = xfont_find_font(style->font, style->xsize, style->ysize, 0, 0, &font, NULL, NULL); */
/*   gui_check_error(); */

  gui_error = xcolourtrans_set_font_colours(font_CURRENT, (sel ? style->fcolour : style->bcolour) << 8,
        (sel ? style->bcolour : style->fcolour) << 8, 14, NULL, NULL, NULL);
  gui_check_error();

  gui_error = xufont_paint(style->handle, text, font_RUBOUT | font_GIVEN_BLOCK | font_GIVEN_FONT | font_KERN,
        (signed int) ((x + gui_window_origin_x) * 400), (signed int) ((gui_window_origin_y - y) * 400), &box, NULL, 0);
  gui_check_error();

/*   gui_error = xfont_lose_font(font); */
/*   gui_check_error(); */
}


/*
 *  gui_ta_position_x -- get offset within string of x offset
 */

unsigned long gui_ta_position_x(struct gui_ta *doc, wchar_t *text, unsigned long s,
      signed long *x)
{
  struct gui_ta_style *style;
/*   font_f font; */
  unsigned int caret;

  style = (*doc->style)[s];

/*   gui_error = xfont_find_font(style->font, style->xsize, style->ysize, 0, 0, &font, NULL, NULL); */
/*   gui_check_error(); */
  gui_error = xufont_scan_string(style->handle, text,
        font_GIVEN_FONT | font_KERN | font_RETURN_CARET_POS,
        *x * 400, 0x7fffffff, NULL, NULL, 0, &caret, (int *) x, NULL, NULL);
  gui_check_error();
/*   gui_error = xfont_lose_font(font); */
/*   gui_check_error(); */

  *x /= 400;
  return caret;
}


/*
 *  gui_ta_position_o -- get x offset within string of character offset
 */

unsigned long gui_ta_position_o(struct gui_ta *doc, wchar_t *text, unsigned long s,
      unsigned long offset)
{
  unsigned int x;
  struct gui_ta_style *style;
/*   font_f font; */

  style = (*doc->style)[s];

/*   gui_error = xfont_find_font(style->font, style->xsize, style->ysize, 0, 0, &font, NULL, NULL); */
/*   gui_check_error(); */
  gui_error = xufont_scan_string(style->handle, text, font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN,
                                0x7fffffff, 0x7fffffff, NULL, NULL, (signed int) offset, NULL, &x, NULL, NULL);
  gui_check_error();
/*   gui_error = xfont_lose_font(font); */
/*   gui_check_error(); */

  return x / 400;
}


/*
 *  gui_ta_caret -- place the wimp caret at the specified position
 */

void gui_ta_caret(gui_window_id window_id, struct gui_ta *doc,
      struct gui_ta_position *pos, unsigned long x, unsigned long y)
{
  assert(window_id < gui_window_list_items && gui_window_list[window_id].used);

  #ifdef DEBUG
  printf("gui_ta_caret: window_id = 0x%x\n", window_id);
  fflush(stdout);
  #endif

  gui_error = xwimp_set_caret_position(gui_window_list[window_id].handle,
        wimp_ICON_WINDOW, (signed int) (x + pos->x), -((signed int) (y + pos->y)),
        (signed int) (*doc->content)[pos->content]->leading, 0);
  gui_check_error();
}


/*
 *  gui_ta_fill_style -- allocate platform dependent style resources
 */

void gui_ta_fill_style(struct gui_ta_style *style)
{
  ufont_f font;

  gui_error = xufont_find_font(style->font, (signed int) style->xsize, (signed int) style->ysize, 0, 0, &font, NULL, NULL);
  gui_check_error();

  style->handle = font;
}


/*
 *  gui_ta_empty_style -- free platform dependent style resources
 */

void gui_ta_empty_style(struct gui_ta_style *style)
{
  gui_error = xufont_lose_font(style->handle);
  gui_check_error();
}
