summaryrefslogtreecommitdiff
path: root/tests/testtooltips.c
blob: 586d2ff4df8c0229e70fe2b396ee0ab070bba30b (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* testtooltips.c: Test application for GTK+ >= 2.12 tooltips code
 *
 * Copyright (C) 2006-2007  Imendio AB
 * Contact: Kristian Rietveld <kris@imendio.com>
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */

#include <gtk/gtk.h>

static gboolean
query_tooltip_cb (GtkWidget  *widget,
		  gint        x,
		  gint        y,
		  gboolean    keyboard_tip,
		  GtkTooltip *tooltip,
		  gpointer    data)
{
  gtk_tooltip_set_markup (tooltip, gtk_button_get_label (GTK_BUTTON (widget)));
  gtk_tooltip_set_icon_from_stock (tooltip, GTK_STOCK_DELETE,
				   GTK_ICON_SIZE_MENU);

  return TRUE;
}

static gboolean
query_tooltip_custom_cb (GtkWidget  *widget,
			 gint        x,
			 gint        y,
			 gboolean    keyboard_tip,
			 GtkTooltip *tooltip,
			 gpointer    data)
{
  GdkColor color = { 0, 0, 65535 };
  GtkWindow *window = gtk_widget_get_tooltip_window (widget);

  gtk_widget_modify_bg (GTK_WIDGET (window), GTK_STATE_NORMAL, &color);

  return TRUE;
}

static gboolean
query_tooltip_text_view_cb (GtkWidget  *widget,
			    gint        x,
			    gint        y,
			    gboolean    keyboard_tip,
			    GtkTooltip *tooltip,
			    gpointer    data)
{
  GtkTextTag *tag = data;
  GtkTextIter iter;
  GtkTextView *text_view = GTK_TEXT_VIEW (widget);

  if (keyboard_tip)
    {
      gint offset;

      g_object_get (text_view->buffer, "cursor-position", &offset, NULL);
      gtk_text_buffer_get_iter_at_offset (text_view->buffer, &iter, offset);
    }
  else
    {
      gint bx, by, trailing;

      gtk_text_view_window_to_buffer_coords (text_view, GTK_TEXT_WINDOW_TEXT,
					     x, y, &bx, &by);
      gtk_text_view_get_iter_at_position (text_view, &iter, &trailing, bx, by);
    }

  if (gtk_text_iter_has_tag (&iter, tag))
    gtk_tooltip_set_text (tooltip, "Tooltip on text tag");
  else
   return FALSE;

  return TRUE;
}

static gboolean
query_tooltip_tree_view_cb (GtkWidget  *widget,
			    gint        x,
			    gint        y,
			    gboolean    keyboard_tip,
			    GtkTooltip *tooltip,
			    gpointer    data)
{
  GtkTreeIter iter;
  GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
  GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
  GtkTreePath *path = NULL;
  gchar *tmp;
  gchar *pathstring;

  char buffer[512];

  if (!gtk_tree_view_get_tooltip_context (tree_view, &x, &y,
					  keyboard_tip,
					  &model, &path, &iter))
    return FALSE;

  gtk_tree_model_get (model, &iter, 0, &tmp, -1);
  pathstring = gtk_tree_path_to_string (path);

  g_snprintf (buffer, 511, "<b>Path %s:</b> %s", pathstring, tmp);
  gtk_tooltip_set_markup (tooltip, buffer);

  gtk_tree_view_set_tooltip_row (tree_view, tooltip, path);

  gtk_tree_path_free (path);
  g_free (pathstring);
  g_free (tmp);

  return TRUE;
}

static GtkTreeModel *
create_model (void)
{
  GtkTreeStore *store;
  GtkTreeIter iter;

  store = gtk_tree_store_new (1, G_TYPE_STRING);

  /* A tree store with some random words ... */
  gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
				     0, "File Manager", -1);
  gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
				     0, "Gossip", -1);
  gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
				     0, "System Settings", -1);
  gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
				     0, "The GIMP", -1);
  gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
				     0, "Terminal", -1);
  gtk_tree_store_insert_with_values (store, &iter, NULL, 0,
				     0, "Word Processor", -1);

  return GTK_TREE_MODEL (store);
}

static void
selection_changed_cb (GtkTreeSelection *selection,
		      GtkWidget        *tree_view)
{
  gtk_widget_trigger_tooltip_query (tree_view);
}

