


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

#define CELL_SIZE    32

#define DFT_SQUARE_COLUMNS 4
#define DFT_SQUARE_ROWS    5 


#define MIN_WIN_WIDTH     (CELL_SIZE * DFT_SQUARE_COLUMNS)
#define MIN_WIN_HEIGHT    (CELL_SIZE * DFT_SQUARE_ROWS)
#define MARGIN_WIDTH      1
#define MARGIN_HEIGHT     1

/* Globals variables */
static struct square_info *squares_dialog=NULL;



static struct square_info *create_squares_dialog_real(void);
static void resize_scrollbar(struct square_info *info);
static gint resize_table (GtkWidget *widget, GdkEvent *event, struct square_info *info);
static void display_squares(struct square_info *info);
static void display_square(struct square_info *info, struct t_square *s, int col, int row);

/**
 * Create squares dialog or if the dialog is already create popup, raise it.
 */
void
create_squares_dialog(void)
{
  if (!squares_dialog)
   {
     squares_dialog = create_squares_dialog_real();
   }
  else
   {
     /*  Popup the dialog  */
     if (!GTK_WIDGET_VISIBLE (squares_dialog->window))
       gtk_widget_show (squares_dialog->window);
     else
       gdk_window_raise(squares_dialog->window->window);
   }
}

/**
 * Create a window who will contains all square that can be draw on a map.
 * You can only scroll in one direction. Size if automaticaly aligned of a
 * square size.
 */
static struct square_info *
create_squares_dialog_real(void)
{
  GtkWidget *window, *hbox, *sbar, *frame;
  struct square_info *info;

  /* Create a new window and a new structure */
  info = g_new0(struct square_info,1);

  info->ncols = DFT_SQUARE_COLUMNS;
  info->nrows = DFT_SQUARE_ROWS;

  /* Load the squares */
  info->squares = Square_Load_Default();
  info->nsquares = g_list_length(info->squares);
  trace("nb d'images: %d\n",info->nsquares);

 
  /* Create the scrolled window */
  info->window = window = gtk_window_new(GTK_WINDOW_TOPLEVEL);


  /* Build a box who contains square picture and vertical scrollbar */
  hbox = gtk_hbox_new (FALSE, 1);
  gtk_container_add(GTK_CONTAINER(window),hbox);

#if 0
  info->sb_adj = gtk_adjustment_new (0, 0, MIN_WIN_HEIGHT, 1, 1, MIN_WIN_HEIGHT);
  gtk_signal_connect (GTK_OBJECT (info->sb_adj), "value_changed",
		      (GtkSignalFunc) resize_scrollbar,
	      info);
  sbar = gtk_vscrollbar_new (info->sb_adj);
  gtk_box_pack_start (GTK_BOX (hbox), sbar, FALSE, FALSE, 0);

#endif

  info->table = gtk_table_new (info->nrows,info->ncols,TRUE);
  gtk_box_pack_start (GTK_BOX (hbox), info->table, TRUE, TRUE, 0);
  gtk_widget_set_usize(GTK_WIDGET(info->table),MIN_WIN_HEIGHT,MIN_WIN_HEIGHT);
  gtk_table_set_row_spacings (GTK_TABLE (info->table), 1);
  gtk_table_set_col_spacings (GTK_TABLE (info->table), 1);
//  gtk_signal_connect_after (GTK_OBJECT(frame), "size_allocate",
//      (GtkSignalFunc)resize_table, info);

  info->sb_adj = (GtkAdjustment *)gtk_adjustment_new (0, 0, MIN_WIN_HEIGHT, 1, 1, MIN_WIN_HEIGHT);
  sbar = gtk_vscrollbar_new (info->sb_adj);
  gtk_box_pack_start (GTK_BOX (hbox), sbar, FALSE, FALSE, 0);

  //gtk_container_add (GTK_CONTAINER (frame), info->table);
  
  gtk_widget_show(sbar);
  gtk_widget_show(hbox);
 // gtk_widget_show(frame);
  gtk_widget_show(info->table);
  gtk_widget_show(info->window);

  resize_scrollbar(info);
  display_squares(info);

#if 0
  scroll = gtk_scrolled_window_new(NULL,NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
      GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
  gtk_container_add(GTK_CONTAINER(window),scroll);
  gtk_widget_show(scroll);

  info->squares = Square_Load_Default();
  n = g_list_length(info->squares);
  trace("nb d'images: %d\n",n);

  /* create two colums of squares */
  table = gtk_table_new (2, (n&1?(n+1)>>1:n>>1), FALSE);

  //gtk_table_set_row_spacings (GTK_TABLE (table), 10);
  //gtk_table_set_col_spacings (GTK_TABLE (table), 10);

  /* pack the table into the scrolled window */
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW (scroll), table);
  gtk_widget_show(table);
  
  for (i=0, pix=info->squares ; i<n ; i++, pix=pix->next)
   {
     GtkWidget *pixmap, *button;
     struct t_square *s = (struct t_square *)pix->data;

     pixmap = s->pixmaps[0];

     gtk_widget_show(pixmap);
     button = gtk_button_new();
     gtk_container_add (GTK_CONTAINER(button), pixmap);
     gtk_widget_show(button);
     gtk_signal_connect( GTK_OBJECT(button), "clicked",
     	   GTK_SIGNAL_FUNC(display_square_properties), s );
     gtk_table_attach_defaults (GTK_TABLE (table), button,
	 i&1, (i&1)+1, (i/2), (i/2)+1);
   }

  gtk_widget_show(table);

  gtk_widget_set_usize(GTK_WIDGET(scroll),100,240);

#endif
  return info;
}



