]> Pileus Git - ~andy/gtk/blob - gtk/gtktooltip.c
Include "config.h" instead of <config.h> Command used: find -name
[~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       pointer_widget = tooltip_widget = tooltip->keyboard_widget;
915     }
916   else
917     {
918       window = tooltip->last_window;
919
920       if (!GDK_IS_WINDOW (window))
921         return;
922
923       gdk_window_get_origin (window, &x, &y);
924       x = tooltip->last_x - x;
925       y = tooltip->last_y - y;
926
927       pointer_widget = tooltip_widget = find_widget_under_pointer (window,
928                                                                    &x, &y);
929     }
930
931   if (!tooltip_widget)
932     return;
933
934   g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
935
936   g_assert (tooltip != NULL);
937
938   return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
939   if (!return_value)
940     return;
941
942   if (!tooltip->current_window)
943     {
944       if (gtk_widget_get_tooltip_window (tooltip_widget))
945         tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
946       else
947         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
948     }
949
950   screen = gtk_widget_get_screen (tooltip_widget);
951
952   /* FIXME: should use tooltip->current_window iso tooltip->window */
953   if (screen != gtk_widget_get_screen (tooltip->window))
954     {
955       g_signal_handlers_disconnect_by_func (display,
956                                             gtk_tooltip_display_closed,
957                                             tooltip);
958
959       gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
960
961       g_signal_connect (display, "closed",
962                         G_CALLBACK (gtk_tooltip_display_closed), tooltip);
963     }
964
965   gtk_tooltip_position (tooltip, display, tooltip_widget);
966
967   /* Now a tooltip is visible again on the display, make sure browse
968    * mode is enabled.
969    */
970   tooltip->browse_mode_enabled = TRUE;
971   if (tooltip->browse_mode_timeout_id)
972     {
973       g_source_remove (tooltip->browse_mode_timeout_id);
974       tooltip->browse_mode_timeout_id = 0;
975     }
976 }
977
978 static void
979 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
980 {
981   if (!tooltip)
982     return;
983
984   if (tooltip->timeout_id)
985     {
986       g_source_remove (tooltip->timeout_id);
987       tooltip->timeout_id = 0;
988     }
989
990   if (!GTK_TOOLTIP_VISIBLE (tooltip))
991     return;
992
993   tooltip->tooltip_widget = NULL;
994
995   if (!tooltip->keyboard_mode_enabled)
996     {
997       guint timeout;
998       GtkSettings *settings;
999
1000       settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1001
1002       g_object_get (settings,
1003                     "gtk-tooltip-browse-mode-timeout", &timeout,
1004                     NULL);
1005
1006       /* The tooltip is gone, after (by default, should be configurable) 500ms
1007        * we want to turn off browse mode
1008        */
1009       if (!tooltip->browse_mode_timeout_id)
1010         tooltip->browse_mode_timeout_id =
1011           gdk_threads_add_timeout_full (0, timeout,
1012                                         tooltip_browse_mode_expired,
1013                                         g_object_ref (tooltip),
1014                                         g_object_unref);
1015     }
1016   else
1017     {
1018       if (tooltip->browse_mode_timeout_id)
1019         {
1020           g_source_remove (tooltip->browse_mode_timeout_id);
1021           tooltip->browse_mode_timeout_id = 0;
1022         }
1023     }
1024
1025   if (tooltip->current_window)
1026     {
1027       gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1028       tooltip->current_window = NULL;
1029     }
1030 }
1031
1032 static gint
1033 tooltip_popup_timeout (gpointer data)
1034 {
1035   GdkDisplay *display;
1036   GtkTooltip *tooltip;
1037
1038   display = GDK_DISPLAY_OBJECT (data);
1039
1040   gtk_tooltip_show_tooltip (display);
1041
1042   tooltip = g_object_get_data (G_OBJECT (display),
1043                                "gdk-display-current-tooltip");
1044   tooltip->timeout_id = 0;
1045
1046   return FALSE;
1047 }
1048
1049 static void
1050 gtk_tooltip_start_delay (GdkDisplay *display)
1051 {
1052   guint timeout;
1053   GtkTooltip *tooltip;
1054   GtkSettings *settings;
1055
1056   tooltip = g_object_get_data (G_OBJECT (display),
1057                                "gdk-display-current-tooltip");
1058
1059   if (tooltip && GTK_TOOLTIP_VISIBLE (tooltip))
1060     return;
1061
1062   if (tooltip->timeout_id)
1063     g_source_remove (tooltip->timeout_id);
1064
1065   settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1066
1067   if (tooltip->browse_mode_enabled)
1068     g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1069   else
1070     g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1071
1072   tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1073                                                       tooltip_popup_timeout,
1074                                                       g_object_ref (display),
1075                                                       g_object_unref);
1076 }
1077
1078 void
1079 _gtk_tooltip_focus_in (GtkWidget *widget)
1080 {
1081   gint x, y;
1082   gboolean return_value = FALSE;
1083   GdkDisplay *display;
1084   GtkTooltip *tooltip;
1085
1086   /* Get current tooltip for this display */
1087   display = gtk_widget_get_display (widget);
1088   tooltip = g_object_get_data (G_OBJECT (display),
1089                                "gdk-display-current-tooltip");
1090
1091   /* Check if keyboard mode is enabled at this moment */
1092   if (!tooltip || !tooltip->keyboard_mode_enabled)
1093     return;
1094
1095   if (tooltip->keyboard_widget)
1096     g_object_unref (tooltip->keyboard_widget);
1097
1098   tooltip->keyboard_widget = g_object_ref (widget);
1099
1100   gdk_window_get_pointer (widget->window, &x, &y, NULL);
1101
1102   return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1103   if (!return_value)
1104     {
1105       gtk_tooltip_hide_tooltip (tooltip);
1106       return;
1107     }
1108
1109   if (!tooltip->current_window)
1110     {
1111       if (gtk_widget_get_tooltip_window (widget))
1112         tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1113       else
1114         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1115     }
1116
1117   gtk_tooltip_show_tooltip (display);
1118 }
1119
1120 void
1121 _gtk_tooltip_focus_out (GtkWidget *widget)
1122 {
1123   GdkDisplay *display;
1124   GtkTooltip *tooltip;
1125
1126   /* Get current tooltip for this display */
1127   display = gtk_widget_get_display (widget);
1128   tooltip = g_object_get_data (G_OBJECT (display),
1129                                "gdk-display-current-tooltip");
1130
1131   if (!tooltip || !tooltip->keyboard_mode_enabled)
1132     return;
1133
1134   if (tooltip->keyboard_widget)
1135     {
1136       g_object_unref (tooltip->keyboard_widget);
1137       tooltip->keyboard_widget = NULL;
1138     }
1139
1140   gtk_tooltip_hide_tooltip (tooltip);
1141 }
1142
1143 void
1144 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1145 {
1146   GdkDisplay *display;
1147   GtkTooltip *tooltip;
1148
1149   display = gtk_widget_get_display (widget);
1150   tooltip = g_object_get_data (G_OBJECT (display),
1151                                "gdk-display-current-tooltip");
1152
1153   if (!tooltip)
1154     {
1155       tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1156       g_object_set_data_full (G_OBJECT (display),
1157                               "gdk-display-current-tooltip",
1158                               tooltip, g_object_unref);
1159       g_signal_connect (display, "closed",
1160                         G_CALLBACK (gtk_tooltip_display_closed),
1161                         tooltip);
1162     }
1163
1164   tooltip->keyboard_mode_enabled ^= 1;
1165
1166   if (tooltip->keyboard_mode_enabled)
1167     {
1168       tooltip->keyboard_widget = g_object_ref (widget);
1169       _gtk_tooltip_focus_in (widget);
1170     }
1171   else
1172     {
1173       if (tooltip->keyboard_widget)
1174         {
1175           g_object_unref (tooltip->keyboard_widget);
1176           tooltip->keyboard_widget = NULL;
1177         }
1178
1179       gtk_tooltip_hide_tooltip (tooltip);
1180     }
1181 }
1182
1183 void
1184 _gtk_tooltip_hide (GtkWidget *widget)
1185 {
1186   GtkWidget *toplevel;
1187   GdkDisplay *display;
1188   GtkTooltip *tooltip;
1189
1190   display = gtk_widget_get_display (widget);
1191   tooltip = g_object_get_data (G_OBJECT (display),
1192                                "gdk-display-current-tooltip");
1193
1194   if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1195     return;
1196
1197   toplevel = gtk_widget_get_toplevel (widget);
1198
1199   if (widget == tooltip->tooltip_widget
1200       || toplevel->window == tooltip->toplevel_window)
1201     gtk_tooltip_hide_tooltip (tooltip);
1202 }
1203
1204 void
1205 _gtk_tooltip_handle_event (GdkEvent *event)
1206 {
1207   gint x, y;
1208   gboolean return_value = FALSE;
1209   gboolean touchscreen;
1210   GtkWidget *has_tooltip_widget = NULL;
1211   GdkScreen *screen;
1212   GdkDisplay *display;
1213   GtkTooltip *current_tooltip;
1214   GtkSettings *settings;
1215
1216   /* Disable tooltips in touchscreen mode */
1217   screen = gdk_drawable_get_screen (event->any.window);
1218   settings = gtk_settings_get_for_screen (screen);
1219   g_object_get (settings, "gtk-touchscreen-mode", &touchscreen, NULL);
1220
1221   if (touchscreen)
1222     return;
1223
1224   /* Returns coordinates relative to has_tooltip_widget's allocation. */
1225   has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1226   display = gdk_drawable_get_display (event->any.window);
1227   current_tooltip = g_object_get_data (G_OBJECT (display),
1228                                        "gdk-display-current-tooltip");
1229
1230   if (current_tooltip)
1231     {
1232       gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1233       gdk_event_get_root_coords (event,
1234                                 &current_tooltip->last_x,
1235                                 &current_tooltip->last_y);
1236     }
1237
1238   if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1239     {
1240       has_tooltip_widget = current_tooltip->keyboard_widget;
1241       if (!has_tooltip_widget)
1242         return;
1243
1244       return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1245                                               current_tooltip,
1246                                               &x, &y);
1247
1248       if (!return_value)
1249         gtk_tooltip_hide_tooltip (current_tooltip);
1250       else
1251         gtk_tooltip_start_delay (display);
1252
1253       return;
1254     }
1255
1256 #ifdef DEBUG_TOOLTIP
1257   if (has_tooltip_widget)
1258     g_print ("%p (%s) at (%d, %d) %dx%d     pointer: (%d, %d)\n",
1259              has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1260              has_tooltip_widget->allocation.x,
1261              has_tooltip_widget->allocation.y,
1262              has_tooltip_widget->allocation.width,
1263              has_tooltip_widget->allocation.height,
1264              x, y);
1265 #endif /* DEBUG_TOOLTIP */
1266
1267   /* Always poll for a next motion event */
1268   gdk_event_request_motions (&event->motion);
1269
1270   /* Hide the tooltip when there's no new tooltip widget */
1271   if (!has_tooltip_widget)
1272     {
1273       if (current_tooltip)
1274         gtk_tooltip_hide_tooltip (current_tooltip);
1275
1276       return;
1277     }
1278
1279   switch (event->type)
1280     {
1281       case GDK_BUTTON_PRESS:
1282       case GDK_2BUTTON_PRESS:
1283       case GDK_3BUTTON_PRESS:
1284       case GDK_KEY_PRESS:
1285       case GDK_DRAG_ENTER:
1286       case GDK_GRAB_BROKEN:
1287         gtk_tooltip_hide_tooltip (current_tooltip);
1288         break;
1289
1290       case GDK_MOTION_NOTIFY:
1291       case GDK_ENTER_NOTIFY:
1292       case GDK_LEAVE_NOTIFY:
1293       case GDK_SCROLL:
1294         if (current_tooltip)
1295           {
1296             gboolean tip_area_set;
1297             GdkRectangle tip_area;
1298             gboolean hide_tooltip;
1299
1300             tip_area_set = current_tooltip->tip_area_set;
1301             tip_area = current_tooltip->tip_area;
1302
1303             return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1304                                                     current_tooltip,
1305                                                     &x, &y);
1306
1307             /* Requested to be hidden? */
1308             hide_tooltip = !return_value;
1309
1310             /* Leave notify should override the query function */
1311             hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1312
1313             /* Is the pointer above another widget now? */
1314             if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1315               hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1316
1317             /* Did the pointer move out of the previous "context area"? */
1318             if (tip_area_set)
1319               hide_tooltip |= (x <= tip_area.x
1320                                || x >= tip_area.x + tip_area.width
1321                                || y <= tip_area.y
1322                                || y >= tip_area.y + tip_area.height);
1323
1324             if (hide_tooltip)
1325               gtk_tooltip_hide_tooltip (current_tooltip);
1326             else
1327               gtk_tooltip_start_delay (display);
1328           }
1329         else
1330           {
1331             /* Need a new tooltip for this display */
1332             current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1333             g_object_set_data_full (G_OBJECT (display),
1334                                     "gdk-display-current-tooltip",
1335                                     current_tooltip, g_object_unref);
1336             g_signal_connect (display, "closed",
1337                               G_CALLBACK (gtk_tooltip_display_closed),
1338                               current_tooltip);
1339
1340             gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1341             gdk_event_get_root_coords (event,
1342                                        &current_tooltip->last_x,
1343                                        &current_tooltip->last_y);
1344
1345             gtk_tooltip_start_delay (display);
1346           }
1347         break;
1348
1349       default:
1350         break;
1351     }
1352 }
1353
1354
1355 #define __GTK_TOOLTIP_C__
1356 #include "gtkaliasdef.c"