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