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