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