#include <gtk/gtk.h>

static GtkWidget *anim_box = NULL;

static GtkWidget *toggle_button = NULL;
static GtkWidget *bbox_1 = NULL;
static GtkWidget *bbox_2 = NULL;
static GtkWidget *image = NULL;

static gboolean image_added = FALSE;

static gboolean
button_cb (GtkToggleButton *button,
	         gpointer         user_data)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle_button)))
    gtk_container_add (GTK_CONTAINER (anim_box), bbox_2);
  else
    gtk_container_remove (GTK_CONTAINER (anim_box), bbox_2);

  return FALSE;
}

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
image_cb (GtkButton *button,
                gpointer   user_data)
{
  g_debug ("ADD IMAGE");

  if (image_added)
    gtk_container_remove (GTK_CONTAINER (anim_box), image);
  else
    gtk_container_add (GTK_CONTAINER (anim_box), image);

  image_added = !image_added;

  return FALSE;
}

int main(int argc, char *argv[])
{
  GtkWidget *window, *bbox, *button; 
  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);

  anim_box = gtk_anim_box_new (GTK_ORIENTATION_HORIZONTAL,
                               FALSE, 0);

  bbox = gtk_hbutton_box_new ();
  bbox_1 = gtk_hbutton_box_new ();

  image = gtk_image_new_from_file ("gnu.jpg");

  image = g_object_ref (image);

  toggle_button = gtk_toggle_button_new_with_label ("Animation");
  g_signal_connect (G_OBJECT (toggle_button), "clicked",
                    G_CALLBACK (button_cb), NULL);
  gtk_container_add (GTK_CONTAINER (bbox), toggle_button);

  button = gtk_button_new_from_stock (GTK_STOCK_OK);
  g_signal_connect (G_OBJECT (button), "clicked",
                    G_CALLBACK (orientation_cb), NULL);
  gtk_container_add (GTK_CONTAINER (bbox_1), button);

  bbox_2 = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
  g_signal_connect (G_OBJECT (bbox_2), "clicked",
                    G_CALLBACK (image_cb), NULL);
  bbox_2 = g_object_ref (bbox_2);

  gtk_container_add (GTK_CONTAINER (anim_box), bbox);
  gtk_container_add (GTK_CONTAINER (anim_box), bbox_1);

  gtk_container_add (GTK_CONTAINER (window), anim_box);

  gtk_widget_show_all (bbox_2);
  gtk_widget_show_all (image);

  gtk_widget_show_all (window);

  gtk_main ();
    
  return 0;
}
