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