/*
 * Repro for a problem with gtk_widget_trigger_tooltip_query().
 *
 * Build:
 * gcc -Wall -Wextra -Wno-unused $(pkg-config --cflags --libs gtk+-2.0) -g gtktip.c
 *
 * Run:
 * valgrind ./a.out
 *
 * Repro:
 * 1. Move your mouse into the window that pops up.
 * 2. A tooltip should appear.
 * 3. If a tooltip actually appears and valgrind doesn't print
 *   anything, move your mouse out of the window and try again.
 *
 * After less than 5 attempts I usually get this from valgrind:
 * ==3029== Conditional jump or move depends on uninitialised value(s)
 * ==3029==    at 0x426A930: child_location_foreach (gtktooltip.c:515)
 * ==3029==    by 0x40A1FFE: gtk_bin_forall (gtkbin.c:133)
 * ==3029==    by 0x40E99E5: gtk_container_forall (gtkcontainer.c:1453)
 * ==3029==    by 0x426A85A: find_widget_under_pointer (gtktooltip.c:640)
 * ==3029==    by 0x426B858: gtk_tooltip_show_tooltip (gtktooltip.c:900)
 * ==3029==    by 0x426B980: tooltip_popup_timeout (gtktooltip.c:1013)
 * ==3029==    by 0x43D878A: gdk_threads_dispatch (gdk.c:470)
 * ==3029==    by 0x45D88E5: g_timeout_dispatch (gmain.c:3446)
 * ==3029==    by 0x45D81B7: g_main_context_dispatch (gmain.c:2012)
 * ==3029==    by 0x45DB852: g_main_context_iterate (gmain.c:2645)
 * ==3029==    by 0x45DBD71: g_main_loop_run (gmain.c:2853)
 * ==3029==    by 0x4186BA8: gtk_main (gtkmain.c:1163)
 *
 * Commenting out the call to gtk_widget_trigger_tooltip_query() makes
 * valgrind happy (but me unhappy since I *want* to update the
 * tooltip).
 */
#include <gtk/gtk.h>

static void
on_destroy (GtkWidget * widget, gpointer data)
{
  gtk_main_quit();
}

static int
update_tooltip (gpointer pointer)
{
  static int i = 0;
  static char tip[200];
  GtkWidget *tipMe = pointer;

  sprintf(tip, "Tip #%d: Don't run with scissors", i++);
  gtk_widget_set_tooltip_text(tipMe, tip);
  gtk_widget_trigger_tooltip_query(tipMe);
  return TRUE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *label;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello World");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 50);
  g_signal_connect (G_OBJECT (window), "destroy",
		    G_CALLBACK (on_destroy), NULL);

  label = gtk_label_new ("Hover me and I'll give you a tip!");
  gtk_widget_set_tooltip_text(label, "Don't run with scissors");
  gtk_container_add(GTK_CONTAINER (window), label);

  gtk_widget_show_all(window);

  // Add a timeout that updates the tooltip once per second
  gtk_timeout_add(1000, update_tooltip, label);

  gtk_main ();

  return 0;
}

