]> Pileus Git - ~andy/gtk/blob - gtk/gtktooltip.c
2d60b7c65dc760a303d10456ed3d9f8157206fd2
[~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       GtkWidget *custom = tooltip->custom_widget;
318       /* Note: We must reset tooltip->custom_widget first, 
319        * since gtk_container_remove() will recurse into 
320        * gtk_tooltip_set_custom()
321        */
322       tooltip->custom_widget = NULL;
323       gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
324       g_object_unref (custom);
325     }
326
327   if (custom_widget)
328     {
329       tooltip->custom_widget = g_object_ref (custom_widget);
330
331       gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
332       gtk_widget_show (custom_widget);
333     }
334 }
335
336 /**
337  * gtk_tooltip_set_tip_area:
338  * @tooltip: a #GtkTooltip
339  * @rect: a #GdkRectangle
340  *
341  * Sets the area of the widget, where the contents of this tooltip apply,
342  * to be @rect (in widget coordinates).  This is especially useful for
343  * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
344  * etc.
345  *
346  * For setting tooltips on #GtkTreeView, please refer to the convenience
347  * functions for this: gtk_tree_view_set_tooltip_row() and
348  * gtk_tree_view_set_tooltip_cell().
349  *
350  * Since: 2.12
351  */
352 void
353 gtk_tooltip_set_tip_area (GtkTooltip   *tooltip,
354                           GdkRectangle *rect)
355 {
356   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
357
358   if (!rect)
359     tooltip->tip_area_set = FALSE;
360   else
361     {
362       tooltip->tip_area_set = TRUE;
363       tooltip->tip_area = *rect;
364     }
365 }
366
367 /**
368  * gtk_tooltip_trigger_tooltip_query:
369  * @display: a #GdkDisplay
370  *
371  * Triggers a new tooltip query on @display, in order to update the current
372  * visible tooltip, or to show/hide the current tooltip.  This function is
373  * useful to call when, for example, the state of the widget changed by a
374  * key press.
375  *
376  * Since: 2.12
377  */
378 void
379 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
380 {
381   gint x, y;
382   GdkWindow *window;
383   GdkEvent event;
384
385   /* Trigger logic as if the mouse moved */
386   window = gdk_display_get_window_at_pointer (display, &x, &y);
387   if (!window)
388     return;
389
390   event.type = GDK_MOTION_NOTIFY;
391   event.motion.window = window;
392   event.motion.x = x;
393   event.motion.y = y;
394   event.motion.is_hint = FALSE;
395
396   _gtk_tooltip_handle_event (&event);
397 }
398
399 /* private functions */
400
401 static void
402 gtk_tooltip_reset (GtkTooltip *tooltip)
403 {
404   gtk_tooltip_set_markup (tooltip, NULL);
405   gtk_tooltip_set_icon (tooltip, NULL);
406   gtk_tooltip_set_custom (tooltip, NULL);
407   gtk_tooltip_set_tip_area (tooltip, NULL);
408 }
409
410 static gboolean
411 gtk_tooltip_paint_window (GtkTooltip *tooltip)
412 {
413   gtk_paint_flat_box (tooltip->window->style,
414                       tooltip->window->window,
415                       GTK_STATE_NORMAL,
416                       GTK_SHADOW_OUT,
417                       NULL,
418                       tooltip->window,
419                       "tooltip",
420                       0, 0,
421                       tooltip->window->allocation.width,
422                       tooltip->window->allocation.height);
423
424   return FALSE;
425 }
426
427 static void
428 gtk_tooltip_window_hide (GtkWidget *widget,
429                          gpointer   user_data)
430 {
431   GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
432
433   gtk_tooltip_set_custom (tooltip, NULL);
434 }
435
436 /* event handling, etc */
437
438 struct ChildLocation
439 {
440   GtkWidget *child;
441   GtkWidget *container;
442
443   gint x;
444   gint y;
445 };
446
447 static void
448 child_location_foreach (GtkWidget *child,
449                         gpointer   data)
450 {
451   gint x, y;
452   struct ChildLocation *child_loc = data;
453
454   /* Ignore invisible widgets */
455   if (!GTK_WIDGET_DRAWABLE (child))
456     return;
457
458   if (!child_loc->child &&
459       gtk_widget_translate_coordinates (child_loc->container, child,
460                                         child_loc->x, child_loc->y,
461                                         &x, &y))
462     {
463       if (x >= 0 && x < child->allocation.width
464           && y >= 0 && y < child->allocation.height)
465         {
466           if (GTK_IS_CONTAINER (child))
467             {
468               struct ChildLocation tmp = { NULL, NULL, 0, 0 };
469
470               tmp.x = x;
471               tmp.y = y;
472               tmp.container = child;
473
474               gtk_container_forall (GTK_CONTAINER (child),
475                                     child_location_foreach, &tmp);
476
477               if (tmp.child)
478                 child_loc->child = tmp.child;
479               else
480                 child_loc->child = child;
481             }
482           else
483             child_loc->child = child;
484         }
485     }
486 }
487
488 static void
489 window_to_alloc (GtkWidget *dest_widget,
490                  gint       src_x,
491                  gint       src_y,
492                  gint      *dest_x,
493                  gint      *dest_y)
494 {
495   /* Translate from window relative to allocation relative */
496   if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
497     {
498       gint wx, wy;
499       gdk_window_get_position (dest_widget->window, &wx, &wy);
500
501       src_x += wx - dest_widget->allocation.x;
502       src_y += wy - dest_widget->allocation.y;
503     }
504   else
505     {
506       src_x -= dest_widget->allocation.x;
507       src_y -= dest_widget->allocation.y;
508     }
509
510   if (dest_x)
511     *dest_x = src_x;
512   if (dest_y)
513     *dest_y = src_y;
514 }
515
516 static GtkWidget *
517 find_widget_under_pointer (GdkWindow *window,
518                            gint      *x,
519                            gint      *y)
520 {
521   GtkWidget *event_widget;
522   struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
523
524   gdk_window_get_user_data (window, (void **)&event_widget);
525
526   if (!event_widget)
527     return NULL;
528
529   child_loc.x = *x;
530   child_loc.y = *y;
531
532   while (window && window != event_widget->window)
533     {
534       gint px, py;
535
536       gdk_window_get_position (window, &px, &py);
537       child_loc.x += px;
538       child_loc.y += py;
539
540       window = gdk_window_get_parent (window);
541     }
542
543   if (GTK_WIDGET_NO_WINDOW (event_widget))
544     {
545       child_loc.x += event_widget->allocation.x;
546       child_loc.y += event_widget->allocation.y;
547     }
548
549   /* Failing to find widget->window can happen for e.g. a detached handle box;
550    * chaining ::query-tooltip up to its parent probably makes little sense,
551    * and users better implement tooltips on handle_box->child.
552    * so we simply ignore the event for tooltips here.
553    */
554   if (!window)
555     return NULL;
556
557   if (GTK_IS_CONTAINER (event_widget))
558     {
559       window_to_alloc (event_widget,
560                        child_loc.x, child_loc.y,
561                        &child_loc.x, &child_loc.y);
562
563       child_loc.container = event_widget;
564       child_loc.child = NULL;
565
566       gtk_container_forall (GTK_CONTAINER (event_widget),
567                             child_location_foreach, &child_loc);
568
569       if (child_loc.child)
570         event_widget = child_loc.child;
571       else if (child_loc.container)
572         event_widget = child_loc.container;
573     }
574
575   if (x)
576     *x = child_loc.x;
577   if (y)
578     *y = child_loc.y;
579
580   return event_widget;
581 }
582
583 static GtkWidget *
584 find_topmost_widget_coords_from_event (GdkEvent *event,
585                                        gint     *x,
586                                        gint     *y)
587 {
588   gint tx, ty;
589   gdouble dx, dy;
590   GtkWidget *tmp;
591
592   gdk_event_get_coords (event, &dx, &dy);
593   tx = dx;
594   ty = dy;
595
596   tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
597
598   if (!tmp)
599     return NULL;
600
601   /* Make sure the pointer can actually be on the widget returned */
602   if (GTK_WIDGET_NO_WINDOW (tmp))
603     {
604       if (tx < tmp->allocation.x ||
605           tx >= tmp->allocation.x + tmp->allocation.width ||
606           ty < tmp->allocation.y ||
607           ty >= tmp->allocation.y + tmp->allocation.height)
608         return NULL;
609     }
610   else
611     {
612       if (tx < 0 || tx >= tmp->allocation.width ||
613           ty < 0 || ty >= tmp->allocation.height)
614         return NULL;
615     }
616
617   if (x)
618     *x = tx;
619   if (y)
620     *y = ty;
621
622   return tmp;
623 }
624
625 static gint
626 tooltip_browse_mode_expired (gpointer data)
627 {
628   GtkTooltip *tooltip;
629
630   tooltip = GTK_TOOLTIP (data);
631
632   tooltip->browse_mode_enabled = FALSE;
633   tooltip->browse_mode_timeout_id = 0;
634
635   /* destroy tooltip */
636   g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
637                      "gdk-display-current-tooltip", NULL);
638
639   return FALSE;
640 }
641
642 static void
643 gtk_tooltip_display_closed (GdkDisplay *display,
644                             gboolean    was_error,
645                             GtkTooltip *tooltip)
646 {
647   g_object_set (display, "gdk-display-current-tooltip", NULL);
648 }
649
650 static gboolean
651 gtk_tooltip_run_requery (GtkWidget  **widget,
652                          GtkTooltip  *tooltip,
653                          gint        *x,
654                          gint        *y)
655 {
656   gboolean has_tooltip = FALSE;
657   gboolean return_value = FALSE;
658
659   gtk_tooltip_reset (tooltip);
660
661   do
662     {
663       g_object_get (*widget,
664                     "has-tooltip", &has_tooltip,
665                     NULL);
666
667       if (has_tooltip)
668         g_signal_emit_by_name (*widget,
669                                "query-tooltip",
670                                *x, *y,
671                                tooltip->keyboard_mode_enabled,
672                                tooltip,
673                                &return_value);
674
675       if (!return_value)
676         {
677           GtkWidget *parent = (*widget)->parent;
678
679           if (parent)
680             gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
681
682           *widget = parent;
683         }
684       else
685         break;
686     }
687   while (*widget);
688
689   return return_value;
690 }
691
692 static void
693 gtk_tooltip_position (GtkTooltip *tooltip,
694                       GdkDisplay *display,
695                       GtkWidget  *new_tooltip_widget)
696 {
697   gint x, y;
698   GdkScreen *screen;
699
700   tooltip->tooltip_widget = new_tooltip_widget;
701
702   /* Position the tooltip */
703   /* FIXME: should we swap this when RTL is enabled? */
704   if (tooltip->keyboard_mode_enabled)
705     {
706       gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
707       if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
708         {
709           x += new_tooltip_widget->allocation.x;
710           y += new_tooltip_widget->allocation.y;
711         }
712
713       /* For keyboard mode we position the tooltip below the widget,
714        * right of the center of the widget.
715        */
716       x += new_tooltip_widget->allocation.width / 2;
717       y += new_tooltip_widget->allocation.height + 4;
718     }
719   else
720     {
721       guint cursor_size;
722
723       x = tooltip->last_x;
724       y = tooltip->last_y;
725
726       /* For mouse mode, we position the tooltip right of the cursor,
727        * a little below the cursor's center.
728        */
729       cursor_size = gdk_display_get_default_cursor_size (display);
730       x += cursor_size / 2;
731       y += cursor_size / 2;
732     }
733
734   screen = gtk_widget_get_screen (new_tooltip_widget);
735
736   /* Show it */
737   if (tooltip->current_window)
738     {
739       gint monitor_num;
740       GdkRectangle monitor;
741       GtkRequisition requisition;
742
743       gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
744                                &requisition);
745
746       monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
747       gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
748
749       if (x + requisition.width > monitor.x + monitor.width)
750         x -= x - (monitor.x + monitor.width) + requisition.width;
751       else if (x < monitor.x)
752         x = monitor.x;
753
754       if (y + requisition.height > monitor.y + monitor.height)
755         y -= y - (monitor.y + monitor.height) + requisition.height;
756   
757       if (!tooltip->keyboard_mode_enabled)
758         {
759           /* don't pop up under the pointer */
760           if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
761               y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
762             y = tooltip->last_y - requisition.height - 2;
763         }
764   
765       gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
766       gtk_widget_show (GTK_WIDGET (tooltip->current_window));
767     }
768 }
769
770 static void
771 gtk_tooltip_show_tooltip (GdkDisplay *display)
772 {
773   gint x, y;
774   GdkScreen *screen;
775
776   GdkWindow *window;
777   GtkWidget *tooltip_widget;
778   GtkWidget *pointer_widget;
779   GtkTooltip *tooltip;
780   gboolean has_tooltip;
781   gboolean return_value = FALSE;
782
783   tooltip = g_object_get_data (G_OBJECT (display),
784                                "gdk-display-current-tooltip");
785
786   if (tooltip->keyboard_mode_enabled)
787     {
788       pointer_widget = tooltip_widget = tooltip->keyboard_widget;
789     }
790   else
791     {
792       window = tooltip->last_window;
793
794       if (!GDK_IS_WINDOW (window))
795         return;
796
797       gdk_window_get_origin (window, &x, &y);
798       x = tooltip->last_x - x;
799       y = tooltip->last_y - y;
800
801       pointer_widget = tooltip_widget = find_widget_under_pointer (window,
802                                                                    &x, &y);
803     }
804
805   if (!tooltip_widget)
806     return;
807
808   g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
809
810   g_assert (tooltip != NULL);
811
812   return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
813   if (!return_value)
814     return;
815
816   if (!tooltip->current_window)
817     {
818       if (gtk_widget_get_tooltip_window (tooltip_widget))
819         tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
820       else
821         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
822     }
823
824   screen = gtk_widget_get_screen (tooltip_widget);
825
826   /* FIXME: should use tooltip->current_window iso tooltip->window */
827   if (screen != gtk_widget_get_screen (tooltip->window))
828     {
829       g_signal_handlers_disconnect_by_func (display,
830                                             gtk_tooltip_display_closed,
831                                             tooltip);
832
833       gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
834
835       g_signal_connect (display, "closed",
836                         G_CALLBACK (gtk_tooltip_display_closed), tooltip);
837     }
838
839   gtk_tooltip_position (tooltip, display, tooltip_widget);
840
841   /* Now a tooltip is visible again on the display, make sure browse
842    * mode is enabled.
843    */
844   tooltip->browse_mode_enabled = TRUE;
845   if (tooltip->browse_mode_timeout_id)
846     {
847       g_source_remove (tooltip->browse_mode_timeout_id);
848       tooltip->browse_mode_timeout_id = 0;
849     }
850 }
851
852 static void
853 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
854 {
855   if (!tooltip)
856     return;
857
858   if (tooltip->timeout_id)
859     {
860       g_source_remove (tooltip->timeout_id);
861       tooltip->timeout_id = 0;
862     }
863
864   if (!GTK_TOOLTIP_VISIBLE (tooltip))
865     return;
866
867   tooltip->tooltip_widget = NULL;
868
869   if (!tooltip->keyboard_mode_enabled)
870     {
871       guint timeout;
872       GtkSettings *settings;
873
874       settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
875
876       g_object_get (settings,
877                     "gtk-tooltip-browse-mode-timeout", &timeout,
878                     NULL);
879
880       /* The tooltip is gone, after (by default, should be configurable) 500ms
881        * we want to turn off browse mode
882        */
883       if (!tooltip->browse_mode_timeout_id)
884         tooltip->browse_mode_timeout_id =
885           gdk_threads_add_timeout_full (0, timeout,
886                                         tooltip_browse_mode_expired,
887                                         g_object_ref (tooltip),
888                                         g_object_unref);
889     }
890   else
891     {
892       if (tooltip->browse_mode_timeout_id)
893         {
894           g_source_remove (tooltip->browse_mode_timeout_id);
895           tooltip->browse_mode_timeout_id = 0;
896         }
897     }
898
899   if (tooltip->current_window)
900     {
901       gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
902       tooltip->current_window = NULL;
903     }
904 }
905
906 static gint
907 tooltip_popup_timeout (gpointer data)
908 {
909   GdkDisplay *display;
910   GtkTooltip *tooltip;
911
912   display = GDK_DISPLAY_OBJECT (data);
913
914   gtk_tooltip_show_tooltip (display);
915
916   tooltip = g_object_get_data (G_OBJECT (display),
917                                "gdk-display-current-tooltip");
918   tooltip->timeout_id = 0;
919
920   return FALSE;
921 }
922
923 static void
924 gtk_tooltip_start_delay (GdkDisplay *display)
925 {
926   guint timeout;
927   GtkTooltip *tooltip;
928   GtkSettings *settings;
929
930   tooltip = g_object_get_data (G_OBJECT (display),
931                                "gdk-display-current-tooltip");
932
933   if (tooltip && GTK_TOOLTIP_VISIBLE (tooltip))
934     return;
935
936   if (tooltip->timeout_id)
937     g_source_remove (tooltip->timeout_id);
938
939   settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
940
941   if (tooltip->browse_mode_enabled)
942     g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
943   else
944     g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
945
946   tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
947                                                       tooltip_popup_timeout,
948                                                       g_object_ref (display),
949                                                       g_object_unref);
950 }
951
952 void
953 _gtk_tooltip_focus_in (GtkWidget *widget)
954 {
955   gint x, y;
956   gboolean return_value = FALSE;
957   GdkDisplay *display;
958   GtkTooltip *tooltip;
959
960   /* Get current tooltip for this display */
961   display = gtk_widget_get_display (widget);
962   tooltip = g_object_get_data (G_OBJECT (display),
963                                "gdk-display-current-tooltip");
964
965   /* Check if keyboard mode is enabled at this moment */
966   if (!tooltip || !tooltip->keyboard_mode_enabled)
967     return;
968
969   if (tooltip->keyboard_widget)
970     g_object_unref (tooltip->keyboard_widget);
971
972   tooltip->keyboard_widget = g_object_ref (widget);
973
974   gdk_window_get_pointer (widget->window, &x, &y, NULL);
975
976   return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
977   if (!return_value)
978     {
979       gtk_tooltip_hide_tooltip (tooltip);
980       return;
981     }
982
983   if (!tooltip->current_window)
984     {
985       if (gtk_widget_get_tooltip_window (widget))
986         tooltip->current_window = gtk_widget_get_tooltip_window (widget);
987       else
988         tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
989     }
990
991   gtk_tooltip_show_tooltip (display);
992 }
993
994 void
995 _gtk_tooltip_focus_out (GtkWidget *widget)
996 {
997   GdkDisplay *display;
998   GtkTooltip *tooltip;
999
1000   /* Get current tooltip for this display */
1001   display = gtk_widget_get_display (widget);
1002   tooltip = g_object_get_data (G_OBJECT (display),
1003                                "gdk-display-current-tooltip");
1004
1005   if (!tooltip || !tooltip->keyboard_mode_enabled)
1006     return;
1007
1008   if (tooltip->keyboard_widget)
1009     {
1010       g_object_unref (tooltip->keyboard_widget);
1011       tooltip->keyboard_widget = NULL;
1012     }
1013
1014   gtk_tooltip_hide_tooltip (tooltip);
1015 }
1016
1017 void
1018 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1019 {
1020   GdkDisplay *display;
1021   GtkTooltip *tooltip;
1022
1023   display = gtk_widget_get_display (widget);
1024   tooltip = g_object_get_data (G_OBJECT (display),
1025                                "gdk-display-current-tooltip");
1026
1027   if (!tooltip)
1028     {
1029       tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1030       g_object_set_data_full (G_OBJECT (display),
1031                               "gdk-display-current-tooltip",
1032                               tooltip, g_object_unref);
1033     }
1034
1035   tooltip->keyboard_mode_enabled ^= 1;
1036
1037   if (tooltip->keyboard_mode_enabled)
1038     {
1039       tooltip->keyboard_widget = g_object_ref (widget);
1040       _gtk_tooltip_focus_in (widget);
1041     }
1042   else
1043     {
1044       if (tooltip->keyboard_widget)
1045         {
1046           g_object_unref (tooltip->keyboard_widget);
1047           tooltip->keyboard_widget = NULL;
1048         }
1049
1050       gtk_tooltip_hide_tooltip (tooltip);
1051     }
1052 }
1053
1054 void
1055 _gtk_tooltip_hide (GtkWidget *widget)
1056 {
1057   GtkWidget *toplevel;
1058   GdkDisplay *display;
1059   GtkTooltip *tooltip;
1060
1061   display = gtk_widget_get_display (widget);
1062   tooltip = g_object_get_data (G_OBJECT (display),
1063                                "gdk-display-current-tooltip");
1064
1065   if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1066     return;
1067
1068   toplevel = gtk_widget_get_toplevel (widget);
1069
1070   if (widget == tooltip->tooltip_widget
1071       || toplevel->window == tooltip->toplevel_window)
1072     gtk_tooltip_hide_tooltip (tooltip);
1073 }
1074
1075 void
1076 _gtk_tooltip_handle_event (GdkEvent *event)
1077 {
1078   gint x, y;
1079   gboolean return_value = FALSE;
1080   GtkWidget *has_tooltip_widget = NULL;
1081   GdkDisplay *display;
1082   GtkTooltip *current_tooltip;
1083
1084   has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1085   display = gdk_drawable_get_display (event->any.window);
1086   current_tooltip = g_object_get_data (G_OBJECT (display),
1087                                        "gdk-display-current-tooltip");
1088
1089   if (current_tooltip)
1090     {
1091       current_tooltip->last_window = event->any.window;
1092       gdk_event_get_root_coords (event,
1093                                 &current_tooltip->last_x,
1094                                 &current_tooltip->last_y);
1095     }
1096
1097   if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1098     {
1099       has_tooltip_widget = current_tooltip->keyboard_widget;
1100       if (!has_tooltip_widget)
1101         return;
1102
1103       return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1104                                               current_tooltip,
1105                                               &x, &y);
1106
1107       if (!return_value)
1108         gtk_tooltip_hide_tooltip (current_tooltip);
1109       else
1110         gtk_tooltip_start_delay (display);
1111
1112       return;
1113     }
1114
1115   /* Always poll for a next motion event */
1116   gdk_event_request_motions (&event->motion);
1117
1118   /* Hide the tooltip when there's no new tooltip widget */
1119   if (!has_tooltip_widget)
1120     {
1121       if (current_tooltip && GTK_TOOLTIP_VISIBLE (current_tooltip))
1122         gtk_tooltip_hide_tooltip (current_tooltip);
1123
1124       return;
1125     }
1126
1127   switch (event->type)
1128     {
1129       case GDK_BUTTON_PRESS:
1130       case GDK_2BUTTON_PRESS:
1131       case GDK_3BUTTON_PRESS:
1132       case GDK_KEY_PRESS:
1133       case GDK_DRAG_ENTER:
1134       case GDK_GRAB_BROKEN:
1135         gtk_tooltip_hide_tooltip (current_tooltip);
1136         break;
1137
1138       case GDK_MOTION_NOTIFY:
1139       case GDK_ENTER_NOTIFY:
1140       case GDK_LEAVE_NOTIFY:
1141       case GDK_SCROLL:
1142         if (current_tooltip)
1143           {
1144             gboolean tip_area_set;
1145             GdkRectangle tip_area;
1146             gboolean hide_tooltip;
1147
1148             tip_area_set = current_tooltip->tip_area_set;
1149             tip_area = current_tooltip->tip_area;
1150
1151             return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1152                                                     current_tooltip,
1153                                                     &x, &y);
1154
1155             /* Requested to be hidden? */
1156             hide_tooltip = !return_value;
1157
1158             /* Is the pointer above another widget now? */
1159             if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1160               hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1161
1162             /* Did the pointer move out of the previous "context area"? */
1163             if (tip_area_set)
1164               hide_tooltip |= (x <= tip_area.x
1165                                || x >= tip_area.x + tip_area.width
1166                                || y <= tip_area.y
1167                                || y >= tip_area.y + tip_area.height);
1168
1169             if (hide_tooltip)
1170               gtk_tooltip_hide_tooltip (current_tooltip);
1171             else
1172               gtk_tooltip_start_delay (display);
1173           }
1174         else
1175           {
1176             /* Need a new tooltip for this display */
1177             current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1178             g_object_set_data_full (G_OBJECT (display),
1179                                     "gdk-display-current-tooltip",
1180                                     current_tooltip, g_object_unref);
1181
1182             current_tooltip->last_window = event->any.window;
1183             gdk_event_get_root_coords (event,
1184                                        &current_tooltip->last_x,
1185                                        &current_tooltip->last_y);
1186
1187             gtk_tooltip_start_delay (display);
1188           }
1189         break;
1190
1191       default:
1192         break;
1193     }
1194 }
1195
1196
1197 #define __GTK_TOOLTIP_C__
1198 #include "gtkaliasdef.c"