static struct Rectangle
{
  gint x;
  gint y;
  gfloat r;
  gfloat g;
  gfloat b;
  const char *tooltip;
}
rectangles[] =
{
  { 10, 10, 0.0, 0.0, 0.9, "Blue box!" },
  { 200, 170, 1.0, 0.0, 0.0, "Red thing" },
  { 100, 50, 0.8, 0.8, 0.0, "Yellow thing" }
};

static gboolean
query_tooltip_drawing_area_cb (GtkWidget  *widget,
			       gint        x,
			       gint        y,
			       gboolean    keyboard_tip,
			       GtkTooltip *tooltip,
			       gpointer    data)
{
  gint i;

  if (keyboard_tip)
    return FALSE;

  for (i = 0; i < G_N_ELEMENTS (rectangles); i++)
    {
      struct Rectangle *r = &rectangles[i];

      if (r->x < x && x < r->x + 50
	  && r->y < y && y < r->y + 50)
        {
	  gtk_tooltip_set_markup (tooltip, r->tooltip);
	  return TRUE;
	}
    }

  return FALSE;
}

static gboolean
drawing_area_expose (GtkWidget      *drawing_area,
		     GdkEventExpose *event,
		     gpointer        data)
{
  gint i;
  cairo_t *cr;

  gdk_window_get_pointer (drawing_area->window, NULL, NULL, NULL);

  cr = gdk_cairo_create (drawing_area->window);

  cairo_rectangle (cr, 0, 0,
		   drawing_area->allocation.width,
		   drawing_area->allocation.height);
  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_fill (cr);

  for (i = 0; i < G_N_ELEMENTS (rectangles); i++)
    {
      struct Rectangle *r = &rectangles[i];

      cairo_rectangle (cr, r->x, r->y, 50, 50);
      cairo_set_source_rgb (cr, r->r, r->g, r->b);
      cairo_stroke (cr);

      cairo_rectangle (cr, r->x, r->y, 50, 50);
      cairo_set_source_rgba (cr, r->r, r->g, r->b, 0.5);
      cairo_fill (cr);
    }

  cairo_destroy (cr);

  return FALSE;
}