/**
 * Calc the size of scrollbar
 */
static void
resize_scrollbar(struct square_info *info)
{
  int num_rows;
  int max;

  trace(" ");

  info->sb_offset = 0;
  num_rows = (info->nsquares + info->ncols - 1) / info->ncols;
  max = num_rows * CELL_SIZE;
  if (num_rows==0)
    num_rows = 1;

  info->sb_adj->value = info->sb_offset;
  info->sb_adj->upper = max;
  info->sb_adj->page_size = info->ncols * CELL_SIZE;
  info->sb_adj->page_increment = info->ncols * CELL_SIZE;
  info->sb_adj->step_increment = CELL_SIZE;

  gtk_signal_emit_by_name (GTK_OBJECT (info->sb_adj), "changed");
}

static gint
resize_table (GtkWidget *widget, GdkEvent *event, struct square_info *info)
{  
  /* calculate the best-fit approximation... */  
  gint wid;
  gint now;

  trace(" ");

  wid = widget->allocation.width-4;
  if (wid<CELL_SIZE+1)
    return FALSE;

  info->ncols = wid / CELL_SIZE;
  info->nrows = (info->nsquares + info->ncols-1) / info->ncols;

  gtk_table_resize((GtkTable *)info->table,info->nrows+1,info->ncols+1);

  /*  recalculate scrollbar extents  */
  resize_scrollbar(info);

  /*  render the patterns into the newly created image structure  */
  display_squares(info);

  return FALSE;
}


/**
 *
 */
static void
display_squares(struct square_info *info)
{
  int row, col;
  GList *pix;

  trace(" ");
  row = col = 0;
  pix=info->squares;
  while (pix)
   {
     struct t_square *s = (struct t_square *)pix->data;

     display_square(info, s, col, row);

     col++;
     if (col >= info->ncols)
      {
	row++;
	col=0;
      }
     pix=pix->next;
   }
}

/**
 *
 */

static void
display_square(struct square_info *info, struct t_square *s, int col, int row)
{
  GtkWidget *pixmap;
  GtkWidget *button;

  trace("x:%d y:%d name=%s",col,row,s->name);
  /* For now, we use only the first picture */
  pixmap = s->pixmaps[0];
  gtk_widget_show(pixmap);
  button = gtk_button_new();
  gtk_signal_connect( GTK_OBJECT(button), "clicked",
      GTK_SIGNAL_FUNC(display_square_properties), s);
  gtk_table_attach_defaults (GTK_TABLE (info->table), button,
      col,col+1,row,row+1);
  gtk_container_add (GTK_CONTAINER(button), pixmap);
  gtk_widget_show(button);
}


