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"
37 #include "gtkwindowprivate.h"
42 * @Short_description: Add tips to your widgets
45 * Basic tooltips can be realized simply by using gtk_widget_set_tooltip_text()
46 * or gtk_widget_set_tooltip_markup() without any explicit tooltip object.
48 * When you need a tooltip with a little more fancy contents, like adding an
49 * image, or you want the tooltip to have different contents per #GtkTreeView
50 * row or cell, you will have to do a little more work:
54 * Set the #GtkWidget:has-tooltip property to %TRUE, this will make GTK+
55 * monitor the widget for motion and related events which are needed to
56 * determine when and where to show a tooltip.
61 * Connect to the #GtkWidget::query-tooltip signal. This signal will be
62 * emitted when a tooltip is supposed to be shown. One of the arguments passed
63 * to the signal handler is a GtkTooltip object. This is the object that we
64 * are about to display as a tooltip, and can be manipulated in your callback
65 * using functions like gtk_tooltip_set_icon(). There are functions for setting
66 * the tooltip's markup, setting an image from a stock icon, or even putting in
72 * Return %TRUE from your query-tooltip handler. This causes the tooltip to be
73 * show. If you return %FALSE, it will not be shown.
78 * In the probably rare case where you want to have even more control over the
79 * tooltip that is about to be shown, you can set your own #GtkWindow which
80 * will be used as tooltip window. This works as follows:
84 * Set #GtkWidget:has-tooltip and connect to #GtkWidget::query-tooltip as
90 * Use gtk_widget_set_tooltip_window() to set a #GtkWindow created by you as
96 * In the #GtkWidget::query-tooltip callback you can access your window using
97 * gtk_widget_get_tooltip_window() and manipulate as you wish. The semantics of
98 * the return value are exactly as before, return %TRUE to show the window,
99 * %FALSE to not show it.
109 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
110 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
111 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
113 typedef struct _GtkTooltipClass GtkTooltipClass;
117 GObject parent_instance;
120 GtkWidget *alignment;
124 GtkWidget *custom_widget;
126 GtkWindow *current_window;
127 GtkWidget *keyboard_widget;
129 GtkWidget *tooltip_widget;
130 GdkWindow *toplevel_window;
134 GdkWindow *last_window;
137 guint browse_mode_timeout_id;
139 GdkRectangle tip_area;
141 guint browse_mode_enabled : 1;
142 guint keyboard_mode_enabled : 1;
143 guint tip_area_set : 1;
144 guint custom_was_reset : 1;
147 struct _GtkTooltipClass
149 GObjectClass parent_class;
152 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
155 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
156 static void gtk_tooltip_init (GtkTooltip *tooltip);
157 static void gtk_tooltip_dispose (GObject *object);
159 static void gtk_tooltip_window_style_updated (GtkTooltip *tooltip);
160 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip,
162 static void gtk_tooltip_window_hide (GtkWidget *widget,
164 static void gtk_tooltip_display_closed (GdkDisplay *display,
166 GtkTooltip *tooltip);
167 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
171 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
174 gtk_tooltip_class_init (GtkTooltipClass *klass)
176 GObjectClass *object_class;
178 object_class = G_OBJECT_CLASS (klass);
180 object_class->dispose = gtk_tooltip_dispose;
184 gtk_tooltip_init (GtkTooltip *tooltip)
186 GtkStyleContext *context;
187 GtkBorder padding, border;
189 tooltip->timeout_id = 0;
190 tooltip->browse_mode_timeout_id = 0;
192 tooltip->browse_mode_enabled = FALSE;
193 tooltip->keyboard_mode_enabled = FALSE;
195 tooltip->current_window = NULL;
196 tooltip->keyboard_widget = NULL;
198 tooltip->tooltip_widget = NULL;
199 tooltip->toplevel_window = NULL;
201 tooltip->last_window = NULL;
203 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
204 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
205 GDK_WINDOW_TYPE_HINT_TOOLTIP);
206 gtk_widget_set_app_paintable (tooltip->window, TRUE);
207 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
208 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
209 g_signal_connect (tooltip->window, "hide",
210 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
212 context = gtk_widget_get_style_context (tooltip->window);
213 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
215 gtk_style_context_get_padding (context, 0, &padding);
216 gtk_style_context_get_border (context, 0, &border);
218 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
219 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
220 border.top + padding.top,
221 border.bottom + padding.bottom,
222 border.left + padding.left,
223 border.right + padding.right);
224 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
225 gtk_widget_show (tooltip->alignment);
227 g_signal_connect_swapped (tooltip->window, "style-updated",
228 G_CALLBACK (gtk_tooltip_window_style_updated), tooltip);
229 g_signal_connect_swapped (tooltip->window, "draw",
230 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
232 tooltip->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, padding.left);
233 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
234 gtk_widget_show (tooltip->box);
236 tooltip->image = gtk_image_new ();
237 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
240 tooltip->label = gtk_label_new ("");
241 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
242 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
245 tooltip->custom_widget = NULL;
249 gtk_tooltip_dispose (GObject *object)
251 GtkTooltip *tooltip = GTK_TOOLTIP (object);
253 if (tooltip->timeout_id)
255 g_source_remove (tooltip->timeout_id);
256 tooltip->timeout_id = 0;
259 if (tooltip->browse_mode_timeout_id)
261 g_source_remove (tooltip->browse_mode_timeout_id);
262 tooltip->browse_mode_timeout_id = 0;
265 gtk_tooltip_set_custom (tooltip, NULL);
266 gtk_tooltip_set_last_window (tooltip, NULL);
272 display = gtk_widget_get_display (tooltip->window);
273 g_signal_handlers_disconnect_by_func (display,
274 gtk_tooltip_display_closed,
276 gtk_widget_destroy (tooltip->window);
277 tooltip->window = NULL;
280 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
286 * gtk_tooltip_set_markup:
287 * @tooltip: a #GtkTooltip
288 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
290 * Sets the text of the tooltip to be @markup, which is marked up
292 * linkend="PangoMarkupFormat">Pango text markup language</link>.
293 * If @markup is %NULL, the label will be hidden.
298 gtk_tooltip_set_markup (GtkTooltip *tooltip,
301 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
303 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
306 gtk_widget_show (tooltip->label);
308 gtk_widget_hide (tooltip->label);
312 * gtk_tooltip_set_text:
313 * @tooltip: a #GtkTooltip
314 * @text: (allow-none): a text string or %NULL
316 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
317 * will be hidden. See also gtk_tooltip_set_markup().
322 gtk_tooltip_set_text (GtkTooltip *tooltip,
325 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
327 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
330 gtk_widget_show (tooltip->label);
332 gtk_widget_hide (tooltip->label);
336 * gtk_tooltip_set_icon:
337 * @tooltip: a #GtkTooltip
338 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
340 * Sets the icon of the tooltip (which is in front of the text) to be
341 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
346 gtk_tooltip_set_icon (GtkTooltip *tooltip,
349 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
351 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
353 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
356 gtk_widget_show (tooltip->image);
358 gtk_widget_hide (tooltip->image);
362 * gtk_tooltip_set_icon_from_stock:
363 * @tooltip: a #GtkTooltip
364 * @stock_id: (allow-none): a stock id, or %NULL
365 * @size: (type int): a stock icon size
367 * Sets the icon of the tooltip (which is in front of the text) to be
368 * the stock item indicated by @stock_id with the size indicated
369 * by @size. If @stock_id is %NULL, the image will be hidden.
374 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
375 const gchar *stock_id,
378 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
380 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
383 gtk_widget_show (tooltip->image);
385 gtk_widget_hide (tooltip->image);
389 * gtk_tooltip_set_icon_from_icon_name:
390 * @tooltip: a #GtkTooltip
391 * @icon_name: (allow-none): an icon name, or %NULL
392 * @size: (type int): a stock icon size
394 * Sets the icon of the tooltip (which is in front of the text) to be
395 * the icon indicated by @icon_name with the size indicated
396 * by @size. If @icon_name is %NULL, the image will be hidden.
401 gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
402 const gchar *icon_name,
405 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
407 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
410 gtk_widget_show (tooltip->image);
412 gtk_widget_hide (tooltip->image);
416 * gtk_tooltip_set_icon_from_gicon:
417 * @tooltip: a #GtkTooltip
418 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
419 * @size: (type int): a stock icon size
421 * Sets the icon of the tooltip (which is in front of the text)
422 * to be the icon indicated by @gicon with the size indicated
423 * by @size. If @gicon is %NULL, the image will be hidden.
428 gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
432 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
434 gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
437 gtk_widget_show (tooltip->image);
439 gtk_widget_hide (tooltip->image);
443 * gtk_tooltip_set_custom:
444 * @tooltip: a #GtkTooltip
445 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
447 * Replaces the widget packed into the tooltip with
448 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
450 * By default a box with a #GtkImage and #GtkLabel is embedded in
451 * the tooltip, which can be configured using gtk_tooltip_set_markup()
452 * and gtk_tooltip_set_icon().
458 gtk_tooltip_set_custom (GtkTooltip *tooltip,
459 GtkWidget *custom_widget)
461 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
463 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
465 /* The custom widget has been updated from the query-tooltip
466 * callback, so we do not want to reset the custom widget later on.
468 tooltip->custom_was_reset = TRUE;
470 /* No need to do anything if the custom widget stays the same */
471 if (tooltip->custom_widget == custom_widget)
474 if (tooltip->custom_widget)
476 GtkWidget *custom = tooltip->custom_widget;
477 /* Note: We must reset tooltip->custom_widget first,
478 * since gtk_container_remove() will recurse into
479 * gtk_tooltip_set_custom()
481 tooltip->custom_widget = NULL;
482 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
483 g_object_unref (custom);
488 tooltip->custom_widget = g_object_ref (custom_widget);
490 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
491 gtk_widget_show (custom_widget);
496 * gtk_tooltip_set_tip_area:
497 * @tooltip: a #GtkTooltip
498 * @rect: a #GdkRectangle
500 * Sets the area of the widget, where the contents of this tooltip apply,
501 * to be @rect (in widget coordinates). This is especially useful for
502 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
505 * For setting tooltips on #GtkTreeView, please refer to the convenience
506 * functions for this: gtk_tree_view_set_tooltip_row() and
507 * gtk_tree_view_set_tooltip_cell().
512 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
513 const GdkRectangle *rect)
515 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
518 tooltip->tip_area_set = FALSE;
521 tooltip->tip_area_set = TRUE;
522 tooltip->tip_area = *rect;
527 * gtk_tooltip_trigger_tooltip_query:
528 * @display: a #GdkDisplay
530 * Triggers a new tooltip query on @display, in order to update the current
531 * visible tooltip, or to show/hide the current tooltip. This function is
532 * useful to call when, for example, the state of the widget changed by a
538 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
545 /* Trigger logic as if the mouse moved */
546 device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
547 window = gdk_device_get_window_at_position (device, &x, &y);
551 event.type = GDK_MOTION_NOTIFY;
552 event.motion.window = window;
555 event.motion.is_hint = FALSE;
557 gdk_window_get_root_coords (window, x, y, &x, &y);
558 event.motion.x_root = x;
559 event.motion.y_root = y;
561 _gtk_tooltip_handle_event (&event);
564 /* private functions */
567 gtk_tooltip_reset (GtkTooltip *tooltip)
569 gtk_tooltip_set_markup (tooltip, NULL);
570 gtk_tooltip_set_icon (tooltip, NULL);
571 gtk_tooltip_set_tip_area (tooltip, NULL);
573 /* See if the custom widget is again set from the query-tooltip
576 tooltip->custom_was_reset = FALSE;
580 gtk_tooltip_window_style_updated (GtkTooltip *tooltip)
582 GtkStyleContext *context;
583 GtkBorder padding, border;
585 context = gtk_widget_get_style_context (tooltip->window);
586 gtk_style_context_get_padding (context, 0, &padding);
587 gtk_style_context_get_border (context, 0, &border);
589 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
590 border.top + padding.top,
591 border.bottom + padding.bottom,
592 border.left + padding.left,
593 border.right + padding.right);
595 gtk_box_set_spacing (GTK_BOX (tooltip->box), padding.left);
597 gtk_widget_queue_draw (tooltip->window);
601 gtk_tooltip_paint_window (GtkTooltip *tooltip,
604 GtkStyleContext *context;
606 context = gtk_widget_get_style_context (tooltip->window);
608 gtk_render_background (context, cr, 0, 0,
609 gtk_widget_get_allocated_width (tooltip->window),
610 gtk_widget_get_allocated_height (tooltip->window));
611 gtk_render_frame (context, cr, 0, 0,
612 gtk_widget_get_allocated_width (tooltip->window),
613 gtk_widget_get_allocated_height (tooltip->window));
619 gtk_tooltip_window_hide (GtkWidget *widget,
622 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
624 gtk_tooltip_set_custom (tooltip, NULL);
627 /* event handling, etc */
632 GtkWidget *container;
639 child_location_foreach (GtkWidget *child,
642 GtkAllocation child_allocation;
644 struct ChildLocation *child_loc = data;
646 /* Ignore invisible widgets */
647 if (!gtk_widget_is_drawable (child))
650 gtk_widget_get_allocation (child, &child_allocation);
655 /* (child_loc->x, child_loc->y) are relative to
656 * child_loc->container's allocation.
659 if (!child_loc->child &&
660 gtk_widget_translate_coordinates (child_loc->container, child,
661 child_loc->x, child_loc->y,
665 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
666 gtk_widget_get_name (child),
669 child_allocation.width,
670 child_allocation.height,
671 child_loc->x, child_loc->y,
673 #endif /* DEBUG_TOOLTIP */
675 /* (x, y) relative to child's allocation. */
676 if (x >= 0 && x < child_allocation.width
677 && y >= 0 && y < child_allocation.height)
679 if (GTK_IS_CONTAINER (child))
681 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
683 /* Take (x, y) relative the child's allocation and
688 tmp.container = child;
690 gtk_container_forall (GTK_CONTAINER (child),
691 child_location_foreach, &tmp);
694 child_loc->child = tmp.child;
696 child_loc->child = child;
699 child_loc->child = child;
704 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
705 * to allocation relative (dest_x, dest_y) of dest_widget.
708 window_to_alloc (GtkWidget *dest_widget,
714 GtkAllocation allocation;
716 gtk_widget_get_allocation (dest_widget, &allocation);
718 /* Translate from window relative to allocation relative */
719 if (gtk_widget_get_has_window (dest_widget) &&
720 gtk_widget_get_parent (dest_widget))
723 gdk_window_get_position (gtk_widget_get_window (dest_widget),
726 /* Offset coordinates if widget->window is smaller than
727 * widget->allocation.
729 src_x += wx - allocation.x;
730 src_y += wy - allocation.y;
734 src_x -= allocation.x;
735 src_y -= allocation.y;
744 /* Translates coordinates from window relative (x, y) to
745 * allocation relative (x, y) of the returned widget.
748 _gtk_widget_find_at_coords (GdkWindow *window,
754 GtkWidget *event_widget;
755 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
757 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
759 gdk_window_get_user_data (window, (void **)&event_widget);
765 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
766 window, event_widget, gtk_widget_get_name (event_widget),
770 /* Coordinates are relative to event window */
771 child_loc.x = window_x;
772 child_loc.y = window_y;
774 /* We go down the window hierarchy to the widget->window,
775 * coordinates stay relative to the current window.
776 * We end up with window == widget->window, coordinates relative to that.
778 while (window && window != gtk_widget_get_window (event_widget))
782 gdk_window_coords_to_parent (window,
783 child_loc.x, child_loc.y,
788 window = gdk_window_get_effective_parent (window);
791 /* Failing to find widget->window can happen for e.g. a detached handle box;
792 * chaining ::query-tooltip up to its parent probably makes little sense,
793 * and users better implement tooltips on handle_box->child.
794 * so we simply ignore the event for tooltips here.
799 /* Convert the window relative coordinates to allocation
800 * relative coordinates.
802 window_to_alloc (event_widget,
803 child_loc.x, child_loc.y,
804 &child_loc.x, &child_loc.y);
806 if (GTK_IS_CONTAINER (event_widget))
808 GtkWidget *container = event_widget;
810 child_loc.container = event_widget;
811 child_loc.child = NULL;
813 gtk_container_forall (GTK_CONTAINER (event_widget),
814 child_location_foreach, &child_loc);
816 /* Here we have a widget, with coordinates relative to
817 * child_loc.container's allocation.
821 event_widget = child_loc.child;
822 else if (child_loc.container)
823 event_widget = child_loc.container;
825 /* Translate to event_widget's allocation */
826 gtk_widget_translate_coordinates (container, event_widget,
827 child_loc.x, child_loc.y,
828 &child_loc.x, &child_loc.y);
831 /* We return (x, y) relative to the allocation of event_widget. */
833 *widget_x = child_loc.x;
835 *widget_y = child_loc.y;
840 /* Ignores (x, y) on input, translates event coordinates to
841 * allocation relative (x, y) of the returned widget.
844 find_topmost_widget_coords_from_event (GdkEvent *event,
848 GtkAllocation allocation;
853 gdk_event_get_coords (event, &dx, &dy);
855 /* Returns coordinates relative to tmp's allocation. */
856 tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
861 /* Make sure the pointer can actually be on the widget returned. */
862 gtk_widget_get_allocation (tmp, &allocation);
863 if (tx < 0 || tx >= allocation.width ||
864 ty < 0 || ty >= allocation.height)
876 tooltip_browse_mode_expired (gpointer data)
880 tooltip = GTK_TOOLTIP (data);
882 tooltip->browse_mode_enabled = FALSE;
883 tooltip->browse_mode_timeout_id = 0;
885 /* destroy tooltip */
886 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
887 "gdk-display-current-tooltip", NULL);
893 gtk_tooltip_display_closed (GdkDisplay *display,
897 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
901 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
904 if (tooltip->last_window == window)
907 if (tooltip->last_window)
908 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
909 (gpointer *) &tooltip->last_window);
911 tooltip->last_window = window;
914 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
915 (gpointer *) &tooltip->last_window);
919 gtk_tooltip_run_requery (GtkWidget **widget,
924 gboolean has_tooltip = FALSE;
925 gboolean return_value = FALSE;
927 gtk_tooltip_reset (tooltip);
931 g_object_get (*widget,
932 "has-tooltip", &has_tooltip,
936 g_signal_emit_by_name (*widget,
939 tooltip->keyboard_mode_enabled,
945 GtkWidget *parent = gtk_widget_get_parent (*widget);
948 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
957 /* If the custom widget was not reset in the query-tooltip
958 * callback, we clear it here.
960 if (!tooltip->custom_was_reset)
961 gtk_tooltip_set_custom (tooltip, NULL);
967 get_bounding_box (GtkWidget *widget,
968 GdkRectangle *bounds)
970 GtkAllocation allocation;
979 window = gtk_widget_get_parent_window (widget);
981 window = gtk_widget_get_window (widget);
983 gtk_widget_get_allocation (widget, &allocation);
986 w = allocation.width;
987 h = allocation.height;
989 gdk_window_get_root_coords (window, x, y, &x1, &y1);
990 gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
991 gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
992 gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
994 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
995 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
997 bounds->x = floor (MIN4 (x1, x2, x3, x4));
998 bounds->y = floor (MIN4 (y1, y2, y3, y4));
999 bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
1000 bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
1004 gtk_tooltip_position (GtkTooltip *tooltip,
1005 GdkDisplay *display,
1006 GtkWidget *new_tooltip_widget)
1008 gint x, y, width, height;
1011 GdkRectangle monitor;
1013 GdkRectangle bounds;
1015 #define MAX_DISTANCE 32
1017 gtk_widget_realize (GTK_WIDGET (tooltip->current_window));
1019 tooltip->tooltip_widget = new_tooltip_widget;
1021 screen = gtk_widget_get_screen (new_tooltip_widget);
1023 width = gtk_widget_get_allocated_width (GTK_WIDGET (tooltip->current_window));
1024 height = gtk_widget_get_allocated_height (GTK_WIDGET (tooltip->current_window));
1026 monitor_num = gdk_screen_get_monitor_at_point (screen,
1029 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1031 get_bounding_box (new_tooltip_widget, &bounds);
1033 /* Position the tooltip */
1035 cursor_size = gdk_display_get_default_cursor_size (display);
1038 x = bounds.x + bounds.width / 2 - width / 2;
1039 y = bounds.y + bounds.height + 4;
1041 if (y + height <= monitor.y + monitor.height)
1043 if (tooltip->keyboard_mode_enabled)
1046 if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
1048 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1049 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1050 else if (x + width < tooltip->last_x - MAX_DISTANCE)
1051 x = tooltip->last_x - MAX_DISTANCE - width;
1058 x = bounds.x + bounds.width / 2 - width / 2;
1059 y = bounds.y - height - 4;
1063 if (tooltip->keyboard_mode_enabled)
1066 if (y + height >= tooltip->last_y - MAX_DISTANCE)
1068 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1069 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1070 else if (x + width < tooltip->last_x - MAX_DISTANCE)
1071 x = tooltip->last_x - MAX_DISTANCE - width;
1077 /* Try right FIXME: flip on rtl ? */
1078 x = bounds.x + bounds.width + 4;
1079 y = bounds.y + bounds.height / 2 - height / 2;
1081 if (x + width <= monitor.x + monitor.width)
1083 if (tooltip->keyboard_mode_enabled)
1086 if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
1088 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1089 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1090 else if (y + height < tooltip->last_y - MAX_DISTANCE)
1091 y = tooltip->last_y - MAX_DISTANCE - height;
1097 /* Try left FIXME: flip on rtl ? */
1098 x = bounds.x - width - 4;
1099 y = bounds.y + bounds.height / 2 - height / 2;
1103 if (tooltip->keyboard_mode_enabled)
1106 if (x + width >= tooltip->last_x - MAX_DISTANCE)
1108 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1109 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1110 else if (y + height < tooltip->last_y - MAX_DISTANCE)
1111 y = tooltip->last_y - MAX_DISTANCE - height;
1118 if (tooltip->keyboard_mode_enabled)
1120 x = bounds.x + bounds.width / 2 - width / 2;
1121 y = bounds.y + bounds.height + 4;
1126 x = tooltip->last_x + cursor_size * 3 / 4;
1127 y = tooltip->last_y + cursor_size * 3 / 4;
1132 if (tooltip->current_window)
1134 if (x + width > monitor.x + monitor.width)
1135 x -= x - (monitor.x + monitor.width) + width;
1136 else if (x < monitor.x)
1139 if (y + height > monitor.y + monitor.height)
1140 y -= y - (monitor.y + monitor.height) + height;
1141 else if (y < monitor.y)
1144 if (!tooltip->keyboard_mode_enabled)
1146 /* don't pop up under the pointer */
1147 if (x <= tooltip->last_x && tooltip->last_x < x + width &&
1148 y <= tooltip->last_y && tooltip->last_y < y + height)
1149 y = tooltip->last_y - height - 2;
1152 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1153 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1158 gtk_tooltip_show_tooltip (GdkDisplay *display)
1164 GtkWidget *tooltip_widget;
1165 GtkTooltip *tooltip;
1166 gboolean has_tooltip;
1167 gboolean return_value = FALSE;
1169 tooltip = g_object_get_data (G_OBJECT (display),
1170 "gdk-display-current-tooltip");
1172 if (tooltip->keyboard_mode_enabled)
1175 tooltip_widget = tooltip->keyboard_widget;
1181 window = tooltip->last_window;
1183 if (!GDK_IS_WINDOW (window))
1186 gdk_window_get_pointer (window, &x, &y, NULL);
1188 gdk_window_get_root_coords (window, x, y, &tx, &ty);
1189 tooltip->last_x = tx;
1190 tooltip->last_y = ty;
1192 tooltip_widget = _gtk_widget_find_at_coords (window, x, y, &x, &y);
1195 if (!tooltip_widget)
1198 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1200 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1204 if (!tooltip->current_window)
1206 if (gtk_widget_get_tooltip_window (tooltip_widget))
1207 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1209 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1212 screen = gtk_widget_get_screen (tooltip_widget);
1214 /* FIXME: should use tooltip->current_window iso tooltip->window */
1215 if (screen != gtk_widget_get_screen (tooltip->window))
1217 g_signal_handlers_disconnect_by_func (display,
1218 gtk_tooltip_display_closed,
1221 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1223 g_signal_connect (display, "closed",
1224 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1227 gtk_tooltip_position (tooltip, display, tooltip_widget);
1229 /* Now a tooltip is visible again on the display, make sure browse
1232 tooltip->browse_mode_enabled = TRUE;
1233 if (tooltip->browse_mode_timeout_id)
1235 g_source_remove (tooltip->browse_mode_timeout_id);
1236 tooltip->browse_mode_timeout_id = 0;
1241 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1246 if (tooltip->timeout_id)
1248 g_source_remove (tooltip->timeout_id);
1249 tooltip->timeout_id = 0;
1252 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1255 tooltip->tooltip_widget = NULL;
1257 if (!tooltip->keyboard_mode_enabled)
1260 GtkSettings *settings;
1262 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1264 g_object_get (settings,
1265 "gtk-tooltip-browse-mode-timeout", &timeout,
1268 /* The tooltip is gone, after (by default, should be configurable) 500ms
1269 * we want to turn off browse mode
1271 if (!tooltip->browse_mode_timeout_id)
1272 tooltip->browse_mode_timeout_id =
1273 gdk_threads_add_timeout_full (0, timeout,
1274 tooltip_browse_mode_expired,
1275 g_object_ref (tooltip),
1280 if (tooltip->browse_mode_timeout_id)
1282 g_source_remove (tooltip->browse_mode_timeout_id);
1283 tooltip->browse_mode_timeout_id = 0;
1287 if (tooltip->current_window)
1289 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1290 tooltip->current_window = NULL;
1295 tooltip_popup_timeout (gpointer data)
1297 GdkDisplay *display;
1298 GtkTooltip *tooltip;
1300 display = GDK_DISPLAY (data);
1301 tooltip = g_object_get_data (G_OBJECT (display),
1302 "gdk-display-current-tooltip");
1304 /* This usually does not happen. However, it does occur in language
1305 * bindings were reference counting of objects behaves differently.
1310 gtk_tooltip_show_tooltip (display);
1312 tooltip->timeout_id = 0;
1318 gtk_tooltip_start_delay (GdkDisplay *display)
1321 GtkTooltip *tooltip;
1322 GtkSettings *settings;
1324 tooltip = g_object_get_data (G_OBJECT (display),
1325 "gdk-display-current-tooltip");
1327 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1330 if (tooltip->timeout_id)
1331 g_source_remove (tooltip->timeout_id);
1333 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1335 if (tooltip->browse_mode_enabled)
1336 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1338 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1340 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1341 tooltip_popup_timeout,
1342 g_object_ref (display),
1347 _gtk_tooltip_focus_in (GtkWidget *widget)
1350 gboolean return_value = FALSE;
1351 GdkDisplay *display;
1352 GtkTooltip *tooltip;
1355 /* Get current tooltip for this display */
1356 display = gtk_widget_get_display (widget);
1357 tooltip = g_object_get_data (G_OBJECT (display),
1358 "gdk-display-current-tooltip");
1360 /* Check if keyboard mode is enabled at this moment */
1361 if (!tooltip || !tooltip->keyboard_mode_enabled)
1364 device = gtk_get_current_event_device ();
1366 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1367 device = gdk_device_get_associated_device (device);
1369 /* This function should be called by either a focus in event,
1370 * or a key binding. In either case there should be a device.
1375 if (tooltip->keyboard_widget)
1376 g_object_unref (tooltip->keyboard_widget);
1378 tooltip->keyboard_widget = g_object_ref (widget);
1380 gdk_window_get_device_position (gtk_widget_get_window (widget),
1381 device, &x, &y, NULL);
1383 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1386 gtk_tooltip_hide_tooltip (tooltip);
1390 if (!tooltip->current_window)
1392 if (gtk_widget_get_tooltip_window (widget))
1393 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1395 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1398 gtk_tooltip_show_tooltip (display);
1402 _gtk_tooltip_focus_out (GtkWidget *widget)
1404 GdkDisplay *display;
1405 GtkTooltip *tooltip;
1407 /* Get current tooltip for this display */
1408 display = gtk_widget_get_display (widget);
1409 tooltip = g_object_get_data (G_OBJECT (display),
1410 "gdk-display-current-tooltip");
1412 if (!tooltip || !tooltip->keyboard_mode_enabled)
1415 if (tooltip->keyboard_widget)
1417 g_object_unref (tooltip->keyboard_widget);
1418 tooltip->keyboard_widget = NULL;
1421 gtk_tooltip_hide_tooltip (tooltip);
1425 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1427 GdkDisplay *display;
1428 GtkTooltip *tooltip;
1430 display = gtk_widget_get_display (widget);
1431 tooltip = g_object_get_data (G_OBJECT (display),
1432 "gdk-display-current-tooltip");
1436 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1437 g_object_set_data_full (G_OBJECT (display),
1438 "gdk-display-current-tooltip",
1439 tooltip, g_object_unref);
1440 g_signal_connect (display, "closed",
1441 G_CALLBACK (gtk_tooltip_display_closed),
1445 tooltip->keyboard_mode_enabled ^= 1;
1447 if (tooltip->keyboard_mode_enabled)
1449 tooltip->keyboard_widget = g_object_ref (widget);
1450 _gtk_tooltip_focus_in (widget);
1454 if (tooltip->keyboard_widget)
1456 g_object_unref (tooltip->keyboard_widget);
1457 tooltip->keyboard_widget = NULL;
1460 gtk_tooltip_hide_tooltip (tooltip);
1465 _gtk_tooltip_hide (GtkWidget *widget)
1467 GtkWidget *toplevel;
1468 GdkDisplay *display;
1469 GtkTooltip *tooltip;
1471 display = gtk_widget_get_display (widget);
1472 tooltip = g_object_get_data (G_OBJECT (display),
1473 "gdk-display-current-tooltip");
1475 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1478 toplevel = gtk_widget_get_toplevel (widget);
1480 if (widget == tooltip->tooltip_widget
1481 || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
1482 gtk_tooltip_hide_tooltip (tooltip);
1486 tooltips_enabled (GdkWindow *window)
1489 gboolean touchscreen;
1491 GtkSettings *settings;
1493 screen = gdk_window_get_screen (window);
1494 settings = gtk_settings_get_for_screen (screen);
1496 g_object_get (settings,
1497 "gtk-touchscreen-mode", &touchscreen,
1498 "gtk-enable-tooltips", &enabled,
1501 return (!touchscreen && enabled);
1505 _gtk_tooltip_handle_event (GdkEvent *event)
1508 gboolean return_value = FALSE;
1509 GtkWidget *has_tooltip_widget = NULL;
1510 GdkDisplay *display;
1511 GtkTooltip *current_tooltip;
1513 if (!tooltips_enabled (event->any.window))
1516 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1517 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1518 display = gdk_window_get_display (event->any.window);
1519 current_tooltip = g_object_get_data (G_OBJECT (display),
1520 "gdk-display-current-tooltip");
1522 if (current_tooltip)
1524 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1527 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1529 has_tooltip_widget = current_tooltip->keyboard_widget;
1530 if (!has_tooltip_widget)
1533 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1538 gtk_tooltip_hide_tooltip (current_tooltip);
1540 gtk_tooltip_start_delay (display);
1545 #ifdef DEBUG_TOOLTIP
1546 if (has_tooltip_widget)
1547 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1548 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1549 has_tooltip_widget->allocation.x,
1550 has_tooltip_widget->allocation.y,
1551 has_tooltip_widget->allocation.width,
1552 has_tooltip_widget->allocation.height,
1554 #endif /* DEBUG_TOOLTIP */
1556 /* Always poll for a next motion event */
1557 gdk_event_request_motions (&event->motion);
1559 /* Hide the tooltip when there's no new tooltip widget */
1560 if (!has_tooltip_widget)
1562 if (current_tooltip)
1563 gtk_tooltip_hide_tooltip (current_tooltip);
1568 switch (event->type)
1570 case GDK_BUTTON_PRESS:
1571 case GDK_2BUTTON_PRESS:
1572 case GDK_3BUTTON_PRESS:
1574 case GDK_DRAG_ENTER:
1575 case GDK_GRAB_BROKEN:
1576 gtk_tooltip_hide_tooltip (current_tooltip);
1579 case GDK_MOTION_NOTIFY:
1580 case GDK_ENTER_NOTIFY:
1581 case GDK_LEAVE_NOTIFY:
1583 if (current_tooltip)
1585 gboolean tip_area_set;
1586 GdkRectangle tip_area;
1587 gboolean hide_tooltip;
1589 tip_area_set = current_tooltip->tip_area_set;
1590 tip_area = current_tooltip->tip_area;
1592 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1596 /* Requested to be hidden? */
1597 hide_tooltip = !return_value;
1599 /* Leave notify should override the query function */
1600 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1602 /* Is the pointer above another widget now? */
1603 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1604 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1606 /* Did the pointer move out of the previous "context area"? */
1608 hide_tooltip |= (x <= tip_area.x
1609 || x >= tip_area.x + tip_area.width
1611 || y >= tip_area.y + tip_area.height);
1614 gtk_tooltip_hide_tooltip (current_tooltip);
1616 gtk_tooltip_start_delay (display);
1620 /* Need a new tooltip for this display */
1621 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1622 g_object_set_data_full (G_OBJECT (display),
1623 "gdk-display-current-tooltip",
1624 current_tooltip, g_object_unref);
1625 g_signal_connect (display, "closed",
1626 G_CALLBACK (gtk_tooltip_display_closed),
1629 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1631 gtk_tooltip_start_delay (display);