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