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

GtkWidget *label;

static void
add_cb (GtkButton *button, gpointer user_data)
{
  GtkWidget *combo = user_data;
  gchar *temp;

  gtk_combo_box_prepend_text (GTK_COMBO_BOX (combo), "This is a test string");
  
  temp = g_strdup_printf("The combobox has %d rows",gtk_combo_box_get_num_entries_text (GTK_COMBO_BOX (combo)));
  gtk_label_set_text (GTK_LABEL(label), temp);
}

static void
remove_cb (GtkButton *button, gpointer user_data)
{
  GtkWidget *combo = user_data;
  gchar* temp;

  gtk_combo_box_remove_text (GTK_COMBO_BOX (combo), 0);

  temp = g_strdup_printf("The combobox has %d rows",gtk_combo_box_get_num_entries_text (GTK_COMBO_BOX (combo)));
  gtk_label_set_text (GTK_LABEL(label), temp);
}

static void
clear_cb (GtkButton *button, gpointer user_data)
{
  GtkWidget *combo = user_data;
  gchar *temp;

  gtk_combo_box_clear_text (GTK_COMBO_BOX (combo));

  temp = g_strdup_printf("The combobox has %d rows",gtk_combo_box_get_num_entries_text (GTK_COMBO_BOX (combo)));
  gtk_label_set_text (GTK_LABEL(label), temp);
}


int main (int argc, char **argv)
{
  GtkWidget *window, *vbox, *combobox, *addbutton, *removebutton, *clearbutton;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);

  g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);

  vbox = gtk_vbox_new (FALSE, 5);

  gtk_container_add (GTK_CONTAINER (window), vbox);

  combobox = gtk_combo_box_new_text ();

  addbutton = gtk_button_new_with_label ("Add test string");

  removebutton = gtk_button_new_with_label ("Remove test string");

  clearbutton = gtk_button_new_with_label ("Clear combobox");

  label = gtk_label_new ("Use buttons to add/remove rows");

  gtk_box_pack_start (GTK_BOX (vbox), combobox, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), addbutton, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), removebutton, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), clearbutton, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

  g_signal_connect (G_OBJECT (addbutton), "pressed", G_CALLBACK (add_cb),
                    GTK_COMBO_BOX (combobox));

  g_signal_connect (G_OBJECT (removebutton), "pressed", G_CALLBACK (remove_cb),
                    GTK_COMBO_BOX (combobox));

  g_signal_connect (G_OBJECT (clearbutton), "pressed", G_CALLBACK (clear_cb),
                    GTK_COMBO_BOX (combobox));

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
