/* gcc -o move move.c $(pkg-config --cflags --libs gtk+-2.0) */

#include <gtk/gtk.h>

/* #define _MOVE */

static gboolean
gtk_widget_is_visible (GtkWidget* widget)
{
    g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
    return GTK_WIDGET_VISIBLE (widget);
}

static gboolean
timeout_show_cb (GtkWidget* dialog)
{
  #ifdef _MOVE
  gint x, y;
  #endif
  GtkWidget* himage;
  GtkWidget* vimage;

  #ifdef _MOVE
  gtk_window_get_position (GTK_WINDOW (dialog), &x, &y);
  gtk_window_move (GTK_WINDOW (dialog), x + 5, y + 5);
  #endif

  himage = (GtkWidget*)g_object_get_data (G_OBJECT (dialog), "himage");
  vimage = (GtkWidget*)g_object_get_data (G_OBJECT (dialog), "vimage");
  if (gtk_widget_is_visible (himage))
    {
      gtk_widget_hide (himage);
      gtk_widget_hide (vimage);
    }
  else
    {
      gtk_widget_show (himage);
      gtk_widget_show (vimage);
    }
}

static gboolean
timeout_close_cb (GtkWidget* dialog)
{
  gtk_main_quit ();
}

int
main (int    argc,
      gchar* argv[])
{
  GtkWidget* dialog;
  GtkWidget* label;
  GtkWidget* hbox;
  GtkWidget* vbox;
  GtkWidget* image;
  GtkWidget* combo;
  GtkWidget* entry;
  GtkEntryCompletion* completion;
  GtkListStore* model;
  GtkTreeIter iter;
  static const gchar* items [] = { "Wurst", "Wurstfachverkäufer", "Wurm", "Wussy" };
  guint i;


  gtk_init (&argc, &argv);
  g_set_application_name ("Testkiste");

  dialog = g_object_new (GTK_TYPE_DIALOG,
                         "icon-name", GTK_STOCK_ABOUT,
                         "title", "test",
                         "default-width", 400,
                         "default-height", 200,
                         NULL);
  g_signal_connect (dialog, "response",
                    G_CALLBACK (gtk_main_quit), dialog);


  label = gtk_label_new ("Es kommt ein Wandersmann daher, mit einer\n"
                         "Wasserflasche und einem Wurstbrot.");
  gtk_widget_show (label);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), label,
                      FALSE, FALSE, 2);

  hbox = gtk_hbox_new (FALSE, 2);

  image = gtk_image_new_from_stock (GTK_STOCK_HELP, GTK_ICON_SIZE_DIALOG);
  g_object_set_data (G_OBJECT (dialog), "himage", image);
  gtk_widget_show (image);
  gtk_box_pack_start (GTK_BOX (hbox), image,
                      FALSE, FALSE, 2);

  vbox = gtk_vbox_new (FALSE, 2);

  image = gtk_image_new_from_stock (GTK_STOCK_HELP, GTK_ICON_SIZE_DIALOG);
  g_object_set_data (G_OBJECT (dialog), "vimage", image);
  gtk_widget_show (image);
  gtk_box_pack_start (GTK_BOX (vbox), image,
                      FALSE, FALSE, 2);

  combo = gtk_combo_box_new_text ();
  for (i = 0; i < G_N_ELEMENTS (items); i++)
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), items[i]);
  gtk_widget_show (combo);
  gtk_box_pack_start (GTK_BOX (vbox), combo,
                      FALSE, FALSE, 2);

  /* we want the widget to have appears-as-list applied */
  gtk_rc_parse_string ("style \"custom-combo-style\" {\n"
                       "  GtkComboBox::appears-as-list = 1\n }\n"
                       "widget_class \"*GtkComboBoxEntry\" "
                       "style \"custom-combo-style\"\n");
  combo = gtk_combo_box_entry_new_text ();
  for (i = 0; i < G_N_ELEMENTS (items); i++)
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), items[i]);
  gtk_widget_show (combo);
  gtk_box_pack_start (GTK_BOX (vbox), combo,
                      FALSE, FALSE, 2);

  entry = gtk_entry_new ();
  completion = gtk_entry_completion_new ();

  model = gtk_list_store_new (1, G_TYPE_STRING);
  for (i = 0; i < G_N_ELEMENTS (items); i++)
  {
      gtk_list_store_append (model, &iter);
      gtk_list_store_set (model, &iter, 0, items[i], -1);
  }
  gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
  g_object_unref (model);
  gtk_entry_completion_set_text_column (completion, 0);

  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
  g_object_unref (completion);
  gtk_widget_show (entry);
  gtk_box_pack_start (GTK_BOX (vbox), entry,
                      FALSE, FALSE, 2);

  gtk_widget_show (vbox);
  gtk_box_pack_start (GTK_BOX (hbox), vbox,
                      TRUE, TRUE, 2);

  gtk_widget_show (hbox);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
                      TRUE, TRUE, 2);

  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                          "Hilf mir", GTK_RESPONSE_HELP,
                          "Weg hier", GTK_RESPONSE_OK,
                          NULL);

  gtk_widget_show (dialog);

  g_timeout_add_seconds (7, (GSourceFunc)timeout_show_cb, dialog);

  g_timeout_add_seconds (70, (GSourceFunc)timeout_close_cb, dialog);

  gtk_main ();
}