/*
 *  dialog_gtk.c -- dialog creation, GTK
 */

#include "gui.h"

/****************************************************************************************/
/*  Dialog creation functions                                                           */
/****************************************************************************************/

#define DIALOG_ICON_CHUNK 10

static gui_window_id dialog_id = -1;
static GtkWidget *dialog;
static GtkWidget *table;
static unsigned int table_rows;


/*
 *  gui_start_dialog -- start creating a dialogue window
 *
 *  => title -- window title
 *
 *  <= nothing
 */

void gui_start_dialog(char *title, unsigned int width,
      bool close, const char *help)
{
  assert(dialog_id == -1);

  #ifdef DEBUG
  printf("gui_start_dialog: title = '%s'\n", title);
  fflush(stdout);
  #endif

  dialog_id = gui_get_free_window_id();

  table = gtk_table_new(1, 2, FALSE);
  dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title(GTK_WINDOW (dialog), title);
  gtk_container_add(GTK_CONTAINER (dialog), table);

  gui_window_list[dialog_id].title = title;
  gui_window_list[dialog_id].help = help;

  table_rows = 0;
}


/*
 *  gui_create_dialog -- finish creating and open a dialogue window
 *
 *  => nothing
 *
 *  <= window id
 */

gui_window_id gui_create_dialog()
{
  gui_window_id id = dialog_id;

  assert(dialog_id != -1);

  gui_window_list[dialog_id].used = DIALOG;
  gui_window_list[dialog_id].handle = dialog;
  gui_window_list[dialog_id].close_fn = gui_remove_window;
  gui_window_list[dialog_id].redraw_fn = NULL;
  gui_window_list[dialog_id].click_fn = NULL;
  gui_window_list[dialog_id].resize_fn = NULL;
  gui_window_list[dialog_id].key_fn = NULL;
  gui_window_list[dialog_id].input_fn = NULL;
  gui_window_list[dialog_id].menu_fn = NULL;
  gui_window_list[dialog_id].width = 0;
  gui_window_list[dialog_id].height = 0;
  gui_window_list[dialog_id].menu = NULL;

  gtk_widget_show(table);
  gtk_widget_show(dialog);

  dialog_id = -1;

  #ifdef DEBUG
  printf("gui_create_dialog: id = 0x%x, handle = 0x%x\n", dialog_id,
        (unsigned int) dialog);
  fflush(stdout);
  #endif

  return id;
}


/*
 *  gui_create_label -- create a text label
 *
 *  => text -- label text
 *
 *  <= nothing
 */

void gui_create_label(char *text, const char *help)
{
  GtkWidget *label;

  assert(dialog_id != -1);

  #ifdef DEBUG
  printf("gui_create_label: text = '%s'\n", text);
  fflush(stdout);
  #endif

  label = gtk_label_new(text);
  gtk_widget_show(label);

  gtk_table_resize(table, ++table_rows, 2);
  gtk_table_attach(table, label, 0, 2, table_rows - 1, table_rows,
      GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
}


/*
 *  gui_create_text_box -- create a labelled writeable text box
 *
 *  => text -- label text
 *     mask -- entry mask (eg. '0-9' etc)
 *
 *  <= gadget id
 *     0 if an error occurred
 */

gui_gadget_id gui_create_text_box(char *text, const char *mask, const char *help)
{
  GtkWidget *label;
  GtkWidget *entry;

  assert(dialog_id != -1);

  #ifdef DEBUG
  printf("gui_create_text_box: text = '%s', mask = '%s'\n", text, mask);
  fflush(stdout);
  #endif

  label = gtk_label_new(text);
  entry = gtk_entry_new();

  gtk_label_set_justify(label, GTK_JUSTIFY_RIGHT);

  gtk_table_resize(table, ++table_rows, 2);
  gtk_table_attach(table, label, 0, 1, table_rows - 1, table_rows,
      GTK_EXPAND | GTK_FILL, 0, 0, 0);
  gtk_table_attach(table, entry, 1, 2, table_rows - 1, table_rows,
      GTK_EXPAND | GTK_FILL, 0, 0, 0);

  gtk_widget_show(label);
  gtk_widget_show(entry);

  return entry;
}


/*
 *  gui_create_text_box -- create a labelled display text box
 *
 *  => text -- label text
 *     value -- display text
 *
 *  <= gadget id
 *     0 if an error occurred
 */

gui_gadget_id gui_create_display_box(char *text, char *value, const char *help)
{
  GtkWidget *label;
  GtkWidget *display;

  assert(dialog_id != -1);

  #ifdef DEBUG
  printf("gui_create_display_box: text = '%s', value = '%s'\n", text, value);
  fflush(stdout);
  #endif

  label = gtk_label_new(text);
  display = gtk_label_new(value);

  gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
  gtk_label_set_justify(GTK_LABEL(display), GTK_JUSTIFY_LEFT);

  gtk_table_resize(GTK_TABLE(table), ++table_rows, 2);
  gtk_table_attach(GTK_TABLE(table), label, 0, 1, table_rows - 1, table_rows,
      GTK_EXPAND | GTK_FILL, 0, 0, 0);
  gtk_table_attach(GTK_TABLE(table), display, 1, 2, table_rows - 1, table_rows,
      GTK_EXPAND | GTK_FILL, 0, 0, 0);

  gtk_widget_show(label);
  gtk_widget_show(display);

  return display;
}


/*
 *  gui_create_button -- create an action button
 *
 *  => text -- button text
 *     click_fn -- function called when button is clicked
 *
 *  <= nothing
 */

void gui_create_button(char *text, void (*click_fn)(), const char *help)
{
/*   unsigned const int border = (dialog_x == dialog_block->extent.x1 ? 8 : 0);
 *
 *   assert(dialog_id != -1);
 *
 *   #ifdef DEBUG
 *   printf("gui_create_button: text = '%s'\n", text);
 *   fflush(stdout);
 *   #endif
 *
 *   ensure_dialog_space();
 *
 *   if (dialog_x == dialog_block->extent.x1) dialog_y += 84;
 *
 *   dialog_block->icons[dialog_icons].extent.x0 = dialog_x - 140 - border * 2;
 *   dialog_block->icons[dialog_icons].extent.y0 = -(dialog_y - 16 + border);
 *   dialog_block->icons[dialog_icons].extent.x1 = dialog_x - 8;
 *   dialog_block->icons[dialog_icons].extent.y1 = -(dialog_y - 68 - border);
 *   dialog_block->icons[dialog_icons].flags = wimp_ICON_TEXT | wimp_ICON_HCENTRED |
 *       wimp_ICON_VCENTRED | wimp_ICON_INDIRECTED | wimp_ICON_BORDER | wimp_ICON_FILLED |
 *       wimp_BUTTON_CLICK << wimp_ICON_BUTTON_TYPE_SHIFT |
 *       wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT |
 *       wimp_COLOUR_VERY_LIGHT_GREY << wimp_ICON_BG_COLOUR_SHIFT;
 *   dialog_block->icons[dialog_icons].data.indirected_text.text = text;
 *   dialog_block->icons[dialog_icons].data.indirected_text.validation =
 *                                     (dialog_x == dialog_block->extent.x1 ? "r6,3" : "r5,3");
 *
 *   dialog_icon[dialog_icons].type = ICON_ACTION;
 *   dialog_icon[dialog_icons].click_fn = click_fn;
 *   dialog_icon[dialog_icons].help = help;
 *
 *   dialog_icons++;
 *   dialog_x -= 140 + border * 2;
 */
}
