#include <gtk/gtk.h>

#define WIDGET_COUNT 4

gchar *widget_names[] = 
{
  "Button", 
  "Textbox",
  "Calendar",
  "Scrolled window",
  "Image"
};

static GtkWidget *anim_box = NULL;

static GtkWidget *w1 = NULL;
static GtkWidget *w2 = NULL;
static GtkWidget *w3 = NULL;
static GtkWidget *w4 = NULL;
static GtkWidget *w5 = NULL;

static gboolean
orientation_cb (GtkButton *button,
                gpointer   user_data)
{
  GtkOrientation orientation;

  orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (anim_box));

  switch (orientation)
    {
      case GTK_ORIENTATION_HORIZONTAL:
        orientation = GTK_ORIENTATION_VERTICAL;
        break;
      case GTK_ORIENTATION_VERTICAL:
        orientation = GTK_ORIENTATION_HORIZONTAL;
        break;
    }

  gtk_anim_box_set_orientation (GTK_ANIM_BOX (anim_box), orientation);

  return FALSE;
}

static gboolean
toggle_widget_cb (GtkCheckButton *button,
                  gpointer        user_data)
{
  GtkWidget *widget = NULL;
  gint widget_number = 0;

  widget_number = GPOINTER_TO_INT (user_data);

  g_debug ("NUMBER: %d", widget_number);

  switch (widget_number)
    {
      case 0:
        widget = w1;
        break;
      case 1:
        widget = w2;
        break;
      case 2:
        widget = w3;
        break;
      case 3:
        widget = w4;
        break;
      case 4:
        widget = w5;
        break;
    }

  if (!widget)
    return FALSE;

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
    gtk_container_add (GTK_CONTAINER (anim_box), widget);
  else
    gtk_container_remove (GTK_CONTAINER (anim_box), widget);

  return FALSE;
}

static void
init_user_interface (GtkWidget *window)
{
  GtkWidget *box, *hbbox, *check_box, *widget;
  gint x = 0;

  box = gtk_hbox_new (FALSE, 0);
  hbbox = gtk_vbutton_box_new ();

  for (; x < WIDGET_COUNT; x++)
    {
      check_box = gtk_check_button_new_with_label (widget_names[x]);
      g_signal_connect (G_OBJECT (check_box), "clicked",
                        G_CALLBACK (toggle_widget_cb), GINT_TO_POINTER (x));

      gtk_container_add (GTK_CONTAINER (hbbox), check_box);
    }

  anim_box = gtk_anim_box_new (GTK_ORIENTATION_HORIZONTAL,
                               FALSE, 0);

  gtk_container_add (GTK_CONTAINER (box), anim_box);
  gtk_container_add (GTK_CONTAINER (box), hbbox);

  /* Orientation button */
  w1 = gtk_hbutton_box_new ();
  widget = gtk_button_new_with_label ("Orientation");
  g_signal_connect (G_OBJECT (widget), "clicked",
                    G_CALLBACK (orientation_cb), NULL);
  gtk_container_add (GTK_CONTAINER (w1), widget);
  w1 = g_object_ref (w1);
  gtk_widget_show_all (w1);

  /* Textbox */
  w2 = gtk_entry_new ();
  w2 = g_object_ref (w2);
  gtk_widget_show_all (w2);

  /* Calendar */
  w3 = gtk_calendar_new ();
  w3 = g_object_ref (w3);
  gtk_widget_show_all (w3);

  /* Textarea in scrolled window */
  w4 = gtk_scrolled_window_new (NULL, NULL);
  widget = gtk_image_new_from_file ("gnu.jpg");
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (w4), widget);
  //widget = gtk_text_view_new ();
  //gtk_container_add (GTK_CONTAINER (w4), widget);
  w4 = g_object_ref (w4);
  gtk_widget_show_all (w4);

  /* Image */
  /*
  w5 = gtk_image_new_from_file ("gnu.jpg");
  w5 = g_object_ref (w5);
  gtk_widget_show_all (w5);
  */

  gtk_container_add (GTK_CONTAINER (window), box);

  gtk_widget_show_all (window);
}

int main(int argc, char *argv[])
{
  GtkWidget *window; 
  GdkScreen *screen;
  GdkColormap *cmap;
    
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  screen = gtk_widget_get_screen (window);
  cmap = gdk_screen_get_rgba_colormap (screen);

  if (cmap && gdk_screen_is_composited (screen))
    gtk_widget_set_default_colormap (cmap);

  /* It's a good idea to do this for all windows. */
  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);

  g_signal_connect (G_OBJECT (window), "delete_event",
    		    G_CALLBACK (gtk_main_quit), NULL);


  init_user_interface (window);

  gtk_main ();
    
  return 0;
}
