]> Pileus Git - ~andy/gtk/blob - tests/testtooltips.c
Introduce convenience property "GtkWidget:tooltip-text" taking care of
[~andy/gtk] / tests / testtooltips.c
1 /* testtooltips.c: Test application for GTK+ >= 2.12 tooltips code
2  *
3  * Copyright (C) 2006-2007  Imendio AB
4  * Contact: Kristian Rietveld <kris@imendio.com>
5  *
6  * This work is provided "as is"; redistribution and modification
7  * in whole or in part, in any medium, physical or electronic is
8  * permitted without restriction.
9  *
10  * This work is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * In no event shall the authors or contributors be liable for any
15  * direct, indirect, incidental, special, exemplary, or consequential
16  * damages (including, but not limited to, procurement of substitute
17  * goods or services; loss of use, data, or profits; or business
18  * interruption) however caused and on any theory of liability, whether
19  * in contract, strict liability, or tort (including negligence or
20  * otherwise) arising in any way out of the use of this software, even
21  * if advised of the possibility of such damage.
22  */
23
24 #include <gtk/gtk.h>
25
26 static gboolean
27 query_tooltip_cb (GtkWidget  *widget,
28                   gint        x,
29                   gint        y,
30                   gboolean    keyboard_tip,
31                   GtkTooltip *tooltip,
32                   gpointer    data)
33 {
34   gtk_tooltip_set_markup (tooltip, gtk_button_get_label (GTK_BUTTON (widget)));
35   gtk_tooltip_set_icon_from_stock (tooltip, GTK_STOCK_DELETE,
36                                    GTK_ICON_SIZE_MENU);
37
38   return TRUE;
39 }
40
41 static gboolean
42 query_tooltip_custom_cb (GtkWidget  *widget,
43                          gint        x,
44                          gint        y,
45                          gboolean    keyboard_tip,
46                          GtkTooltip *tooltip,
47                          gpointer    data)
48 {
49   GdkColor color = { 0, 0, 65535 };
50   GtkWindow *window = gtk_widget_get_tooltip_window (widget);
51
52   gtk_widget_modify_bg (GTK_WIDGET (window), GTK_STATE_NORMAL, &color);
53
54   return TRUE;
55 }
56
57 static gboolean
58 query_tooltip_text_view_cb (GtkWidget  *widget,
59                             gint        x,
60                             gint        y,
61                             gboolean    keyboard_tip,
62                             GtkTooltip *tooltip,
63                             gpointer    data)
64 {
65   GtkTextTag *tag = data;
66   GtkTextIter iter;
67   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
68
69   if (keyboard_tip)
70     {
71       gint offset;
72
73       g_object_get (text_view->buffer, "cursor-position", &offset, NULL);
74       gtk_text_buffer_get_iter_at_offset (text_view->buffer, &iter, offset);
75     }
76   else
77     {
78       gint bx, by, trailing;
79
80       gtk_text_view_window_to_buffer_coords (text_view, GTK_TEXT_WINDOW_TEXT,
81                                              x, y, &bx, &by);
82       gtk_text_view_get_iter_at_position (text_view, &iter, &trailing, bx, by);
83     }
84
85   if (gtk_text_iter_has_tag (&iter, tag))
86     gtk_tooltip_set_text (tooltip, "Tooltip on text tag");
87   else
88    return FALSE;
89
90   return TRUE;
91 }
92
93 static gboolean
94 query_tooltip_tree_view_cb (GtkWidget  *widget,
95                             gint        x,
96                             gint        y,
97                             gboolean    keyboard_tip,
98                             GtkTooltip *tooltip,
99                             gpointer    data)
100 {
101   GtkTreeIter iter;
102   GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
103   GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
104   GtkTreePath *path = NULL;
105   gchar *tmp;
106   gchar *pathstring;
107
108   char buffer[512];
109
110   if (keyboard_tip)
111     {
112       /* Keyboard mode */
113       gtk_tree_view_get_cursor (tree_view, &path, NULL);
114
115       if (!path)
116         return FALSE;
117     }
118   else
119     {
120       gint bin_x, bin_y;
121
122       gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y,
123                                                          &bin_x, &bin_y);
124
125       /* Mouse mode */
126       if (!gtk_tree_view_get_path_at_pos (tree_view, bin_x, bin_y,
127                                           &path, NULL, NULL, NULL))
128         return FALSE;
129     }
130
131   gtk_tree_model_get_iter (model, &iter, path);
132   gtk_tree_model_get (model, &iter, 0, &tmp, -1);
133   pathstring = gtk_tree_path_to_string (path);
134
135   g_snprintf (buffer, 511, "<b>Path %s:</b> %s", pathstring, tmp);
136   gtk_tooltip_set_markup (tooltip, buffer);
137
138   gtk_tree_path_free (path);
139   g_free (pathstring);
140   g_free (tmp);
141
142   return TRUE;
143 }
144
145 static GtkTreeModel *
146 create_model (void)
147 {
148   GtkTreeStore *store;
149   GtkTreeIter iter;
150
151   store = gtk_tree_store_new (1, G_TYPE_STRING);
152
153   /* A tree store with some random words ... */
154   gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
155                                      0, "File Manager", -1);
156   gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
157                                      0, "Gossip", -1);
158   gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
159                                      0, "System Settings", -1);
160   gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
161                                      0, "The GIMP", -1);
162   gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
163                                      0, "Terminal", -1);
164   gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
165                                      0, "Word Processor", -1);
166
167   return GTK_TREE_MODEL (store);
168 }
169
170 static void
171 selection_changed_cb (GtkTreeSelection *selection,
172                       GtkWidget        *tree_view)
173 {
174   gtk_widget_trigger_tooltip_query (tree_view);
175 }
176
177 static struct Rectangle
178 {
179   gint x;
180   gint y;
181   gfloat r;
182   gfloat g;
183   gfloat b;
184   const char *tooltip;
185 }
186 rectangles[] =
187 {
188   { 10, 10, 0.0, 0.0, 0.9, "Blue box!" },
189   { 200, 170, 1.0, 0.0, 0.0, "Red thing" },
190   { 100, 50, 0.8, 0.8, 0.0, "Yellow thing" }
191 };
192
193 static gboolean
194 query_tooltip_drawing_area_cb (GtkWidget  *widget,
195                                gint        x,
196                                gint        y,
197                                gboolean    keyboard_tip,
198                                GtkTooltip *tooltip,
199                                gpointer    data)
200 {
201   gint i;
202
203   if (keyboard_tip)
204     return FALSE;
205
206   for (i = 0; i < G_N_ELEMENTS (rectangles); i++)
207     {
208       struct Rectangle *r = &rectangles[i];
209
210       if (r->x < x && x < r->x + 50
211           && r->y < y && y < r->y + 50)
212         {
213           gtk_tooltip_set_markup (tooltip, r->tooltip);
214           return TRUE;
215         }
216     }
217
218   return FALSE;
219 }
220
221 static gboolean
222 drawing_area_expose (GtkWidget      *drawing_area,
223                      GdkEventExpose *event,
224                      gpointer        data)
225 {
226   gint i;
227   cairo_t *cr;
228
229   gdk_window_get_pointer (drawing_area->window, NULL, NULL, NULL);
230
231   cr = gdk_cairo_create (drawing_area->window);
232
233   cairo_rectangle (cr, 0, 0,
234                    drawing_area->allocation.width,
235                    drawing_area->allocation.height);
236   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
237   cairo_fill (cr);
238
239   for (i = 0; i < G_N_ELEMENTS (rectangles); i++)
240     {
241       struct Rectangle *r = &rectangles[i];
242
243       cairo_rectangle (cr, r->x, r->y, 50, 50);
244       cairo_set_source_rgb (cr, r->r, r->g, r->b);
245       cairo_stroke (cr);
246
247       cairo_rectangle (cr, r->x, r->y, 50, 50);
248       cairo_set_source_rgba (cr, r->r, r->g, r->b, 0.5);
249       cairo_fill (cr);
250     }
251
252   cairo_destroy (cr);
253
254   return FALSE;
255 }
256
257 int
258 main (int argc, char *argv[])
259 {
260   GtkWidget *window;
261   GtkWidget *box;
262   GtkWidget *drawing_area;
263   GtkWidget *button;
264
265   GtkWidget *tooltip_window;
266   GtkWidget *tooltip_button;
267
268   GtkWidget *tree_view;
269   GtkTreeViewColumn *column;
270
271   GtkWidget *text_view;
272   GtkTextBuffer *buffer;
273   GtkTextIter iter;
274   GtkTextTag *tag;
275
276   gtk_init (&argc, &argv);
277
278   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
279   gtk_window_set_title (GTK_WINDOW (window), "Tooltips test");
280   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
281   g_signal_connect (window, "delete_event",
282                     G_CALLBACK (gtk_main_quit), NULL);
283
284   box = gtk_vbox_new (FALSE, 3);
285   gtk_container_add (GTK_CONTAINER (window), box);
286
287   /* A check button using the tooltip-markup property */
288   button = gtk_check_button_new_with_label ("This one uses the tooltip-markup property");
289   gtk_widget_set_tooltip_text (button, "Hello, I am a static tooltip.");
290   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
291
292   /* A check button using the query-tooltip signal */
293   button = gtk_check_button_new_with_label ("I use the query-tooltip signal");
294   g_object_set (button, "has-tooltip", TRUE, NULL);
295   g_signal_connect (button, "query-tooltip",
296                     G_CALLBACK (query_tooltip_cb), NULL);
297   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
298
299   /* A label */
300   button = gtk_label_new ("I am just a label");
301   gtk_label_set_selectable (GTK_LABEL (button), FALSE);
302   gtk_widget_set_tooltip_text (button, "Label & and tooltip");
303   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
304
305   /* A selectable label */
306   button = gtk_label_new ("I am a selectable label");
307   gtk_label_set_selectable (GTK_LABEL (button), TRUE);
308   gtk_widget_set_tooltip_markup (button, "<b>Another</b> Label tooltip");
309   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
310
311   /* Another one, with a custom tooltip window */
312   button = gtk_check_button_new_with_label ("This one has a custom tooltip window!");
313   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
314
315   tooltip_window = gtk_window_new (GTK_WINDOW_POPUP);
316   tooltip_button = gtk_label_new ("blaat!");
317   gtk_container_add (GTK_CONTAINER (tooltip_window), tooltip_button);
318   gtk_widget_show (tooltip_button);
319
320   gtk_widget_set_tooltip_window (button, GTK_WINDOW (tooltip_window));
321   g_signal_connect (button, "query-tooltip",
322                     G_CALLBACK (query_tooltip_custom_cb), NULL);
323   g_object_set (button, "has-tooltip", TRUE, NULL);
324
325   /* An insensitive button */
326   button = gtk_button_new_with_label ("This one is insensitive");
327   gtk_widget_set_sensitive (button, FALSE);
328   g_object_set (button, "tooltip-text", "Insensitive!", NULL);
329   gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
330
331   /* Testcases from Kris without a tree view don't exist. */
332   tree_view = gtk_tree_view_new_with_model (create_model ());
333   gtk_widget_set_size_request (tree_view, 200, 240);
334
335   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
336                                                0, "Test",
337                                                gtk_cell_renderer_text_new (),
338                                                "text", 0,
339                                                NULL);
340
341   g_object_set (tree_view, "has-tooltip", TRUE, NULL);
342   g_signal_connect (tree_view, "query-tooltip",
343                     G_CALLBACK (query_tooltip_tree_view_cb), NULL);
344   g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
345                     "changed", G_CALLBACK (selection_changed_cb), tree_view);
346
347   /* Set a tooltip on the column */
348   column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree_view), 0);
349   gtk_tree_view_column_set_clickable (column, TRUE);
350   g_object_set (column->button, "tooltip-text", "Header", NULL);
351
352   gtk_box_pack_start (GTK_BOX (box), tree_view, FALSE, FALSE, 2);
353
354   /* And a text view for Matthias */
355   buffer = gtk_text_buffer_new (NULL);
356
357   gtk_text_buffer_get_end_iter (buffer, &iter);
358   gtk_text_buffer_insert (buffer, &iter, "Hello, the text ", -1);
359
360   tag = gtk_text_buffer_create_tag (buffer, "bold", NULL);
361   g_object_set (tag, "weight", PANGO_WEIGHT_BOLD, NULL);
362
363   gtk_text_buffer_get_end_iter (buffer, &iter);
364   gtk_text_buffer_insert_with_tags (buffer, &iter, "in bold", -1, tag, NULL);
365
366   gtk_text_buffer_get_end_iter (buffer, &iter);
367   gtk_text_buffer_insert (buffer, &iter, " has a tooltip!", -1);
368
369   text_view = gtk_text_view_new_with_buffer (buffer);
370   gtk_widget_set_size_request (text_view, 200, 50);
371
372   g_object_set (text_view, "has-tooltip", TRUE, NULL);
373   g_signal_connect (text_view, "query-tooltip",
374                     G_CALLBACK (query_tooltip_text_view_cb), tag);
375
376   gtk_box_pack_start (GTK_BOX (box), text_view, FALSE, FALSE, 2);
377
378   /* Drawing area */
379   drawing_area = gtk_drawing_area_new ();
380   gtk_widget_set_size_request (drawing_area, 320, 240);
381   g_object_set (drawing_area, "has-tooltip", TRUE, NULL);
382   g_signal_connect (drawing_area, "expose_event",
383                     G_CALLBACK (drawing_area_expose), NULL);
384   g_signal_connect (drawing_area, "query-tooltip",
385                     G_CALLBACK (query_tooltip_drawing_area_cb), NULL);
386   gtk_box_pack_start (GTK_BOX (box), drawing_area, FALSE, FALSE, 2);
387
388   /* Done! */
389   gtk_widget_show_all (window);
390
391   gtk_main ();
392
393   return 0;
394 }