3 * Copyright (C) 2006-2007 Imendio AB
4 * Contact: Kristian Rietveld <kris@imendio.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtktooltip.h"
30 #include "gtkwindow.h"
35 #include "gtkalignment.h"
36 #include "gtksizerequest.h"
42 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
43 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
44 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
46 typedef struct _GtkTooltipClass GtkTooltipClass;
50 GObject parent_instance;
57 GtkWidget *custom_widget;
59 GtkWindow *current_window;
60 GtkWidget *keyboard_widget;
62 GtkWidget *tooltip_widget;
63 GdkWindow *toplevel_window;
67 GdkWindow *last_window;
70 guint browse_mode_timeout_id;
72 GdkRectangle tip_area;
74 guint browse_mode_enabled : 1;
75 guint keyboard_mode_enabled : 1;
76 guint tip_area_set : 1;
77 guint custom_was_reset : 1;
80 struct _GtkTooltipClass
82 GObjectClass parent_class;
85 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
88 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
89 static void gtk_tooltip_init (GtkTooltip *tooltip);
90 static void gtk_tooltip_dispose (GObject *object);
92 static void gtk_tooltip_window_style_set (GtkTooltip *tooltip);
93 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip);
94 static void gtk_tooltip_window_hide (GtkWidget *widget,
96 static void gtk_tooltip_display_closed (GdkDisplay *display,
99 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
103 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
106 gtk_tooltip_class_init (GtkTooltipClass *klass)
108 GObjectClass *object_class;
110 object_class = G_OBJECT_CLASS (klass);
112 object_class->dispose = gtk_tooltip_dispose;
116 gtk_tooltip_init (GtkTooltip *tooltip)
120 tooltip->timeout_id = 0;
121 tooltip->browse_mode_timeout_id = 0;
123 tooltip->browse_mode_enabled = FALSE;
124 tooltip->keyboard_mode_enabled = FALSE;
126 tooltip->current_window = NULL;
127 tooltip->keyboard_widget = NULL;
129 tooltip->tooltip_widget = NULL;
130 tooltip->toplevel_window = NULL;
132 tooltip->last_window = NULL;
134 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
135 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
136 GDK_WINDOW_TYPE_HINT_TOOLTIP);
137 gtk_widget_set_app_paintable (tooltip->window, TRUE);
138 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
139 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
140 g_signal_connect (tooltip->window, "hide",
141 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
143 style = gtk_widget_get_style (tooltip->window);
145 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
146 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
147 style->ythickness, style->ythickness,
148 style->xthickness, style->xthickness);
149 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
150 gtk_widget_show (tooltip->alignment);
152 g_signal_connect_swapped (tooltip->window, "style-set",
153 G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
154 g_signal_connect_swapped (tooltip->window, "expose-event",
155 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
157 tooltip->box = gtk_hbox_new (FALSE, style->xthickness);
158 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
159 gtk_widget_show (tooltip->box);
161 tooltip->image = gtk_image_new ();
162 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
165 tooltip->label = gtk_label_new ("");
166 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
167 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
170 tooltip->custom_widget = NULL;
174 gtk_tooltip_dispose (GObject *object)
176 GtkTooltip *tooltip = GTK_TOOLTIP (object);
178 if (tooltip->timeout_id)
180 g_source_remove (tooltip->timeout_id);
181 tooltip->timeout_id = 0;
184 if (tooltip->browse_mode_timeout_id)
186 g_source_remove (tooltip->browse_mode_timeout_id);
187 tooltip->browse_mode_timeout_id = 0;
190 gtk_tooltip_set_custom (tooltip, NULL);
191 gtk_tooltip_set_last_window (tooltip, NULL);
197 display = gtk_widget_get_display (tooltip->window);
198 g_signal_handlers_disconnect_by_func (display,
199 gtk_tooltip_display_closed,
201 gtk_widget_destroy (tooltip->window);
202 tooltip->window = NULL;
205 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
211 * gtk_tooltip_set_markup:
212 * @tooltip: a #GtkTooltip
213 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
215 * Sets the text of the tooltip to be @markup, which is marked up
217 * linkend="PangoMarkupFormat">Pango text markup language</link>.
218 * If @markup is %NULL, the label will be hidden.
223 gtk_tooltip_set_markup (GtkTooltip *tooltip,
226 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
228 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
231 gtk_widget_show (tooltip->label);
233 gtk_widget_hide (tooltip->label);
237 * gtk_tooltip_set_text:
238 * @tooltip: a #GtkTooltip
239 * @text: (allow-none): a text string or %NULL
241 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
242 * will be hidden. See also gtk_tooltip_set_markup().
247 gtk_tooltip_set_text (GtkTooltip *tooltip,
250 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
252 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
255 gtk_widget_show (tooltip->label);
257 gtk_widget_hide (tooltip->label);
261 * gtk_tooltip_set_icon:
262 * @tooltip: a #GtkTooltip
263 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
265 * Sets the icon of the tooltip (which is in front of the text) to be
266 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
271 gtk_tooltip_set_icon (GtkTooltip *tooltip,
274 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
276 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
278 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
281 gtk_widget_show (tooltip->image);
283 gtk_widget_hide (tooltip->image);
287 * gtk_tooltip_set_icon_from_stock:
288 * @tooltip: a #GtkTooltip
289 * @stock_id: (allow-none): a stock id, or %NULL
290 * @size: (type int): a stock icon size
292 * Sets the icon of the tooltip (which is in front of the text) to be
293 * the stock item indicated by @stock_id with the size indicated
294 * by @size. If @stock_id is %NULL, the image will be hidden.
299 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
300 const gchar *stock_id,
303 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
305 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
308 gtk_widget_show (tooltip->image);
310 gtk_widget_hide (tooltip->image);
314 * gtk_tooltip_set_icon_from_icon_name:
315 * @tooltip: a #GtkTooltip
316 * @icon_name: (allow-none): an icon name, or %NULL
317 * @size: (type int): a stock icon size
319 * Sets the icon of the tooltip (which is in front of the text) to be
320 * the icon indicated by @icon_name with the size indicated
321 * by @size. If @icon_name is %NULL, the image will be hidden.
326 gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
327 const gchar *icon_name,
330 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
332 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
335 gtk_widget_show (tooltip->image);
337 gtk_widget_hide (tooltip->image);
341 * gtk_tooltip_set_icon_from_gicon:
342 * @tooltip: a #GtkTooltip
343 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
344 * @size: (type int): a stock icon size
346 * Sets the icon of the tooltip (which is in front of the text)
347 * to be the icon indicated by @gicon with the size indicated
348 * by @size. If @gicon is %NULL, the image will be hidden.
353 gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
357 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
359 gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
362 gtk_widget_show (tooltip->image);
364 gtk_widget_hide (tooltip->image);
368 * gtk_tooltip_set_custom:
369 * @tooltip: a #GtkTooltip
370 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
372 * Replaces the widget packed into the tooltip with
373 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
375 * By default a box with a #GtkImage and #GtkLabel is embedded in
376 * the tooltip, which can be configured using gtk_tooltip_set_markup()
377 * and gtk_tooltip_set_icon().
383 gtk_tooltip_set_custom (GtkTooltip *tooltip,
384 GtkWidget *custom_widget)
386 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
388 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
390 /* The custom widget has been updated from the query-tooltip
391 * callback, so we do not want to reset the custom widget later on.
393 tooltip->custom_was_reset = TRUE;
395 /* No need to do anything if the custom widget stays the same */
396 if (tooltip->custom_widget == custom_widget)
399 if (tooltip->custom_widget)
401 GtkWidget *custom = tooltip->custom_widget;
402 /* Note: We must reset tooltip->custom_widget first,
403 * since gtk_container_remove() will recurse into
404 * gtk_tooltip_set_custom()
406 tooltip->custom_widget = NULL;
407 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
408 g_object_unref (custom);
413 tooltip->custom_widget = g_object_ref (custom_widget);
415 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
416 gtk_widget_show (custom_widget);
421 * gtk_tooltip_set_tip_area:
422 * @tooltip: a #GtkTooltip
423 * @rect: a #GdkRectangle
425 * Sets the area of the widget, where the contents of this tooltip apply,
426 * to be @rect (in widget coordinates). This is especially useful for
427 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
430 * For setting tooltips on #GtkTreeView, please refer to the convenience
431 * functions for this: gtk_tree_view_set_tooltip_row() and
432 * gtk_tree_view_set_tooltip_cell().
437 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
438 const GdkRectangle *rect)
440 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
443 tooltip->tip_area_set = FALSE;
446 tooltip->tip_area_set = TRUE;
447 tooltip->tip_area = *rect;
452 * gtk_tooltip_trigger_tooltip_query:
453 * @display: a #GdkDisplay
455 * Triggers a new tooltip query on @display, in order to update the current
456 * visible tooltip, or to show/hide the current tooltip. This function is
457 * useful to call when, for example, the state of the widget changed by a
463 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
469 /* Trigger logic as if the mouse moved */
470 window = gdk_display_get_window_at_pointer (display, &x, &y);
474 event.type = GDK_MOTION_NOTIFY;
475 event.motion.window = window;
478 event.motion.is_hint = FALSE;
480 gdk_window_get_root_coords (window, x, y, &x, &y);
481 event.motion.x_root = x;
482 event.motion.y_root = y;
484 _gtk_tooltip_handle_event (&event);
487 /* private functions */
490 gtk_tooltip_reset (GtkTooltip *tooltip)
492 gtk_tooltip_set_markup (tooltip, NULL);
493 gtk_tooltip_set_icon (tooltip, NULL);
494 gtk_tooltip_set_tip_area (tooltip, NULL);
496 /* See if the custom widget is again set from the query-tooltip
499 tooltip->custom_was_reset = FALSE;
503 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
507 style = gtk_widget_get_style (tooltip->window);
509 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
510 style->ythickness, style->ythickness,
511 style->xthickness, style->xthickness);
513 gtk_box_set_spacing (GTK_BOX (tooltip->box),
516 gtk_widget_queue_draw (tooltip->window);
520 gtk_tooltip_paint_window (GtkTooltip *tooltip)
522 GtkAllocation allocation;
524 gtk_widget_get_allocation (tooltip->window, &allocation);
525 gtk_paint_flat_box (gtk_widget_get_style (tooltip->window),
526 gtk_widget_get_window (tooltip->window),
533 allocation.width, allocation.height);
539 gtk_tooltip_window_hide (GtkWidget *widget,
542 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
544 gtk_tooltip_set_custom (tooltip, NULL);
547 /* event handling, etc */
552 GtkWidget *container;
559 child_location_foreach (GtkWidget *child,
562 GtkAllocation child_allocation;
564 struct ChildLocation *child_loc = data;
566 /* Ignore invisible widgets */
567 if (!gtk_widget_is_drawable (child))
570 gtk_widget_get_allocation (child, &child_allocation);
575 /* (child_loc->x, child_loc->y) are relative to
576 * child_loc->container's allocation.
579 if (!child_loc->child &&
580 gtk_widget_translate_coordinates (child_loc->container, child,
581 child_loc->x, child_loc->y,
585 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
586 gtk_widget_get_name (child),
589 child_allocation.width,
590 child_allocation.height,
591 child_loc->x, child_loc->y,
593 #endif /* DEBUG_TOOLTIP */
595 /* (x, y) relative to child's allocation. */
596 if (x >= 0 && x < child_allocation.width
597 && y >= 0 && y < child_allocation.height)
599 if (GTK_IS_CONTAINER (child))
601 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
603 /* Take (x, y) relative the child's allocation and
608 tmp.container = child;
610 gtk_container_forall (GTK_CONTAINER (child),
611 child_location_foreach, &tmp);
614 child_loc->child = tmp.child;
616 child_loc->child = child;
619 child_loc->child = child;
624 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
625 * to allocation relative (dest_x, dest_y) of dest_widget.
628 window_to_alloc (GtkWidget *dest_widget,
634 GtkAllocation allocation;
636 gtk_widget_get_allocation (dest_widget, &allocation);
638 /* Translate from window relative to allocation relative */
639 if (gtk_widget_get_has_window (dest_widget) &&
640 gtk_widget_get_parent (dest_widget))
643 gdk_window_get_position (gtk_widget_get_window (dest_widget),
646 /* Offset coordinates if widget->window is smaller than
647 * widget->allocation.
649 src_x += wx - allocation.x;
650 src_y += wy - allocation.y;
654 src_x -= allocation.x;
655 src_y -= allocation.y;
664 /* Translates coordinates from window relative (x, y) to
665 * allocation relative (x, y) of the returned widget.
668 _gtk_widget_find_at_coords (GdkWindow *window,
674 GtkWidget *event_widget;
675 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
677 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
679 gdk_window_get_user_data (window, (void **)&event_widget);
685 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
686 window, event_widget, gtk_widget_get_name (event_widget),
690 /* Coordinates are relative to event window */
691 child_loc.x = window_x;
692 child_loc.y = window_y;
694 /* We go down the window hierarchy to the widget->window,
695 * coordinates stay relative to the current window.
696 * We end up with window == widget->window, coordinates relative to that.
698 while (window && window != gtk_widget_get_window (event_widget))
702 gdk_window_coords_to_parent (window,
703 child_loc.x, child_loc.y,
708 window = gdk_window_get_effective_parent (window);
711 /* Failing to find widget->window can happen for e.g. a detached handle box;
712 * chaining ::query-tooltip up to its parent probably makes little sense,
713 * and users better implement tooltips on handle_box->child.
714 * so we simply ignore the event for tooltips here.
719 /* Convert the window relative coordinates to allocation
720 * relative coordinates.
722 window_to_alloc (event_widget,
723 child_loc.x, child_loc.y,
724 &child_loc.x, &child_loc.y);
726 if (GTK_IS_CONTAINER (event_widget))
728 GtkWidget *container = event_widget;
730 child_loc.container = event_widget;
731 child_loc.child = NULL;
733 gtk_container_forall (GTK_CONTAINER (event_widget),
734 child_location_foreach, &child_loc);
736 /* Here we have a widget, with coordinates relative to
737 * child_loc.container's allocation.
741 event_widget = child_loc.child;
742 else if (child_loc.container)
743 event_widget = child_loc.container;
745 /* Translate to event_widget's allocation */
746 gtk_widget_translate_coordinates (container, event_widget,
747 child_loc.x, child_loc.y,
748 &child_loc.x, &child_loc.y);
751 /* We return (x, y) relative to the allocation of event_widget. */
753 *widget_x = child_loc.x;
755 *widget_y = child_loc.y;
760 /* Ignores (x, y) on input, translates event coordinates to
761 * allocation relative (x, y) of the returned widget.
764 find_topmost_widget_coords_from_event (GdkEvent *event,
768 GtkAllocation allocation;
773 gdk_event_get_coords (event, &dx, &dy);
775 /* Returns coordinates relative to tmp's allocation. */
776 tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
781 /* Make sure the pointer can actually be on the widget returned. */
782 gtk_widget_get_allocation (tmp, &allocation);
783 if (tx < 0 || tx >= allocation.width ||
784 ty < 0 || ty >= allocation.height)
796 tooltip_browse_mode_expired (gpointer data)
800 tooltip = GTK_TOOLTIP (data);
802 tooltip->browse_mode_enabled = FALSE;
803 tooltip->browse_mode_timeout_id = 0;
805 /* destroy tooltip */
806 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
807 "gdk-display-current-tooltip", NULL);
813 gtk_tooltip_display_closed (GdkDisplay *display,
817 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
821 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
824 if (tooltip->last_window == window)
827 if (tooltip->last_window)
828 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
829 (gpointer *) &tooltip->last_window);
831 tooltip->last_window = window;
834 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
835 (gpointer *) &tooltip->last_window);
839 gtk_tooltip_run_requery (GtkWidget **widget,
844 gboolean has_tooltip = FALSE;
845 gboolean return_value = FALSE;
847 gtk_tooltip_reset (tooltip);
851 g_object_get (*widget,
852 "has-tooltip", &has_tooltip,
856 g_signal_emit_by_name (*widget,
859 tooltip->keyboard_mode_enabled,
865 GtkWidget *parent = gtk_widget_get_parent (*widget);
868 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
877 /* If the custom widget was not reset in the query-tooltip
878 * callback, we clear it here.
880 if (!tooltip->custom_was_reset)
881 gtk_tooltip_set_custom (tooltip, NULL);
887 get_bounding_box (GtkWidget *widget,
888 GdkRectangle *bounds)
890 GtkAllocation allocation;
899 window = gtk_widget_get_parent_window (widget);
901 window = gtk_widget_get_window (widget);
903 gtk_widget_get_allocation (widget, &allocation);
906 w = allocation.width;
907 h = allocation.height;
909 gdk_window_get_root_coords (window, x, y, &x1, &y1);
910 gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
911 gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
912 gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
914 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
915 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
917 bounds->x = floor (MIN4 (x1, x2, x3, x4));
918 bounds->y = floor (MIN4 (y1, y2, y3, y4));
919 bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
920 bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
924 gtk_tooltip_position (GtkTooltip *tooltip,
926 GtkWidget *new_tooltip_widget)
931 GdkRectangle monitor;
932 GtkRequisition requisition;
936 #define MAX_DISTANCE 32
938 tooltip->tooltip_widget = new_tooltip_widget;
940 screen = gtk_widget_get_screen (new_tooltip_widget);
942 gtk_size_request_get_size (GTK_SIZE_REQUEST (tooltip->current_window),
945 monitor_num = gdk_screen_get_monitor_at_point (screen,
948 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
950 get_bounding_box (new_tooltip_widget, &bounds);
952 /* Position the tooltip */
954 cursor_size = gdk_display_get_default_cursor_size (display);
957 x = bounds.x + bounds.width / 2 - requisition.width / 2;
958 y = bounds.y + bounds.height + 4;
960 if (y + requisition.height <= monitor.y + monitor.height)
962 if (tooltip->keyboard_mode_enabled)
965 if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
967 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
968 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
969 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
970 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
977 x = bounds.x + bounds.width / 2 - requisition.width / 2;
978 y = bounds.y - requisition.height - 4;
982 if (tooltip->keyboard_mode_enabled)
985 if (y + requisition.height >= tooltip->last_y - MAX_DISTANCE)
987 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
988 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
989 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
990 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
996 /* Try right FIXME: flip on rtl ? */
997 x = bounds.x + bounds.width + 4;
998 y = bounds.y + bounds.height / 2 - requisition.height / 2;
1000 if (x + requisition.width <= monitor.x + monitor.width)
1002 if (tooltip->keyboard_mode_enabled)
1005 if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
1007 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1008 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1009 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1010 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1016 /* Try left FIXME: flip on rtl ? */
1017 x = bounds.x - requisition.width - 4;
1018 y = bounds.y + bounds.height / 2 - requisition.height / 2;
1022 if (tooltip->keyboard_mode_enabled)
1025 if (x + requisition.width >= tooltip->last_x - MAX_DISTANCE)
1027 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1028 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1029 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1030 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1037 if (tooltip->keyboard_mode_enabled)
1039 x = bounds.x + bounds.width / 2 - requisition.width / 2;
1040 y = bounds.y + bounds.height + 4;
1045 x = tooltip->last_x + cursor_size * 3 / 4;
1046 y = tooltip->last_y + cursor_size * 3 / 4;
1051 if (tooltip->current_window)
1053 if (x + requisition.width > monitor.x + monitor.width)
1054 x -= x - (monitor.x + monitor.width) + requisition.width;
1055 else if (x < monitor.x)
1058 if (y + requisition.height > monitor.y + monitor.height)
1059 y -= y - (monitor.y + monitor.height) + requisition.height;
1060 else if (y < monitor.y)
1063 if (!tooltip->keyboard_mode_enabled)
1065 /* don't pop up under the pointer */
1066 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
1067 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
1068 y = tooltip->last_y - requisition.height - 2;
1071 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1072 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1077 gtk_tooltip_show_tooltip (GdkDisplay *display)
1083 GtkWidget *tooltip_widget;
1084 GtkWidget *pointer_widget;
1085 GtkTooltip *tooltip;
1086 gboolean has_tooltip;
1087 gboolean return_value = FALSE;
1089 tooltip = g_object_get_data (G_OBJECT (display),
1090 "gdk-display-current-tooltip");
1092 if (tooltip->keyboard_mode_enabled)
1095 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
1101 window = tooltip->last_window;
1103 if (!GDK_IS_WINDOW (window))
1106 gdk_window_get_pointer (window, &x, &y, NULL);
1108 gdk_window_get_root_coords (window, x, y, &tx, &ty);
1109 tooltip->last_x = tx;
1110 tooltip->last_y = ty;
1112 pointer_widget = tooltip_widget = _gtk_widget_find_at_coords (window,
1117 if (!tooltip_widget)
1120 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1122 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1126 if (!tooltip->current_window)
1128 if (gtk_widget_get_tooltip_window (tooltip_widget))
1129 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1131 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1134 screen = gtk_widget_get_screen (tooltip_widget);
1136 /* FIXME: should use tooltip->current_window iso tooltip->window */
1137 if (screen != gtk_widget_get_screen (tooltip->window))
1139 g_signal_handlers_disconnect_by_func (display,
1140 gtk_tooltip_display_closed,
1143 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1145 g_signal_connect (display, "closed",
1146 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1149 gtk_tooltip_position (tooltip, display, tooltip_widget);
1151 /* Now a tooltip is visible again on the display, make sure browse
1154 tooltip->browse_mode_enabled = TRUE;
1155 if (tooltip->browse_mode_timeout_id)
1157 g_source_remove (tooltip->browse_mode_timeout_id);
1158 tooltip->browse_mode_timeout_id = 0;
1163 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1168 if (tooltip->timeout_id)
1170 g_source_remove (tooltip->timeout_id);
1171 tooltip->timeout_id = 0;
1174 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1177 tooltip->tooltip_widget = NULL;
1179 if (!tooltip->keyboard_mode_enabled)
1182 GtkSettings *settings;
1184 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1186 g_object_get (settings,
1187 "gtk-tooltip-browse-mode-timeout", &timeout,
1190 /* The tooltip is gone, after (by default, should be configurable) 500ms
1191 * we want to turn off browse mode
1193 if (!tooltip->browse_mode_timeout_id)
1194 tooltip->browse_mode_timeout_id =
1195 gdk_threads_add_timeout_full (0, timeout,
1196 tooltip_browse_mode_expired,
1197 g_object_ref (tooltip),
1202 if (tooltip->browse_mode_timeout_id)
1204 g_source_remove (tooltip->browse_mode_timeout_id);
1205 tooltip->browse_mode_timeout_id = 0;
1209 if (tooltip->current_window)
1211 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1212 tooltip->current_window = NULL;
1217 tooltip_popup_timeout (gpointer data)
1219 GdkDisplay *display;
1220 GtkTooltip *tooltip;
1222 display = GDK_DISPLAY_OBJECT (data);
1223 tooltip = g_object_get_data (G_OBJECT (display),
1224 "gdk-display-current-tooltip");
1226 /* This usually does not happen. However, it does occur in language
1227 * bindings were reference counting of objects behaves differently.
1232 gtk_tooltip_show_tooltip (display);
1234 tooltip->timeout_id = 0;
1240 gtk_tooltip_start_delay (GdkDisplay *display)
1243 GtkTooltip *tooltip;
1244 GtkSettings *settings;
1246 tooltip = g_object_get_data (G_OBJECT (display),
1247 "gdk-display-current-tooltip");
1249 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1252 if (tooltip->timeout_id)
1253 g_source_remove (tooltip->timeout_id);
1255 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1257 if (tooltip->browse_mode_enabled)
1258 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1260 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1262 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1263 tooltip_popup_timeout,
1264 g_object_ref (display),
1269 _gtk_tooltip_focus_in (GtkWidget *widget)
1272 gboolean return_value = FALSE;
1273 GdkDisplay *display;
1274 GtkTooltip *tooltip;
1277 /* Get current tooltip for this display */
1278 display = gtk_widget_get_display (widget);
1279 tooltip = g_object_get_data (G_OBJECT (display),
1280 "gdk-display-current-tooltip");
1282 /* Check if keyboard mode is enabled at this moment */
1283 if (!tooltip || !tooltip->keyboard_mode_enabled)
1286 device = gtk_get_current_event_device ();
1288 if (device && device->source == GDK_SOURCE_KEYBOARD)
1289 device = gdk_device_get_associated_device (device);
1291 /* This function should be called by either a focus in event,
1292 * or a key binding. In either case there should be a device.
1297 if (tooltip->keyboard_widget)
1298 g_object_unref (tooltip->keyboard_widget);
1300 tooltip->keyboard_widget = g_object_ref (widget);
1302 gdk_window_get_device_position (gtk_widget_get_window (widget),
1303 device, &x, &y, NULL);
1305 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1308 gtk_tooltip_hide_tooltip (tooltip);
1312 if (!tooltip->current_window)
1314 if (gtk_widget_get_tooltip_window (widget))
1315 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1317 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1320 gtk_tooltip_show_tooltip (display);
1324 _gtk_tooltip_focus_out (GtkWidget *widget)
1326 GdkDisplay *display;
1327 GtkTooltip *tooltip;
1329 /* Get current tooltip for this display */
1330 display = gtk_widget_get_display (widget);
1331 tooltip = g_object_get_data (G_OBJECT (display),
1332 "gdk-display-current-tooltip");
1334 if (!tooltip || !tooltip->keyboard_mode_enabled)
1337 if (tooltip->keyboard_widget)
1339 g_object_unref (tooltip->keyboard_widget);
1340 tooltip->keyboard_widget = NULL;
1343 gtk_tooltip_hide_tooltip (tooltip);
1347 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1349 GdkDisplay *display;
1350 GtkTooltip *tooltip;
1352 display = gtk_widget_get_display (widget);
1353 tooltip = g_object_get_data (G_OBJECT (display),
1354 "gdk-display-current-tooltip");
1358 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1359 g_object_set_data_full (G_OBJECT (display),
1360 "gdk-display-current-tooltip",
1361 tooltip, g_object_unref);
1362 g_signal_connect (display, "closed",
1363 G_CALLBACK (gtk_tooltip_display_closed),
1367 tooltip->keyboard_mode_enabled ^= 1;
1369 if (tooltip->keyboard_mode_enabled)
1371 tooltip->keyboard_widget = g_object_ref (widget);
1372 _gtk_tooltip_focus_in (widget);
1376 if (tooltip->keyboard_widget)
1378 g_object_unref (tooltip->keyboard_widget);
1379 tooltip->keyboard_widget = NULL;
1382 gtk_tooltip_hide_tooltip (tooltip);
1387 _gtk_tooltip_hide (GtkWidget *widget)
1389 GtkWidget *toplevel;
1390 GdkDisplay *display;
1391 GtkTooltip *tooltip;
1393 display = gtk_widget_get_display (widget);
1394 tooltip = g_object_get_data (G_OBJECT (display),
1395 "gdk-display-current-tooltip");
1397 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1400 toplevel = gtk_widget_get_toplevel (widget);
1402 if (widget == tooltip->tooltip_widget
1403 || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
1404 gtk_tooltip_hide_tooltip (tooltip);
1408 tooltips_enabled (GdkWindow *window)
1411 gboolean touchscreen;
1413 GtkSettings *settings;
1415 screen = gdk_window_get_screen (window);
1416 settings = gtk_settings_get_for_screen (screen);
1418 g_object_get (settings,
1419 "gtk-touchscreen-mode", &touchscreen,
1420 "gtk-enable-tooltips", &enabled,
1423 return (!touchscreen && enabled);
1427 _gtk_tooltip_handle_event (GdkEvent *event)
1430 gboolean return_value = FALSE;
1431 GtkWidget *has_tooltip_widget = NULL;
1432 GdkDisplay *display;
1433 GtkTooltip *current_tooltip;
1435 if (!tooltips_enabled (event->any.window))
1438 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1439 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1440 display = gdk_window_get_display (event->any.window);
1441 current_tooltip = g_object_get_data (G_OBJECT (display),
1442 "gdk-display-current-tooltip");
1444 if (current_tooltip)
1446 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1449 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1451 has_tooltip_widget = current_tooltip->keyboard_widget;
1452 if (!has_tooltip_widget)
1455 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1460 gtk_tooltip_hide_tooltip (current_tooltip);
1462 gtk_tooltip_start_delay (display);
1467 #ifdef DEBUG_TOOLTIP
1468 if (has_tooltip_widget)
1469 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1470 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1471 has_tooltip_widget->allocation.x,
1472 has_tooltip_widget->allocation.y,
1473 has_tooltip_widget->allocation.width,
1474 has_tooltip_widget->allocation.height,
1476 #endif /* DEBUG_TOOLTIP */
1478 /* Always poll for a next motion event */
1479 gdk_event_request_motions (&event->motion);
1481 /* Hide the tooltip when there's no new tooltip widget */
1482 if (!has_tooltip_widget)
1484 if (current_tooltip)
1485 gtk_tooltip_hide_tooltip (current_tooltip);
1490 switch (event->type)
1492 case GDK_BUTTON_PRESS:
1493 case GDK_2BUTTON_PRESS:
1494 case GDK_3BUTTON_PRESS:
1496 case GDK_DRAG_ENTER:
1497 case GDK_GRAB_BROKEN:
1498 gtk_tooltip_hide_tooltip (current_tooltip);
1501 case GDK_MOTION_NOTIFY:
1502 case GDK_ENTER_NOTIFY:
1503 case GDK_LEAVE_NOTIFY:
1505 if (current_tooltip)
1507 gboolean tip_area_set;
1508 GdkRectangle tip_area;
1509 gboolean hide_tooltip;
1511 tip_area_set = current_tooltip->tip_area_set;
1512 tip_area = current_tooltip->tip_area;
1514 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1518 /* Requested to be hidden? */
1519 hide_tooltip = !return_value;
1521 /* Leave notify should override the query function */
1522 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1524 /* Is the pointer above another widget now? */
1525 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1526 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1528 /* Did the pointer move out of the previous "context area"? */
1530 hide_tooltip |= (x <= tip_area.x
1531 || x >= tip_area.x + tip_area.width
1533 || y >= tip_area.y + tip_area.height);
1536 gtk_tooltip_hide_tooltip (current_tooltip);
1538 gtk_tooltip_start_delay (display);
1542 /* Need a new tooltip for this display */
1543 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1544 g_object_set_data_full (G_OBJECT (display),
1545 "gdk-display-current-tooltip",
1546 current_tooltip, g_object_unref);
1547 g_signal_connect (display, "closed",
1548 G_CALLBACK (gtk_tooltip_display_closed),
1551 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1553 gtk_tooltip_start_delay (display);