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