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"
41 * @Short_description: Add tips to your widgets
44 * Basic tooltips can be realized simply by using gtk_widget_set_tooltip_text()
45 * or gtk_widget_set_tooltip_markup() without any explicit tooltip object.
47 * When you need a tooltip with a little more fancy contents, like adding an
48 * image, or you want the tooltip to have different contents per #GtkTreeView
49 * row or cell, you will have to do a little more work:
53 * Set the #GtkWidget:has-tooltip property to %TRUE, this will make GTK+
54 * monitor the widget for motion and related events which are needed to
55 * determine when and where to show a tooltip.
60 * Connect to the #GtkWidget::query-tooltip signal. This signal will be
61 * emitted when a tooltip is supposed to be shown. One of the arguments passed
62 * to the signal handler is a GtkTooltip object. This is the object that we
63 * are about to display as a tooltip, and can be manipulated in your callback
64 * using functions like gtk_tooltip_set_icon(). There are functions for setting
65 * the tooltip's markup, setting an image from a stock icon, or even putting in
71 * Return %TRUE from your query-tooltip handler. This causes the tooltip to be
72 * show. If you return %FALSE, it will not be shown.
77 * In the probably rare case where you want to have even more control over the
78 * tooltip that is about to be shown, you can set your own #GtkWindow which
79 * will be used as tooltip window. This works as follows:
83 * Set #GtkWidget:has-tooltip and connect to #GtkWidget::query-tooltip as
89 * Use gtk_widget_set_tooltip_window() to set a #GtkWindow created by you as
95 * In the #GtkWidget::query-tooltip callback you can access your window using
96 * gtk_widget_get_tooltip_window() and manipulate as you wish. The semantics of
97 * the return value are exactly as before, return %TRUE to show the window,
98 * %FALSE to not show it.
108 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
109 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
110 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
112 typedef struct _GtkTooltipClass GtkTooltipClass;
116 GObject parent_instance;
119 GtkWidget *alignment;
123 GtkWidget *custom_widget;
125 GtkWindow *current_window;
126 GtkWidget *keyboard_widget;
128 GtkWidget *tooltip_widget;
129 GdkWindow *toplevel_window;
133 GdkWindow *last_window;
136 guint browse_mode_timeout_id;
138 GdkRectangle tip_area;
140 guint browse_mode_enabled : 1;
141 guint keyboard_mode_enabled : 1;
142 guint tip_area_set : 1;
143 guint custom_was_reset : 1;
146 struct _GtkTooltipClass
148 GObjectClass parent_class;
151 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
154 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
155 static void gtk_tooltip_init (GtkTooltip *tooltip);
156 static void gtk_tooltip_dispose (GObject *object);
158 static void gtk_tooltip_window_style_set (GtkTooltip *tooltip);
159 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip,
161 static void gtk_tooltip_window_hide (GtkWidget *widget,
163 static void gtk_tooltip_display_closed (GdkDisplay *display,
165 GtkTooltip *tooltip);
166 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
170 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
173 gtk_tooltip_class_init (GtkTooltipClass *klass)
175 GObjectClass *object_class;
177 object_class = G_OBJECT_CLASS (klass);
179 object_class->dispose = gtk_tooltip_dispose;
183 gtk_tooltip_init (GtkTooltip *tooltip)
187 tooltip->timeout_id = 0;
188 tooltip->browse_mode_timeout_id = 0;
190 tooltip->browse_mode_enabled = FALSE;
191 tooltip->keyboard_mode_enabled = FALSE;
193 tooltip->current_window = NULL;
194 tooltip->keyboard_widget = NULL;
196 tooltip->tooltip_widget = NULL;
197 tooltip->toplevel_window = NULL;
199 tooltip->last_window = NULL;
201 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
202 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
203 GDK_WINDOW_TYPE_HINT_TOOLTIP);
204 gtk_widget_set_app_paintable (tooltip->window, TRUE);
205 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
206 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
207 g_signal_connect (tooltip->window, "hide",
208 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
210 style = gtk_widget_get_style (tooltip->window);
212 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
213 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
214 style->ythickness, style->ythickness,
215 style->xthickness, style->xthickness);
216 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
217 gtk_widget_show (tooltip->alignment);
219 g_signal_connect_swapped (tooltip->window, "style-set",
220 G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
221 g_signal_connect_swapped (tooltip->window, "draw",
222 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
224 tooltip->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, style->xthickness);
225 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
226 gtk_widget_show (tooltip->box);
228 tooltip->image = gtk_image_new ();
229 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
232 tooltip->label = gtk_label_new ("");
233 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
234 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
237 tooltip->custom_widget = NULL;
241 gtk_tooltip_dispose (GObject *object)
243 GtkTooltip *tooltip = GTK_TOOLTIP (object);
245 if (tooltip->timeout_id)
247 g_source_remove (tooltip->timeout_id);
248 tooltip->timeout_id = 0;
251 if (tooltip->browse_mode_timeout_id)
253 g_source_remove (tooltip->browse_mode_timeout_id);
254 tooltip->browse_mode_timeout_id = 0;
257 gtk_tooltip_set_custom (tooltip, NULL);
258 gtk_tooltip_set_last_window (tooltip, NULL);
264 display = gtk_widget_get_display (tooltip->window);
265 g_signal_handlers_disconnect_by_func (display,
266 gtk_tooltip_display_closed,
268 gtk_widget_destroy (tooltip->window);
269 tooltip->window = NULL;
272 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
278 * gtk_tooltip_set_markup:
279 * @tooltip: a #GtkTooltip
280 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
282 * Sets the text of the tooltip to be @markup, which is marked up
284 * linkend="PangoMarkupFormat">Pango text markup language</link>.
285 * If @markup is %NULL, the label will be hidden.
290 gtk_tooltip_set_markup (GtkTooltip *tooltip,
293 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
295 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
298 gtk_widget_show (tooltip->label);
300 gtk_widget_hide (tooltip->label);
304 * gtk_tooltip_set_text:
305 * @tooltip: a #GtkTooltip
306 * @text: (allow-none): a text string or %NULL
308 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
309 * will be hidden. See also gtk_tooltip_set_markup().
314 gtk_tooltip_set_text (GtkTooltip *tooltip,
317 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
319 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
322 gtk_widget_show (tooltip->label);
324 gtk_widget_hide (tooltip->label);
328 * gtk_tooltip_set_icon:
329 * @tooltip: a #GtkTooltip
330 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
332 * Sets the icon of the tooltip (which is in front of the text) to be
333 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
338 gtk_tooltip_set_icon (GtkTooltip *tooltip,
341 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
343 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
345 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
348 gtk_widget_show (tooltip->image);
350 gtk_widget_hide (tooltip->image);
354 * gtk_tooltip_set_icon_from_stock:
355 * @tooltip: a #GtkTooltip
356 * @stock_id: (allow-none): a stock id, or %NULL
357 * @size: (type int): a stock icon size
359 * Sets the icon of the tooltip (which is in front of the text) to be
360 * the stock item indicated by @stock_id with the size indicated
361 * by @size. If @stock_id is %NULL, the image will be hidden.
366 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
367 const gchar *stock_id,
370 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
372 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
375 gtk_widget_show (tooltip->image);
377 gtk_widget_hide (tooltip->image);
381 * gtk_tooltip_set_icon_from_icon_name:
382 * @tooltip: a #GtkTooltip
383 * @icon_name: (allow-none): an icon name, or %NULL
384 * @size: (type int): a stock icon size
386 * Sets the icon of the tooltip (which is in front of the text) to be
387 * the icon indicated by @icon_name with the size indicated
388 * by @size. If @icon_name is %NULL, the image will be hidden.
393 gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
394 const gchar *icon_name,
397 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
399 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
402 gtk_widget_show (tooltip->image);
404 gtk_widget_hide (tooltip->image);
408 * gtk_tooltip_set_icon_from_gicon:
409 * @tooltip: a #GtkTooltip
410 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
411 * @size: (type int): a stock icon size
413 * Sets the icon of the tooltip (which is in front of the text)
414 * to be the icon indicated by @gicon with the size indicated
415 * by @size. If @gicon is %NULL, the image will be hidden.
420 gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
424 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
426 gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
429 gtk_widget_show (tooltip->image);
431 gtk_widget_hide (tooltip->image);
435 * gtk_tooltip_set_custom:
436 * @tooltip: a #GtkTooltip
437 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
439 * Replaces the widget packed into the tooltip with
440 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
442 * By default a box with a #GtkImage and #GtkLabel is embedded in
443 * the tooltip, which can be configured using gtk_tooltip_set_markup()
444 * and gtk_tooltip_set_icon().
450 gtk_tooltip_set_custom (GtkTooltip *tooltip,
451 GtkWidget *custom_widget)
453 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
455 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
457 /* The custom widget has been updated from the query-tooltip
458 * callback, so we do not want to reset the custom widget later on.
460 tooltip->custom_was_reset = TRUE;
462 /* No need to do anything if the custom widget stays the same */
463 if (tooltip->custom_widget == custom_widget)
466 if (tooltip->custom_widget)
468 GtkWidget *custom = tooltip->custom_widget;
469 /* Note: We must reset tooltip->custom_widget first,
470 * since gtk_container_remove() will recurse into
471 * gtk_tooltip_set_custom()
473 tooltip->custom_widget = NULL;
474 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
475 g_object_unref (custom);
480 tooltip->custom_widget = g_object_ref (custom_widget);
482 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
483 gtk_widget_show (custom_widget);
488 * gtk_tooltip_set_tip_area:
489 * @tooltip: a #GtkTooltip
490 * @rect: a #GdkRectangle
492 * Sets the area of the widget, where the contents of this tooltip apply,
493 * to be @rect (in widget coordinates). This is especially useful for
494 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
497 * For setting tooltips on #GtkTreeView, please refer to the convenience
498 * functions for this: gtk_tree_view_set_tooltip_row() and
499 * gtk_tree_view_set_tooltip_cell().
504 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
505 const GdkRectangle *rect)
507 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
510 tooltip->tip_area_set = FALSE;
513 tooltip->tip_area_set = TRUE;
514 tooltip->tip_area = *rect;
519 * gtk_tooltip_trigger_tooltip_query:
520 * @display: a #GdkDisplay
522 * Triggers a new tooltip query on @display, in order to update the current
523 * visible tooltip, or to show/hide the current tooltip. This function is
524 * useful to call when, for example, the state of the widget changed by a
530 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
537 /* Trigger logic as if the mouse moved */
538 device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
539 window = gdk_display_get_window_at_device_position (display,
545 event.type = GDK_MOTION_NOTIFY;
546 event.motion.window = window;
549 event.motion.is_hint = FALSE;
551 gdk_window_get_root_coords (window, x, y, &x, &y);
552 event.motion.x_root = x;
553 event.motion.y_root = y;
555 _gtk_tooltip_handle_event (&event);
558 /* private functions */
561 gtk_tooltip_reset (GtkTooltip *tooltip)
563 gtk_tooltip_set_markup (tooltip, NULL);
564 gtk_tooltip_set_icon (tooltip, NULL);
565 gtk_tooltip_set_tip_area (tooltip, NULL);
567 /* See if the custom widget is again set from the query-tooltip
570 tooltip->custom_was_reset = FALSE;
574 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
578 style = gtk_widget_get_style (tooltip->window);
580 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
581 style->ythickness, style->ythickness,
582 style->xthickness, style->xthickness);
584 gtk_box_set_spacing (GTK_BOX (tooltip->box),
587 gtk_widget_queue_draw (tooltip->window);
591 gtk_tooltip_paint_window (GtkTooltip *tooltip,
594 gtk_paint_flat_box (gtk_widget_get_style (tooltip->window),
601 gtk_widget_get_allocated_width (tooltip->window),
602 gtk_widget_get_allocated_height (tooltip->window));
608 gtk_tooltip_window_hide (GtkWidget *widget,
611 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
613 gtk_tooltip_set_custom (tooltip, NULL);
616 /* event handling, etc */
621 GtkWidget *container;
628 child_location_foreach (GtkWidget *child,
631 GtkAllocation child_allocation;
633 struct ChildLocation *child_loc = data;
635 /* Ignore invisible widgets */
636 if (!gtk_widget_is_drawable (child))
639 gtk_widget_get_allocation (child, &child_allocation);
644 /* (child_loc->x, child_loc->y) are relative to
645 * child_loc->container's allocation.
648 if (!child_loc->child &&
649 gtk_widget_translate_coordinates (child_loc->container, child,
650 child_loc->x, child_loc->y,
654 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
655 gtk_widget_get_name (child),
658 child_allocation.width,
659 child_allocation.height,
660 child_loc->x, child_loc->y,
662 #endif /* DEBUG_TOOLTIP */
664 /* (x, y) relative to child's allocation. */
665 if (x >= 0 && x < child_allocation.width
666 && y >= 0 && y < child_allocation.height)
668 if (GTK_IS_CONTAINER (child))
670 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
672 /* Take (x, y) relative the child's allocation and
677 tmp.container = child;
679 gtk_container_forall (GTK_CONTAINER (child),
680 child_location_foreach, &tmp);
683 child_loc->child = tmp.child;
685 child_loc->child = child;
688 child_loc->child = child;
693 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
694 * to allocation relative (dest_x, dest_y) of dest_widget.
697 window_to_alloc (GtkWidget *dest_widget,
703 GtkAllocation allocation;
705 gtk_widget_get_allocation (dest_widget, &allocation);
707 /* Translate from window relative to allocation relative */
708 if (gtk_widget_get_has_window (dest_widget) &&
709 gtk_widget_get_parent (dest_widget))
712 gdk_window_get_position (gtk_widget_get_window (dest_widget),
715 /* Offset coordinates if widget->window is smaller than
716 * widget->allocation.
718 src_x += wx - allocation.x;
719 src_y += wy - allocation.y;
723 src_x -= allocation.x;
724 src_y -= allocation.y;
733 /* Translates coordinates from window relative (x, y) to
734 * allocation relative (x, y) of the returned widget.
737 _gtk_widget_find_at_coords (GdkWindow *window,
743 GtkWidget *event_widget;
744 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
746 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
748 gdk_window_get_user_data (window, (void **)&event_widget);
754 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
755 window, event_widget, gtk_widget_get_name (event_widget),
759 /* Coordinates are relative to event window */
760 child_loc.x = window_x;
761 child_loc.y = window_y;
763 /* We go down the window hierarchy to the widget->window,
764 * coordinates stay relative to the current window.
765 * We end up with window == widget->window, coordinates relative to that.
767 while (window && window != gtk_widget_get_window (event_widget))
771 gdk_window_coords_to_parent (window,
772 child_loc.x, child_loc.y,
777 window = gdk_window_get_effective_parent (window);
780 /* Failing to find widget->window can happen for e.g. a detached handle box;
781 * chaining ::query-tooltip up to its parent probably makes little sense,
782 * and users better implement tooltips on handle_box->child.
783 * so we simply ignore the event for tooltips here.
788 /* Convert the window relative coordinates to allocation
789 * relative coordinates.
791 window_to_alloc (event_widget,
792 child_loc.x, child_loc.y,
793 &child_loc.x, &child_loc.y);
795 if (GTK_IS_CONTAINER (event_widget))
797 GtkWidget *container = event_widget;
799 child_loc.container = event_widget;
800 child_loc.child = NULL;
802 gtk_container_forall (GTK_CONTAINER (event_widget),
803 child_location_foreach, &child_loc);
805 /* Here we have a widget, with coordinates relative to
806 * child_loc.container's allocation.
810 event_widget = child_loc.child;
811 else if (child_loc.container)
812 event_widget = child_loc.container;
814 /* Translate to event_widget's allocation */
815 gtk_widget_translate_coordinates (container, event_widget,
816 child_loc.x, child_loc.y,
817 &child_loc.x, &child_loc.y);
820 /* We return (x, y) relative to the allocation of event_widget. */
822 *widget_x = child_loc.x;
824 *widget_y = child_loc.y;
829 /* Ignores (x, y) on input, translates event coordinates to
830 * allocation relative (x, y) of the returned widget.
833 find_topmost_widget_coords_from_event (GdkEvent *event,
837 GtkAllocation allocation;
842 gdk_event_get_coords (event, &dx, &dy);
844 /* Returns coordinates relative to tmp's allocation. */
845 tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
850 /* Make sure the pointer can actually be on the widget returned. */
851 gtk_widget_get_allocation (tmp, &allocation);
852 if (tx < 0 || tx >= allocation.width ||
853 ty < 0 || ty >= allocation.height)
865 tooltip_browse_mode_expired (gpointer data)
869 tooltip = GTK_TOOLTIP (data);
871 tooltip->browse_mode_enabled = FALSE;
872 tooltip->browse_mode_timeout_id = 0;
874 /* destroy tooltip */
875 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
876 "gdk-display-current-tooltip", NULL);
882 gtk_tooltip_display_closed (GdkDisplay *display,
886 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
890 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
893 if (tooltip->last_window == window)
896 if (tooltip->last_window)
897 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
898 (gpointer *) &tooltip->last_window);
900 tooltip->last_window = window;
903 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
904 (gpointer *) &tooltip->last_window);
908 gtk_tooltip_run_requery (GtkWidget **widget,
913 gboolean has_tooltip = FALSE;
914 gboolean return_value = FALSE;
916 gtk_tooltip_reset (tooltip);
920 g_object_get (*widget,
921 "has-tooltip", &has_tooltip,
925 g_signal_emit_by_name (*widget,
928 tooltip->keyboard_mode_enabled,
934 GtkWidget *parent = gtk_widget_get_parent (*widget);
937 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
946 /* If the custom widget was not reset in the query-tooltip
947 * callback, we clear it here.
949 if (!tooltip->custom_was_reset)
950 gtk_tooltip_set_custom (tooltip, NULL);
956 get_bounding_box (GtkWidget *widget,
957 GdkRectangle *bounds)
959 GtkAllocation allocation;
968 window = gtk_widget_get_parent_window (widget);
970 window = gtk_widget_get_window (widget);
972 gtk_widget_get_allocation (widget, &allocation);
975 w = allocation.width;
976 h = allocation.height;
978 gdk_window_get_root_coords (window, x, y, &x1, &y1);
979 gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
980 gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
981 gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
983 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
984 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
986 bounds->x = floor (MIN4 (x1, x2, x3, x4));
987 bounds->y = floor (MIN4 (y1, y2, y3, y4));
988 bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
989 bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
993 gtk_tooltip_position (GtkTooltip *tooltip,
995 GtkWidget *new_tooltip_widget)
1000 GdkRectangle monitor;
1001 GtkRequisition requisition;
1003 GdkRectangle bounds;
1005 #define MAX_DISTANCE 32
1007 tooltip->tooltip_widget = new_tooltip_widget;
1009 screen = gtk_widget_get_screen (new_tooltip_widget);
1011 gtk_widget_get_preferred_size (GTK_WIDGET (tooltip->current_window),
1012 &requisition, NULL);
1014 monitor_num = gdk_screen_get_monitor_at_point (screen,
1017 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1019 get_bounding_box (new_tooltip_widget, &bounds);
1021 /* Position the tooltip */
1023 cursor_size = gdk_display_get_default_cursor_size (display);
1026 x = bounds.x + bounds.width / 2 - requisition.width / 2;
1027 y = bounds.y + bounds.height + 4;
1029 if (y + requisition.height <= monitor.y + monitor.height)
1031 if (tooltip->keyboard_mode_enabled)
1034 if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
1036 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1037 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1038 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
1039 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
1046 x = bounds.x + bounds.width / 2 - requisition.width / 2;
1047 y = bounds.y - requisition.height - 4;
1051 if (tooltip->keyboard_mode_enabled)
1054 if (y + requisition.height >= tooltip->last_y - MAX_DISTANCE)
1056 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1057 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1058 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
1059 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
1065 /* Try right FIXME: flip on rtl ? */
1066 x = bounds.x + bounds.width + 4;
1067 y = bounds.y + bounds.height / 2 - requisition.height / 2;
1069 if (x + requisition.width <= monitor.x + monitor.width)
1071 if (tooltip->keyboard_mode_enabled)
1074 if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
1076 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1077 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1078 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1079 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1085 /* Try left FIXME: flip on rtl ? */
1086 x = bounds.x - requisition.width - 4;
1087 y = bounds.y + bounds.height / 2 - requisition.height / 2;
1091 if (tooltip->keyboard_mode_enabled)
1094 if (x + requisition.width >= tooltip->last_x - MAX_DISTANCE)
1096 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1097 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1098 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1099 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1106 if (tooltip->keyboard_mode_enabled)
1108 x = bounds.x + bounds.width / 2 - requisition.width / 2;
1109 y = bounds.y + bounds.height + 4;
1114 x = tooltip->last_x + cursor_size * 3 / 4;
1115 y = tooltip->last_y + cursor_size * 3 / 4;
1120 if (tooltip->current_window)
1122 if (x + requisition.width > monitor.x + monitor.width)
1123 x -= x - (monitor.x + monitor.width) + requisition.width;
1124 else if (x < monitor.x)
1127 if (y + requisition.height > monitor.y + monitor.height)
1128 y -= y - (monitor.y + monitor.height) + requisition.height;
1129 else if (y < monitor.y)
1132 if (!tooltip->keyboard_mode_enabled)
1134 /* don't pop up under the pointer */
1135 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
1136 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
1137 y = tooltip->last_y - requisition.height - 2;
1140 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1141 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1146 gtk_tooltip_show_tooltip (GdkDisplay *display)
1152 GtkWidget *tooltip_widget;
1153 GtkWidget *pointer_widget;
1154 GtkTooltip *tooltip;
1155 gboolean has_tooltip;
1156 gboolean return_value = FALSE;
1158 tooltip = g_object_get_data (G_OBJECT (display),
1159 "gdk-display-current-tooltip");
1161 if (tooltip->keyboard_mode_enabled)
1164 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
1170 window = tooltip->last_window;
1172 if (!GDK_IS_WINDOW (window))
1175 gdk_window_get_pointer (window, &x, &y, NULL);
1177 gdk_window_get_root_coords (window, x, y, &tx, &ty);
1178 tooltip->last_x = tx;
1179 tooltip->last_y = ty;
1181 pointer_widget = tooltip_widget = _gtk_widget_find_at_coords (window,
1186 if (!tooltip_widget)
1189 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1191 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1195 if (!tooltip->current_window)
1197 if (gtk_widget_get_tooltip_window (tooltip_widget))
1198 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1200 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1203 screen = gtk_widget_get_screen (tooltip_widget);
1205 /* FIXME: should use tooltip->current_window iso tooltip->window */
1206 if (screen != gtk_widget_get_screen (tooltip->window))
1208 g_signal_handlers_disconnect_by_func (display,
1209 gtk_tooltip_display_closed,
1212 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1214 g_signal_connect (display, "closed",
1215 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1218 gtk_tooltip_position (tooltip, display, tooltip_widget);
1220 /* Now a tooltip is visible again on the display, make sure browse
1223 tooltip->browse_mode_enabled = TRUE;
1224 if (tooltip->browse_mode_timeout_id)
1226 g_source_remove (tooltip->browse_mode_timeout_id);
1227 tooltip->browse_mode_timeout_id = 0;
1232 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1237 if (tooltip->timeout_id)
1239 g_source_remove (tooltip->timeout_id);
1240 tooltip->timeout_id = 0;
1243 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1246 tooltip->tooltip_widget = NULL;
1248 if (!tooltip->keyboard_mode_enabled)
1251 GtkSettings *settings;
1253 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1255 g_object_get (settings,
1256 "gtk-tooltip-browse-mode-timeout", &timeout,
1259 /* The tooltip is gone, after (by default, should be configurable) 500ms
1260 * we want to turn off browse mode
1262 if (!tooltip->browse_mode_timeout_id)
1263 tooltip->browse_mode_timeout_id =
1264 gdk_threads_add_timeout_full (0, timeout,
1265 tooltip_browse_mode_expired,
1266 g_object_ref (tooltip),
1271 if (tooltip->browse_mode_timeout_id)
1273 g_source_remove (tooltip->browse_mode_timeout_id);
1274 tooltip->browse_mode_timeout_id = 0;
1278 if (tooltip->current_window)
1280 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1281 tooltip->current_window = NULL;
1286 tooltip_popup_timeout (gpointer data)
1288 GdkDisplay *display;
1289 GtkTooltip *tooltip;
1291 display = GDK_DISPLAY (data);
1292 tooltip = g_object_get_data (G_OBJECT (display),
1293 "gdk-display-current-tooltip");
1295 /* This usually does not happen. However, it does occur in language
1296 * bindings were reference counting of objects behaves differently.
1301 gtk_tooltip_show_tooltip (display);
1303 tooltip->timeout_id = 0;
1309 gtk_tooltip_start_delay (GdkDisplay *display)
1312 GtkTooltip *tooltip;
1313 GtkSettings *settings;
1315 tooltip = g_object_get_data (G_OBJECT (display),
1316 "gdk-display-current-tooltip");
1318 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1321 if (tooltip->timeout_id)
1322 g_source_remove (tooltip->timeout_id);
1324 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1326 if (tooltip->browse_mode_enabled)
1327 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1329 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1331 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1332 tooltip_popup_timeout,
1333 g_object_ref (display),
1338 _gtk_tooltip_focus_in (GtkWidget *widget)
1341 gboolean return_value = FALSE;
1342 GdkDisplay *display;
1343 GtkTooltip *tooltip;
1346 /* Get current tooltip for this display */
1347 display = gtk_widget_get_display (widget);
1348 tooltip = g_object_get_data (G_OBJECT (display),
1349 "gdk-display-current-tooltip");
1351 /* Check if keyboard mode is enabled at this moment */
1352 if (!tooltip || !tooltip->keyboard_mode_enabled)
1355 device = gtk_get_current_event_device ();
1357 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1358 device = gdk_device_get_associated_device (device);
1360 /* This function should be called by either a focus in event,
1361 * or a key binding. In either case there should be a device.
1366 if (tooltip->keyboard_widget)
1367 g_object_unref (tooltip->keyboard_widget);
1369 tooltip->keyboard_widget = g_object_ref (widget);
1371 gdk_window_get_device_position (gtk_widget_get_window (widget),
1372 device, &x, &y, NULL);
1374 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1377 gtk_tooltip_hide_tooltip (tooltip);
1381 if (!tooltip->current_window)
1383 if (gtk_widget_get_tooltip_window (widget))
1384 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1386 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1389 gtk_tooltip_show_tooltip (display);
1393 _gtk_tooltip_focus_out (GtkWidget *widget)
1395 GdkDisplay *display;
1396 GtkTooltip *tooltip;
1398 /* Get current tooltip for this display */
1399 display = gtk_widget_get_display (widget);
1400 tooltip = g_object_get_data (G_OBJECT (display),
1401 "gdk-display-current-tooltip");
1403 if (!tooltip || !tooltip->keyboard_mode_enabled)
1406 if (tooltip->keyboard_widget)
1408 g_object_unref (tooltip->keyboard_widget);
1409 tooltip->keyboard_widget = NULL;
1412 gtk_tooltip_hide_tooltip (tooltip);
1416 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1418 GdkDisplay *display;
1419 GtkTooltip *tooltip;
1421 display = gtk_widget_get_display (widget);
1422 tooltip = g_object_get_data (G_OBJECT (display),
1423 "gdk-display-current-tooltip");
1427 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1428 g_object_set_data_full (G_OBJECT (display),
1429 "gdk-display-current-tooltip",
1430 tooltip, g_object_unref);
1431 g_signal_connect (display, "closed",
1432 G_CALLBACK (gtk_tooltip_display_closed),
1436 tooltip->keyboard_mode_enabled ^= 1;
1438 if (tooltip->keyboard_mode_enabled)
1440 tooltip->keyboard_widget = g_object_ref (widget);
1441 _gtk_tooltip_focus_in (widget);
1445 if (tooltip->keyboard_widget)
1447 g_object_unref (tooltip->keyboard_widget);
1448 tooltip->keyboard_widget = NULL;
1451 gtk_tooltip_hide_tooltip (tooltip);
1456 _gtk_tooltip_hide (GtkWidget *widget)
1458 GtkWidget *toplevel;
1459 GdkDisplay *display;
1460 GtkTooltip *tooltip;
1462 display = gtk_widget_get_display (widget);
1463 tooltip = g_object_get_data (G_OBJECT (display),
1464 "gdk-display-current-tooltip");
1466 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1469 toplevel = gtk_widget_get_toplevel (widget);
1471 if (widget == tooltip->tooltip_widget
1472 || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
1473 gtk_tooltip_hide_tooltip (tooltip);
1477 tooltips_enabled (GdkWindow *window)
1480 gboolean touchscreen;
1482 GtkSettings *settings;
1484 screen = gdk_window_get_screen (window);
1485 settings = gtk_settings_get_for_screen (screen);
1487 g_object_get (settings,
1488 "gtk-touchscreen-mode", &touchscreen,
1489 "gtk-enable-tooltips", &enabled,
1492 return (!touchscreen && enabled);
1496 _gtk_tooltip_handle_event (GdkEvent *event)
1499 gboolean return_value = FALSE;
1500 GtkWidget *has_tooltip_widget = NULL;
1501 GdkDisplay *display;
1502 GtkTooltip *current_tooltip;
1504 if (!tooltips_enabled (event->any.window))
1507 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1508 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1509 display = gdk_window_get_display (event->any.window);
1510 current_tooltip = g_object_get_data (G_OBJECT (display),
1511 "gdk-display-current-tooltip");
1513 if (current_tooltip)
1515 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1518 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1520 has_tooltip_widget = current_tooltip->keyboard_widget;
1521 if (!has_tooltip_widget)
1524 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1529 gtk_tooltip_hide_tooltip (current_tooltip);
1531 gtk_tooltip_start_delay (display);
1536 #ifdef DEBUG_TOOLTIP
1537 if (has_tooltip_widget)
1538 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1539 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1540 has_tooltip_widget->allocation.x,
1541 has_tooltip_widget->allocation.y,
1542 has_tooltip_widget->allocation.width,
1543 has_tooltip_widget->allocation.height,
1545 #endif /* DEBUG_TOOLTIP */
1547 /* Always poll for a next motion event */
1548 gdk_event_request_motions (&event->motion);
1550 /* Hide the tooltip when there's no new tooltip widget */
1551 if (!has_tooltip_widget)
1553 if (current_tooltip)
1554 gtk_tooltip_hide_tooltip (current_tooltip);
1559 switch (event->type)
1561 case GDK_BUTTON_PRESS:
1562 case GDK_2BUTTON_PRESS:
1563 case GDK_3BUTTON_PRESS:
1565 case GDK_DRAG_ENTER:
1566 case GDK_GRAB_BROKEN:
1567 gtk_tooltip_hide_tooltip (current_tooltip);
1570 case GDK_MOTION_NOTIFY:
1571 case GDK_ENTER_NOTIFY:
1572 case GDK_LEAVE_NOTIFY:
1574 if (current_tooltip)
1576 gboolean tip_area_set;
1577 GdkRectangle tip_area;
1578 gboolean hide_tooltip;
1580 tip_area_set = current_tooltip->tip_area_set;
1581 tip_area = current_tooltip->tip_area;
1583 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1587 /* Requested to be hidden? */
1588 hide_tooltip = !return_value;
1590 /* Leave notify should override the query function */
1591 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1593 /* Is the pointer above another widget now? */
1594 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1595 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1597 /* Did the pointer move out of the previous "context area"? */
1599 hide_tooltip |= (x <= tip_area.x
1600 || x >= tip_area.x + tip_area.width
1602 || y >= tip_area.y + tip_area.height);
1605 gtk_tooltip_hide_tooltip (current_tooltip);
1607 gtk_tooltip_start_delay (display);
1611 /* Need a new tooltip for this display */
1612 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1613 g_object_set_data_full (G_OBJECT (display),
1614 "gdk-display-current-tooltip",
1615 current_tooltip, g_object_unref);
1616 g_signal_connect (display, "closed",
1617 G_CALLBACK (gtk_tooltip_display_closed),
1620 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1622 gtk_tooltip_start_delay (display);