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.
23 #include "gtktooltip.h"
25 #include "gtkwindow.h"
30 #include "gtkalignment.h"
39 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
40 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
41 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
43 typedef struct _GtkTooltipClass GtkTooltipClass;
47 GObject parent_instance;
54 GtkWidget *custom_widget;
56 GtkWindow *current_window;
57 GtkWidget *keyboard_widget;
59 GtkWidget *tooltip_widget;
60 GdkWindow *toplevel_window;
64 GdkWindow *last_window;
67 guint browse_mode_timeout_id;
69 GdkRectangle tip_area;
71 guint browse_mode_enabled : 1;
72 guint keyboard_mode_enabled : 1;
73 guint tip_area_set : 1;
74 guint custom_was_reset : 1;
77 struct _GtkTooltipClass
79 GObjectClass parent_class;
82 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && GTK_WIDGET_VISIBLE ((tooltip)->current_window))
85 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
86 static void gtk_tooltip_init (GtkTooltip *tooltip);
87 static void gtk_tooltip_finalize (GObject *object);
89 static void gtk_tooltip_window_style_set (GtkTooltip *tooltip);
90 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip);
91 static void gtk_tooltip_window_hide (GtkWidget *widget,
93 static void gtk_tooltip_display_closed (GdkDisplay *display,
96 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
100 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
103 gtk_tooltip_class_init (GtkTooltipClass *klass)
105 GObjectClass *object_class;
107 object_class = G_OBJECT_CLASS (klass);
109 object_class->finalize = gtk_tooltip_finalize;
113 gtk_tooltip_init (GtkTooltip *tooltip)
115 tooltip->timeout_id = 0;
116 tooltip->browse_mode_timeout_id = 0;
118 tooltip->browse_mode_enabled = FALSE;
119 tooltip->keyboard_mode_enabled = FALSE;
121 tooltip->current_window = NULL;
122 tooltip->keyboard_widget = NULL;
124 tooltip->tooltip_widget = NULL;
125 tooltip->toplevel_window = NULL;
127 tooltip->last_window = NULL;
129 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
130 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
131 GDK_WINDOW_TYPE_HINT_TOOLTIP);
132 gtk_widget_set_app_paintable (tooltip->window, TRUE);
133 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
134 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
135 g_signal_connect (tooltip->window, "hide",
136 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
138 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
139 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
140 tooltip->window->style->ythickness,
141 tooltip->window->style->ythickness,
142 tooltip->window->style->xthickness,
143 tooltip->window->style->xthickness);
144 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
145 gtk_widget_show (tooltip->alignment);
147 g_signal_connect_swapped (tooltip->window, "style_set",
148 G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
149 g_signal_connect_swapped (tooltip->window, "expose_event",
150 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
152 tooltip->box = gtk_hbox_new (FALSE, tooltip->window->style->xthickness);
153 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
154 gtk_widget_show (tooltip->box);
156 tooltip->image = gtk_image_new ();
157 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
160 tooltip->label = gtk_label_new ("");
161 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
162 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
165 tooltip->custom_widget = NULL;
169 gtk_tooltip_finalize (GObject *object)
171 GtkTooltip *tooltip = GTK_TOOLTIP (object);
173 if (tooltip->timeout_id)
175 g_source_remove (tooltip->timeout_id);
176 tooltip->timeout_id = 0;
179 if (tooltip->browse_mode_timeout_id)
181 g_source_remove (tooltip->browse_mode_timeout_id);
182 tooltip->browse_mode_timeout_id = 0;
185 gtk_tooltip_set_last_window (tooltip, NULL);
191 display = gtk_widget_get_display (tooltip->window);
192 g_signal_handlers_disconnect_by_func (display,
193 gtk_tooltip_display_closed,
195 gtk_widget_destroy (tooltip->window);
198 G_OBJECT_CLASS (gtk_tooltip_parent_class)->finalize (object);
204 * gtk_tooltip_set_markup:
205 * @tooltip: a #GtkTooltip
206 * @markup: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
208 * Sets the text of the tooltip to be @markup, which is marked up
210 * linkend="PangoMarkupFormat">Pango text markup language</link>.
211 * If @markup is %NULL, the label will be hidden.
216 gtk_tooltip_set_markup (GtkTooltip *tooltip,
219 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
221 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
224 gtk_widget_show (tooltip->label);
226 gtk_widget_hide (tooltip->label);
230 * gtk_tooltip_set_text:
231 * @tooltip: a #GtkTooltip
232 * @text: a text string or %NULL
234 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
235 * will be hidden. See also gtk_tooltip_set_markup().
240 gtk_tooltip_set_text (GtkTooltip *tooltip,
243 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
245 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
248 gtk_widget_show (tooltip->label);
250 gtk_widget_hide (tooltip->label);
254 * gtk_tooltip_set_icon:
255 * @tooltip: a #GtkTooltip
256 * @pixbuf: a #GdkPixbuf, or %NULL
258 * Sets the icon of the tooltip (which is in front of the text) to be
259 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
264 gtk_tooltip_set_icon (GtkTooltip *tooltip,
267 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
269 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
271 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
274 gtk_widget_show (tooltip->image);
276 gtk_widget_hide (tooltip->image);
280 * gtk_tooltip_set_icon_from_stock:
281 * @tooltip: a #GtkTooltip
282 * @stock_id: a stock id, or %NULL
283 * @size: a stock icon size
285 * Sets the icon of the tooltip (which is in front of the text) to be
286 * the stock item indicated by @stock_id with the size indicated
287 * by @size. If @stock_id is %NULL, the image will be hidden.
292 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
293 const gchar *stock_id,
296 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
298 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
301 gtk_widget_show (tooltip->image);
303 gtk_widget_hide (tooltip->image);
307 * gtk_tooltip_set_icon_from_icon_name:
308 * @tooltip: a #GtkTooltip
309 * @icon_name: an icon name, or %NULL
310 * @size: a stock icon size
312 * Sets the icon of the tooltip (which is in front of the text) to be
313 * the icon indicated by @icon_name with the size indicated
314 * by @size. If @icon_name is %NULL, the image will be hidden.
319 gtk_tooltip_set_icon_from_icon_name(GtkTooltip *tooltip,
320 const gchar *icon_name,
323 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
325 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
328 gtk_widget_show (tooltip->image);
330 gtk_widget_hide (tooltip->image);
334 * gtk_tooltip_set_custom:
335 * @tooltip: a #GtkTooltip
336 * @custom_widget: a #GtkWidget
338 * Replaces the widget packed into the tooltip with @custom_widget.
339 * By default a box with a #GtkImage and #GtkLabel is embedded in
340 * the tooltip, which can be configured using gtk_tooltip_set_markup()
341 * and gtk_tooltip_set_icon().
346 gtk_tooltip_set_custom (GtkTooltip *tooltip,
347 GtkWidget *custom_widget)
349 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
351 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
353 /* The custom widget has been updated from the query-tooltip
354 * callback, so we do not want to reset the custom widget later on.
356 tooltip->custom_was_reset = TRUE;
358 /* No need to do anything if the custom widget stays the same */
359 if (tooltip->custom_widget == custom_widget)
362 if (tooltip->custom_widget)
364 GtkWidget *custom = tooltip->custom_widget;
365 /* Note: We must reset tooltip->custom_widget first,
366 * since gtk_container_remove() will recurse into
367 * gtk_tooltip_set_custom()
369 tooltip->custom_widget = NULL;
370 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
371 g_object_unref (custom);
376 tooltip->custom_widget = g_object_ref (custom_widget);
378 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
379 gtk_widget_show (custom_widget);
384 * gtk_tooltip_set_tip_area:
385 * @tooltip: a #GtkTooltip
386 * @rect: a #GdkRectangle
388 * Sets the area of the widget, where the contents of this tooltip apply,
389 * to be @rect (in widget coordinates). This is especially useful for
390 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
393 * For setting tooltips on #GtkTreeView, please refer to the convenience
394 * functions for this: gtk_tree_view_set_tooltip_row() and
395 * gtk_tree_view_set_tooltip_cell().
400 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
401 const GdkRectangle *rect)
403 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
406 tooltip->tip_area_set = FALSE;
409 tooltip->tip_area_set = TRUE;
410 tooltip->tip_area = *rect;
415 * gtk_tooltip_trigger_tooltip_query:
416 * @display: a #GdkDisplay
418 * Triggers a new tooltip query on @display, in order to update the current
419 * visible tooltip, or to show/hide the current tooltip. This function is
420 * useful to call when, for example, the state of the widget changed by a
426 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
432 /* Trigger logic as if the mouse moved */
433 window = gdk_display_get_window_at_pointer (display, &x, &y);
437 event.type = GDK_MOTION_NOTIFY;
438 event.motion.window = window;
441 event.motion.is_hint = FALSE;
443 _gtk_tooltip_handle_event (&event);
446 /* private functions */
449 gtk_tooltip_reset (GtkTooltip *tooltip)
451 gtk_tooltip_set_markup (tooltip, NULL);
452 gtk_tooltip_set_icon (tooltip, NULL);
453 gtk_tooltip_set_tip_area (tooltip, NULL);
455 /* See if the custom widget is again set from the query-tooltip
458 tooltip->custom_was_reset = FALSE;
462 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
464 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
465 tooltip->window->style->ythickness,
466 tooltip->window->style->ythickness,
467 tooltip->window->style->xthickness,
468 tooltip->window->style->xthickness);
470 gtk_widget_queue_draw (tooltip->window);
474 gtk_tooltip_paint_window (GtkTooltip *tooltip)
476 gtk_paint_flat_box (tooltip->window->style,
477 tooltip->window->window,
484 tooltip->window->allocation.width,
485 tooltip->window->allocation.height);
491 gtk_tooltip_window_hide (GtkWidget *widget,
494 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
496 gtk_tooltip_set_custom (tooltip, NULL);
499 /* event handling, etc */
504 GtkWidget *container;
511 child_location_foreach (GtkWidget *child,
515 struct ChildLocation *child_loc = data;
517 /* Ignore invisible widgets */
518 if (!GTK_WIDGET_DRAWABLE (child))
521 /* (child_loc->x, child_loc->y) are relative to
522 * child_loc->container's allocation.
525 if (!child_loc->child &&
526 gtk_widget_translate_coordinates (child_loc->container, child,
527 child_loc->x, child_loc->y,
531 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
532 gtk_widget_get_name (child),
535 child->allocation.width,
536 child->allocation.height,
537 child_loc->x, child_loc->y,
539 #endif /* DEBUG_TOOLTIP */
541 /* (x, y) relative to child's allocation. */
542 if (x >= 0 && x < child->allocation.width
543 && y >= 0 && y < child->allocation.height)
545 if (GTK_IS_CONTAINER (child))
547 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
549 /* Take (x, y) relative the child's allocation and
554 tmp.container = child;
556 gtk_container_forall (GTK_CONTAINER (child),
557 child_location_foreach, &tmp);
560 child_loc->child = tmp.child;
562 child_loc->child = child;
565 child_loc->child = child;
570 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
571 * to allocation relative (dest_x, dest_y) of dest_widget.
574 window_to_alloc (GtkWidget *dest_widget,
580 /* Translate from window relative to allocation relative */
581 if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
584 gdk_window_get_position (dest_widget->window, &wx, &wy);
586 /* Offset coordinates if widget->window is smaller than
587 * widget->allocation.
589 src_x += wx - dest_widget->allocation.x;
590 src_y += wy - dest_widget->allocation.y;
594 src_x -= dest_widget->allocation.x;
595 src_y -= dest_widget->allocation.y;
604 /* Translates coordinates from window relative (x, y) to
605 * allocation relative (x, y) of the returned widget.
608 find_widget_under_pointer (GdkWindow *window,
612 GtkWidget *event_widget;
613 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
615 gdk_window_get_user_data (window, (void **)&event_widget);
621 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
622 window, event_widget, gtk_widget_get_name (event_widget),
626 /* Coordinates are relative to event window */
630 /* We go down the window hierarchy to the widget->window,
631 * coordinates stay relative to the current window.
632 * We end up with window == widget->window, coordinates relative to that.
634 while (window && window != event_widget->window)
638 gdk_window_get_position (window, &px, &py);
642 window = gdk_window_get_parent (window);
645 /* Failing to find widget->window can happen for e.g. a detached handle box;
646 * chaining ::query-tooltip up to its parent probably makes little sense,
647 * and users better implement tooltips on handle_box->child.
648 * so we simply ignore the event for tooltips here.
653 /* Convert the window relative coordinates to allocation
654 * relative coordinates.
656 window_to_alloc (event_widget,
657 child_loc.x, child_loc.y,
658 &child_loc.x, &child_loc.y);
660 if (GTK_IS_CONTAINER (event_widget))
662 GtkWidget *container = event_widget;
664 child_loc.container = event_widget;
665 child_loc.child = NULL;
667 gtk_container_forall (GTK_CONTAINER (event_widget),
668 child_location_foreach, &child_loc);
670 /* Here we have a widget, with coordinates relative to
671 * child_loc.container's allocation.
675 event_widget = child_loc.child;
676 else if (child_loc.container)
677 event_widget = child_loc.container;
679 /* Translate to event_widget's allocation */
680 gtk_widget_translate_coordinates (container, event_widget,
681 child_loc.x, child_loc.y,
682 &child_loc.x, &child_loc.y);
686 /* We return (x, y) relative to the allocation of event_widget. */
695 /* Ignores (x, y) on input, translates event coordinates to
696 * allocation relative (x, y) of the returned widget.
699 find_topmost_widget_coords_from_event (GdkEvent *event,
707 gdk_event_get_coords (event, &dx, &dy);
711 /* Returns coordinates relative to tmp's allocation. */
712 tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
717 /* Make sure the pointer can actually be on the widget returned. */
718 if (tx < 0 || tx >= tmp->allocation.width ||
719 ty < 0 || ty >= tmp->allocation.height)
731 tooltip_browse_mode_expired (gpointer data)
735 tooltip = GTK_TOOLTIP (data);
737 tooltip->browse_mode_enabled = FALSE;
738 tooltip->browse_mode_timeout_id = 0;
740 /* destroy tooltip */
741 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
742 "gdk-display-current-tooltip", NULL);
748 gtk_tooltip_display_closed (GdkDisplay *display,
752 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
756 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
759 if (tooltip->last_window)
760 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
761 (gpointer *) &tooltip->last_window);
763 tooltip->last_window = window;
766 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
767 (gpointer *) &tooltip->last_window);
771 gtk_tooltip_run_requery (GtkWidget **widget,
776 gboolean has_tooltip = FALSE;
777 gboolean return_value = FALSE;
779 gtk_tooltip_reset (tooltip);
783 g_object_get (*widget,
784 "has-tooltip", &has_tooltip,
788 g_signal_emit_by_name (*widget,
791 tooltip->keyboard_mode_enabled,
797 GtkWidget *parent = (*widget)->parent;
800 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
809 /* If the custom widget was not reset in the query-tooltip
810 * callback, we clear it here.
812 if (!tooltip->custom_was_reset)
813 gtk_tooltip_set_custom (tooltip, NULL);
819 gtk_tooltip_position (GtkTooltip *tooltip,
821 GtkWidget *new_tooltip_widget)
826 tooltip->tooltip_widget = new_tooltip_widget;
828 /* Position the tooltip */
829 /* FIXME: should we swap this when RTL is enabled? */
830 if (tooltip->keyboard_mode_enabled)
832 gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
833 if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
835 x += new_tooltip_widget->allocation.x;
836 y += new_tooltip_widget->allocation.y;
839 /* For keyboard mode we position the tooltip below the widget,
840 * right of the center of the widget.
842 x += new_tooltip_widget->allocation.width / 2;
843 y += new_tooltip_widget->allocation.height + 4;
852 /* For mouse mode, we position the tooltip right of the cursor,
853 * a little below the cursor's center.
855 cursor_size = gdk_display_get_default_cursor_size (display);
856 x += cursor_size / 2;
857 y += cursor_size / 2;
860 screen = gtk_widget_get_screen (new_tooltip_widget);
863 if (tooltip->current_window)
866 GdkRectangle monitor;
867 GtkRequisition requisition;
869 gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
872 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
873 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
875 if (x + requisition.width > monitor.x + monitor.width)
876 x -= x - (monitor.x + monitor.width) + requisition.width;
877 else if (x < monitor.x)
880 if (y + requisition.height > monitor.y + monitor.height)
881 y -= y - (monitor.y + monitor.height) + requisition.height;
883 if (!tooltip->keyboard_mode_enabled)
885 /* don't pop up under the pointer */
886 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
887 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
888 y = tooltip->last_y - requisition.height - 2;
891 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
892 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
897 gtk_tooltip_show_tooltip (GdkDisplay *display)
903 GtkWidget *tooltip_widget;
904 GtkWidget *pointer_widget;
906 gboolean has_tooltip;
907 gboolean return_value = FALSE;
909 tooltip = g_object_get_data (G_OBJECT (display),
910 "gdk-display-current-tooltip");
912 if (tooltip->keyboard_mode_enabled)
914 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
918 window = tooltip->last_window;
920 if (!GDK_IS_WINDOW (window))
923 gdk_window_get_origin (window, &x, &y);
924 x = tooltip->last_x - x;
925 y = tooltip->last_y - y;
927 pointer_widget = tooltip_widget = find_widget_under_pointer (window,
934 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
936 g_assert (tooltip != NULL);
938 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
942 if (!tooltip->current_window)
944 if (gtk_widget_get_tooltip_window (tooltip_widget))
945 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
947 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
950 screen = gtk_widget_get_screen (tooltip_widget);
952 /* FIXME: should use tooltip->current_window iso tooltip->window */
953 if (screen != gtk_widget_get_screen (tooltip->window))
955 g_signal_handlers_disconnect_by_func (display,
956 gtk_tooltip_display_closed,
959 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
961 g_signal_connect (display, "closed",
962 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
965 gtk_tooltip_position (tooltip, display, tooltip_widget);
967 /* Now a tooltip is visible again on the display, make sure browse
970 tooltip->browse_mode_enabled = TRUE;
971 if (tooltip->browse_mode_timeout_id)
973 g_source_remove (tooltip->browse_mode_timeout_id);
974 tooltip->browse_mode_timeout_id = 0;
979 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
984 if (tooltip->timeout_id)
986 g_source_remove (tooltip->timeout_id);
987 tooltip->timeout_id = 0;
990 if (!GTK_TOOLTIP_VISIBLE (tooltip))
993 tooltip->tooltip_widget = NULL;
995 if (!tooltip->keyboard_mode_enabled)
998 GtkSettings *settings;
1000 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1002 g_object_get (settings,
1003 "gtk-tooltip-browse-mode-timeout", &timeout,
1006 /* The tooltip is gone, after (by default, should be configurable) 500ms
1007 * we want to turn off browse mode
1009 if (!tooltip->browse_mode_timeout_id)
1010 tooltip->browse_mode_timeout_id =
1011 gdk_threads_add_timeout_full (0, timeout,
1012 tooltip_browse_mode_expired,
1013 g_object_ref (tooltip),
1018 if (tooltip->browse_mode_timeout_id)
1020 g_source_remove (tooltip->browse_mode_timeout_id);
1021 tooltip->browse_mode_timeout_id = 0;
1025 if (tooltip->current_window)
1027 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1028 tooltip->current_window = NULL;
1033 tooltip_popup_timeout (gpointer data)
1035 GdkDisplay *display;
1036 GtkTooltip *tooltip;
1038 display = GDK_DISPLAY_OBJECT (data);
1040 gtk_tooltip_show_tooltip (display);
1042 tooltip = g_object_get_data (G_OBJECT (display),
1043 "gdk-display-current-tooltip");
1044 tooltip->timeout_id = 0;
1050 gtk_tooltip_start_delay (GdkDisplay *display)
1053 GtkTooltip *tooltip;
1054 GtkSettings *settings;
1056 tooltip = g_object_get_data (G_OBJECT (display),
1057 "gdk-display-current-tooltip");
1059 if (tooltip && GTK_TOOLTIP_VISIBLE (tooltip))
1062 if (tooltip->timeout_id)
1063 g_source_remove (tooltip->timeout_id);
1065 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1067 if (tooltip->browse_mode_enabled)
1068 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1070 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1072 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1073 tooltip_popup_timeout,
1074 g_object_ref (display),
1079 _gtk_tooltip_focus_in (GtkWidget *widget)
1082 gboolean return_value = FALSE;
1083 GdkDisplay *display;
1084 GtkTooltip *tooltip;
1086 /* Get current tooltip for this display */
1087 display = gtk_widget_get_display (widget);
1088 tooltip = g_object_get_data (G_OBJECT (display),
1089 "gdk-display-current-tooltip");
1091 /* Check if keyboard mode is enabled at this moment */
1092 if (!tooltip || !tooltip->keyboard_mode_enabled)
1095 if (tooltip->keyboard_widget)
1096 g_object_unref (tooltip->keyboard_widget);
1098 tooltip->keyboard_widget = g_object_ref (widget);
1100 gdk_window_get_pointer (widget->window, &x, &y, NULL);
1102 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1105 gtk_tooltip_hide_tooltip (tooltip);
1109 if (!tooltip->current_window)
1111 if (gtk_widget_get_tooltip_window (widget))
1112 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1114 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1117 gtk_tooltip_show_tooltip (display);
1121 _gtk_tooltip_focus_out (GtkWidget *widget)
1123 GdkDisplay *display;
1124 GtkTooltip *tooltip;
1126 /* Get current tooltip for this display */
1127 display = gtk_widget_get_display (widget);
1128 tooltip = g_object_get_data (G_OBJECT (display),
1129 "gdk-display-current-tooltip");
1131 if (!tooltip || !tooltip->keyboard_mode_enabled)
1134 if (tooltip->keyboard_widget)
1136 g_object_unref (tooltip->keyboard_widget);
1137 tooltip->keyboard_widget = NULL;
1140 gtk_tooltip_hide_tooltip (tooltip);
1144 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1146 GdkDisplay *display;
1147 GtkTooltip *tooltip;
1149 display = gtk_widget_get_display (widget);
1150 tooltip = g_object_get_data (G_OBJECT (display),
1151 "gdk-display-current-tooltip");
1155 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1156 g_object_set_data_full (G_OBJECT (display),
1157 "gdk-display-current-tooltip",
1158 tooltip, g_object_unref);
1159 g_signal_connect (display, "closed",
1160 G_CALLBACK (gtk_tooltip_display_closed),
1164 tooltip->keyboard_mode_enabled ^= 1;
1166 if (tooltip->keyboard_mode_enabled)
1168 tooltip->keyboard_widget = g_object_ref (widget);
1169 _gtk_tooltip_focus_in (widget);
1173 if (tooltip->keyboard_widget)
1175 g_object_unref (tooltip->keyboard_widget);
1176 tooltip->keyboard_widget = NULL;
1179 gtk_tooltip_hide_tooltip (tooltip);
1184 _gtk_tooltip_hide (GtkWidget *widget)
1186 GtkWidget *toplevel;
1187 GdkDisplay *display;
1188 GtkTooltip *tooltip;
1190 display = gtk_widget_get_display (widget);
1191 tooltip = g_object_get_data (G_OBJECT (display),
1192 "gdk-display-current-tooltip");
1194 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1197 toplevel = gtk_widget_get_toplevel (widget);
1199 if (widget == tooltip->tooltip_widget
1200 || toplevel->window == tooltip->toplevel_window)
1201 gtk_tooltip_hide_tooltip (tooltip);
1205 _gtk_tooltip_handle_event (GdkEvent *event)
1208 gboolean return_value = FALSE;
1209 gboolean touchscreen;
1210 GtkWidget *has_tooltip_widget = NULL;
1212 GdkDisplay *display;
1213 GtkTooltip *current_tooltip;
1214 GtkSettings *settings;
1216 /* Disable tooltips in touchscreen mode */
1217 screen = gdk_drawable_get_screen (event->any.window);
1218 settings = gtk_settings_get_for_screen (screen);
1219 g_object_get (settings, "gtk-touchscreen-mode", &touchscreen, NULL);
1224 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1225 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1226 display = gdk_drawable_get_display (event->any.window);
1227 current_tooltip = g_object_get_data (G_OBJECT (display),
1228 "gdk-display-current-tooltip");
1230 if (current_tooltip)
1232 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1233 gdk_event_get_root_coords (event,
1234 ¤t_tooltip->last_x,
1235 ¤t_tooltip->last_y);
1238 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1240 has_tooltip_widget = current_tooltip->keyboard_widget;
1241 if (!has_tooltip_widget)
1244 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1249 gtk_tooltip_hide_tooltip (current_tooltip);
1251 gtk_tooltip_start_delay (display);
1256 #ifdef DEBUG_TOOLTIP
1257 if (has_tooltip_widget)
1258 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1259 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1260 has_tooltip_widget->allocation.x,
1261 has_tooltip_widget->allocation.y,
1262 has_tooltip_widget->allocation.width,
1263 has_tooltip_widget->allocation.height,
1265 #endif /* DEBUG_TOOLTIP */
1267 /* Always poll for a next motion event */
1268 gdk_event_request_motions (&event->motion);
1270 /* Hide the tooltip when there's no new tooltip widget */
1271 if (!has_tooltip_widget)
1273 if (current_tooltip)
1274 gtk_tooltip_hide_tooltip (current_tooltip);
1279 switch (event->type)
1281 case GDK_BUTTON_PRESS:
1282 case GDK_2BUTTON_PRESS:
1283 case GDK_3BUTTON_PRESS:
1285 case GDK_DRAG_ENTER:
1286 case GDK_GRAB_BROKEN:
1287 gtk_tooltip_hide_tooltip (current_tooltip);
1290 case GDK_MOTION_NOTIFY:
1291 case GDK_ENTER_NOTIFY:
1292 case GDK_LEAVE_NOTIFY:
1294 if (current_tooltip)
1296 gboolean tip_area_set;
1297 GdkRectangle tip_area;
1298 gboolean hide_tooltip;
1300 tip_area_set = current_tooltip->tip_area_set;
1301 tip_area = current_tooltip->tip_area;
1303 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1307 /* Requested to be hidden? */
1308 hide_tooltip = !return_value;
1310 /* Leave notify should override the query function */
1311 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1313 /* Is the pointer above another widget now? */
1314 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1315 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1317 /* Did the pointer move out of the previous "context area"? */
1319 hide_tooltip |= (x <= tip_area.x
1320 || x >= tip_area.x + tip_area.width
1322 || y >= tip_area.y + tip_area.height);
1325 gtk_tooltip_hide_tooltip (current_tooltip);
1327 gtk_tooltip_start_delay (display);
1331 /* Need a new tooltip for this display */
1332 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1333 g_object_set_data_full (G_OBJECT (display),
1334 "gdk-display-current-tooltip",
1335 current_tooltip, g_object_unref);
1336 g_signal_connect (display, "closed",
1337 G_CALLBACK (gtk_tooltip_display_closed),
1340 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1341 gdk_event_get_root_coords (event,
1342 ¤t_tooltip->last_x,
1343 ¤t_tooltip->last_y);
1345 gtk_tooltip_start_delay (display);
1355 #define __GTK_TOOLTIP_C__
1356 #include "gtkaliasdef.c"