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 "gtksizerequest.h"
36 #include "gtkwindowprivate.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 gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip,
160 static void gtk_tooltip_realize_window (GtkTooltip *tooltip,
162 static void maybe_update_shape (GtkTooltip *tooltip);
163 static void gtk_tooltip_window_hide (GtkWidget *widget,
165 static void gtk_tooltip_display_closed (GdkDisplay *display,
167 GtkTooltip *tooltip);
168 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
172 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
175 gtk_tooltip_class_init (GtkTooltipClass *klass)
177 GObjectClass *object_class;
179 object_class = G_OBJECT_CLASS (klass);
181 object_class->dispose = gtk_tooltip_dispose;
185 gtk_tooltip_init (GtkTooltip *tooltip)
187 GtkStyleContext *context;
195 tooltip->timeout_id = 0;
196 tooltip->browse_mode_timeout_id = 0;
198 tooltip->browse_mode_enabled = FALSE;
199 tooltip->keyboard_mode_enabled = FALSE;
201 tooltip->current_window = NULL;
202 tooltip->keyboard_widget = NULL;
204 tooltip->tooltip_widget = NULL;
205 tooltip->toplevel_window = NULL;
207 tooltip->last_window = NULL;
209 window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
210 screen = gtk_widget_get_screen (window);
211 visual = gdk_screen_get_rgba_visual (screen);
214 gtk_widget_set_visual (window, visual);
216 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
217 gtk_widget_set_app_paintable (window, TRUE);
218 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
219 gtk_widget_set_name (window, "gtk-tooltip");
220 g_signal_connect (window, "hide",
221 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
223 context = gtk_widget_get_style_context (window);
224 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
226 g_signal_connect_swapped (window, "draw",
227 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
228 g_signal_connect_swapped (window, "realize",
229 G_CALLBACK (gtk_tooltip_realize_window), tooltip);
230 g_signal_connect_swapped (window, "composited-changed",
231 G_CALLBACK (maybe_update_shape), tooltip);
233 /* FIXME: don't hardcode the padding */
234 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
235 gtk_widget_set_margin_left (box, 6);
236 gtk_widget_set_margin_right (box, 6);
237 gtk_widget_set_margin_top (box, 6);
238 gtk_widget_set_margin_bottom (box, 6);
239 gtk_container_add (GTK_CONTAINER (window), box);
240 gtk_widget_show (box);
242 image = gtk_image_new ();
243 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
245 label = gtk_label_new ("");
246 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
247 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
249 tooltip->window = window;
251 tooltip->image = image;
252 tooltip->label = label;
253 tooltip->custom_widget = NULL;
257 gtk_tooltip_dispose (GObject *object)
259 GtkTooltip *tooltip = GTK_TOOLTIP (object);
261 if (tooltip->timeout_id)
263 g_source_remove (tooltip->timeout_id);
264 tooltip->timeout_id = 0;
267 if (tooltip->browse_mode_timeout_id)
269 g_source_remove (tooltip->browse_mode_timeout_id);
270 tooltip->browse_mode_timeout_id = 0;
273 gtk_tooltip_set_custom (tooltip, NULL);
274 gtk_tooltip_set_last_window (tooltip, NULL);
280 display = gtk_widget_get_display (tooltip->window);
281 g_signal_handlers_disconnect_by_func (display,
282 gtk_tooltip_display_closed,
284 gtk_widget_destroy (tooltip->window);
285 tooltip->window = NULL;
288 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
294 * gtk_tooltip_set_markup:
295 * @tooltip: a #GtkTooltip
296 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
298 * Sets the text of the tooltip to be @markup, which is marked up
300 * linkend="PangoMarkupFormat">Pango text markup language</link>.
301 * If @markup is %NULL, the label will be hidden.
306 gtk_tooltip_set_markup (GtkTooltip *tooltip,
309 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
311 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
314 gtk_widget_show (tooltip->label);
316 gtk_widget_hide (tooltip->label);
320 * gtk_tooltip_set_text:
321 * @tooltip: a #GtkTooltip
322 * @text: (allow-none): a text string or %NULL
324 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
325 * will be hidden. See also gtk_tooltip_set_markup().
330 gtk_tooltip_set_text (GtkTooltip *tooltip,
333 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
335 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
338 gtk_widget_show (tooltip->label);
340 gtk_widget_hide (tooltip->label);
344 * gtk_tooltip_set_icon:
345 * @tooltip: a #GtkTooltip
346 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
348 * Sets the icon of the tooltip (which is in front of the text) to be
349 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
354 gtk_tooltip_set_icon (GtkTooltip *tooltip,
357 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
359 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
361 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
364 gtk_widget_show (tooltip->image);
366 gtk_widget_hide (tooltip->image);
370 * gtk_tooltip_set_icon_from_stock:
371 * @tooltip: a #GtkTooltip
372 * @stock_id: (allow-none): a stock id, or %NULL
373 * @size: (type int): a stock icon size
375 * Sets the icon of the tooltip (which is in front of the text) to be
376 * the stock item indicated by @stock_id with the size indicated
377 * by @size. If @stock_id is %NULL, the image will be hidden.
382 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
383 const gchar *stock_id,
386 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
388 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
391 gtk_widget_show (tooltip->image);
393 gtk_widget_hide (tooltip->image);
397 * gtk_tooltip_set_icon_from_icon_name:
398 * @tooltip: a #GtkTooltip
399 * @icon_name: (allow-none): an icon name, or %NULL
400 * @size: (type int): a stock icon size
402 * Sets the icon of the tooltip (which is in front of the text) to be
403 * the icon indicated by @icon_name with the size indicated
404 * by @size. If @icon_name is %NULL, the image will be hidden.
409 gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
410 const gchar *icon_name,
413 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
415 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
418 gtk_widget_show (tooltip->image);
420 gtk_widget_hide (tooltip->image);
424 * gtk_tooltip_set_icon_from_gicon:
425 * @tooltip: a #GtkTooltip
426 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
427 * @size: (type int): a stock icon size
429 * Sets the icon of the tooltip (which is in front of the text)
430 * to be the icon indicated by @gicon with the size indicated
431 * by @size. If @gicon is %NULL, the image will be hidden.
436 gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
440 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
442 gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
445 gtk_widget_show (tooltip->image);
447 gtk_widget_hide (tooltip->image);
451 * gtk_tooltip_set_custom:
452 * @tooltip: a #GtkTooltip
453 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
455 * Replaces the widget packed into the tooltip with
456 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
458 * By default a box with a #GtkImage and #GtkLabel is embedded in
459 * the tooltip, which can be configured using gtk_tooltip_set_markup()
460 * and gtk_tooltip_set_icon().
466 gtk_tooltip_set_custom (GtkTooltip *tooltip,
467 GtkWidget *custom_widget)
469 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
471 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
473 /* The custom widget has been updated from the query-tooltip
474 * callback, so we do not want to reset the custom widget later on.
476 tooltip->custom_was_reset = TRUE;
478 /* No need to do anything if the custom widget stays the same */
479 if (tooltip->custom_widget == custom_widget)
482 if (tooltip->custom_widget)
484 GtkWidget *custom = tooltip->custom_widget;
485 /* Note: We must reset tooltip->custom_widget first,
486 * since gtk_container_remove() will recurse into
487 * gtk_tooltip_set_custom()
489 tooltip->custom_widget = NULL;
490 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
491 g_object_unref (custom);
496 tooltip->custom_widget = g_object_ref (custom_widget);
498 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
499 gtk_widget_show (custom_widget);
504 * gtk_tooltip_set_tip_area:
505 * @tooltip: a #GtkTooltip
506 * @rect: a #GdkRectangle
508 * Sets the area of the widget, where the contents of this tooltip apply,
509 * to be @rect (in widget coordinates). This is especially useful for
510 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
513 * For setting tooltips on #GtkTreeView, please refer to the convenience
514 * functions for this: gtk_tree_view_set_tooltip_row() and
515 * gtk_tree_view_set_tooltip_cell().
520 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
521 const GdkRectangle *rect)
523 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
526 tooltip->tip_area_set = FALSE;
529 tooltip->tip_area_set = TRUE;
530 tooltip->tip_area = *rect;
535 * gtk_tooltip_trigger_tooltip_query:
536 * @display: a #GdkDisplay
538 * Triggers a new tooltip query on @display, in order to update the current
539 * visible tooltip, or to show/hide the current tooltip. This function is
540 * useful to call when, for example, the state of the widget changed by a
546 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
553 /* Trigger logic as if the mouse moved */
554 device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
555 window = gdk_device_get_window_at_position (device, &x, &y);
559 event.type = GDK_MOTION_NOTIFY;
560 event.motion.window = window;
563 event.motion.is_hint = FALSE;
565 gdk_window_get_root_coords (window, x, y, &x, &y);
566 event.motion.x_root = x;
567 event.motion.y_root = y;
569 _gtk_tooltip_handle_event (&event);
572 /* private functions */
575 gtk_tooltip_reset (GtkTooltip *tooltip)
577 gtk_tooltip_set_markup (tooltip, NULL);
578 gtk_tooltip_set_icon (tooltip, NULL);
579 gtk_tooltip_set_tip_area (tooltip, NULL);
581 /* See if the custom widget is again set from the query-tooltip
584 tooltip->custom_was_reset = FALSE;
588 paint_background_and_frame (GtkTooltip *tooltip,
591 GtkStyleContext *context;
594 width = gtk_widget_get_allocated_width (tooltip->window);
595 height = gtk_widget_get_allocated_height (tooltip->window);
596 context = gtk_widget_get_style_context (tooltip->window);
598 gtk_render_background (context, cr,
599 0, 0, width, height);
600 gtk_render_frame (context, cr,
601 0, 0, width, height);
605 maybe_update_shape (GtkTooltip *tooltip)
608 cairo_surface_t *surface;
609 cairo_region_t *region;
612 /* fallback to XShape only for non-composited clients */
613 if (gtk_widget_is_composited (tooltip->window))
615 gtk_widget_shape_combine_region (tooltip->window, NULL);
619 surface = gdk_window_create_similar_surface (gtk_widget_get_window (tooltip->window),
620 CAIRO_CONTENT_COLOR_ALPHA,
621 gtk_widget_get_allocated_width (tooltip->window),
622 gtk_widget_get_allocated_height (tooltip->window));
624 cr = cairo_create (surface);
625 paint_background_and_frame (tooltip, cr);
628 region = gdk_cairo_region_create_from_surface (surface);
629 gtk_widget_shape_combine_region (tooltip->window, region);
631 cairo_surface_destroy (surface);
632 cairo_region_destroy (region);
636 gtk_tooltip_realize_window (GtkTooltip *tooltip,
639 maybe_update_shape (tooltip);
643 gtk_tooltip_paint_window (GtkTooltip *tooltip,
646 if (gtk_widget_is_composited (tooltip->window))
648 /* clear any background */
650 cairo_set_source_rgba (cr, 0, 0, 0, 0);
651 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
656 maybe_update_shape (tooltip);
657 paint_background_and_frame (tooltip, cr);
663 gtk_tooltip_window_hide (GtkWidget *widget,
666 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
668 gtk_tooltip_set_custom (tooltip, NULL);
671 /* event handling, etc */
676 GtkWidget *container;
683 child_location_foreach (GtkWidget *child,
686 GtkAllocation child_allocation;
688 struct ChildLocation *child_loc = data;
690 /* Ignore invisible widgets */
691 if (!gtk_widget_is_drawable (child))
694 gtk_widget_get_allocation (child, &child_allocation);
699 /* (child_loc->x, child_loc->y) are relative to
700 * child_loc->container's allocation.
703 if (!child_loc->child &&
704 gtk_widget_translate_coordinates (child_loc->container, child,
705 child_loc->x, child_loc->y,
709 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
710 gtk_widget_get_name (child),
713 child_allocation.width,
714 child_allocation.height,
715 child_loc->x, child_loc->y,
717 #endif /* DEBUG_TOOLTIP */
719 /* (x, y) relative to child's allocation. */
720 if (x >= 0 && x < child_allocation.width
721 && y >= 0 && y < child_allocation.height)
723 if (GTK_IS_CONTAINER (child))
725 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
727 /* Take (x, y) relative the child's allocation and
732 tmp.container = child;
734 gtk_container_forall (GTK_CONTAINER (child),
735 child_location_foreach, &tmp);
738 child_loc->child = tmp.child;
740 child_loc->child = child;
743 child_loc->child = child;
748 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
749 * to allocation relative (dest_x, dest_y) of dest_widget.
752 window_to_alloc (GtkWidget *dest_widget,
758 GtkAllocation allocation;
760 gtk_widget_get_allocation (dest_widget, &allocation);
762 /* Translate from window relative to allocation relative */
763 if (gtk_widget_get_has_window (dest_widget) &&
764 gtk_widget_get_parent (dest_widget))
767 gdk_window_get_position (gtk_widget_get_window (dest_widget),
770 /* Offset coordinates if widget->window is smaller than
771 * widget->allocation.
773 src_x += wx - allocation.x;
774 src_y += wy - allocation.y;
778 src_x -= allocation.x;
779 src_y -= allocation.y;
788 /* Translates coordinates from window relative (x, y) to
789 * allocation relative (x, y) of the returned widget.
792 _gtk_widget_find_at_coords (GdkWindow *window,
798 GtkWidget *event_widget;
799 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
801 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
803 gdk_window_get_user_data (window, (void **)&event_widget);
809 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
810 window, event_widget, gtk_widget_get_name (event_widget),
814 /* Coordinates are relative to event window */
815 child_loc.x = window_x;
816 child_loc.y = window_y;
818 /* We go down the window hierarchy to the widget->window,
819 * coordinates stay relative to the current window.
820 * We end up with window == widget->window, coordinates relative to that.
822 while (window && window != gtk_widget_get_window (event_widget))
826 gdk_window_coords_to_parent (window,
827 child_loc.x, child_loc.y,
832 window = gdk_window_get_effective_parent (window);
835 /* Failing to find widget->window can happen for e.g. a detached handle box;
836 * chaining ::query-tooltip up to its parent probably makes little sense,
837 * and users better implement tooltips on handle_box->child.
838 * so we simply ignore the event for tooltips here.
843 /* Convert the window relative coordinates to allocation
844 * relative coordinates.
846 window_to_alloc (event_widget,
847 child_loc.x, child_loc.y,
848 &child_loc.x, &child_loc.y);
850 if (GTK_IS_CONTAINER (event_widget))
852 GtkWidget *container = event_widget;
854 child_loc.container = event_widget;
855 child_loc.child = NULL;
857 gtk_container_forall (GTK_CONTAINER (event_widget),
858 child_location_foreach, &child_loc);
860 /* Here we have a widget, with coordinates relative to
861 * child_loc.container's allocation.
865 event_widget = child_loc.child;
866 else if (child_loc.container)
867 event_widget = child_loc.container;
869 /* Translate to event_widget's allocation */
870 gtk_widget_translate_coordinates (container, event_widget,
871 child_loc.x, child_loc.y,
872 &child_loc.x, &child_loc.y);
875 /* We return (x, y) relative to the allocation of event_widget. */
877 *widget_x = child_loc.x;
879 *widget_y = child_loc.y;
884 /* Ignores (x, y) on input, translates event coordinates to
885 * allocation relative (x, y) of the returned widget.
888 find_topmost_widget_coords_from_event (GdkEvent *event,
892 GtkAllocation allocation;
897 gdk_event_get_coords (event, &dx, &dy);
899 /* Returns coordinates relative to tmp's allocation. */
900 tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
905 /* Make sure the pointer can actually be on the widget returned. */
906 gtk_widget_get_allocation (tmp, &allocation);
907 if (tx < 0 || tx >= allocation.width ||
908 ty < 0 || ty >= allocation.height)
920 tooltip_browse_mode_expired (gpointer data)
924 tooltip = GTK_TOOLTIP (data);
926 tooltip->browse_mode_enabled = FALSE;
927 tooltip->browse_mode_timeout_id = 0;
929 /* destroy tooltip */
930 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
931 "gdk-display-current-tooltip", NULL);
937 gtk_tooltip_display_closed (GdkDisplay *display,
941 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
945 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
948 if (tooltip->last_window == window)
951 if (tooltip->last_window)
952 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
953 (gpointer *) &tooltip->last_window);
955 tooltip->last_window = window;
958 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
959 (gpointer *) &tooltip->last_window);
963 gtk_tooltip_run_requery (GtkWidget **widget,
968 gboolean has_tooltip = FALSE;
969 gboolean return_value = FALSE;
971 gtk_tooltip_reset (tooltip);
975 g_object_get (*widget,
976 "has-tooltip", &has_tooltip,
980 g_signal_emit_by_name (*widget,
983 tooltip->keyboard_mode_enabled,
989 GtkWidget *parent = gtk_widget_get_parent (*widget);
992 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
1001 /* If the custom widget was not reset in the query-tooltip
1002 * callback, we clear it here.
1004 if (!tooltip->custom_was_reset)
1005 gtk_tooltip_set_custom (tooltip, NULL);
1007 return return_value;
1011 get_bounding_box (GtkWidget *widget,
1012 GdkRectangle *bounds)
1014 GtkAllocation allocation;
1023 window = gtk_widget_get_parent_window (widget);
1025 window = gtk_widget_get_window (widget);
1027 gtk_widget_get_allocation (widget, &allocation);
1030 w = allocation.width;
1031 h = allocation.height;
1033 gdk_window_get_root_coords (window, x, y, &x1, &y1);
1034 gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
1035 gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
1036 gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
1038 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
1039 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
1041 bounds->x = floor (MIN4 (x1, x2, x3, x4));
1042 bounds->y = floor (MIN4 (y1, y2, y3, y4));
1043 bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
1044 bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
1048 gtk_tooltip_position (GtkTooltip *tooltip,
1049 GdkDisplay *display,
1050 GtkWidget *new_tooltip_widget)
1052 gint x, y, width, height;
1055 GdkRectangle monitor;
1057 GdkRectangle bounds;
1059 #define MAX_DISTANCE 32
1061 gtk_widget_realize (GTK_WIDGET (tooltip->current_window));
1063 tooltip->tooltip_widget = new_tooltip_widget;
1065 screen = gtk_widget_get_screen (new_tooltip_widget);
1067 width = gtk_widget_get_allocated_width (GTK_WIDGET (tooltip->current_window));
1068 height = gtk_widget_get_allocated_height (GTK_WIDGET (tooltip->current_window));
1070 monitor_num = gdk_screen_get_monitor_at_point (screen,
1073 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1075 get_bounding_box (new_tooltip_widget, &bounds);
1077 /* Position the tooltip */
1079 cursor_size = gdk_display_get_default_cursor_size (display);
1082 x = bounds.x + bounds.width / 2 - width / 2;
1083 y = bounds.y + bounds.height + 4;
1085 if (y + height <= monitor.y + monitor.height)
1087 if (tooltip->keyboard_mode_enabled)
1090 if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
1092 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1093 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1094 else if (x + width < tooltip->last_x - MAX_DISTANCE)
1095 x = tooltip->last_x - MAX_DISTANCE - width;
1102 x = bounds.x + bounds.width / 2 - width / 2;
1103 y = bounds.y - height - 4;
1107 if (tooltip->keyboard_mode_enabled)
1110 if (y + height >= tooltip->last_y - MAX_DISTANCE)
1112 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
1113 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1114 else if (x + width < tooltip->last_x - MAX_DISTANCE)
1115 x = tooltip->last_x - MAX_DISTANCE - width;
1121 /* Try right FIXME: flip on rtl ? */
1122 x = bounds.x + bounds.width + 4;
1123 y = bounds.y + bounds.height / 2 - height / 2;
1125 if (x + width <= monitor.x + monitor.width)
1127 if (tooltip->keyboard_mode_enabled)
1130 if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
1132 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1133 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1134 else if (y + height < tooltip->last_y - MAX_DISTANCE)
1135 y = tooltip->last_y - MAX_DISTANCE - height;
1141 /* Try left FIXME: flip on rtl ? */
1142 x = bounds.x - width - 4;
1143 y = bounds.y + bounds.height / 2 - height / 2;
1147 if (tooltip->keyboard_mode_enabled)
1150 if (x + width >= tooltip->last_x - MAX_DISTANCE)
1152 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1153 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1154 else if (y + height < tooltip->last_y - MAX_DISTANCE)
1155 y = tooltip->last_y - MAX_DISTANCE - height;
1162 if (tooltip->keyboard_mode_enabled)
1164 x = bounds.x + bounds.width / 2 - width / 2;
1165 y = bounds.y + bounds.height + 4;
1170 x = tooltip->last_x + cursor_size * 3 / 4;
1171 y = tooltip->last_y + cursor_size * 3 / 4;
1176 if (tooltip->current_window)
1178 if (x + width > monitor.x + monitor.width)
1179 x -= x - (monitor.x + monitor.width) + width;
1180 else if (x < monitor.x)
1183 if (y + height > monitor.y + monitor.height)
1184 y -= y - (monitor.y + monitor.height) + height;
1185 else if (y < monitor.y)
1188 if (!tooltip->keyboard_mode_enabled)
1190 /* don't pop up under the pointer */
1191 if (x <= tooltip->last_x && tooltip->last_x < x + width &&
1192 y <= tooltip->last_y && tooltip->last_y < y + height)
1193 y = tooltip->last_y - height - 2;
1196 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1197 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1202 gtk_tooltip_show_tooltip (GdkDisplay *display)
1208 GtkWidget *tooltip_widget;
1209 GtkTooltip *tooltip;
1210 gboolean has_tooltip;
1211 gboolean return_value = FALSE;
1213 tooltip = g_object_get_data (G_OBJECT (display),
1214 "gdk-display-current-tooltip");
1216 if (tooltip->keyboard_mode_enabled)
1219 tooltip_widget = tooltip->keyboard_widget;
1225 window = tooltip->last_window;
1227 if (!GDK_IS_WINDOW (window))
1230 gdk_window_get_pointer (window, &x, &y, NULL);
1232 gdk_window_get_root_coords (window, x, y, &tx, &ty);
1233 tooltip->last_x = tx;
1234 tooltip->last_y = ty;
1236 tooltip_widget = _gtk_widget_find_at_coords (window, x, y, &x, &y);
1239 if (!tooltip_widget)
1242 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1244 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1248 if (!tooltip->current_window)
1250 if (gtk_widget_get_tooltip_window (tooltip_widget))
1251 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1253 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1256 screen = gtk_widget_get_screen (tooltip_widget);
1258 /* FIXME: should use tooltip->current_window iso tooltip->window */
1259 if (screen != gtk_widget_get_screen (tooltip->window))
1261 g_signal_handlers_disconnect_by_func (display,
1262 gtk_tooltip_display_closed,
1265 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1267 g_signal_connect (display, "closed",
1268 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1271 gtk_tooltip_position (tooltip, display, tooltip_widget);
1273 /* Now a tooltip is visible again on the display, make sure browse
1276 tooltip->browse_mode_enabled = TRUE;
1277 if (tooltip->browse_mode_timeout_id)
1279 g_source_remove (tooltip->browse_mode_timeout_id);
1280 tooltip->browse_mode_timeout_id = 0;
1285 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1290 if (tooltip->timeout_id)
1292 g_source_remove (tooltip->timeout_id);
1293 tooltip->timeout_id = 0;
1296 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1299 tooltip->tooltip_widget = NULL;
1301 if (!tooltip->keyboard_mode_enabled)
1304 GtkSettings *settings;
1306 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1308 g_object_get (settings,
1309 "gtk-tooltip-browse-mode-timeout", &timeout,
1312 /* The tooltip is gone, after (by default, should be configurable) 500ms
1313 * we want to turn off browse mode
1315 if (!tooltip->browse_mode_timeout_id)
1316 tooltip->browse_mode_timeout_id =
1317 gdk_threads_add_timeout_full (0, timeout,
1318 tooltip_browse_mode_expired,
1319 g_object_ref (tooltip),
1324 if (tooltip->browse_mode_timeout_id)
1326 g_source_remove (tooltip->browse_mode_timeout_id);
1327 tooltip->browse_mode_timeout_id = 0;
1331 if (tooltip->current_window)
1333 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1334 tooltip->current_window = NULL;
1339 tooltip_popup_timeout (gpointer data)
1341 GdkDisplay *display;
1342 GtkTooltip *tooltip;
1344 display = GDK_DISPLAY (data);
1345 tooltip = g_object_get_data (G_OBJECT (display),
1346 "gdk-display-current-tooltip");
1348 /* This usually does not happen. However, it does occur in language
1349 * bindings were reference counting of objects behaves differently.
1354 gtk_tooltip_show_tooltip (display);
1356 tooltip->timeout_id = 0;
1362 gtk_tooltip_start_delay (GdkDisplay *display)
1365 GtkTooltip *tooltip;
1366 GtkSettings *settings;
1368 tooltip = g_object_get_data (G_OBJECT (display),
1369 "gdk-display-current-tooltip");
1371 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1374 if (tooltip->timeout_id)
1375 g_source_remove (tooltip->timeout_id);
1377 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1379 if (tooltip->browse_mode_enabled)
1380 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1382 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1384 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1385 tooltip_popup_timeout,
1386 g_object_ref (display),
1391 _gtk_tooltip_focus_in (GtkWidget *widget)
1394 gboolean return_value = FALSE;
1395 GdkDisplay *display;
1396 GtkTooltip *tooltip;
1399 /* Get current tooltip for this display */
1400 display = gtk_widget_get_display (widget);
1401 tooltip = g_object_get_data (G_OBJECT (display),
1402 "gdk-display-current-tooltip");
1404 /* Check if keyboard mode is enabled at this moment */
1405 if (!tooltip || !tooltip->keyboard_mode_enabled)
1408 device = gtk_get_current_event_device ();
1410 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1411 device = gdk_device_get_associated_device (device);
1413 /* This function should be called by either a focus in event,
1414 * or a key binding. In either case there should be a device.
1419 if (tooltip->keyboard_widget)
1420 g_object_unref (tooltip->keyboard_widget);
1422 tooltip->keyboard_widget = g_object_ref (widget);
1424 gdk_window_get_device_position (gtk_widget_get_window (widget),
1425 device, &x, &y, NULL);
1427 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1430 gtk_tooltip_hide_tooltip (tooltip);
1434 if (!tooltip->current_window)
1436 if (gtk_widget_get_tooltip_window (widget))
1437 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1439 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1442 gtk_tooltip_show_tooltip (display);
1446 _gtk_tooltip_focus_out (GtkWidget *widget)
1448 GdkDisplay *display;
1449 GtkTooltip *tooltip;
1451 /* Get current tooltip for this display */
1452 display = gtk_widget_get_display (widget);
1453 tooltip = g_object_get_data (G_OBJECT (display),
1454 "gdk-display-current-tooltip");
1456 if (!tooltip || !tooltip->keyboard_mode_enabled)
1459 if (tooltip->keyboard_widget)
1461 g_object_unref (tooltip->keyboard_widget);
1462 tooltip->keyboard_widget = NULL;
1465 gtk_tooltip_hide_tooltip (tooltip);
1469 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1471 GdkDisplay *display;
1472 GtkTooltip *tooltip;
1474 display = gtk_widget_get_display (widget);
1475 tooltip = g_object_get_data (G_OBJECT (display),
1476 "gdk-display-current-tooltip");
1480 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1481 g_object_set_data_full (G_OBJECT (display),
1482 "gdk-display-current-tooltip",
1483 tooltip, g_object_unref);
1484 g_signal_connect (display, "closed",
1485 G_CALLBACK (gtk_tooltip_display_closed),
1489 tooltip->keyboard_mode_enabled ^= 1;
1491 if (tooltip->keyboard_mode_enabled)
1493 tooltip->keyboard_widget = g_object_ref (widget);
1494 _gtk_tooltip_focus_in (widget);
1498 if (tooltip->keyboard_widget)
1500 g_object_unref (tooltip->keyboard_widget);
1501 tooltip->keyboard_widget = NULL;
1504 gtk_tooltip_hide_tooltip (tooltip);
1509 _gtk_tooltip_hide (GtkWidget *widget)
1511 GtkWidget *toplevel;
1512 GdkDisplay *display;
1513 GtkTooltip *tooltip;
1515 display = gtk_widget_get_display (widget);
1516 tooltip = g_object_get_data (G_OBJECT (display),
1517 "gdk-display-current-tooltip");
1519 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1522 toplevel = gtk_widget_get_toplevel (widget);
1524 if (widget == tooltip->tooltip_widget
1525 || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
1526 gtk_tooltip_hide_tooltip (tooltip);
1530 tooltips_enabled (GdkWindow *window)
1533 gboolean touchscreen;
1535 GtkSettings *settings;
1537 screen = gdk_window_get_screen (window);
1538 settings = gtk_settings_get_for_screen (screen);
1540 g_object_get (settings,
1541 "gtk-touchscreen-mode", &touchscreen,
1542 "gtk-enable-tooltips", &enabled,
1545 return (!touchscreen && enabled);
1549 _gtk_tooltip_handle_event (GdkEvent *event)
1552 gboolean return_value = FALSE;
1553 GtkWidget *has_tooltip_widget = NULL;
1554 GdkDisplay *display;
1555 GtkTooltip *current_tooltip;
1557 if (!tooltips_enabled (event->any.window))
1560 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1561 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1562 display = gdk_window_get_display (event->any.window);
1563 current_tooltip = g_object_get_data (G_OBJECT (display),
1564 "gdk-display-current-tooltip");
1566 if (current_tooltip)
1568 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1571 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1573 has_tooltip_widget = current_tooltip->keyboard_widget;
1574 if (!has_tooltip_widget)
1577 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1582 gtk_tooltip_hide_tooltip (current_tooltip);
1584 gtk_tooltip_start_delay (display);
1589 #ifdef DEBUG_TOOLTIP
1590 if (has_tooltip_widget)
1591 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1592 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1593 has_tooltip_widget->allocation.x,
1594 has_tooltip_widget->allocation.y,
1595 has_tooltip_widget->allocation.width,
1596 has_tooltip_widget->allocation.height,
1598 #endif /* DEBUG_TOOLTIP */
1600 /* Always poll for a next motion event */
1601 gdk_event_request_motions (&event->motion);
1603 /* Hide the tooltip when there's no new tooltip widget */
1604 if (!has_tooltip_widget)
1606 if (current_tooltip)
1607 gtk_tooltip_hide_tooltip (current_tooltip);
1612 switch (event->type)
1614 case GDK_BUTTON_PRESS:
1615 case GDK_2BUTTON_PRESS:
1616 case GDK_3BUTTON_PRESS:
1618 case GDK_DRAG_ENTER:
1619 case GDK_GRAB_BROKEN:
1621 gtk_tooltip_hide_tooltip (current_tooltip);
1624 case GDK_MOTION_NOTIFY:
1625 case GDK_ENTER_NOTIFY:
1626 case GDK_LEAVE_NOTIFY:
1627 if (current_tooltip)
1629 gboolean tip_area_set;
1630 GdkRectangle tip_area;
1631 gboolean hide_tooltip;
1633 tip_area_set = current_tooltip->tip_area_set;
1634 tip_area = current_tooltip->tip_area;
1636 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1640 /* Requested to be hidden? */
1641 hide_tooltip = !return_value;
1643 /* Leave notify should override the query function */
1644 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1646 /* Is the pointer above another widget now? */
1647 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1648 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1650 /* Did the pointer move out of the previous "context area"? */
1652 hide_tooltip |= (x <= tip_area.x
1653 || x >= tip_area.x + tip_area.width
1655 || y >= tip_area.y + tip_area.height);
1658 gtk_tooltip_hide_tooltip (current_tooltip);
1660 gtk_tooltip_start_delay (display);
1664 /* Need a new tooltip for this display */
1665 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1666 g_object_set_data_full (G_OBJECT (display),
1667 "gdk-display-current-tooltip",
1668 current_tooltip, g_object_unref);
1669 g_signal_connect (display, "closed",
1670 G_CALLBACK (gtk_tooltip_display_closed),
1673 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1675 gtk_tooltip_start_delay (display);