#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>

GtkWidget *toolbars;
gint times;
gboolean flag;

gchar * my_stock_items[] = {
	   GTK_STOCK_ADD,
	   GTK_STOCK_APPLY,
	   GTK_STOCK_BOLD,
	   GTK_STOCK_CANCEL,
	   GTK_STOCK_CDROM,
	   GTK_STOCK_CLEAR,
	   GTK_STOCK_CLOSE,
	   GTK_STOCK_CONVERT,
	   GTK_STOCK_COPY,
	   GTK_STOCK_CUT,
	   GTK_STOCK_DELETE,
	   GTK_STOCK_EXECUTE,
	   GTK_STOCK_FIND,
	   GTK_STOCK_FIND_AND_REPLACE,
	   GTK_STOCK_FLOPPY,
	   GTK_STOCK_GOTO_BOTTOM,
	   GTK_STOCK_GOTO_FIRST,
	   GTK_STOCK_GOTO_LAST,
	   GTK_STOCK_GOTO_TOP,
	   GTK_STOCK_GO_BACK,
	   GTK_STOCK_GO_DOWN,
	   GTK_STOCK_GO_FORWARD,
	   GTK_STOCK_GO_UP,
	   GTK_STOCK_HELP,
	   GTK_STOCK_HOME,
	   GTK_STOCK_INDEX,
	   GTK_STOCK_ITALIC,
	   GTK_STOCK_JUMP_TO,
	   GTK_STOCK_JUSTIFY_CENTER,
	   GTK_STOCK_JUSTIFY_FILL,
	   GTK_STOCK_JUSTIFY_LEFT,
	   GTK_STOCK_JUSTIFY_RIGHT,
	   GTK_STOCK_MISSING_IMAGE,
	   GTK_STOCK_NEW,
	   GTK_STOCK_NO,
	   GTK_STOCK_OK,
	   GTK_STOCK_OPEN,
	   GTK_STOCK_PASTE,
	   GTK_STOCK_PREFERENCES,
	   GTK_STOCK_PRINT,
	   GTK_STOCK_PRINT_PREVIEW,
	   GTK_STOCK_PROPERTIES,
	   GTK_STOCK_QUIT,
	   GTK_STOCK_REDO,
	   GTK_STOCK_REFRESH,
	   GTK_STOCK_REMOVE,
	   GTK_STOCK_REVERT_TO_SAVED,
	   GTK_STOCK_SAVE,
	   GTK_STOCK_SAVE_AS,
	   GTK_STOCK_SELECT_COLOR,
	   GTK_STOCK_SELECT_FONT,
	   GTK_STOCK_SORT_ASCENDING,
	   GTK_STOCK_SORT_DESCENDING,
	   GTK_STOCK_SPELL_CHECK,
	   GTK_STOCK_STOP,
	   GTK_STOCK_STRIKETHROUGH,
	   GTK_STOCK_UNDELETE,
	   GTK_STOCK_UNDERLINE,
	   GTK_STOCK_UNDO,
	   GTK_STOCK_YES,
	   GTK_STOCK_ZOOM_100,
	   GTK_STOCK_ZOOM_FIT,
	   GTK_STOCK_ZOOM_IN,
	   GTK_STOCK_ZOOM_OUT,
};

void
cb_set_sensitive (GtkWidget *button, gpointer   data )
{
	   gtk_widget_set_sensitive (toolbars, TRUE);
}

void
cb_set_unsensitive (GtkWidget *button, gpointer   data )
{
	   gtk_widget_set_sensitive (toolbars, FALSE);
}

void
cb_toggle_style (GtkWidget *not_used)
{
	   static GtkToolbarStyle new_style = GTK_TOOLBAR_ICONS;
	   GList *l;
	   GtkToolbar *t;

	   new_style = (new_style == GTK_TOOLBAR_ICONS) ? GTK_TOOLBAR_TEXT : GTK_TOOLBAR_ICONS;

	   l = GTK_BOX (toolbars)->children;
	   while (l) {
			 t = GTK_TOOLBAR (((GtkBoxChild *)(l->data))->widget);
			 gtk_toolbar_set_style (t, new_style);
			 l = l->next;
	   }
}

