/*
 *  textarea_gtk.c -- text area, GTK
 */

#include "gui.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;
  int width;

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

  width = gdk_text_width_wc(style->handle, text, wcslen(text));

  return width * 2;
}


/*
 *  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;
  GdkGC *gc;
  GdkColor fg, bg;

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

  BGR_TO_GDK(sel ? style->bcolour : style->fcolour, fg);
  BGR_TO_GDK(sel ? style->fcolour : style->bcolour, bg);

  gc = gdk_gc_new(gui_redraw_widget->window);

  gdk_colormap_alloc_color(gdk_window_get_colormap(gui_redraw_widget->window), &bg, FALSE, TRUE);
//  gdk_gc_set_foreground(gui_redraw_widget->style->fg_gc[gui_redraw_widget->state], &bg);
  gdk_gc_set_foreground(gc, &bg);
  gdk_draw_rectangle(gui_redraw_widget->window, gc,
        TRUE, box_x0 / 2, box_y0 / 2, (box_x1-box_x0) / 2 + 1, (box_y1-box_y0) / 2 + 1);

  gdk_colormap_alloc_color(gdk_window_get_colormap(gui_redraw_widget->window), &fg, FALSE, TRUE);
//  gdk_gc_set_foreground(gui_redraw_widget->style->fg_gc[gui_redraw_widget->state], &fg);
  gdk_gc_set_foreground(gc, &fg);
  gdk_draw_text_wc(gui_redraw_widget->window, style->handle, gc,
        x / 2, y / 2, text, wcslen(text));
}


/*
 *  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;
  unsigned int caret;

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

  return 0;
}


/*
 *  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;

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

  return 0;
}


/*
 *  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_ta_fill_style -- allocate platform dependent style resources
 */

void gui_ta_fill_style(struct gui_ta_style *style)
{
  char xlfd[200];

/*   sprintf(xlfd, "-*-helvetica-regular-r-normal-*-*-%i-*-*-*-*-iso8859-1", style->font, style->xsize / 16 * 10);  */
  style->handle = gdk_font_load("-urw-nimbus sans l-regular-r-normal--16-154-75-75-p-87-iso8859-1");
}


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

void gui_ta_empty_style(struct gui_ta_style *style)
{
  gdk_font_unref(style->font);
}
