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