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