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