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