static gboolean
query_tooltip_label_cb (GtkWidget  *widget,
			gint        x,
			gint        y,
			gboolean    keyboard_tip,
			GtkTooltip *tooltip,
			gpointer    data)
{
  GtkWidget *custom = data;

  gtk_tooltip_set_custom (tooltip, custom);

  return TRUE;
}

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

  GtkWidget *tooltip_window;
  GtkWidget *tooltip_button;

  GtkWidget *tree_view;
  GtkTreeViewColumn *column;

  GtkWidget *text_view;
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  GtkTextTag *tag;

  gchar *text, *markup;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Tooltips test");
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  g_signal_connect (window, "delete_event",
		    G_CALLBACK (gtk_main_quit), NULL);

  box = gtk_vbox_new (FALSE, 3);
  gtk_container_add (GTK_CONTAINER (window), box);

  /* A check button using the tooltip-markup property */
  button = gtk_check_button_new_with_label ("This one uses the tooltip-markup property");
  gtk_widget_set_tooltip_text (button, "Hello, I am a static tooltip.");
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  text = gtk_widget_get_tooltip_text (button);
  markup = gtk_widget_get_tooltip_markup (button);
  g_assert (g_str_equal ("Hello, I am a static tooltip.", text));
  g_assert (g_str_equal ("Hello, I am a static tooltip.", markup));
  g_free (text); g_free (markup);

  /* A check button using the query-tooltip signal */
  button = gtk_check_button_new_with_label ("I use the query-tooltip signal");
  g_object_set (button, "has-tooltip", TRUE, NULL);
  g_signal_connect (button, "query-tooltip",
		    G_CALLBACK (query_tooltip_cb), NULL);
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  /* A label */
  button = gtk_label_new ("I am just a label");
  gtk_label_set_selectable (GTK_LABEL (button), FALSE);
  gtk_widget_set_tooltip_text (button, "Label & and tooltip");
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  text = gtk_widget_get_tooltip_text (button);
  markup = gtk_widget_get_tooltip_markup (button);
  g_assert (g_str_equal ("Label & and tooltip", text));
  g_assert (g_str_equal ("Label &amp; and tooltip", markup));
  g_free (text); g_free (markup);

  /* A selectable label */
  button = gtk_label_new ("I am a selectable label");
  gtk_label_set_selectable (GTK_LABEL (button), TRUE);
  gtk_widget_set_tooltip_markup (button, "<b>Another</b> Label tooltip");
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  text = gtk_widget_get_tooltip_text (button);
  markup = gtk_widget_get_tooltip_markup (button);
  g_assert (g_str_equal ("Another Label tooltip", text));
  g_assert (g_str_equal ("<b>Another</b> Label tooltip", markup));
  g_free (text); g_free (markup);

  /* Another one, with a custom tooltip window */
  button = gtk_check_button_new_with_label ("This one has a custom tooltip window!");
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  tooltip_window = gtk_window_new (GTK_WINDOW_POPUP);
  tooltip_button = gtk_label_new ("blaat!");
  gtk_container_add (GTK_CONTAINER (tooltip_window), tooltip_button);
  gtk_widget_show (tooltip_button);

  gtk_widget_set_tooltip_window (button, GTK_WINDOW (tooltip_window));
  g_signal_connect (button, "query-tooltip",
		    G_CALLBACK (query_tooltip_custom_cb), NULL);
  g_object_set (button, "has-tooltip", TRUE, NULL);

  /* An insensitive button */
  button = gtk_button_new_with_label ("This one is insensitive");
  gtk_widget_set_sensitive (button, FALSE);
  g_object_set (button, "tooltip-text", "Insensitive!", NULL);
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  /* Testcases from Kris without a tree view don't exist. */
  tree_view = gtk_tree_view_new_with_model (create_model ());
  gtk_widget_set_size_request (tree_view, 200, 240);

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
					       0, "Test",
					       gtk_cell_renderer_text_new (),
					       "text", 0,
					       NULL);

  g_object_set (tree_view, "has-tooltip", TRUE, NULL);
  g_signal_connect (tree_view, "query-tooltip",
		    G_CALLBACK (query_tooltip_tree_view_cb), NULL);
  g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
		    "changed", G_CALLBACK (selection_changed_cb), tree_view);

  /* Set a tooltip on the column */
  column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree_view), 0);
  gtk_tree_view_column_set_clickable (column, TRUE);
  g_object_set (column->button, "tooltip-text", "Header", NULL);

  gtk_box_pack_start (GTK_BOX (box), tree_view, FALSE, FALSE, 2);

  /* And a text view for Matthias */
  buffer = gtk_text_buffer_new (NULL);

  gtk_text_buffer_get_end_iter (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, "Hello, the text ", -1);

  tag = gtk_text_buffer_create_tag (buffer, "bold", NULL);
  g_object_set (tag, "weight", PANGO_WEIGHT_BOLD, NULL);

  gtk_text_buffer_get_end_iter (buffer, &iter);
  gtk_text_buffer_insert_with_tags (buffer, &iter, "in bold", -1, tag, NULL);

  gtk_text_buffer_get_end_iter (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, " has a tooltip!", -1);

  text_view = gtk_text_view_new_with_buffer (buffer);
  gtk_widget_set_size_request (text_view, 200, 50);

  g_object_set (text_view, "has-tooltip", TRUE, NULL);
  g_signal_connect (text_view, "query-tooltip",
		    G_CALLBACK (query_tooltip_text_view_cb), tag);

  gtk_box_pack_start (GTK_BOX (box), text_view, FALSE, FALSE, 2);

  /* Drawing area */
  drawing_area = gtk_drawing_area_new ();
  gtk_widget_set_size_request (drawing_area, 320, 240);
  g_object_set (drawing_area, "has-tooltip", TRUE, NULL);
  g_signal_connect (drawing_area, "expose_event",
		    G_CALLBACK (drawing_area_expose), NULL);
  g_signal_connect (drawing_area, "query-tooltip",
		    G_CALLBACK (query_tooltip_drawing_area_cb), NULL);
  gtk_box_pack_start (GTK_BOX (box), drawing_area, FALSE, FALSE, 2);

  button = gtk_label_new ("Custom tooltip I");
  label = gtk_label_new ("See, custom");
  g_object_ref_sink (label);
  g_object_set (button, "has-tooltip", TRUE, NULL);
  g_signal_connect (button, "query-tooltip",
		    G_CALLBACK (query_tooltip_label_cb), label);
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 2);

  button = gtk_label_new ("Custom tooltip II");
  label = gtk_label_new ("See, custom, too");
  g_object_ref_sink (label);
  g_object_set (button, "has-tooltip", TRUE, NULL);
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 2);
  g_signal_connect (button, "query-tooltip",
		    G_CALLBACK (query_tooltip_label_cb), label);

  /* Done! */
  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}