gboolean
idle_cb (gpointer data)
{
	   if (flag)
			 cb_set_sensitive (NULL, NULL);
	   else
			 cb_set_unsensitive (NULL, NULL);
	   flag = !flag;
			 
	   return (--times != 0);
}

void
cb_toggle_sensitivity_multiple (GtkWidget *vbox)
{
	   flag = FALSE;
	   times = 20;
	   gtk_idle_add (idle_cb, NULL);
}


GtkWidget *
make_toolbars (void)
{
	   GtkWidget *vbox;
	   GtkWidget *w, *item;
	   GtkToolbar *t;
	   gint max, i;

	   vbox = gtk_vbox_new (FALSE, 10);
	   
	   w = gtk_toolbar_new ();
	   t = GTK_TOOLBAR (w);

	   max = sizeof (my_stock_items) / sizeof (gchar *);

	   for (i = max; i != 0; i--) {
			 item = gtk_toolbar_insert_stock (t, my_stock_items[i-1],
									    NULL, NULL, NULL, NULL, 0);
			 gtk_widget_show (item);
			 if (((i-1) % 15) == 0) {
				    gtk_box_pack_end (GTK_BOX (vbox), w, TRUE, TRUE, 1);
				    w = gtk_toolbar_new ();
				    t = GTK_TOOLBAR (w);
			 }
	   }

	   gtk_box_pack_start_defaults (GTK_BOX (vbox), w);
	   gtk_widget_show_all (vbox);
	   
	   return vbox;
}

gint
main (gint argc, gchar *argv[])
{
	   GtkWidget *window;
	   GtkWidget *vbox, *vbox2, *hbox;
	   GtkWidget *button;

	   gtk_init (&argc, &argv);

	   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	   gtk_widget_set_size_request (GTK_WIDGET (window), 600, 350);
	   gtk_window_set_title (GTK_WINDOW (window), "GTK toolbar test");
	   g_signal_connect (G_OBJECT (window), "delete_event",
					 G_CALLBACK (exit), NULL);

	   vbox = gtk_vbox_new (FALSE, 1);
	   gtk_container_add (GTK_CONTAINER (window), vbox);
	   gtk_widget_show (vbox);

	   /* Toolbar */
	   toolbars = make_toolbars ();
	   gtk_box_pack_start (GTK_BOX (vbox), toolbars, TRUE, TRUE, 2);

	   /* Buttons */
	   vbox2 = gtk_vbox_new (FALSE, 1);
	   hbox = gtk_hbox_new (FALSE, 1);
	   gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 1);
	   gtk_container_add (GTK_CONTAINER (vbox), hbox);
	   gtk_widget_show (vbox2);
	   gtk_widget_show (hbox);
	   
	   button = gtk_button_new_with_label ("Set sensitive");
	   g_signal_connect (G_OBJECT (button), "clicked",
					 G_CALLBACK (cb_set_sensitive), NULL);
	   gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, TRUE, 2);
	   gtk_widget_show (button);              
	   
	   button = gtk_button_new_with_label ("Set unsensitive");
	   g_signal_connect (G_OBJECT (button), "clicked",
					 G_CALLBACK (cb_set_unsensitive), NULL);
	   gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, TRUE, 2);
	   gtk_widget_show (button);

	   button = gtk_button_new_with_label ("Toggle sensitivity multiple");
	   g_signal_connect (G_OBJECT (button), "clicked",
					 G_CALLBACK (cb_toggle_sensitivity_multiple), NULL);
	   gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, TRUE, 2);
	   gtk_widget_show (button);
	   
	   
	   button = gtk_button_new_with_label ("Toggle style");
	   g_signal_connect (G_OBJECT (button), "clicked",
					 G_CALLBACK (cb_toggle_style), NULL);
	   gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, TRUE, 2);
	   gtk_widget_show (button);
	   
	   /* Go */
	   gtk_widget_show (window);

	   gtk_main ();

	   return 0;
}
