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