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