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