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