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