
#include "stonage.h"
#include "window_properties.h"
#include "square.h"
#include <gtk/gtk.h>

static void on_changed_delay(GtkSpinButton *spinbutton, struct properties_info *info);
//static void on_selection_changed_properties (GtkList *list, struct properties_info *info);
static void on_unselect_child_properties (GtkList *list, GtkWidget *widget, struct properties_info *info);
static void on_select_child_properties (GtkList *list, GtkWidget *widget, struct properties_info *info);

void
display_square_properties(GtkWidget *widget, gpointer data)
{
  static struct properties_info *properties_info=NULL;
  struct t_square *s = (struct t_square *)data;
  int i;
  GList *square_properties;
  GtkWidget *item;

  if (properties_info == NULL)
    properties_info=create_properties_window();

  trace("square: %s",s->name);
  properties_info->square=s;

  /* Manage properties */
  i=0; square_properties=NULL;
  gtk_signal_handler_block_by_func(GTK_OBJECT(properties_info->properties),
              GTK_SIGNAL_FUNC (on_select_child_properties), properties_info);
  gtk_signal_handler_block_by_func(GTK_OBJECT(properties_info->properties),
              GTK_SIGNAL_FUNC (on_unselect_child_properties), properties_info);
  gtk_list_clear_items(properties_info->properties,0,-1);
  while (square_properties_text2enum[i].text)
   {
     item = gtk_list_item_new_with_label (square_properties_text2enum[i].text);
     gtk_object_set_user_data(GTK_OBJECT(item), (gpointer)square_properties_text2enum[i].value);
     if (s->properties & square_properties_text2enum[i].value)
       gtk_list_item_select((GtkListItem *)item);
     gtk_widget_show(item);
     square_properties = g_list_append(square_properties, item);
     i++;
   }
  gtk_list_append_items(GTK_LIST(properties_info->properties),square_properties);

  gtk_signal_handler_unblock_by_func(GTK_OBJECT(properties_info->properties),
              GTK_SIGNAL_FUNC (on_select_child_properties), properties_info);
  gtk_signal_handler_unblock_by_func(GTK_OBJECT(properties_info->properties),
              GTK_SIGNAL_FUNC (on_unselect_child_properties), properties_info);
  
  gtk_widget_show(properties_info->window);
}


struct properties_info *
create_properties_window(void)
{
  GtkWidget *window, *vbox, *hbox, *widget, *scrolledwindow;
  GtkList *list;
  GtkObject *adj;
  struct properties_info *info;

  info = g_new0(struct properties_info,1);

  info->window = window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window),_("Square Properties"));

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER(window), vbox);
  
  /*****************************************************
   *
   *****************************************************/

  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

  widget = gtk_label_new (_("delay (ms)"));
  gtk_box_pack_start (GTK_BOX(hbox), widget, FALSE, FALSE, 0);

  adj = gtk_adjustment_new (1, 0, 500, 1, 10, 10);
  widget = gtk_spin_button_new (GTK_ADJUSTMENT (adj), 1, 0);
  gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
  gtk_signal_connect (GTK_OBJECT (widget), "changed",
      GTK_SIGNAL_FUNC (on_changed_delay),
      (gpointer)info);
  info->delay = (GtkSpinButton *)widget;

  /*****************************************************
   *
   *****************************************************/

  widget = gtk_label_new (_("Properties"));
  gtk_box_pack_start (GTK_BOX(vbox), widget, FALSE, FALSE, 0);

  scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_set_usize ((GtkWidget *)scrolledwindow, 180, 260);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
      GTK_POLICY_AUTOMATIC,
      GTK_POLICY_ALWAYS);
  gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0);

  info->properties = list = (GtkList *)gtk_list_new();
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW (scrolledwindow), (GtkWidget *)list);
  gtk_list_set_selection_mode (list, GTK_SELECTION_MULTIPLE);

  gtk_signal_connect (GTK_OBJECT (list), "select-child",
      GTK_SIGNAL_FUNC (on_select_child_properties),info);
  gtk_signal_connect (GTK_OBJECT (list), "unselect-child",
      GTK_SIGNAL_FUNC (on_unselect_child_properties),info);


  gtk_widget_show_all(vbox);
  /*****************************************************
   *
   *****************************************************/

  return info;
}


static void
on_changed_delay(GtkSpinButton *spinbutton, struct properties_info *info)
{
  gint value;

  value = gtk_spin_button_get_value_as_int(info->delay);

  trace("delay=%d",value);
  info->square->delay = value;
}

static void
on_select_child_properties (GtkList *list, GtkWidget *widget, struct properties_info *info)
{
  int property;
  property = (gint)gtk_object_get_user_data((GtkObject *)widget);
  info->square->properties|=property;
  trace("square:%s flags:%x",info->square->name,info->square->properties);
}

static void
on_unselect_child_properties (GtkList *list, GtkWidget *widget, struct properties_info *info)
{
  int property;
  property = (gint)gtk_object_get_user_data((GtkObject *)widget);
  info->square->properties&=~property;
  trace("square:%s flags:%x",info->square->name,info->square->properties);
}


