]> Pileus Git - ~andy/gtk/blob - gtk/gtktooltip.c
Correct a small oversight
[~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: 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: 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: 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: a stock id, or %NULL
285  * @size: 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: an icon name, or %NULL
312  * @size: 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_from_gicon:
337  * @tooltip: a #GtkTooltip
338  * @gicon: a #GIcon representing the icon, or %NULL
339  * @size: a stock icon size
340  * Sets the icon of the tooltip (which is in front of the text) to be
341  * the icon indicated by @gicon with the size indicated
342  * by @size.  If @icon_name is %NULL, the image will be hidden.
343  *
344  * Since: 2.20
345  */
346 void
347 gtk_tooltip_set_icon_from_gicon (GtkTooltip  *tooltip,
348                                  GIcon       *gicon,
349                                  GtkIconSize  size)
350 {
351   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
352
353   gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
354
355   if (gicon)
356     gtk_widget_show (tooltip->image);
357   else
358     gtk_widget_hide (tooltip->image);
359 }
360
361 /**
362  * gtk_tooltip_set_custom:
363  * @tooltip: a #GtkTooltip
364  * @custom_widget: a #GtkWidget, or %NULL to unset the old custom widget.
365  *
366  * Replaces the widget packed into the tooltip with
367  * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
368  * away.
369  * By default a box with a #GtkImage and #GtkLabel is embedded in 
370  * the tooltip, which can be configured using gtk_tooltip_set_markup() 
371  * and gtk_tooltip_set_icon().
372
373  *
374  * Since: 2.12
375  */
376 void
377 gtk_tooltip_set_custom (GtkTooltip *tooltip,
378                         GtkWidget  *custom_widget)
379 {
380   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
381   if (custom_widget)
382     g_return_if_fail (GTK_IS_WIDGET (custom_widget));
383
384   /* The custom widget has been updated from the query-tooltip
385    * callback, so we do not want to reset the custom widget later on.
386    */
387   tooltip->custom_was_reset = TRUE;
388
389   /* No need to do anything if the custom widget stays the same */
390   if (tooltip->custom_widget == custom_widget)
391     return;
392
393   if (tooltip->custom_widget)
394     {
395       GtkWidget *custom = tooltip->custom_widget;
396       /* Note: We must reset tooltip->custom_widget first, 
397        * since gtk_container_remove() will recurse into 
398        * gtk_tooltip_set_custom()
399        */
400       tooltip->custom_widget = NULL;
401       gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
402       g_object_unref (custom);
403     }
404
405   if (custom_widget)
406     {
407       tooltip->custom_widget = g_object_ref (custom_widget);
408
409       gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
410       gtk_widget_show (custom_widget);
411     }
412 }
413
414 /**
415  * gtk_tooltip_set_tip_area:
416  * @tooltip: a #GtkTooltip
417  * @rect: a #GdkRectangle
418  *
419  * Sets the area of the widget, where the contents of this tooltip apply,
420  * to be @rect (in widget coordinates).  This is especially useful for
421  * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
422  * etc.
423  *
424  * For setting tooltips on #GtkTreeView, please refer to the convenience
425  * functions for this: gtk_tree_view_set_tooltip_row() and
426  * gtk_tree_view_set_tooltip_cell().
427  *
428  * Since: 2.12
429  */
430 void
431 gtk_tooltip_set_tip_area (GtkTooltip         *tooltip,
432                           const GdkRectangle *rect)
433 {
434   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
435
436   if (!rect)
437     tooltip->tip_area_set = FALSE;
438   else
439     {
440       tooltip->tip_area_set = TRUE;
441       tooltip->tip_area = *rect;
442     }
443 }
444
445 /**
446  * gtk_tooltip_trigger_tooltip_query:
447  * @display: a #GdkDisplay
448  *
449  * Triggers a new tooltip query on @display, in order to update the current
450  * visible tooltip, or to show/hide the current tooltip.  This function is
451  * useful to call when, for example, the state of the widget changed by a
452  * key press.
453  *
454  * Since: 2.12
455  */
456 void
457 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
458 {
459   gint x, y;
460   GdkWindow *window;
461   GdkEvent event;
462
463   /* Trigger logic as if the mouse moved */
464   window = gdk_display_get_window_at_pointer (display, &x, &y);
465   if (!window)
466     return;
467
468   event.type = GDK_MOTION_NOTIFY;
469   event.motion.window = window;
470   event.motion.x = x;
471   event.motion.y = y;
472   event.motion.is_hint = FALSE;
473
474   gdk_window_get_origin (window, &x, &y);
475   event.motion.x_root = event.motion.x + x;
476   event.motion.y_root = event.motion.y + y;
477
478   _gtk_tooltip_handle_event (&event);
479 }
480
481 /* private functions */
482
483 static void
484 gtk_tooltip_reset (GtkTooltip *tooltip)
485 {
486   gtk_tooltip_set_markup (tooltip, NULL);
487   gtk_tooltip_set_icon (tooltip, NULL);
488   gtk_tooltip_set_tip_area (tooltip, NULL);
489
490   /* See if the custom widget is again set from the query-tooltip
491    * callback.
492    */
493   tooltip->custom_was_reset = FALSE;
494 }
495
496 static void
497 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
498 {
499   gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
500                              tooltip->window->style->ythickness,
501                              tooltip->window->style->ythickness,
502                              tooltip->window->style->xthickness,
503                              tooltip->window->style->xthickness);
504   gtk_box_set_spacing (GTK_BOX (tooltip->hbox,
505                        tooltip->window->style->xthickness);
506
507   gtk_widget_queue_draw (tooltip->window);
508 }
509
510 static gboolean
511 gtk_tooltip_paint_window (GtkTooltip *tooltip)
512 {
513   gtk_paint_flat_box (tooltip->window->style,
514                       tooltip->window->window,
515                       GTK_STATE_NORMAL,
516                       GTK_SHADOW_OUT,
517                       NULL,
518                       tooltip->window,
519                       "tooltip",
520                       0, 0,
521                       tooltip->window->allocation.width,
522                       tooltip->window->allocation.height);
523
524   return FALSE;
525 }
526
527 static void
528 gtk_tooltip_window_hide (GtkWidget *widget,
529                          gpointer   user_data)
530 {
531   GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
532
533   gtk_tooltip_set_custom (tooltip, NULL);
534 }
535
536 /* event handling, etc */
537
538 struct ChildLocation
539 {
540   GtkWidget *child;
541   GtkWidget *container;
542
543   gint x;
544   gint y;
545 };
546
547 static void
548 child_location_foreach (GtkWidget *child,
549                         gpointer   data)
550 {
551   gint x, y;
552   struct ChildLocation *child_loc = data;
553
554   /* Ignore invisible widgets */
555   if (!GTK_WIDGET_DRAWABLE (child))
556     return;
557
558   x = 0;
559   y = 0;
560
561   /* (child_loc->x, child_loc->y) are relative to
562    * child_loc->container's allocation.
563    */
564
565   if (!child_loc->child &&
566       gtk_widget_translate_coordinates (child_loc->container, child,
567                                         child_loc->x, child_loc->y,
568                                         &x, &y))
569     {
570 #ifdef DEBUG_TOOLTIP
571       g_print ("candidate: %s  alloc=[(%d,%d)  %dx%d]     (%d, %d)->(%d, %d)\n",
572                gtk_widget_get_name (child),
573                child->allocation.x,
574                child->allocation.y,
575                child->allocation.width,
576                child->allocation.height,
577                child_loc->x, child_loc->y,
578                x, y);
579 #endif /* DEBUG_TOOLTIP */
580
581       /* (x, y) relative to child's allocation. */
582       if (x >= 0 && x < child->allocation.width
583           && y >= 0 && y < child->allocation.height)
584         {
585           if (GTK_IS_CONTAINER (child))
586             {
587               struct ChildLocation tmp = { NULL, NULL, 0, 0 };
588
589               /* Take (x, y) relative the child's allocation and
590                * recurse.
591                */
592               tmp.x = x;
593               tmp.y = y;
594               tmp.container = child;
595
596               gtk_container_forall (GTK_CONTAINER (child),
597                                     child_location_foreach, &tmp);
598
599               if (tmp.child)
600                 child_loc->child = tmp.child;
601               else
602                 child_loc->child = child;
603             }
604           else
605             child_loc->child = child;
606         }
607     }
608 }
609
610 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
611  * to allocation relative (dest_x, dest_y) of dest_widget.
612  */
613 static void
614 window_to_alloc (GtkWidget *dest_widget,
615                  gint       src_x,
616                  gint       src_y,
617                  gint      *dest_x,
618                  gint      *dest_y)
619 {
620   /* Translate from window relative to allocation relative */
621   if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
622     {
623       gint wx, wy;
624       gdk_window_get_position (dest_widget->window, &wx, &wy);
625
626       /* Offset coordinates if widget->window is smaller than
627        * widget->allocation.
628        */
629       src_x += wx - dest_widget->allocation.x;
630       src_y += wy - dest_widget->allocation.y;
631     }
632   else
633     {
634       src_x -= dest_widget->allocation.x;
635       src_y -= dest_widget->allocation.y;
636     }
637
638   if (dest_x)
639     *dest_x = src_x;
640   if (dest_y)
641     *dest_y = src_y;
642 }
643
644 /* Translates coordinates from window relative (x, y) to
645  * allocation relative (x, y) of the returned widget.
646  */
647 static GtkWidget *
648 find_widget_under_pointer (GdkWindow *window,
649                            gint      *x,
650                            gint      *y)
651 {
652   GtkWidget *event_widget;
653   struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
654
655   gdk_window_get_user_data (window, (void **)&event_widget);
656
657   if (!event_widget)
658     return NULL;
659
660 #ifdef DEBUG_TOOLTIP
661   g_print ("event window %p (belonging to %p (%s))  (%d, %d)\n",
662            window, event_widget, gtk_widget_get_name (event_widget),
663            *x, *y);
664 #endif
665
666   /* Coordinates are relative to event window */
667   child_loc.x = *x;
668   child_loc.y = *y;
669
670   /* We go down the window hierarchy to the widget->window,
671    * coordinates stay relative to the current window.
672    * We end up with window == widget->window, coordinates relative to that.
673    */
674   while (window && window != event_widget->window)
675     {
676       gint px, py;
677
678       gdk_window_get_position (window, &px, &py);
679       child_loc.x += px;
680       child_loc.y += py;
681
682       window = gdk_window_get_parent (window);
683     }
684
685   /* Failing to find widget->window can happen for e.g. a detached handle box;
686    * chaining ::query-tooltip up to its parent probably makes little sense,
687    * and users better implement tooltips on handle_box->child.
688    * so we simply ignore the event for tooltips here.
689    */
690   if (!window)
691     return NULL;
692
693   /* Convert the window relative coordinates to allocation
694    * relative coordinates.
695    */
696   window_to_alloc (event_widget,
697                    child_loc.x, child_loc.y,
698                    &child_loc.x, &child_loc.y);
699
700   if (GTK_IS_CONTAINER (event_widget))
701     {
702       GtkWidget *container = event_widget;
703
704       child_loc.container = event_widget;
705       child_loc.child = NULL;
706
707       gtk_container_forall (GTK_CONTAINER (event_widget),
708                             child_location_foreach, &child_loc);
709
710       /* Here we have a widget, with coordinates relative to
711        * child_loc.container's allocation.
712        */
713
714       if (child_loc.child)
715         event_widget = child_loc.child;
716       else if (child_loc.container)
717         event_widget = child_loc.container;
718
719       /* Translate to event_widget's allocation */
720       gtk_widget_translate_coordinates (container, event_widget,
721                                         child_loc.x, child_loc.y,
722                                         &child_loc.x, &child_loc.y);
723
724     }
725
726   /* We return (x, y) relative to the allocation of event_widget. */
727   if (x)
728     *x = child_loc.x;
729   if (y)
730     *y = child_loc.y;
731
732   return event_widget;
733 }
734
735 /* Ignores (x, y) on input, translates event coordinates to
736  * allocation relative (x, y) of the returned widget.
737  */
738 static GtkWidget *
739 find_topmost_widget_coords_from_event (GdkEvent *event,
740                                        gint     *x,
741                                        gint     *y)
742 {
743   gint tx, ty;
744   gdouble dx, dy;
745   GtkWidget *tmp;
746
747   gdk_event_get_coords (event, &dx, &dy);
748   tx = dx;
749   ty = dy;
750
751   /* Returns coordinates relative to tmp's allocation. */
752   tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
753
754   if (!tmp)
755     return NULL;
756
757   /* Make sure the pointer can actually be on the widget returned. */
758   if (tx < 0 || tx >= tmp->allocation.width ||
759       ty < 0 || ty >= tmp->allocation.height)
760     return NULL;
761
762   if (x)
763     *x = tx;
764   if (y)
765     *y = ty;
766
767   return tmp;
768 }
769
770 static gint
771 tooltip_browse_mode_expired (gpointer data)
772 {
773   GtkTooltip *tooltip;
774
775   tooltip = GTK_TOOLTIP (data);
776
777   tooltip->browse_mode_enabled = FALSE;
778   tooltip->browse_mode_timeout_id = 0;
779
780   /* destroy tooltip */
781   g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
782                      "gdk-display-current-tooltip", NULL);
783
784   return FALSE;
785 }
786
787 static void
788 gtk_tooltip_display_closed (GdkDisplay *display,
789                             gboolean    was_error,
790                             GtkTooltip *tooltip)
791 {
792   g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
793 }
794
795 static void
796 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
797                              GdkWindow  *window)
798 {
799   if (tooltip->last_window == window)
800     return;
801
802   if (tooltip->last_window)
803     g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
804                                   (gpointer *) &tooltip->last_window);
805
806   tooltip->last_window = window;
807
808   if (window)
809     g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
810                                (gpointer *) &tooltip->last_window);
811 }
812
813 static gboolean
814 gtk_tooltip_run_requery (GtkWidget  **widget,
815                          GtkTooltip  *tooltip,
816                          gint        *x,
817                          gint        *y)
818 {
819   gboolean has_tooltip = FALSE;
820   gboolean return_value = FALSE;
821
822   gtk_tooltip_reset (tooltip);
823
824   do
825     {
826       g_object_get (*widget,
827                     "has-tooltip", &has_tooltip,
828                     NULL);
829
830       if (has_tooltip)
831         g_signal_emit_by_name (*widget,
832                                "query-tooltip",
833                                *x, *y,
834                                tooltip->keyboard_mode_enabled,
835                                tooltip,
836                                &return_value);
837
838       if (!return_value)
839         {
840           GtkWidget *parent = (*widget)->parent;
841
842           if (parent)
843             gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
844
845           *widget = parent;
846         }
847       else
848         break;
849     }
850   while (*widget);
851
852   /* If the custom widget was not reset in the query-tooltip
853    * callback, we clear it here.
854    */
855   if (!tooltip->custom_was_reset)
856     gtk_tooltip_set_custom (tooltip, NULL);
857
858   return return_value;
859 }
860
861 static void
862 gtk_tooltip_position (GtkTooltip *tooltip,
863                       GdkDisplay *display,
864                       GtkWidget  *new_tooltip_widget)
865 {
866   gint x, y;
867   GdkScreen *screen;
868
869   tooltip->tooltip_widget = new_tooltip_widget;
870
871   /* Position the tooltip */
872   /* FIXME: should we swap this when RTL is enabled? */
873   if (tooltip->keyboard_mode_enabled)
874     {
875       gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
876       if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
877         {
878           x += new_tooltip_widget->allocation.x;
879           y += new_tooltip_widget->allocation.y;
880         }
881
882       /* For keyboard mode we position the tooltip below the widget,
883        * right of the center of the widget.
884        */
885       x += new_tooltip_widget->allocation.width / 2;
886       y += new_tooltip_widget->allocation.height + 4;
887     }
888   else
889     {
890       guint cursor_size;
891
892       x = tooltip->last_x;
893       y = tooltip->last_y;
894
895       /* For mouse mode, we position the tooltip right of the cursor,
896        * a little below the cursor's center.
897        */
898       cursor_size = gdk_display_get_default_cursor_size (display);
899       x += cursor_size / 2;
900       y += cursor_size / 2;
901     }
902
903   screen = gtk_widget_get_screen (new_tooltip_widget);
904
905   /* Show it */
906   if (tooltip->current_window)
907     {
908       gint monitor_num;
909       GdkRectangle monitor;
910       GtkRequisition requisition;
911
912       gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
913                                &requisition);
914
915       monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
916       gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
917
918       if (x + requisition.width > monitor.x + monitor.width)
919         x -= x - (monitor.x + monitor.width) + requisition.width;
920       else if (x < monitor.x)
921         x = monitor.x;
922
923       if (y + requisition.height > monitor.y + monitor.height)
924         y -= y - (monitor.y + monitor.height) + requisition.height;
925   
926       if (!tooltip->keyboard_mode_enabled)
927         {
928           /* don't pop up under the pointer */
929           if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
930               y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
931             y = tooltip->last_y - requisition.height - 2;
932         }
933   
934       gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
935       gtk_widget_show (GTK_WIDGET (tooltip->current_window));
936     }
937 }
938
939 static void
940 gtk_tooltip_show_tooltip (GdkDisplay *display)
941 {
942   gint x, y;
943   GdkScreen *screen;
944
945   GdkWindow *window;
946   GtkWidget *tooltip_widget;
947   GtkWidget *pointer_widget;
948   GtkTooltip *tooltip;
949   gboolean has_tooltip;
950   gboolean return_value = FALSE;
951
952   tooltip = g_object_get_data (G_OBJECT (display),
953                                "gdk-display-current-tooltip");
954
955   if (tooltip->keyboard_mode_enabled)
956     {
957       x = y = -1;
958       pointer_widget = tooltip_widget = tooltip->keyboard_widget;
959     }
960   else
961     {
962       window = tooltip->last_window;
963
964       if (!GDK_IS_WINDOW (window))
965         return;
966
967       gdk_window_get_origin (window, &x, &y);
968       x = tooltip->last_x - x;
969       y = tooltip->last_y - y;
970
971       pointer_widget = tooltip_widget = find_widget_under_pointer (window,
972                                                                    &x, &y);
973     }
974
975   if (!tooltip_widget)
976     return;
977
978   g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
979
980   return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
981   if (!return_value)
982     return;
983
984   if (!tooltip->current_window)
985     {
986       if (gtk_widget_get_tooltip_window (tooltip_widget))
987         tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
988       else
989         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
990     }
991
992   screen = gtk_widget_get_screen (tooltip_widget);
993
994   /* FIXME: should use tooltip->current_window iso tooltip->window */
995   if (screen != gtk_widget_get_screen (tooltip->window))
996     {
997       g_signal_handlers_disconnect_by_func (display,
998                                             gtk_tooltip_display_closed,
999                                             tooltip);
1000
1001       gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1002
1003       g_signal_connect (display, "closed",
1004                         G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1005     }
1006
1007   gtk_tooltip_position (tooltip, display, tooltip_widget);
1008
1009   /* Now a tooltip is visible again on the display, make sure browse
1010    * mode is enabled.
1011    */
1012   tooltip->browse_mode_enabled = TRUE;
1013   if (tooltip->browse_mode_timeout_id)
1014     {
1015       g_source_remove (tooltip->browse_mode_timeout_id);
1016       tooltip->browse_mode_timeout_id = 0;
1017     }
1018 }
1019
1020 static void
1021 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1022 {
1023   if (!tooltip)
1024     return;
1025
1026   if (tooltip->timeout_id)
1027     {
1028       g_source_remove (tooltip->timeout_id);
1029       tooltip->timeout_id = 0;
1030     }
1031
1032   if (!GTK_TOOLTIP_VISIBLE (tooltip))
1033     return;
1034
1035   tooltip->tooltip_widget = NULL;
1036
1037   if (!tooltip->keyboard_mode_enabled)
1038     {
1039       guint timeout;
1040       GtkSettings *settings;
1041
1042       settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1043
1044       g_object_get (settings,
1045                     "gtk-tooltip-browse-mode-timeout", &timeout,
1046                     NULL);
1047
1048       /* The tooltip is gone, after (by default, should be configurable) 500ms
1049        * we want to turn off browse mode
1050        */
1051       if (!tooltip->browse_mode_timeout_id)
1052         tooltip->browse_mode_timeout_id =
1053           gdk_threads_add_timeout_full (0, timeout,
1054                                         tooltip_browse_mode_expired,
1055                                         g_object_ref (tooltip),
1056                                         g_object_unref);
1057     }
1058   else
1059     {
1060       if (tooltip->browse_mode_timeout_id)
1061         {
1062           g_source_remove (tooltip->browse_mode_timeout_id);
1063           tooltip->browse_mode_timeout_id = 0;
1064         }
1065     }
1066
1067   if (tooltip->current_window)
1068     {
1069       gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1070       tooltip->current_window = NULL;
1071     }
1072 }
1073
1074 static gint
1075 tooltip_popup_timeout (gpointer data)
1076 {
1077   GdkDisplay *display;
1078   GtkTooltip *tooltip;
1079
1080   display = GDK_DISPLAY_OBJECT (data);
1081   tooltip = g_object_get_data (G_OBJECT (display),
1082                                "gdk-display-current-tooltip");
1083
1084   /* This usually does not happen.  However, it does occur in language
1085    * bindings were reference counting of objects behaves differently.
1086    */
1087   if (!tooltip)
1088     return FALSE;
1089
1090   gtk_tooltip_show_tooltip (display);
1091
1092   tooltip->timeout_id = 0;
1093
1094   return FALSE;
1095 }
1096
1097 static void
1098 gtk_tooltip_start_delay (GdkDisplay *display)
1099 {
1100   guint timeout;
1101   GtkTooltip *tooltip;
1102   GtkSettings *settings;
1103
1104   tooltip = g_object_get_data (G_OBJECT (display),
1105                                "gdk-display-current-tooltip");
1106
1107   if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1108     return;
1109
1110   if (tooltip->timeout_id)
1111     g_source_remove (tooltip->timeout_id);
1112
1113   settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1114
1115   if (tooltip->browse_mode_enabled)
1116     g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1117   else
1118     g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1119
1120   tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1121                                                       tooltip_popup_timeout,
1122                                                       g_object_ref (display),
1123                                                       g_object_unref);
1124 }
1125
1126 void
1127 _gtk_tooltip_focus_in (GtkWidget *widget)
1128 {
1129   gint x, y;
1130   gboolean return_value = FALSE;
1131   GdkDisplay *display;
1132   GtkTooltip *tooltip;
1133
1134   /* Get current tooltip for this display */
1135   display = gtk_widget_get_display (widget);
1136   tooltip = g_object_get_data (G_OBJECT (display),
1137                                "gdk-display-current-tooltip");
1138
1139   /* Check if keyboard mode is enabled at this moment */
1140   if (!tooltip || !tooltip->keyboard_mode_enabled)
1141     return;
1142
1143   if (tooltip->keyboard_widget)
1144     g_object_unref (tooltip->keyboard_widget);
1145
1146   tooltip->keyboard_widget = g_object_ref (widget);
1147
1148   gdk_window_get_pointer (widget->window, &x, &y, NULL);
1149
1150   return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1151   if (!return_value)
1152     {
1153       gtk_tooltip_hide_tooltip (tooltip);
1154       return;
1155     }
1156
1157   if (!tooltip->current_window)
1158     {
1159       if (gtk_widget_get_tooltip_window (widget))
1160         tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1161       else
1162         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1163     }
1164
1165   gtk_tooltip_show_tooltip (display);
1166 }
1167
1168 void
1169 _gtk_tooltip_focus_out (GtkWidget *widget)
1170 {
1171   GdkDisplay *display;
1172   GtkTooltip *tooltip;
1173
1174   /* Get current tooltip for this display */
1175   display = gtk_widget_get_display (widget);
1176   tooltip = g_object_get_data (G_OBJECT (display),
1177                                "gdk-display-current-tooltip");
1178
1179   if (!tooltip || !tooltip->keyboard_mode_enabled)
1180     return;
1181
1182   if (tooltip->keyboard_widget)
1183     {
1184       g_object_unref (tooltip->keyboard_widget);
1185       tooltip->keyboard_widget = NULL;
1186     }
1187
1188   gtk_tooltip_hide_tooltip (tooltip);
1189 }
1190
1191 void
1192 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1193 {
1194   GdkDisplay *display;
1195   GtkTooltip *tooltip;
1196
1197   display = gtk_widget_get_display (widget);
1198   tooltip = g_object_get_data (G_OBJECT (display),
1199                                "gdk-display-current-tooltip");
1200
1201   if (!tooltip)
1202     {
1203       tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1204       g_object_set_data_full (G_OBJECT (display),
1205                               "gdk-display-current-tooltip",
1206                               tooltip, g_object_unref);
1207       g_signal_connect (display, "closed",
1208                         G_CALLBACK (gtk_tooltip_display_closed),
1209                         tooltip);
1210     }
1211
1212   tooltip->keyboard_mode_enabled ^= 1;
1213
1214   if (tooltip->keyboard_mode_enabled)
1215     {
1216       tooltip->keyboard_widget = g_object_ref (widget);
1217       _gtk_tooltip_focus_in (widget);
1218     }
1219   else
1220     {
1221       if (tooltip->keyboard_widget)
1222         {
1223           g_object_unref (tooltip->keyboard_widget);
1224           tooltip->keyboard_widget = NULL;
1225         }
1226
1227       gtk_tooltip_hide_tooltip (tooltip);
1228     }
1229 }
1230
1231 void
1232 _gtk_tooltip_hide (GtkWidget *widget)
1233 {
1234   GtkWidget *toplevel;
1235   GdkDisplay *display;
1236   GtkTooltip *tooltip;
1237
1238   display = gtk_widget_get_display (widget);
1239   tooltip = g_object_get_data (G_OBJECT (display),
1240                                "gdk-display-current-tooltip");
1241
1242   if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1243     return;
1244
1245   toplevel = gtk_widget_get_toplevel (widget);
1246
1247   if (widget == tooltip->tooltip_widget
1248       || toplevel->window == tooltip->toplevel_window)
1249     gtk_tooltip_hide_tooltip (tooltip);
1250 }
1251
1252 static gboolean
1253 tooltips_enabled (GdkWindow *window)
1254 {
1255   gboolean enabled;
1256   gboolean touchscreen;
1257   GdkScreen *screen;
1258   GtkSettings *settings;
1259
1260   screen = gdk_drawable_get_screen (window);
1261   settings = gtk_settings_get_for_screen (screen);
1262
1263   g_object_get (settings,
1264                 "gtk-touchscreen-mode", &touchscreen,
1265                 "gtk-enable-tooltips", &enabled,
1266                 NULL);
1267
1268   return (!touchscreen && enabled);
1269 }
1270
1271 void
1272 _gtk_tooltip_handle_event (GdkEvent *event)
1273 {
1274   gint x, y;
1275   gboolean return_value = FALSE;
1276   GtkWidget *has_tooltip_widget = NULL;
1277   GdkDisplay *display;
1278   GtkTooltip *current_tooltip;
1279
1280   if (!tooltips_enabled (event->any.window))
1281     return;
1282
1283   /* Returns coordinates relative to has_tooltip_widget's allocation. */
1284   has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1285   display = gdk_drawable_get_display (event->any.window);
1286   current_tooltip = g_object_get_data (G_OBJECT (display),
1287                                        "gdk-display-current-tooltip");
1288
1289   if (current_tooltip)
1290     {
1291       gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1292       gdk_event_get_root_coords (event,
1293                                 &current_tooltip->last_x,
1294                                 &current_tooltip->last_y);
1295     }
1296
1297   if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1298     {
1299       has_tooltip_widget = current_tooltip->keyboard_widget;
1300       if (!has_tooltip_widget)
1301         return;
1302
1303       return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1304                                               current_tooltip,
1305                                               &x, &y);
1306
1307       if (!return_value)
1308         gtk_tooltip_hide_tooltip (current_tooltip);
1309       else
1310         gtk_tooltip_start_delay (display);
1311
1312       return;
1313     }
1314
1315 #ifdef DEBUG_TOOLTIP
1316   if (has_tooltip_widget)
1317     g_print ("%p (%s) at (%d, %d) %dx%d     pointer: (%d, %d)\n",
1318              has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1319              has_tooltip_widget->allocation.x,
1320              has_tooltip_widget->allocation.y,
1321              has_tooltip_widget->allocation.width,
1322              has_tooltip_widget->allocation.height,
1323              x, y);
1324 #endif /* DEBUG_TOOLTIP */
1325
1326   /* Always poll for a next motion event */
1327   gdk_event_request_motions (&event->motion);
1328
1329   /* Hide the tooltip when there's no new tooltip widget */
1330   if (!has_tooltip_widget)
1331     {
1332       if (current_tooltip)
1333         gtk_tooltip_hide_tooltip (current_tooltip);
1334
1335       return;
1336     }
1337
1338   switch (event->type)
1339     {
1340       case GDK_BUTTON_PRESS:
1341       case GDK_2BUTTON_PRESS:
1342       case GDK_3BUTTON_PRESS:
1343       case GDK_KEY_PRESS:
1344       case GDK_DRAG_ENTER:
1345       case GDK_GRAB_BROKEN:
1346         gtk_tooltip_hide_tooltip (current_tooltip);
1347         break;
1348
1349       case GDK_MOTION_NOTIFY:
1350       case GDK_ENTER_NOTIFY:
1351       case GDK_LEAVE_NOTIFY:
1352       case GDK_SCROLL:
1353         if (current_tooltip)
1354           {
1355             gboolean tip_area_set;
1356             GdkRectangle tip_area;
1357             gboolean hide_tooltip;
1358
1359             tip_area_set = current_tooltip->tip_area_set;
1360             tip_area = current_tooltip->tip_area;
1361
1362             return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1363                                                     current_tooltip,
1364                                                     &x, &y);
1365
1366             /* Requested to be hidden? */
1367             hide_tooltip = !return_value;
1368
1369             /* Leave notify should override the query function */
1370             hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1371
1372             /* Is the pointer above another widget now? */
1373             if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1374               hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1375
1376             /* Did the pointer move out of the previous "context area"? */
1377             if (tip_area_set)
1378               hide_tooltip |= (x <= tip_area.x
1379                                || x >= tip_area.x + tip_area.width
1380                                || y <= tip_area.y
1381                                || y >= tip_area.y + tip_area.height);
1382
1383             if (hide_tooltip)
1384               gtk_tooltip_hide_tooltip (current_tooltip);
1385             else
1386               gtk_tooltip_start_delay (display);
1387           }
1388         else
1389           {
1390             /* Need a new tooltip for this display */
1391             current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1392             g_object_set_data_full (G_OBJECT (display),
1393                                     "gdk-display-current-tooltip",
1394                                     current_tooltip, g_object_unref);
1395             g_signal_connect (display, "closed",
1396                               G_CALLBACK (gtk_tooltip_display_closed),
1397                               current_tooltip);
1398
1399             gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1400             gdk_event_get_root_coords (event,
1401                                        &current_tooltip->last_x,
1402                                        &current_tooltip->last_y);
1403
1404             gtk_tooltip_start_delay (display);
1405           }
1406         break;
1407
1408       default:
1409         break;
1410     }
1411 }
1412
1413
1414 #define __GTK_TOOLTIP_C__
1415 #include "gtkaliasdef.c"