Dynamic tooltip in gtk and C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <gtk/gtk.h>

static void click(GtkWidget *widget, gpointer data)
{
  g_print("CLICK\n");
}

static void destroy(GtkWidget *widget, gpointer data)
{
  g_print("DESTROY\n");
  gtk_main_quit();
}

static gboolean query_tooltip(GtkWidget  *widget,
                              gint        x,
                              gint        y,
                              gboolean    keyboard_mode,
                              GtkTooltip *tooltip,
                              gpointer    user_data)
{
  g_print("QUERY TOOLTIP\n");
  gtk_tooltip_set_text(tooltip, "Prints a greeting.");
  return TRUE;
}

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

  gtk_init (&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);

  gtk_container_set_border_width(GTK_CONTAINER(window), 10);

  button = gtk_button_new_with_label("Hello World");

  gtk_widget_set_has_tooltip(button, TRUE);
  g_signal_connect(button, "query-tooltip", G_CALLBACK(query_tooltip), NULL);
  g_signal_connect(button, "clicked", G_CALLBACK(click), NULL);

  gtk_container_add(GTK_CONTAINER (window), button);
  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}