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