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