]> Pileus Git - ~andy/gtk/blob - gtk/gtktooltip.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtktooltip.c
1 /* gtktooltip.c
2  *
3  * Copyright (C) 2006-2007 Imendio AB
4  * Contact: Kristian Rietveld <kris@imendio.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include "gtktooltip.h"
23
24 #include <math.h>
25 #include <string.h>
26
27 #include "gtkintl.h"
28 #include "gtkwindow.h"
29 #include "gtkmain.h"
30 #include "gtklabel.h"
31 #include "gtkimage.h"
32 #include "gtkbox.h"
33 #include "gtksettings.h"
34 #include "gtksizerequest.h"
35 #include "gtkstylecontext.h"
36 #include "gtkwindowprivate.h"
37
38
39 #ifdef GDK_WINDOWING_WAYLAND
40 #include "wayland/gdkwayland.h"
41 #endif
42
43
44 /**
45  * SECTION:gtktooltip
46  * @Short_description: Add tips to your widgets
47  * @Title: GtkTooltip
48  *
49  * Basic tooltips can be realized simply by using gtk_widget_set_tooltip_text()
50  * or gtk_widget_set_tooltip_markup() without any explicit tooltip object.
51  *
52  * When you need a tooltip with a little more fancy contents, like adding an
53  * image, or you want the tooltip to have different contents per #GtkTreeView
54  * row or cell, you will have to do a little more work:
55  * <itemizedlist>
56  * <listitem>
57  * <para>
58  * Set the #GtkWidget:has-tooltip property to %TRUE, this will make GTK+
59  * monitor the widget for motion and related events which are needed to
60  * determine when and where to show a tooltip.
61  * </para>
62  * </listitem>
63  * <listitem>
64  * <para>
65  * Connect to the #GtkWidget::query-tooltip signal.  This signal will be
66  * emitted when a tooltip is supposed to be shown. One of the arguments passed
67  * to the signal handler is a GtkTooltip object. This is the object that we
68  * are about to display as a tooltip, and can be manipulated in your callback
69  * using functions like gtk_tooltip_set_icon(). There are functions for setting
70  * the tooltip's markup, setting an image from a stock icon, or even putting in
71  * a custom widget.
72  * </para>
73  * </listitem>
74  * <listitem>
75  * <para>
76  * Return %TRUE from your query-tooltip handler. This causes the tooltip to be
77  * show. If you return %FALSE, it will not be shown.
78  * </para>
79  * </listitem>
80  * </itemizedlist>
81  *
82  * In the probably rare case where you want to have even more control over the
83  * tooltip that is about to be shown, you can set your own #GtkWindow which
84  * will be used as tooltip window.  This works as follows:
85  * <itemizedlist>
86  * <listitem>
87  * <para>
88  * Set #GtkWidget:has-tooltip and connect to #GtkWidget::query-tooltip as
89  * before.
90  * </para>
91  * </listitem>
92  * <listitem>
93  * <para>
94  * Use gtk_widget_set_tooltip_window() to set a #GtkWindow created by you as
95  * tooltip window.
96  * </para>
97  * </listitem>
98  * <listitem>
99  * <para>
100  * In the #GtkWidget::query-tooltip callback you can access your window using
101  * gtk_widget_get_tooltip_window() and manipulate as you wish. The semantics of
102  * the return value are exactly as before, return %TRUE to show the window,
103  * %FALSE to not show it.
104  * </para>
105  * </listitem>
106  * </itemizedlist>
107  */
108
109
110 #undef DEBUG_TOOLTIP
111
112
113 #define GTK_TOOLTIP_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
114 #define GTK_IS_TOOLTIP_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
115 #define GTK_TOOLTIP_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
116
117 typedef struct _GtkTooltipClass   GtkTooltipClass;
118
119 struct _GtkTooltip
120 {
121   GObject parent_instance;
122
123   GtkWidget *window;
124   GtkWidget *box;
125   GtkWidget *image;
126   GtkWidget *label;
127   GtkWidget *custom_widget;
128
129   GtkWindow *current_window;
130   GtkWidget *keyboard_widget;
131
132   GtkWidget *tooltip_widget;
133   GdkWindow *toplevel_window;
134
135   gdouble last_x;
136   gdouble last_y;
137   GdkWindow *last_window;
138
139   guint timeout_id;
140   guint browse_mode_timeout_id;
141
142   GdkRectangle tip_area;
143
144   guint browse_mode_enabled : 1;
145   guint keyboard_mode_enabled : 1;
146   guint tip_area_set : 1;
147   guint custom_was_reset : 1;
148 };
149
150 struct _GtkTooltipClass
151 {
152   GObjectClass parent_class;
153 };
154
155 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
156
157
158 static void       gtk_tooltip_class_init           (GtkTooltipClass *klass);
159 static void       gtk_tooltip_init                 (GtkTooltip      *tooltip);
160 static void       gtk_tooltip_dispose              (GObject         *object);
161
162 static gboolean   gtk_tooltip_paint_window         (GtkTooltip      *tooltip,
163                                                     cairo_t         *cr);
164 static void       gtk_tooltip_realize_window       (GtkTooltip      *tooltip,
165                                                     GtkWidget       *widget);
166 static void       gtk_tooltip_composited_changed   (GtkTooltip      *tooltip,
167                                                     GtkWidget       *widget);
168 static void       gtk_tooltip_window_hide          (GtkWidget       *widget,
169                                                     gpointer         user_data);
170 static void       gtk_tooltip_display_closed       (GdkDisplay      *display,
171                                                     gboolean         was_error,
172                                                     GtkTooltip      *tooltip);
173 static void       gtk_tooltip_set_last_window      (GtkTooltip      *tooltip,
174                                                     GdkWindow       *window);
175
176
177 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
178
179 static void
180 gtk_tooltip_class_init (GtkTooltipClass *klass)
181 {
182   GObjectClass *object_class;
183
184   object_class = G_OBJECT_CLASS (klass);
185
186   object_class->dispose = gtk_tooltip_dispose;
187 }
188
189 static void
190 gtk_tooltip_init (GtkTooltip *tooltip)
191 {
192   GtkStyleContext *context;
193   GtkWidget *window;
194   GtkWidget *box;
195   GtkWidget *image;
196   GtkWidget *label;
197   GdkScreen *screen;
198   GdkVisual *visual;
199
200   tooltip->timeout_id = 0;
201   tooltip->browse_mode_timeout_id = 0;
202
203   tooltip->browse_mode_enabled = FALSE;
204   tooltip->keyboard_mode_enabled = FALSE;
205
206   tooltip->current_window = NULL;
207   tooltip->keyboard_widget = NULL;
208
209   tooltip->tooltip_widget = NULL;
210   tooltip->toplevel_window = NULL;
211
212   tooltip->last_window = NULL;
213
214   window = gtk_window_new (GTK_WINDOW_POPUP);
215   screen = gtk_widget_get_screen (window);
216   visual = gdk_screen_get_rgba_visual (screen);
217
218   if (visual != NULL)
219     gtk_widget_set_visual (window, visual);
220
221   gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
222   gtk_widget_set_app_paintable (window, TRUE);
223   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
224   gtk_widget_set_name (window, "gtk-tooltip");
225   g_signal_connect (window, "hide",
226                     G_CALLBACK (gtk_tooltip_window_hide), tooltip);
227
228   context = gtk_widget_get_style_context (window);
229   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
230
231   g_signal_connect_swapped (window, "draw",
232                             G_CALLBACK (gtk_tooltip_paint_window), tooltip);
233   g_signal_connect_swapped (window, "realize",
234                             G_CALLBACK (gtk_tooltip_realize_window), tooltip);
235   g_signal_connect_swapped (window, "composited-changed",
236                             G_CALLBACK (gtk_tooltip_composited_changed), tooltip);
237
238   /* FIXME: don't hardcode the padding */
239   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
240   gtk_widget_set_margin_left (box, 6);
241   gtk_widget_set_margin_right (box, 6);
242   gtk_widget_set_margin_top (box, 6);
243   gtk_widget_set_margin_bottom (box, 6);
244   gtk_container_add (GTK_CONTAINER (window), box);
245   gtk_widget_show (box);
246
247   image = gtk_image_new ();
248   gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
249
250   label = gtk_label_new ("");
251   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
252   gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
253
254   tooltip->window = window;
255   tooltip->box = box;
256   tooltip->image = image;
257   tooltip->label = label;
258   tooltip->custom_widget = NULL;
259 }
260
261 static void
262 gtk_tooltip_dispose (GObject *object)
263 {
264   GtkTooltip *tooltip = GTK_TOOLTIP (object);
265
266   if (tooltip->timeout_id)
267     {
268       g_source_remove (tooltip->timeout_id);
269       tooltip->timeout_id = 0;
270     }
271
272   if (tooltip->browse_mode_timeout_id)
273     {
274       g_source_remove (tooltip->browse_mode_timeout_id);
275       tooltip->browse_mode_timeout_id = 0;
276     }
277
278   gtk_tooltip_set_custom (tooltip, NULL);
279   gtk_tooltip_set_last_window (tooltip, NULL);
280
281   if (tooltip->window)
282     {
283       GdkDisplay *display;
284
285       display = gtk_widget_get_display (tooltip->window);
286       g_signal_handlers_disconnect_by_func (display,
287                                             gtk_tooltip_display_closed,
288                                             tooltip);
289       gtk_widget_destroy (tooltip->window);
290       tooltip->window = NULL;
291     }
292
293   G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
294 }
295
296 /* public API */
297
298 /**
299  * gtk_tooltip_set_markup:
300  * @tooltip: a #GtkTooltip
301  * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
302  *
303  * Sets the text of the tooltip to be @markup, which is marked up
304  * with the <link
305  * linkend="PangoMarkupFormat">Pango text markup language</link>.
306  * If @markup is %NULL, the label will be hidden.
307  *
308  * Since: 2.12
309  */
310 void
311 gtk_tooltip_set_markup (GtkTooltip  *tooltip,
312                         const gchar *markup)
313 {
314   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
315
316   gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
317
318   if (markup)
319     gtk_widget_show (tooltip->label);
320   else
321     gtk_widget_hide (tooltip->label);
322 }
323
324 /**
325  * gtk_tooltip_set_text:
326  * @tooltip: a #GtkTooltip
327  * @text: (allow-none): a text string or %NULL
328  *
329  * Sets the text of the tooltip to be @text. If @text is %NULL, the label
330  * will be hidden. See also gtk_tooltip_set_markup().
331  *
332  * Since: 2.12
333  */
334 void
335 gtk_tooltip_set_text (GtkTooltip  *tooltip,
336                       const gchar *text)
337 {
338   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
339
340   gtk_label_set_text (GTK_LABEL (tooltip->label), text);
341
342   if (text)
343     gtk_widget_show (tooltip->label);
344   else
345     gtk_widget_hide (tooltip->label);
346 }
347
348 /**
349  * gtk_tooltip_set_icon:
350  * @tooltip: a #GtkTooltip
351  * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
352  *
353  * Sets the icon of the tooltip (which is in front of the text) to be
354  * @pixbuf.  If @pixbuf is %NULL, the image will be hidden.
355  *
356  * Since: 2.12
357  */
358 void
359 gtk_tooltip_set_icon (GtkTooltip *tooltip,
360                       GdkPixbuf  *pixbuf)
361 {
362   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
363   if (pixbuf)
364     g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
365
366   gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
367
368   if (pixbuf)
369     gtk_widget_show (tooltip->image);
370   else
371     gtk_widget_hide (tooltip->image);
372 }
373
374 /**
375  * gtk_tooltip_set_icon_from_stock:
376  * @tooltip: a #GtkTooltip
377  * @stock_id: (allow-none): a stock id, or %NULL
378  * @size: (type int): a stock icon size
379  *
380  * Sets the icon of the tooltip (which is in front of the text) to be
381  * the stock item indicated by @stock_id with the size indicated
382  * by @size.  If @stock_id is %NULL, the image will be hidden.
383  *
384  * Since: 2.12
385  */
386 void
387 gtk_tooltip_set_icon_from_stock (GtkTooltip  *tooltip,
388                                  const gchar *stock_id,
389                                  GtkIconSize  size)
390 {
391   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
392
393   gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
394
395   if (stock_id)
396     gtk_widget_show (tooltip->image);
397   else
398     gtk_widget_hide (tooltip->image);
399 }
400
401 /**
402  * gtk_tooltip_set_icon_from_icon_name:
403  * @tooltip: a #GtkTooltip
404  * @icon_name: (allow-none): an icon name, or %NULL
405  * @size: (type int): a stock icon size
406  *
407  * Sets the icon of the tooltip (which is in front of the text) to be
408  * the icon indicated by @icon_name with the size indicated
409  * by @size.  If @icon_name is %NULL, the image will be hidden.
410  *
411  * Since: 2.14
412  */
413 void
414 gtk_tooltip_set_icon_from_icon_name (GtkTooltip  *tooltip,
415                                      const gchar *icon_name,
416                                      GtkIconSize  size)
417 {
418   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
419
420   gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
421
422   if (icon_name)
423     gtk_widget_show (tooltip->image);
424   else
425     gtk_widget_hide (tooltip->image);
426 }
427
428 /**
429  * gtk_tooltip_set_icon_from_gicon:
430  * @tooltip: a #GtkTooltip
431  * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
432  * @size: (type int): a stock icon size
433  *
434  * Sets the icon of the tooltip (which is in front of the text)
435  * to be the icon indicated by @gicon with the size indicated
436  * by @size. If @gicon is %NULL, the image will be hidden.
437  *
438  * Since: 2.20
439  */
440 void
441 gtk_tooltip_set_icon_from_gicon (GtkTooltip  *tooltip,
442                                  GIcon       *gicon,
443                                  GtkIconSize  size)
444 {
445   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
446
447   gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
448
449   if (gicon)
450     gtk_widget_show (tooltip->image);
451   else
452     gtk_widget_hide (tooltip->image);
453 }
454
455 /**
456  * gtk_tooltip_set_custom:
457  * @tooltip: a #GtkTooltip
458  * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
459  *
460  * Replaces the widget packed into the tooltip with
461  * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
462  * away.
463  * By default a box with a #GtkImage and #GtkLabel is embedded in 
464  * the tooltip, which can be configured using gtk_tooltip_set_markup() 
465  * and gtk_tooltip_set_icon().
466
467  *
468  * Since: 2.12
469  */
470 void
471 gtk_tooltip_set_custom (GtkTooltip *tooltip,
472                         GtkWidget  *custom_widget)
473 {
474   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
475   if (custom_widget)
476     g_return_if_fail (GTK_IS_WIDGET (custom_widget));
477
478   /* The custom widget has been updated from the query-tooltip
479    * callback, so we do not want to reset the custom widget later on.
480    */
481   tooltip->custom_was_reset = TRUE;
482
483   /* No need to do anything if the custom widget stays the same */
484   if (tooltip->custom_widget == custom_widget)
485     return;
486
487   if (tooltip->custom_widget)
488     {
489       GtkWidget *custom = tooltip->custom_widget;
490       /* Note: We must reset tooltip->custom_widget first, 
491        * since gtk_container_remove() will recurse into 
492        * gtk_tooltip_set_custom()
493        */
494       tooltip->custom_widget = NULL;
495       gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
496       g_object_unref (custom);
497     }
498
499   if (custom_widget)
500     {
501       tooltip->custom_widget = g_object_ref (custom_widget);
502
503       gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
504       gtk_widget_show (custom_widget);
505     }
506 }
507
508 /**
509  * gtk_tooltip_set_tip_area:
510  * @tooltip: a #GtkTooltip
511  * @rect: a #GdkRectangle
512  *
513  * Sets the area of the widget, where the contents of this tooltip apply,
514  * to be @rect (in widget coordinates).  This is especially useful for
515  * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
516  * etc.
517  *
518  * For setting tooltips on #GtkTreeView, please refer to the convenience
519  * functions for this: gtk_tree_view_set_tooltip_row() and
520  * gtk_tree_view_set_tooltip_cell().
521  *
522  * Since: 2.12
523  */
524 void
525 gtk_tooltip_set_tip_area (GtkTooltip         *tooltip,
526                           const GdkRectangle *rect)
527 {
528   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
529
530   if (!rect)
531     tooltip->tip_area_set = FALSE;
532   else
533     {
534       tooltip->tip_area_set = TRUE;
535       tooltip->tip_area = *rect;
536     }
537 }
538
539 /**
540  * gtk_tooltip_trigger_tooltip_query:
541  * @display: a #GdkDisplay
542  *
543  * Triggers a new tooltip query on @display, in order to update the current
544  * visible tooltip, or to show/hide the current tooltip.  This function is
545  * useful to call when, for example, the state of the widget changed by a
546  * key press.
547  *
548  * Since: 2.12
549  */
550 void
551 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
552 {
553   gint x, y;
554   GdkWindow *window;
555   GdkEvent event;
556   GdkDevice *device;
557
558   /* Trigger logic as if the mouse moved */
559   device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
560   window = gdk_device_get_window_at_position (device, &x, &y);
561   if (!window)
562     return;
563
564   event.type = GDK_MOTION_NOTIFY;
565   event.motion.window = window;
566   event.motion.x = x;
567   event.motion.y = y;
568   event.motion.is_hint = FALSE;
569
570   gdk_window_get_root_coords (window, x, y, &x, &y);
571   event.motion.x_root = x;
572   event.motion.y_root = y;
573
574   _gtk_tooltip_handle_event (&event);
575 }
576
577 /* private functions */
578
579 static void
580 gtk_tooltip_reset (GtkTooltip *tooltip)
581 {
582   gtk_tooltip_set_markup (tooltip, NULL);
583   gtk_tooltip_set_icon (tooltip, NULL);
584   gtk_tooltip_set_tip_area (tooltip, NULL);
585
586   /* See if the custom widget is again set from the query-tooltip
587    * callback.
588    */
589   tooltip->custom_was_reset = FALSE;
590 }
591
592 static void
593 paint_background_and_frame (GtkTooltip *tooltip,
594                             cairo_t *cr)
595 {
596   GtkStyleContext *context;
597   gint width, height;
598
599   width = gtk_widget_get_allocated_width (tooltip->window);
600   height = gtk_widget_get_allocated_height (tooltip->window);
601   context = gtk_widget_get_style_context (tooltip->window);
602
603   gtk_render_background (context, cr,
604                          0, 0, width, height);
605   gtk_render_frame (context, cr,
606                     0, 0, width, height);  
607 }
608
609 static void
610 maybe_update_shape (GtkTooltip *tooltip)
611 {
612   cairo_t *cr;
613   cairo_surface_t *surface;
614   cairo_region_t *region;
615
616   /* fallback to XShape only for non-composited clients */
617   if (gtk_widget_is_composited (tooltip->window))
618     {
619       gtk_widget_shape_combine_region (tooltip->window, NULL);
620       return;
621     }
622
623   surface = gdk_window_create_similar_surface (gtk_widget_get_window (tooltip->window),
624                                                CAIRO_CONTENT_COLOR_ALPHA,
625                                                gtk_widget_get_allocated_width (tooltip->window),
626                                                gtk_widget_get_allocated_height (tooltip->window));
627
628   cr = cairo_create (surface);
629   paint_background_and_frame (tooltip, cr);
630   cairo_destroy (cr);
631
632   region = gdk_cairo_region_create_from_surface (surface);
633   gtk_widget_shape_combine_region (tooltip->window, region);
634
635   cairo_surface_destroy (surface);
636   cairo_region_destroy (region);
637 }
638
639 static void
640 gtk_tooltip_composited_changed (GtkTooltip *tooltip,
641                                 GtkWidget  *widget)
642 {
643   if (gtk_widget_get_realized (tooltip->window))
644     maybe_update_shape (tooltip);
645 }
646
647 static void
648 gtk_tooltip_realize_window (GtkTooltip *tooltip,
649                             GtkWidget *widget)
650 {
651   maybe_update_shape (tooltip);
652 }
653
654 static gboolean
655 gtk_tooltip_paint_window (GtkTooltip *tooltip,
656                           cairo_t    *cr)
657 {
658   if (gtk_widget_is_composited (tooltip->window))
659     {
660       /* clear any background */
661       cairo_save (cr);
662       cairo_set_source_rgba (cr, 0, 0, 0, 0);
663       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
664       cairo_paint (cr);
665       cairo_restore (cr);
666     }
667
668   maybe_update_shape (tooltip);
669   paint_background_and_frame (tooltip, cr);
670
671   return FALSE;
672 }
673
674 static void
675 gtk_tooltip_window_hide (GtkWidget *widget,
676                          gpointer   user_data)
677 {
678   GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
679
680   gtk_tooltip_set_custom (tooltip, NULL);
681 }
682
683 /* event handling, etc */
684
685 struct ChildLocation
686 {
687   GtkWidget *child;
688   GtkWidget *container;
689
690   gint x;
691   gint y;
692 };
693
694 static void
695 child_location_foreach (GtkWidget *child,
696                         gpointer   data)
697 {
698   GtkAllocation child_allocation;
699   gint x, y;
700   struct ChildLocation *child_loc = data;
701
702   /* Ignore invisible widgets */
703   if (!gtk_widget_is_drawable (child))
704     return;
705
706   gtk_widget_get_allocation (child, &child_allocation);
707
708   x = 0;
709   y = 0;
710
711   /* (child_loc->x, child_loc->y) are relative to
712    * child_loc->container's allocation.
713    */
714
715   if (!child_loc->child &&
716       gtk_widget_translate_coordinates (child_loc->container, child,
717                                         child_loc->x, child_loc->y,
718                                         &x, &y))
719     {
720 #ifdef DEBUG_TOOLTIP
721       g_print ("candidate: %s  alloc=[(%d,%d)  %dx%d]     (%d, %d)->(%d, %d)\n",
722                gtk_widget_get_name (child),
723                child_allocation.x,
724                child_allocation.y,
725                child_allocation.width,
726                child_allocation.height,
727                child_loc->x, child_loc->y,
728                x, y);
729 #endif /* DEBUG_TOOLTIP */
730
731       /* (x, y) relative to child's allocation. */
732       if (x >= 0 && x < child_allocation.width
733           && y >= 0 && y < child_allocation.height)
734         {
735           if (GTK_IS_CONTAINER (child))
736             {
737               struct ChildLocation tmp = { NULL, NULL, 0, 0 };
738
739               /* Take (x, y) relative the child's allocation and
740                * recurse.
741                */
742               tmp.x = x;
743               tmp.y = y;
744               tmp.container = child;
745
746               gtk_container_forall (GTK_CONTAINER (child),
747                                     child_location_foreach, &tmp);
748
749               if (tmp.child)
750                 child_loc->child = tmp.child;
751               else
752                 child_loc->child = child;
753             }
754           else
755             child_loc->child = child;
756         }
757     }
758 }
759
760 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
761  * to allocation relative (dest_x, dest_y) of dest_widget.
762  */
763 static void
764 window_to_alloc (GtkWidget *dest_widget,
765                  gint       src_x,
766                  gint       src_y,
767                  gint      *dest_x,
768                  gint      *dest_y)
769 {
770   GtkAllocation allocation;
771
772   gtk_widget_get_allocation (dest_widget, &allocation);
773
774   /* Translate from window relative to allocation relative */
775   if (gtk_widget_get_has_window (dest_widget) &&
776       gtk_widget_get_parent (dest_widget))
777     {
778       gint wx, wy;
779       gdk_window_get_position (gtk_widget_get_window (dest_widget),
780                                &wx, &wy);
781
782       /* Offset coordinates if widget->window is smaller than
783        * widget->allocation.
784        */
785       src_x += wx - allocation.x;
786       src_y += wy - allocation.y;
787     }
788   else
789     {
790       src_x -= allocation.x;
791       src_y -= allocation.y;
792     }
793
794   if (dest_x)
795     *dest_x = src_x;
796   if (dest_y)
797     *dest_y = src_y;
798 }
799
800 /* Translates coordinates from window relative (x, y) to
801  * allocation relative (x, y) of the returned widget.
802  */
803 GtkWidget *
804 _gtk_widget_find_at_coords (GdkWindow *window,
805                             gint       window_x,
806                             gint       window_y,
807                             gint      *widget_x,
808                             gint      *widget_y)
809 {
810   GtkWidget *event_widget;
811   struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
812
813   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
814
815   gdk_window_get_user_data (window, (void **)&event_widget);
816
817   if (!event_widget)
818     return NULL;
819
820 #ifdef DEBUG_TOOLTIP
821   g_print ("event window %p (belonging to %p (%s))  (%d, %d)\n",
822            window, event_widget, gtk_widget_get_name (event_widget),
823            window_x, window_y);
824 #endif
825
826   /* Coordinates are relative to event window */
827   child_loc.x = window_x;
828   child_loc.y = window_y;
829
830   /* We go down the window hierarchy to the widget->window,
831    * coordinates stay relative to the current window.
832    * We end up with window == widget->window, coordinates relative to that.
833    */
834   while (window && window != gtk_widget_get_window (event_widget))
835     {
836       gdouble px, py;
837
838       gdk_window_coords_to_parent (window,
839                                    child_loc.x, child_loc.y,
840                                    &px, &py);
841       child_loc.x = px;
842       child_loc.y = py;
843
844       window = gdk_window_get_effective_parent (window);
845     }
846
847   /* Failing to find widget->window can happen for e.g. a detached handle box;
848    * chaining ::query-tooltip up to its parent probably makes little sense,
849    * and users better implement tooltips on handle_box->child.
850    * so we simply ignore the event for tooltips here.
851    */
852   if (!window)
853     return NULL;
854
855   /* Convert the window relative coordinates to allocation
856    * relative coordinates.
857    */
858   window_to_alloc (event_widget,
859                    child_loc.x, child_loc.y,
860                    &child_loc.x, &child_loc.y);
861
862   if (GTK_IS_CONTAINER (event_widget))
863     {
864       GtkWidget *container = event_widget;
865
866       child_loc.container = event_widget;
867       child_loc.child = NULL;
868
869       gtk_container_forall (GTK_CONTAINER (event_widget),
870                             child_location_foreach, &child_loc);
871
872       /* Here we have a widget, with coordinates relative to
873        * child_loc.container's allocation.
874        */
875
876       if (child_loc.child)
877         event_widget = child_loc.child;
878       else if (child_loc.container)
879         event_widget = child_loc.container;
880
881       /* Translate to event_widget's allocation */
882       gtk_widget_translate_coordinates (container, event_widget,
883                                         child_loc.x, child_loc.y,
884                                         &child_loc.x, &child_loc.y);
885     }
886
887   /* We return (x, y) relative to the allocation of event_widget. */
888   if (widget_x)
889     *widget_x = child_loc.x;
890   if (widget_y)
891     *widget_y = child_loc.y;
892
893   return event_widget;
894 }
895
896 /* Ignores (x, y) on input, translates event coordinates to
897  * allocation relative (x, y) of the returned widget.
898  */
899 static GtkWidget *
900 find_topmost_widget_coords_from_event (GdkEvent *event,
901                                        gint     *x,
902                                        gint     *y)
903 {
904   GtkAllocation allocation;
905   gint tx, ty;
906   gdouble dx, dy;
907   GtkWidget *tmp;
908
909   gdk_event_get_coords (event, &dx, &dy);
910
911   /* Returns coordinates relative to tmp's allocation. */
912   tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
913
914   if (!tmp)
915     return NULL;
916
917   /* Make sure the pointer can actually be on the widget returned. */
918   gtk_widget_get_allocation (tmp, &allocation);
919   if (tx < 0 || tx >= allocation.width ||
920       ty < 0 || ty >= allocation.height)
921     return NULL;
922
923   if (x)
924     *x = tx;
925   if (y)
926     *y = ty;
927
928   return tmp;
929 }
930
931 static gint
932 tooltip_browse_mode_expired (gpointer data)
933 {
934   GtkTooltip *tooltip;
935
936   tooltip = GTK_TOOLTIP (data);
937
938   tooltip->browse_mode_enabled = FALSE;
939   tooltip->browse_mode_timeout_id = 0;
940
941   /* destroy tooltip */
942   g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
943                      "gdk-display-current-tooltip", NULL);
944
945   return FALSE;
946 }
947
948 static void
949 gtk_tooltip_display_closed (GdkDisplay *display,
950                             gboolean    was_error,
951                             GtkTooltip *tooltip)
952 {
953   g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
954 }
955
956 static void
957 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
958                              GdkWindow  *window)
959 {
960   if (tooltip->last_window == window)
961     return;
962
963   if (tooltip->last_window)
964     g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
965                                   (gpointer *) &tooltip->last_window);
966
967   tooltip->last_window = window;
968
969   if (window)
970     g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
971                                (gpointer *) &tooltip->last_window);
972 }
973
974 static gboolean
975 gtk_tooltip_run_requery (GtkWidget  **widget,
976                          GtkTooltip  *tooltip,
977                          gint        *x,
978                          gint        *y)
979 {
980   gboolean has_tooltip = FALSE;
981   gboolean return_value = FALSE;
982
983   gtk_tooltip_reset (tooltip);
984
985   do
986     {
987       g_object_get (*widget,
988                     "has-tooltip", &has_tooltip,
989                     NULL);
990
991       if (has_tooltip)
992         g_signal_emit_by_name (*widget,
993                                "query-tooltip",
994                                *x, *y,
995                                tooltip->keyboard_mode_enabled,
996                                tooltip,
997                                &return_value);
998
999       if (!return_value)
1000         {
1001           GtkWidget *parent = gtk_widget_get_parent (*widget);
1002
1003           if (parent)
1004             gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
1005
1006           *widget = parent;
1007         }
1008       else
1009         break;
1010     }
1011   while (*widget);
1012
1013   /* If the custom widget was not reset in the query-tooltip
1014    * callback, we clear it here.
1015    */
1016   if (!tooltip->custom_was_reset)
1017     gtk_tooltip_set_custom (tooltip, NULL);
1018
1019   return return_value;
1020 }
1021
1022 static void
1023 get_bounding_box (GtkWidget    *widget,
1024                   GdkRectangle *bounds)
1025 {
1026   GtkAllocation allocation;
1027   GdkWindow *window;
1028   gint x, y;
1029   gint w, h;
1030   gint x1, y1;
1031   gint x2, y2;
1032   gint x3, y3;
1033   gint x4, y4;
1034
1035   window = gtk_widget_get_parent_window (widget);
1036   if (window == NULL)
1037     window = gtk_widget_get_window (widget);
1038
1039   gtk_widget_get_allocation (widget, &allocation);
1040   x = allocation.x;
1041   y = allocation.y;
1042   w = allocation.width;
1043   h = allocation.height;
1044
1045   gdk_window_get_root_coords (window, x, y, &x1, &y1);
1046   gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
1047   gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
1048   gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
1049
1050 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
1051 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
1052
1053   bounds->x = floor (MIN4 (x1, x2, x3, x4));
1054   bounds->y = floor (MIN4 (y1, y2, y3, y4));
1055   bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
1056   bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
1057 }
1058
1059 static void
1060 gtk_tooltip_position (GtkTooltip *tooltip,
1061                       GdkDisplay *display,
1062                       GtkWidget  *new_tooltip_widget)
1063 {
1064   gint x, y, width, height;
1065   GdkScreen *screen;
1066   gint monitor_num;
1067   GdkRectangle monitor;
1068   guint cursor_size;
1069   GdkRectangle bounds;
1070
1071 #define MAX_DISTANCE 32
1072
1073   gtk_widget_realize (GTK_WIDGET (tooltip->current_window));
1074
1075   tooltip->tooltip_widget = new_tooltip_widget;
1076
1077   screen = gtk_widget_get_screen (new_tooltip_widget);
1078
1079   width = gtk_widget_get_allocated_width (GTK_WIDGET (tooltip->current_window));
1080   height = gtk_widget_get_allocated_height (GTK_WIDGET (tooltip->current_window));
1081
1082   monitor_num = gdk_screen_get_monitor_at_point (screen,
1083                                                  tooltip->last_x,
1084                                                  tooltip->last_y);
1085   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1086
1087   get_bounding_box (new_tooltip_widget, &bounds);
1088
1089   /* Position the tooltip */
1090
1091   cursor_size = gdk_display_get_default_cursor_size (display);
1092
1093   /* Try below */
1094   x = bounds.x + bounds.width / 2 - width / 2;
1095   y = bounds.y + bounds.height + 4;
1096
1097   if (y + height <= monitor.y + monitor.height)
1098     {
1099       if (tooltip->keyboard_mode_enabled)
1100         goto found;
1101
1102       if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
1103         {
1104           if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1105             x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1106           else if (x + width < tooltip->last_x - MAX_DISTANCE)
1107             x = tooltip->last_x - MAX_DISTANCE - width;
1108
1109           goto found;
1110         }
1111    }
1112
1113   /* Try above */
1114   x = bounds.x + bounds.width / 2 - width / 2;
1115   y = bounds.y - height - 4;
1116
1117   if (y >= monitor.y)
1118     {
1119       if (tooltip->keyboard_mode_enabled)
1120         goto found;
1121
1122       if (y + height >= tooltip->last_y - MAX_DISTANCE)
1123         {
1124           if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1125             x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1126           else if (x + width < tooltip->last_x - MAX_DISTANCE)
1127             x = tooltip->last_x - MAX_DISTANCE - width;
1128
1129           goto found;
1130         }
1131     }
1132
1133   /* Try right FIXME: flip on rtl ? */
1134   x = bounds.x + bounds.width + 4;
1135   y = bounds.y + bounds.height / 2 - height / 2;
1136
1137   if (x + width <= monitor.x + monitor.width)
1138     {
1139       if (tooltip->keyboard_mode_enabled)
1140         goto found;
1141
1142       if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
1143         {
1144           if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1145             y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1146           else if (y + height < tooltip->last_y - MAX_DISTANCE)
1147             y = tooltip->last_y - MAX_DISTANCE - height;
1148
1149           goto found;
1150         }
1151     }
1152
1153   /* Try left FIXME: flip on rtl ? */
1154   x = bounds.x - width - 4;
1155   y = bounds.y + bounds.height / 2 - height / 2;
1156
1157   if (x >= monitor.x)
1158     {
1159       if (tooltip->keyboard_mode_enabled)
1160         goto found;
1161
1162       if (x + width >= tooltip->last_x - MAX_DISTANCE)
1163         {
1164           if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1165             y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1166           else if (y + height < tooltip->last_y - MAX_DISTANCE)
1167             y = tooltip->last_y - MAX_DISTANCE - height;
1168
1169           goto found;
1170         }
1171     }
1172
1173    /* Fallback */
1174   if (tooltip->keyboard_mode_enabled)
1175     {
1176       x = bounds.x + bounds.width / 2 - width / 2;
1177       y = bounds.y + bounds.height + 4;
1178     }
1179   else
1180     {
1181       /* At cursor */
1182       x = tooltip->last_x + cursor_size * 3 / 4;
1183       y = tooltip->last_y + cursor_size * 3 / 4;
1184     }
1185
1186 found:
1187   /* Show it */
1188   if (tooltip->current_window)
1189     {
1190       if (x + width > monitor.x + monitor.width)
1191         x -= x - (monitor.x + monitor.width) + width;
1192       else if (x < monitor.x)
1193         x = monitor.x;
1194
1195       if (y + height > monitor.y + monitor.height)
1196         y -= y - (monitor.y + monitor.height) + height;
1197       else if (y < monitor.y)
1198         y = monitor.y;
1199
1200       if (!tooltip->keyboard_mode_enabled)
1201         {
1202           /* don't pop up under the pointer */
1203           if (x <= tooltip->last_x && tooltip->last_x < x + width &&
1204               y <= tooltip->last_y && tooltip->last_y < y + height)
1205             y = tooltip->last_y - height - 2;
1206         }
1207
1208 #ifdef GDK_WINDOWING_WAYLAND
1209       /* set the transient parent on the tooltip when running with the Wayland
1210        * backend to allow correct positioning of the tooltip windows */
1211       if (GDK_IS_WAYLAND_DISPLAY (display))
1212         {
1213           GtkWidget *toplevel;
1214
1215           toplevel = gtk_widget_get_toplevel (tooltip->tooltip_widget);
1216           if (GTK_IS_WINDOW (toplevel))
1217             gtk_window_set_transient_for (GTK_WINDOW (tooltip->current_window),
1218                                           GTK_WINDOW (toplevel));
1219         }
1220 #endif
1221
1222       gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1223       gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1224     }
1225 }
1226
1227 static void
1228 gtk_tooltip_show_tooltip (GdkDisplay *display)
1229 {
1230   gint x, y;
1231   GdkScreen *screen;
1232
1233   GdkWindow *window;
1234   GtkWidget *tooltip_widget;
1235   GtkTooltip *tooltip;
1236   gboolean has_tooltip;
1237   gboolean return_value = FALSE;
1238
1239   tooltip = g_object_get_data (G_OBJECT (display),
1240                                "gdk-display-current-tooltip");
1241
1242   if (tooltip->keyboard_mode_enabled)
1243     {
1244       x = y = -1;
1245       tooltip_widget = tooltip->keyboard_widget;
1246     }
1247   else
1248     {
1249       GdkDevice *device;
1250       gint tx, ty;
1251
1252       window = tooltip->last_window;
1253
1254       if (!GDK_IS_WINDOW (window))
1255         return;
1256
1257       device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
1258
1259       gdk_window_get_device_position (window, device, &x, &y, NULL);
1260
1261       gdk_window_get_root_coords (window, x, y, &tx, &ty);
1262       tooltip->last_x = tx;
1263       tooltip->last_y = ty;
1264
1265       tooltip_widget = _gtk_widget_find_at_coords (window, x, y, &x, &y);
1266     }
1267
1268   if (!tooltip_widget)
1269     return;
1270
1271   g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1272
1273   return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1274   if (!return_value)
1275     return;
1276
1277   if (!tooltip->current_window)
1278     {
1279       if (gtk_widget_get_tooltip_window (tooltip_widget))
1280         tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1281       else
1282         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1283     }
1284
1285   screen = gtk_widget_get_screen (tooltip_widget);
1286
1287   /* FIXME: should use tooltip->current_window iso tooltip->window */
1288   if (screen != gtk_widget_get_screen (tooltip->window))
1289     {
1290       g_signal_handlers_disconnect_by_func (display,
1291                                             gtk_tooltip_display_closed,
1292                                             tooltip);
1293
1294       gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1295
1296       g_signal_connect (display, "closed",
1297                         G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1298     }
1299
1300   gtk_tooltip_position (tooltip, display, tooltip_widget);
1301
1302   /* Now a tooltip is visible again on the display, make sure browse
1303    * mode is enabled.
1304    */
1305   tooltip->browse_mode_enabled = TRUE;
1306   if (tooltip->browse_mode_timeout_id)
1307     {
1308       g_source_remove (tooltip->browse_mode_timeout_id);
1309       tooltip->browse_mode_timeout_id = 0;
1310     }
1311 }
1312
1313 static void
1314 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1315 {
1316   if (!tooltip)
1317     return;
1318
1319   if (tooltip->timeout_id)
1320     {
1321       g_source_remove (tooltip->timeout_id);
1322       tooltip->timeout_id = 0;
1323     }
1324
1325   if (!GTK_TOOLTIP_VISIBLE (tooltip))
1326     return;
1327
1328   tooltip->tooltip_widget = NULL;
1329
1330   if (!tooltip->keyboard_mode_enabled)
1331     {
1332       guint timeout;
1333       GtkSettings *settings;
1334
1335       settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1336
1337       g_object_get (settings,
1338                     "gtk-tooltip-browse-mode-timeout", &timeout,
1339                     NULL);
1340
1341       /* The tooltip is gone, after (by default, should be configurable) 500ms
1342        * we want to turn off browse mode
1343        */
1344       if (!tooltip->browse_mode_timeout_id)
1345         tooltip->browse_mode_timeout_id =
1346           gdk_threads_add_timeout_full (0, timeout,
1347                                         tooltip_browse_mode_expired,
1348                                         g_object_ref (tooltip),
1349                                         g_object_unref);
1350     }
1351   else
1352     {
1353       if (tooltip->browse_mode_timeout_id)
1354         {
1355           g_source_remove (tooltip->browse_mode_timeout_id);
1356           tooltip->browse_mode_timeout_id = 0;
1357         }
1358     }
1359
1360   if (tooltip->current_window)
1361     {
1362       gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1363       tooltip->current_window = NULL;
1364     }
1365 }
1366
1367 static gint
1368 tooltip_popup_timeout (gpointer data)
1369 {
1370   GdkDisplay *display;
1371   GtkTooltip *tooltip;
1372
1373   display = GDK_DISPLAY (data);
1374   tooltip = g_object_get_data (G_OBJECT (display),
1375                                "gdk-display-current-tooltip");
1376
1377   /* This usually does not happen.  However, it does occur in language
1378    * bindings were reference counting of objects behaves differently.
1379    */
1380   if (!tooltip)
1381     return FALSE;
1382
1383   gtk_tooltip_show_tooltip (display);
1384
1385   tooltip->timeout_id = 0;
1386
1387   return FALSE;
1388 }
1389
1390 static void
1391 gtk_tooltip_start_delay (GdkDisplay *display)
1392 {
1393   guint timeout;
1394   GtkTooltip *tooltip;
1395   GtkSettings *settings;
1396
1397   tooltip = g_object_get_data (G_OBJECT (display),
1398                                "gdk-display-current-tooltip");
1399
1400   if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1401     return;
1402
1403   if (tooltip->timeout_id)
1404     g_source_remove (tooltip->timeout_id);
1405
1406   settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1407
1408   if (tooltip->browse_mode_enabled)
1409     g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1410   else
1411     g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1412
1413   tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1414                                                       tooltip_popup_timeout,
1415                                                       g_object_ref (display),
1416                                                       g_object_unref);
1417 }
1418
1419 void
1420 _gtk_tooltip_focus_in (GtkWidget *widget)
1421 {
1422   gint x, y;
1423   gboolean return_value = FALSE;
1424   GdkDisplay *display;
1425   GtkTooltip *tooltip;
1426   GdkDevice *device;
1427
1428   /* Get current tooltip for this display */
1429   display = gtk_widget_get_display (widget);
1430   tooltip = g_object_get_data (G_OBJECT (display),
1431                                "gdk-display-current-tooltip");
1432
1433   /* Check if keyboard mode is enabled at this moment */
1434   if (!tooltip || !tooltip->keyboard_mode_enabled)
1435     return;
1436
1437   device = gtk_get_current_event_device ();
1438
1439   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1440     device = gdk_device_get_associated_device (device);
1441
1442   /* This function should be called by either a focus in event,
1443    * or a key binding. In either case there should be a device.
1444    */
1445   if (!device)
1446     return;
1447
1448   if (tooltip->keyboard_widget)
1449     g_object_unref (tooltip->keyboard_widget);
1450
1451   tooltip->keyboard_widget = g_object_ref (widget);
1452
1453   gdk_window_get_device_position (gtk_widget_get_window (widget),
1454                                   device, &x, &y, NULL);
1455
1456   return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1457   if (!return_value)
1458     {
1459       gtk_tooltip_hide_tooltip (tooltip);
1460       return;
1461     }
1462
1463   if (!tooltip->current_window)
1464     {
1465       if (gtk_widget_get_tooltip_window (widget))
1466         tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1467       else
1468         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1469     }
1470
1471   gtk_tooltip_show_tooltip (display);
1472 }
1473
1474 void
1475 _gtk_tooltip_focus_out (GtkWidget *widget)
1476 {
1477   GdkDisplay *display;
1478   GtkTooltip *tooltip;
1479
1480   /* Get current tooltip for this display */
1481   display = gtk_widget_get_display (widget);
1482   tooltip = g_object_get_data (G_OBJECT (display),
1483                                "gdk-display-current-tooltip");
1484
1485   if (!tooltip || !tooltip->keyboard_mode_enabled)
1486     return;
1487
1488   if (tooltip->keyboard_widget)
1489     {
1490       g_object_unref (tooltip->keyboard_widget);
1491       tooltip->keyboard_widget = NULL;
1492     }
1493
1494   gtk_tooltip_hide_tooltip (tooltip);
1495 }
1496
1497 void
1498 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1499 {
1500   GdkDisplay *display;
1501   GtkTooltip *tooltip;
1502
1503   display = gtk_widget_get_display (widget);
1504   tooltip = g_object_get_data (G_OBJECT (display),
1505                                "gdk-display-current-tooltip");
1506
1507   if (!tooltip)
1508     {
1509       tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1510       g_object_set_data_full (G_OBJECT (display),
1511                               "gdk-display-current-tooltip",
1512                               tooltip, g_object_unref);
1513       g_signal_connect (display, "closed",
1514                         G_CALLBACK (gtk_tooltip_display_closed),
1515                         tooltip);
1516     }
1517
1518   tooltip->keyboard_mode_enabled ^= 1;
1519
1520   if (tooltip->keyboard_mode_enabled)
1521     {
1522       tooltip->keyboard_widget = g_object_ref (widget);
1523       _gtk_tooltip_focus_in (widget);
1524     }
1525   else
1526     {
1527       if (tooltip->keyboard_widget)
1528         {
1529           g_object_unref (tooltip->keyboard_widget);
1530           tooltip->keyboard_widget = NULL;
1531         }
1532
1533       gtk_tooltip_hide_tooltip (tooltip);
1534     }
1535 }
1536
1537 void
1538 _gtk_tooltip_hide (GtkWidget *widget)
1539 {
1540   GtkWidget *toplevel;
1541   GdkDisplay *display;
1542   GtkTooltip *tooltip;
1543
1544   display = gtk_widget_get_display (widget);
1545   tooltip = g_object_get_data (G_OBJECT (display),
1546                                "gdk-display-current-tooltip");
1547
1548   if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1549     return;
1550
1551   toplevel = gtk_widget_get_toplevel (widget);
1552
1553   if (widget == tooltip->tooltip_widget
1554       || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
1555     gtk_tooltip_hide_tooltip (tooltip);
1556 }
1557
1558 static gboolean
1559 tooltips_enabled (GdkEvent *event)
1560 {
1561   GdkDevice *source_device;
1562   GdkInputSource source;
1563   GdkWindow *window;
1564   gboolean enabled;
1565   GdkScreen *screen;
1566   GtkSettings *settings;
1567
1568   window = event->any.window;
1569   source_device = gdk_event_get_source_device (event);
1570
1571   if (!source_device)
1572     return FALSE;
1573
1574   source = gdk_device_get_source (source_device);
1575   screen = gdk_window_get_screen (window);
1576   settings = gtk_settings_get_for_screen (screen);
1577
1578   g_object_get (settings,
1579                 "gtk-enable-tooltips", &enabled,
1580                 NULL);
1581
1582   if (enabled && source != GDK_SOURCE_TOUCHSCREEN)
1583     return TRUE;
1584
1585   return FALSE;
1586 }
1587
1588 void
1589 _gtk_tooltip_handle_event (GdkEvent *event)
1590 {
1591   gint x, y;
1592   gboolean return_value = FALSE;
1593   GtkWidget *has_tooltip_widget = NULL;
1594   GdkDisplay *display;
1595   GtkTooltip *current_tooltip;
1596
1597   if (!tooltips_enabled (event))
1598     return;
1599
1600   /* Returns coordinates relative to has_tooltip_widget's allocation. */
1601   has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1602   display = gdk_window_get_display (event->any.window);
1603   current_tooltip = g_object_get_data (G_OBJECT (display),
1604                                        "gdk-display-current-tooltip");
1605
1606   if (current_tooltip)
1607     {
1608       gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1609     }
1610
1611   if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1612     {
1613       has_tooltip_widget = current_tooltip->keyboard_widget;
1614       if (!has_tooltip_widget)
1615         return;
1616
1617       return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1618                                               current_tooltip,
1619                                               &x, &y);
1620
1621       if (!return_value)
1622         gtk_tooltip_hide_tooltip (current_tooltip);
1623       else
1624         gtk_tooltip_start_delay (display);
1625
1626       return;
1627     }
1628
1629 #ifdef DEBUG_TOOLTIP
1630   if (has_tooltip_widget)
1631     g_print ("%p (%s) at (%d, %d) %dx%d     pointer: (%d, %d)\n",
1632              has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1633              has_tooltip_widget->allocation.x,
1634              has_tooltip_widget->allocation.y,
1635              has_tooltip_widget->allocation.width,
1636              has_tooltip_widget->allocation.height,
1637              x, y);
1638 #endif /* DEBUG_TOOLTIP */
1639
1640   /* Always poll for a next motion event */
1641   gdk_event_request_motions (&event->motion);
1642
1643   /* Hide the tooltip when there's no new tooltip widget */
1644   if (!has_tooltip_widget)
1645     {
1646       if (current_tooltip)
1647         gtk_tooltip_hide_tooltip (current_tooltip);
1648
1649       return;
1650     }
1651
1652   switch (event->type)
1653     {
1654       case GDK_BUTTON_PRESS:
1655       case GDK_2BUTTON_PRESS:
1656       case GDK_3BUTTON_PRESS:
1657       case GDK_KEY_PRESS:
1658       case GDK_DRAG_ENTER:
1659       case GDK_GRAB_BROKEN:
1660       case GDK_SCROLL:
1661         gtk_tooltip_hide_tooltip (current_tooltip);
1662         break;
1663
1664       case GDK_MOTION_NOTIFY:
1665       case GDK_ENTER_NOTIFY:
1666       case GDK_LEAVE_NOTIFY:
1667         if (current_tooltip)
1668           {
1669             gboolean tip_area_set;
1670             GdkRectangle tip_area;
1671             gboolean hide_tooltip;
1672
1673             tip_area_set = current_tooltip->tip_area_set;
1674             tip_area = current_tooltip->tip_area;
1675
1676             return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1677                                                     current_tooltip,
1678                                                     &x, &y);
1679
1680             /* Requested to be hidden? */
1681             hide_tooltip = !return_value;
1682
1683             /* Leave notify should override the query function */
1684             hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1685
1686             /* Is the pointer above another widget now? */
1687             if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1688               hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1689
1690             /* Did the pointer move out of the previous "context area"? */
1691             if (tip_area_set)
1692               hide_tooltip |= (x <= tip_area.x
1693                                || x >= tip_area.x + tip_area.width
1694                                || y <= tip_area.y
1695                                || y >= tip_area.y + tip_area.height);
1696
1697             if (hide_tooltip)
1698               gtk_tooltip_hide_tooltip (current_tooltip);
1699             else
1700               gtk_tooltip_start_delay (display);
1701           }
1702         else
1703           {
1704             /* Need a new tooltip for this display */
1705             current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1706             g_object_set_data_full (G_OBJECT (display),
1707                                     "gdk-display-current-tooltip",
1708                                     current_tooltip, g_object_unref);
1709             g_signal_connect (display, "closed",
1710                               G_CALLBACK (gtk_tooltip_display_closed),
1711                               current_tooltip);
1712
1713             gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1714
1715             gtk_tooltip_start_delay (display);
1716           }
1717         break;
1718
1719       default:
1720         break;
1721     }
1722 }