3 * Copyright (C) 2006-2007 Imendio AB
4 * Contact: Kristian Rietveld <kris@imendio.com>
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.
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.
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.
24 #include "gtktooltip.h"
30 #include "gtkwindow.h"
35 #include "gtkalignment.h"
36 #include "gtksizerequest.h"
42 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
43 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
44 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
46 typedef struct _GtkTooltipClass GtkTooltipClass;
50 GObject parent_instance;
57 GtkWidget *custom_widget;
59 GtkWindow *current_window;
60 GtkWidget *keyboard_widget;
62 GtkWidget *tooltip_widget;
63 GdkWindow *toplevel_window;
67 GdkWindow *last_window;
70 guint browse_mode_timeout_id;
72 GdkRectangle tip_area;
74 guint browse_mode_enabled : 1;
75 guint keyboard_mode_enabled : 1;
76 guint tip_area_set : 1;
77 guint custom_was_reset : 1;
80 struct _GtkTooltipClass
82 GObjectClass parent_class;
85 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
88 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
89 static void gtk_tooltip_init (GtkTooltip *tooltip);
90 static void gtk_tooltip_dispose (GObject *object);
92 static void gtk_tooltip_window_style_set (GtkTooltip *tooltip);
93 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip,
95 static void gtk_tooltip_window_hide (GtkWidget *widget,
97 static void gtk_tooltip_display_closed (GdkDisplay *display,
100 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
104 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
107 gtk_tooltip_class_init (GtkTooltipClass *klass)
109 GObjectClass *object_class;
111 object_class = G_OBJECT_CLASS (klass);
113 object_class->dispose = gtk_tooltip_dispose;
117 gtk_tooltip_init (GtkTooltip *tooltip)
121 tooltip->timeout_id = 0;
122 tooltip->browse_mode_timeout_id = 0;
124 tooltip->browse_mode_enabled = FALSE;
125 tooltip->keyboard_mode_enabled = FALSE;
127 tooltip->current_window = NULL;
128 tooltip->keyboard_widget = NULL;
130 tooltip->tooltip_widget = NULL;
131 tooltip->toplevel_window = NULL;
133 tooltip->last_window = NULL;
135 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
136 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
137 GDK_WINDOW_TYPE_HINT_TOOLTIP);
138 gtk_widget_set_app_paintable (tooltip->window, TRUE);
139 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
140 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
141 g_signal_connect (tooltip->window, "hide",
142 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
144 style = gtk_widget_get_style (tooltip->window);
146 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
147 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
148 style->ythickness, style->ythickness,
149 style->xthickness, style->xthickness);
150 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
151 gtk_widget_show (tooltip->alignment);
153 g_signal_connect_swapped (tooltip->window, "style-set",
154 G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
155 g_signal_connect_swapped (tooltip->window, "draw",
156 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
158 tooltip->box = gtk_hbox_new (FALSE, style->xthickness);
159 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
160 gtk_widget_show (tooltip->box);
162 tooltip->image = gtk_image_new ();
163 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
166 tooltip->label = gtk_label_new ("");
167 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
168 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
171 tooltip->custom_widget = NULL;
175 gtk_tooltip_dispose (GObject *object)
177 GtkTooltip *tooltip = GTK_TOOLTIP (object);
179 if (tooltip->timeout_id)
181 g_source_remove (tooltip->timeout_id);
182 tooltip->timeout_id = 0;
185 if (tooltip->browse_mode_timeout_id)
187 g_source_remove (tooltip->browse_mode_timeout_id);
188 tooltip->browse_mode_timeout_id = 0;
191 gtk_tooltip_set_custom (tooltip, NULL);
192 gtk_tooltip_set_last_window (tooltip, NULL);
198 display = gtk_widget_get_display (tooltip->window);
199 g_signal_handlers_disconnect_by_func (display,
200 gtk_tooltip_display_closed,
202 gtk_widget_destroy (tooltip->window);
203 tooltip->window = NULL;
206 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
212 * gtk_tooltip_set_markup:
213 * @tooltip: a #GtkTooltip
214 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
216 * Sets the text of the tooltip to be @markup, which is marked up
218 * linkend="PangoMarkupFormat">Pango text markup language</link>.
219 * If @markup is %NULL, the label will be hidden.
224 gtk_tooltip_set_markup (GtkTooltip *tooltip,
227 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
229 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
232 gtk_widget_show (tooltip->label);
234 gtk_widget_hide (tooltip->label);
238 * gtk_tooltip_set_text:
239 * @tooltip: a #GtkTooltip
240 * @text: (allow-none): a text string or %NULL
242 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
243 * will be hidden. See also gtk_tooltip_set_markup().
248 gtk_tooltip_set_text (GtkTooltip *tooltip,
251 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
253 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
256 gtk_widget_show (tooltip->label);
258 gtk_widget_hide (tooltip->label);
262 * gtk_tooltip_set_icon:
263 * @tooltip: a #GtkTooltip
264 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
266 * Sets the icon of the tooltip (which is in front of the text) to be
267 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
272 gtk_tooltip_set_icon (GtkTooltip *tooltip,
275 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
277 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
279 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
282 gtk_widget_show (tooltip->image);
284 gtk_widget_hide (tooltip->image);
288 * gtk_tooltip_set_icon_from_stock:
289 * @tooltip: a #GtkTooltip
290 * @stock_id: (allow-none): a stock id, or %NULL
291 * @size: (type int): a stock icon size
293 * Sets the icon of the tooltip (which is in front of the text) to be
294 * the stock item indicated by @stock_id with the size indicated
295 * by @size. If @stock_id is %NULL, the image will be hidden.
300 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
301 const gchar *stock_id,
304 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
306 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
309 gtk_widget_show (tooltip->image);
311 gtk_widget_hide (tooltip->image);
315 * gtk_tooltip_set_icon_from_icon_name:
316 * @tooltip: a #GtkTooltip
317 * @icon_name: (allow-none): an icon name, or %NULL
318 * @size: (type int): a stock icon size
320 * Sets the icon of the tooltip (which is in front of the text) to be
321 * the icon indicated by @icon_name with the size indicated
322 * by @size. If @icon_name is %NULL, the image will be hidden.
327 gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
328 const gchar *icon_name,
331 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
333 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
336 gtk_widget_show (tooltip->image);
338 gtk_widget_hide (tooltip->image);
342 * gtk_tooltip_set_icon_from_gicon:
343 * @tooltip: a #GtkTooltip
344 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
345 * @size: (type int): a stock icon size
347 * Sets the icon of the tooltip (which is in front of the text)
348 * to be the icon indicated by @gicon with the size indicated
349 * by @size. If @gicon is %NULL, the image will be hidden.
354 gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
358 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
360 gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
363 gtk_widget_show (tooltip->image);
365 gtk_widget_hide (tooltip->image);
369 * gtk_tooltip_set_custom:
370 * @tooltip: a #GtkTooltip
371 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
373 * Replaces the widget packed into the tooltip with
374 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
376 * By default a box with a #GtkImage and #GtkLabel is embedded in
377 * the tooltip, which can be configured using gtk_tooltip_set_markup()
378 * and gtk_tooltip_set_icon().
384 gtk_tooltip_set_custom (GtkTooltip *tooltip,
385 GtkWidget *custom_widget)
387 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
389 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
391 /* The custom widget has been updated from the query-tooltip
392 * callback, so we do not want to reset the custom widget later on.
394 tooltip->custom_was_reset = TRUE;
396 /* No need to do anything if the custom widget stays the same */
397 if (tooltip->custom_widget == custom_widget)
400 if (tooltip->custom_widget)
402 GtkWidget *custom = tooltip->custom_widget;
403 /* Note: We must reset tooltip->custom_widget first,
404 * since gtk_container_remove() will recurse into
405 * gtk_tooltip_set_custom()
407 tooltip->custom_widget = NULL;
408 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
409 g_object_unref (custom);
414 tooltip->custom_widget = g_object_ref (custom_widget);
416 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
417 gtk_widget_show (custom_widget);
422 * gtk_tooltip_set_tip_area:
423 * @tooltip: a #GtkTooltip
424 * @rect: a #GdkRectangle
426 * Sets the area of the widget, where the contents of this tooltip apply,
427 * to be @rect (in widget coordinates). This is especially useful for
428 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
431 * For setting tooltips on #GtkTreeView, please refer to the convenience
432 * functions for this: gtk_tree_view_set_tooltip_row() and
433 * gtk_tree_view_set_tooltip_cell().
438 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
439 const GdkRectangle *rect)
441 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
444 tooltip->tip_area_set = FALSE;
447 tooltip->tip_area_set = TRUE;
448 tooltip->tip_area = *rect;
453 * gtk_tooltip_trigger_tooltip_query:
454 * @display: a #GdkDisplay
456 * Triggers a new tooltip query on @display, in order to update the current
457 * visible tooltip, or to show/hide the current tooltip. This function is
458 * useful to call when, for example, the state of the widget changed by a
464 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
470 /* Trigger logic as if the mouse moved */
471 window = gdk_display_get_window_at_pointer (display, &x, &y);
475 event.type = GDK_MOTION_NOTIFY;
476 event.motion.window = window;
479 event.motion.is_hint = FALSE;
481 gdk_window_get_root_coords (window, x, y, &x, &y);
482 event.motion.x_root = x;
483 event.motion.y_root = y;
485 _gtk_tooltip_handle_event (&event);
488 /* private functions */
491 gtk_tooltip_reset (GtkTooltip *tooltip)
493 gtk_tooltip_set_markup (tooltip, NULL);
494 gtk_tooltip_set_icon (tooltip, NULL);
495 gtk_tooltip_set_tip_area (tooltip, NULL);
497 /* See if the custom widget is again set from the query-tooltip
500 tooltip->custom_was_reset = FALSE;
504 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
508 style = gtk_widget_get_style (tooltip->window);
510 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
511 style->ythickness, style->ythickness,
512 style->xthickness, style->xthickness);
514 gtk_box_set_spacing (GTK_BOX (tooltip->box),
517 gtk_widget_queue_draw (tooltip->window);
521 gtk_tooltip_paint_window (GtkTooltip *tooltip,
524 gtk_paint_flat_box (gtk_widget_get_style (tooltip->window),
531 gtk_widget_get_allocated_width (tooltip->window),
532 gtk_widget_get_allocated_height (tooltip->window));
538 gtk_tooltip_window_hide (GtkWidget *widget,
541 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
543 gtk_tooltip_set_custom (tooltip, NULL);
546 /* event handling, etc */
551 GtkWidget *container;
558 child_location_foreach (GtkWidget *child,
561 GtkAllocation child_allocation;
563 struct ChildLocation *child_loc = data;
565 /* Ignore invisible widgets */
566 if (!gtk_widget_is_drawable (child))
569 gtk_widget_get_allocation (child, &child_allocation);
574 /* (child_loc->x, child_loc->y) are relative to
575 * child_loc->container's allocation.
578 if (!child_loc->child &&
579 gtk_widget_translate_coordinates (child_loc->container, child,
580 child_loc->x, child_loc->y,
584 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
585 gtk_widget_get_name (child),
588 child_allocation.width,
589 child_allocation.height,
590 child_loc->x, child_loc->y,
592 #endif /* DEBUG_TOOLTIP */
594 /* (x, y) relative to child's allocation. */
595 if (x >= 0 && x < child_allocation.width
596 && y >= 0 && y < child_allocation.height)
598 if (GTK_IS_CONTAINER (child))
600 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
602 /* Take (x, y) relative the child's allocation and
607 tmp.container = child;
609 gtk_container_forall (GTK_CONTAINER (child),
610 child_location_foreach, &tmp);
613 child_loc->child = tmp.child;
615 child_loc->child = child;
618 child_loc->child = child;
623 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
624 * to allocation relative (dest_x, dest_y) of dest_widget.
627 window_to_alloc (GtkWidget *dest_widget,
633 GtkAllocation allocation;
635 gtk_widget_get_allocation (dest_widget, &allocation);
637 /* Translate from window relative to allocation relative */
638 if (gtk_widget_get_has_window (dest_widget) &&
639 gtk_widget_get_parent (dest_widget))
642 gdk_window_get_position (gtk_widget_get_window (dest_widget),
645 /* Offset coordinates if widget->window is smaller than
646 * widget->allocation.
648 src_x += wx - allocation.x;
649 src_y += wy - allocation.y;
653 src_x -= allocation.x;
654 src_y -= allocation.y;
663 /* Translates coordinates from window relative (x, y) to
664 * allocation relative (x, y) of the returned widget.
667 _gtk_widget_find_at_coords (GdkWindow *window,
673 GtkWidget *event_widget;
674 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
676 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
678 gdk_window_get_user_data (window, (void **)&event_widget);
684 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
685 window, event_widget, gtk_widget_get_name (event_widget),
689 /* Coordinates are relative to event window */
690 child_loc.x = window_x;
691 child_loc.y = window_y;
693 /* We go down the window hierarchy to the widget->window,
694 * coordinates stay relative to the current window.
695 * We end up with window == widget->window, coordinates relative to that.
697 while (window && window != gtk_widget_get_window (event_widget))
701 gdk_window_coords_to_parent (window,
702 child_loc.x, child_loc.y,
707 window = gdk_window_get_effective_parent (window);
710 /* Failing to find widget->window can happen for e.g. a detached handle box;
711 * chaining ::query-tooltip up to its parent probably makes little sense,
712 * and users better implement tooltips on handle_box->child.
713 * so we simply ignore the event for tooltips here.
718 /* Convert the window relative coordinates to allocation
719 * relative coordinates.
721 window_to_alloc (event_widget,
722 child_loc.x, child_loc.y,
723 &child_loc.x, &child_loc.y);
725 if (GTK_IS_CONTAINER (event_widget))
727 GtkWidget *container = event_widget;
729 child_loc.container = event_widget;
730 child_loc.child = NULL;
732 gtk_container_forall (GTK_CONTAINER (event_widget),
733 child_location_foreach, &child_loc);
735 /* Here we have a widget, with coordinates relative to
736 * child_loc.container's allocation.
740 event_widget = child_loc.child;
741 else if (child_loc.container)
742 event_widget = child_loc.container;
744 /* Translate to event_widget's allocation */
745 gtk_widget_translate_coordinates (container, event_widget,
746 child_loc.x, child_loc.y,
747 &child_loc.x, &child_loc.y);
750 /* We return (x, y) relative to the allocation of event_widget. */
752 *widget_x = child_loc.x;
754 *widget_y = child_loc.y;
759 /* Ignores (x, y) on input, translates event coordinates to
760 * allocation relative (x, y) of the returned widget.
763 find_topmost_widget_coords_from_event (GdkEvent *event,
767 GtkAllocation allocation;
772 gdk_event_get_coords (event, &dx, &dy);
774 /* Returns coordinates relative to tmp's allocation. */
775 tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
780 /* Make sure the pointer can actually be on the widget returned. */
781 gtk_widget_get_allocation (tmp, &allocation);
782 if (tx < 0 || tx >= allocation.width ||
783 ty < 0 || ty >= allocation.height)
795 tooltip_browse_mode_expired (gpointer data)
799 tooltip = GTK_TOOLTIP (data);
801 tooltip->browse_mode_enabled = FALSE;
802 tooltip->browse_mode_timeout_id = 0;
804 /* destroy tooltip */
805 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
806 "gdk-display-current-tooltip", NULL);
812 gtk_tooltip_display_closed (GdkDisplay *display,
816 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
820 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
823 if (tooltip->last_window == window)
826 if (tooltip->last_window)
827 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
828 (gpointer *) &tooltip->last_window);
830 tooltip->last_window = window;
833 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
834 (gpointer *) &tooltip->last_window);
838 gtk_tooltip_run_requery (GtkWidget **widget,
843 gboolean has_tooltip = FALSE;
844 gboolean return_value = FALSE;
846 gtk_tooltip_reset (tooltip);
850 g_object_get (*widget,
851 "has-tooltip", &has_tooltip,
855 g_signal_emit_by_name (*widget,
858 tooltip->keyboard_mode_enabled,
864 GtkWidget *parent = gtk_widget_get_parent (*widget);
867 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
876 /* If the custom widget was not reset in the query-tooltip
877 * callback, we clear it here.
879 if (!tooltip->custom_was_reset)
880 gtk_tooltip_set_custom (tooltip, NULL);
886 get_bounding_box (GtkWidget *widget,
887 GdkRectangle *bounds)
889 GtkAllocation allocation;
898 window = gtk_widget_get_parent_window (widget);
900 window = gtk_widget_get_window (widget);
902 gtk_widget_get_allocation (widget, &allocation);
905 w = allocation.width;
906 h = allocation.height;
908 gdk_window_get_root_coords (window, x, y, &x1, &y1);
909 gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
910 gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
911 gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
913 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
914 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
916 bounds->x = floor (MIN4 (x1, x2, x3, x4));
917 bounds->y = floor (MIN4 (y1, y2, y3, y4));
918 bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
919 bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
923 gtk_tooltip_position (GtkTooltip *tooltip,
925 GtkWidget *new_tooltip_widget)
930 GdkRectangle monitor;
931 GtkRequisition requisition;
935 #define MAX_DISTANCE 32
937 tooltip->tooltip_widget = new_tooltip_widget;
939 screen = gtk_widget_get_screen (new_tooltip_widget);
941 gtk_widget_get_preferred_size (GTK_WIDGET (tooltip->current_window),
944 monitor_num = gdk_screen_get_monitor_at_point (screen,
947 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
949 get_bounding_box (new_tooltip_widget, &bounds);
951 /* Position the tooltip */
953 cursor_size = gdk_display_get_default_cursor_size (display);
956 x = bounds.x + bounds.width / 2 - requisition.width / 2;
957 y = bounds.y + bounds.height + 4;
959 if (y + requisition.height <= monitor.y + monitor.height)
961 if (tooltip->keyboard_mode_enabled)
964 if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
966 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
967 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
968 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
969 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
976 x = bounds.x + bounds.width / 2 - requisition.width / 2;
977 y = bounds.y - requisition.height - 4;
981 if (tooltip->keyboard_mode_enabled)
984 if (y + requisition.height >= tooltip->last_y - MAX_DISTANCE)
986 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
987 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
988 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
989 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
995 /* Try right FIXME: flip on rtl ? */
996 x = bounds.x + bounds.width + 4;
997 y = bounds.y + bounds.height / 2 - requisition.height / 2;
999 if (x + requisition.width <= monitor.x + monitor.width)
1001 if (tooltip->keyboard_mode_enabled)
1004 if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
1006 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1007 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1008 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1009 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1015 /* Try left FIXME: flip on rtl ? */
1016 x = bounds.x - requisition.width - 4;
1017 y = bounds.y + bounds.height / 2 - requisition.height / 2;
1021 if (tooltip->keyboard_mode_enabled)
1024 if (x + requisition.width >= tooltip->last_x - MAX_DISTANCE)
1026 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1027 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1028 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1029 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1036 if (tooltip->keyboard_mode_enabled)
1038 x = bounds.x + bounds.width / 2 - requisition.width / 2;
1039 y = bounds.y + bounds.height + 4;
1044 x = tooltip->last_x + cursor_size * 3 / 4;
1045 y = tooltip->last_y + cursor_size * 3 / 4;
1050 if (tooltip->current_window)
1052 if (x + requisition.width > monitor.x + monitor.width)
1053 x -= x - (monitor.x + monitor.width) + requisition.width;
1054 else if (x < monitor.x)
1057 if (y + requisition.height > monitor.y + monitor.height)
1058 y -= y - (monitor.y + monitor.height) + requisition.height;
1059 else if (y < monitor.y)
1062 if (!tooltip->keyboard_mode_enabled)
1064 /* don't pop up under the pointer */
1065 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
1066 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
1067 y = tooltip->last_y - requisition.height - 2;
1070 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1071 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1076 gtk_tooltip_show_tooltip (GdkDisplay *display)
1082 GtkWidget *tooltip_widget;
1083 GtkWidget *pointer_widget;
1084 GtkTooltip *tooltip;
1085 gboolean has_tooltip;
1086 gboolean return_value = FALSE;
1088 tooltip = g_object_get_data (G_OBJECT (display),
1089 "gdk-display-current-tooltip");
1091 if (tooltip->keyboard_mode_enabled)
1094 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
1100 window = tooltip->last_window;
1102 if (!GDK_IS_WINDOW (window))
1105 gdk_window_get_pointer (window, &x, &y, NULL);
1107 gdk_window_get_root_coords (window, x, y, &tx, &ty);
1108 tooltip->last_x = tx;
1109 tooltip->last_y = ty;
1111 pointer_widget = tooltip_widget = _gtk_widget_find_at_coords (window,
1116 if (!tooltip_widget)
1119 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1121 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1125 if (!tooltip->current_window)
1127 if (gtk_widget_get_tooltip_window (tooltip_widget))
1128 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1130 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1133 screen = gtk_widget_get_screen (tooltip_widget);
1135 /* FIXME: should use tooltip->current_window iso tooltip->window */
1136 if (screen != gtk_widget_get_screen (tooltip->window))
1138 g_signal_handlers_disconnect_by_func (display,
1139 gtk_tooltip_display_closed,
1142 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1144 g_signal_connect (display, "closed",
1145 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1148 gtk_tooltip_position (tooltip, display, tooltip_widget);
1150 /* Now a tooltip is visible again on the display, make sure browse
1153 tooltip->browse_mode_enabled = TRUE;
1154 if (tooltip->browse_mode_timeout_id)
1156 g_source_remove (tooltip->browse_mode_timeout_id);
1157 tooltip->browse_mode_timeout_id = 0;
1162 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1167 if (tooltip->timeout_id)
1169 g_source_remove (tooltip->timeout_id);
1170 tooltip->timeout_id = 0;
1173 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1176 tooltip->tooltip_widget = NULL;
1178 if (!tooltip->keyboard_mode_enabled)
1181 GtkSettings *settings;
1183 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1185 g_object_get (settings,
1186 "gtk-tooltip-browse-mode-timeout", &timeout,
1189 /* The tooltip is gone, after (by default, should be configurable) 500ms
1190 * we want to turn off browse mode
1192 if (!tooltip->browse_mode_timeout_id)
1193 tooltip->browse_mode_timeout_id =
1194 gdk_threads_add_timeout_full (0, timeout,
1195 tooltip_browse_mode_expired,
1196 g_object_ref (tooltip),
1201 if (tooltip->browse_mode_timeout_id)
1203 g_source_remove (tooltip->browse_mode_timeout_id);
1204 tooltip->browse_mode_timeout_id = 0;
1208 if (tooltip->current_window)
1210 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1211 tooltip->current_window = NULL;
1216 tooltip_popup_timeout (gpointer data)
1218 GdkDisplay *display;
1219 GtkTooltip *tooltip;
1221 display = GDK_DISPLAY_OBJECT (data);
1222 tooltip = g_object_get_data (G_OBJECT (display),
1223 "gdk-display-current-tooltip");
1225 /* This usually does not happen. However, it does occur in language
1226 * bindings were reference counting of objects behaves differently.
1231 gtk_tooltip_show_tooltip (display);
1233 tooltip->timeout_id = 0;
1239 gtk_tooltip_start_delay (GdkDisplay *display)
1242 GtkTooltip *tooltip;
1243 GtkSettings *settings;
1245 tooltip = g_object_get_data (G_OBJECT (display),
1246 "gdk-display-current-tooltip");
1248 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1251 if (tooltip->timeout_id)
1252 g_source_remove (tooltip->timeout_id);
1254 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1256 if (tooltip->browse_mode_enabled)
1257 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1259 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1261 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1262 tooltip_popup_timeout,
1263 g_object_ref (display),
1268 _gtk_tooltip_focus_in (GtkWidget *widget)
1271 gboolean return_value = FALSE;
1272 GdkDisplay *display;
1273 GtkTooltip *tooltip;
1276 /* Get current tooltip for this display */
1277 display = gtk_widget_get_display (widget);
1278 tooltip = g_object_get_data (G_OBJECT (display),
1279 "gdk-display-current-tooltip");
1281 /* Check if keyboard mode is enabled at this moment */
1282 if (!tooltip || !tooltip->keyboard_mode_enabled)
1285 device = gtk_get_current_event_device ();
1287 if (device && device->source == GDK_SOURCE_KEYBOARD)
1288 device = gdk_device_get_associated_device (device);
1290 /* This function should be called by either a focus in event,
1291 * or a key binding. In either case there should be a device.
1296 if (tooltip->keyboard_widget)
1297 g_object_unref (tooltip->keyboard_widget);
1299 tooltip->keyboard_widget = g_object_ref (widget);
1301 gdk_window_get_device_position (gtk_widget_get_window (widget),
1302 device, &x, &y, NULL);
1304 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1307 gtk_tooltip_hide_tooltip (tooltip);
1311 if (!tooltip->current_window)
1313 if (gtk_widget_get_tooltip_window (widget))
1314 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1316 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1319 gtk_tooltip_show_tooltip (display);
1323 _gtk_tooltip_focus_out (GtkWidget *widget)
1325 GdkDisplay *display;
1326 GtkTooltip *tooltip;
1328 /* Get current tooltip for this display */
1329 display = gtk_widget_get_display (widget);
1330 tooltip = g_object_get_data (G_OBJECT (display),
1331 "gdk-display-current-tooltip");
1333 if (!tooltip || !tooltip->keyboard_mode_enabled)
1336 if (tooltip->keyboard_widget)
1338 g_object_unref (tooltip->keyboard_widget);
1339 tooltip->keyboard_widget = NULL;
1342 gtk_tooltip_hide_tooltip (tooltip);
1346 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1348 GdkDisplay *display;
1349 GtkTooltip *tooltip;
1351 display = gtk_widget_get_display (widget);
1352 tooltip = g_object_get_data (G_OBJECT (display),
1353 "gdk-display-current-tooltip");
1357 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1358 g_object_set_data_full (G_OBJECT (display),
1359 "gdk-display-current-tooltip",
1360 tooltip, g_object_unref);
1361 g_signal_connect (display, "closed",
1362 G_CALLBACK (gtk_tooltip_display_closed),
1366 tooltip->keyboard_mode_enabled ^= 1;
1368 if (tooltip->keyboard_mode_enabled)
1370 tooltip->keyboard_widget = g_object_ref (widget);
1371 _gtk_tooltip_focus_in (widget);
1375 if (tooltip->keyboard_widget)
1377 g_object_unref (tooltip->keyboard_widget);
1378 tooltip->keyboard_widget = NULL;
1381 gtk_tooltip_hide_tooltip (tooltip);
1386 _gtk_tooltip_hide (GtkWidget *widget)
1388 GtkWidget *toplevel;
1389 GdkDisplay *display;
1390 GtkTooltip *tooltip;
1392 display = gtk_widget_get_display (widget);
1393 tooltip = g_object_get_data (G_OBJECT (display),
1394 "gdk-display-current-tooltip");
1396 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1399 toplevel = gtk_widget_get_toplevel (widget);
1401 if (widget == tooltip->tooltip_widget
1402 || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
1403 gtk_tooltip_hide_tooltip (tooltip);
1407 tooltips_enabled (GdkWindow *window)
1410 gboolean touchscreen;
1412 GtkSettings *settings;
1414 screen = gdk_window_get_screen (window);
1415 settings = gtk_settings_get_for_screen (screen);
1417 g_object_get (settings,
1418 "gtk-touchscreen-mode", &touchscreen,
1419 "gtk-enable-tooltips", &enabled,
1422 return (!touchscreen && enabled);
1426 _gtk_tooltip_handle_event (GdkEvent *event)
1429 gboolean return_value = FALSE;
1430 GtkWidget *has_tooltip_widget = NULL;
1431 GdkDisplay *display;
1432 GtkTooltip *current_tooltip;
1434 if (!tooltips_enabled (event->any.window))
1437 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1438 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1439 display = gdk_window_get_display (event->any.window);
1440 current_tooltip = g_object_get_data (G_OBJECT (display),
1441 "gdk-display-current-tooltip");
1443 if (current_tooltip)
1445 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1448 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1450 has_tooltip_widget = current_tooltip->keyboard_widget;
1451 if (!has_tooltip_widget)
1454 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1459 gtk_tooltip_hide_tooltip (current_tooltip);
1461 gtk_tooltip_start_delay (display);
1466 #ifdef DEBUG_TOOLTIP
1467 if (has_tooltip_widget)
1468 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1469 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1470 has_tooltip_widget->allocation.x,
1471 has_tooltip_widget->allocation.y,
1472 has_tooltip_widget->allocation.width,
1473 has_tooltip_widget->allocation.height,
1475 #endif /* DEBUG_TOOLTIP */
1477 /* Always poll for a next motion event */
1478 gdk_event_request_motions (&event->motion);
1480 /* Hide the tooltip when there's no new tooltip widget */
1481 if (!has_tooltip_widget)
1483 if (current_tooltip)
1484 gtk_tooltip_hide_tooltip (current_tooltip);
1489 switch (event->type)
1491 case GDK_BUTTON_PRESS:
1492 case GDK_2BUTTON_PRESS:
1493 case GDK_3BUTTON_PRESS:
1495 case GDK_DRAG_ENTER:
1496 case GDK_GRAB_BROKEN:
1497 gtk_tooltip_hide_tooltip (current_tooltip);
1500 case GDK_MOTION_NOTIFY:
1501 case GDK_ENTER_NOTIFY:
1502 case GDK_LEAVE_NOTIFY:
1504 if (current_tooltip)
1506 gboolean tip_area_set;
1507 GdkRectangle tip_area;
1508 gboolean hide_tooltip;
1510 tip_area_set = current_tooltip->tip_area_set;
1511 tip_area = current_tooltip->tip_area;
1513 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1517 /* Requested to be hidden? */
1518 hide_tooltip = !return_value;
1520 /* Leave notify should override the query function */
1521 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1523 /* Is the pointer above another widget now? */
1524 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1525 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1527 /* Did the pointer move out of the previous "context area"? */
1529 hide_tooltip |= (x <= tip_area.x
1530 || x >= tip_area.x + tip_area.width
1532 || y >= tip_area.y + tip_area.height);
1535 gtk_tooltip_hide_tooltip (current_tooltip);
1537 gtk_tooltip_start_delay (display);
1541 /* Need a new tooltip for this display */
1542 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1543 g_object_set_data_full (G_OBJECT (display),
1544 "gdk-display-current-tooltip",
1545 current_tooltip, g_object_unref);
1546 g_signal_connect (display, "closed",
1547 G_CALLBACK (gtk_tooltip_display_closed),
1550 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1552 gtk_tooltip_start_delay (display);