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_dispose (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->dispose = gtk_tooltip_dispose;
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_dispose (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_custom (tooltip, NULL);
186 gtk_tooltip_set_last_window (tooltip, NULL);
192 display = gtk_widget_get_display (tooltip->window);
193 g_signal_handlers_disconnect_by_func (display,
194 gtk_tooltip_display_closed,
196 gtk_widget_destroy (tooltip->window);
197 tooltip->window = NULL;
200 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
206 * gtk_tooltip_set_markup:
207 * @tooltip: a #GtkTooltip
208 * @markup: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
210 * Sets the text of the tooltip to be @markup, which is marked up
212 * linkend="PangoMarkupFormat">Pango text markup language</link>.
213 * If @markup is %NULL, the label will be hidden.
218 gtk_tooltip_set_markup (GtkTooltip *tooltip,
221 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
223 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
226 gtk_widget_show (tooltip->label);
228 gtk_widget_hide (tooltip->label);
232 * gtk_tooltip_set_text:
233 * @tooltip: a #GtkTooltip
234 * @text: a text string or %NULL
236 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
237 * will be hidden. See also gtk_tooltip_set_markup().
242 gtk_tooltip_set_text (GtkTooltip *tooltip,
245 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
247 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
250 gtk_widget_show (tooltip->label);
252 gtk_widget_hide (tooltip->label);
256 * gtk_tooltip_set_icon:
257 * @tooltip: a #GtkTooltip
258 * @pixbuf: a #GdkPixbuf, or %NULL
260 * Sets the icon of the tooltip (which is in front of the text) to be
261 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
266 gtk_tooltip_set_icon (GtkTooltip *tooltip,
269 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
271 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
273 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
276 gtk_widget_show (tooltip->image);
278 gtk_widget_hide (tooltip->image);
282 * gtk_tooltip_set_icon_from_stock:
283 * @tooltip: a #GtkTooltip
284 * @stock_id: a stock id, or %NULL
285 * @size: a stock icon size
287 * Sets the icon of the tooltip (which is in front of the text) to be
288 * the stock item indicated by @stock_id with the size indicated
289 * by @size. If @stock_id is %NULL, the image will be hidden.
294 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
295 const gchar *stock_id,
298 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
300 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
303 gtk_widget_show (tooltip->image);
305 gtk_widget_hide (tooltip->image);
309 * gtk_tooltip_set_icon_from_icon_name:
310 * @tooltip: a #GtkTooltip
311 * @icon_name: an icon name, or %NULL
312 * @size: a stock icon size
314 * Sets the icon of the tooltip (which is in front of the text) to be
315 * the icon indicated by @icon_name with the size indicated
316 * by @size. If @icon_name is %NULL, the image will be hidden.
321 gtk_tooltip_set_icon_from_icon_name(GtkTooltip *tooltip,
322 const gchar *icon_name,
325 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
327 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
330 gtk_widget_show (tooltip->image);
332 gtk_widget_hide (tooltip->image);
336 * gtk_tooltip_set_custom:
337 * @tooltip: a #GtkTooltip
338 * @custom_widget: a #GtkWidget, or %NULL to unset the old custom widget.
340 * Replaces the widget packed into the tooltip with
341 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
343 * By default a box with a #GtkImage and #GtkLabel is embedded in
344 * the tooltip, which can be configured using gtk_tooltip_set_markup()
345 * and gtk_tooltip_set_icon().
351 gtk_tooltip_set_custom (GtkTooltip *tooltip,
352 GtkWidget *custom_widget)
354 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
356 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
358 /* The custom widget has been updated from the query-tooltip
359 * callback, so we do not want to reset the custom widget later on.
361 tooltip->custom_was_reset = TRUE;
363 /* No need to do anything if the custom widget stays the same */
364 if (tooltip->custom_widget == custom_widget)
367 if (tooltip->custom_widget)
369 GtkWidget *custom = tooltip->custom_widget;
370 /* Note: We must reset tooltip->custom_widget first,
371 * since gtk_container_remove() will recurse into
372 * gtk_tooltip_set_custom()
374 tooltip->custom_widget = NULL;
375 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
376 g_object_unref (custom);
381 tooltip->custom_widget = g_object_ref (custom_widget);
383 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
384 gtk_widget_show (custom_widget);
389 * gtk_tooltip_set_tip_area:
390 * @tooltip: a #GtkTooltip
391 * @rect: a #GdkRectangle
393 * Sets the area of the widget, where the contents of this tooltip apply,
394 * to be @rect (in widget coordinates). This is especially useful for
395 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
398 * For setting tooltips on #GtkTreeView, please refer to the convenience
399 * functions for this: gtk_tree_view_set_tooltip_row() and
400 * gtk_tree_view_set_tooltip_cell().
405 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
406 const GdkRectangle *rect)
408 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
411 tooltip->tip_area_set = FALSE;
414 tooltip->tip_area_set = TRUE;
415 tooltip->tip_area = *rect;
420 * gtk_tooltip_trigger_tooltip_query:
421 * @display: a #GdkDisplay
423 * Triggers a new tooltip query on @display, in order to update the current
424 * visible tooltip, or to show/hide the current tooltip. This function is
425 * useful to call when, for example, the state of the widget changed by a
431 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
437 /* Trigger logic as if the mouse moved */
438 window = gdk_display_get_window_at_pointer (display, &x, &y);
442 event.type = GDK_MOTION_NOTIFY;
443 event.motion.window = window;
446 event.motion.is_hint = FALSE;
448 gdk_window_get_origin (window, &x, &y);
449 event.motion.x_root = event.motion.x + x;
450 event.motion.y_root = event.motion.y + y;
452 _gtk_tooltip_handle_event (&event);
455 /* private functions */
458 gtk_tooltip_reset (GtkTooltip *tooltip)
460 gtk_tooltip_set_markup (tooltip, NULL);
461 gtk_tooltip_set_icon (tooltip, NULL);
462 gtk_tooltip_set_tip_area (tooltip, NULL);
464 /* See if the custom widget is again set from the query-tooltip
467 tooltip->custom_was_reset = FALSE;
471 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
473 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
474 tooltip->window->style->ythickness,
475 tooltip->window->style->ythickness,
476 tooltip->window->style->xthickness,
477 tooltip->window->style->xthickness);
479 gtk_widget_queue_draw (tooltip->window);
483 gtk_tooltip_paint_window (GtkTooltip *tooltip)
485 gtk_paint_flat_box (tooltip->window->style,
486 tooltip->window->window,
493 tooltip->window->allocation.width,
494 tooltip->window->allocation.height);
500 gtk_tooltip_window_hide (GtkWidget *widget,
503 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
505 gtk_tooltip_set_custom (tooltip, NULL);
508 /* event handling, etc */
513 GtkWidget *container;
520 child_location_foreach (GtkWidget *child,
524 struct ChildLocation *child_loc = data;
526 /* Ignore invisible widgets */
527 if (!GTK_WIDGET_DRAWABLE (child))
533 /* (child_loc->x, child_loc->y) are relative to
534 * child_loc->container's allocation.
537 if (!child_loc->child &&
538 gtk_widget_translate_coordinates (child_loc->container, child,
539 child_loc->x, child_loc->y,
543 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
544 gtk_widget_get_name (child),
547 child->allocation.width,
548 child->allocation.height,
549 child_loc->x, child_loc->y,
551 #endif /* DEBUG_TOOLTIP */
553 /* (x, y) relative to child's allocation. */
554 if (x >= 0 && x < child->allocation.width
555 && y >= 0 && y < child->allocation.height)
557 if (GTK_IS_CONTAINER (child))
559 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
561 /* Take (x, y) relative the child's allocation and
566 tmp.container = child;
568 gtk_container_forall (GTK_CONTAINER (child),
569 child_location_foreach, &tmp);
572 child_loc->child = tmp.child;
574 child_loc->child = child;
577 child_loc->child = child;
582 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
583 * to allocation relative (dest_x, dest_y) of dest_widget.
586 window_to_alloc (GtkWidget *dest_widget,
592 /* Translate from window relative to allocation relative */
593 if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
596 gdk_window_get_position (dest_widget->window, &wx, &wy);
598 /* Offset coordinates if widget->window is smaller than
599 * widget->allocation.
601 src_x += wx - dest_widget->allocation.x;
602 src_y += wy - dest_widget->allocation.y;
606 src_x -= dest_widget->allocation.x;
607 src_y -= dest_widget->allocation.y;
616 /* Translates coordinates from window relative (x, y) to
617 * allocation relative (x, y) of the returned widget.
620 find_widget_under_pointer (GdkWindow *window,
624 GtkWidget *event_widget;
625 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
627 gdk_window_get_user_data (window, (void **)&event_widget);
633 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
634 window, event_widget, gtk_widget_get_name (event_widget),
638 /* Coordinates are relative to event window */
642 /* We go down the window hierarchy to the widget->window,
643 * coordinates stay relative to the current window.
644 * We end up with window == widget->window, coordinates relative to that.
646 while (window && window != event_widget->window)
650 gdk_window_get_position (window, &px, &py);
654 window = gdk_window_get_parent (window);
657 /* Failing to find widget->window can happen for e.g. a detached handle box;
658 * chaining ::query-tooltip up to its parent probably makes little sense,
659 * and users better implement tooltips on handle_box->child.
660 * so we simply ignore the event for tooltips here.
665 /* Convert the window relative coordinates to allocation
666 * relative coordinates.
668 window_to_alloc (event_widget,
669 child_loc.x, child_loc.y,
670 &child_loc.x, &child_loc.y);
672 if (GTK_IS_CONTAINER (event_widget))
674 GtkWidget *container = event_widget;
676 child_loc.container = event_widget;
677 child_loc.child = NULL;
679 gtk_container_forall (GTK_CONTAINER (event_widget),
680 child_location_foreach, &child_loc);
682 /* Here we have a widget, with coordinates relative to
683 * child_loc.container's allocation.
687 event_widget = child_loc.child;
688 else if (child_loc.container)
689 event_widget = child_loc.container;
691 /* Translate to event_widget's allocation */
692 gtk_widget_translate_coordinates (container, event_widget,
693 child_loc.x, child_loc.y,
694 &child_loc.x, &child_loc.y);
698 /* We return (x, y) relative to the allocation of event_widget. */
707 /* Ignores (x, y) on input, translates event coordinates to
708 * allocation relative (x, y) of the returned widget.
711 find_topmost_widget_coords_from_event (GdkEvent *event,
719 gdk_event_get_coords (event, &dx, &dy);
723 /* Returns coordinates relative to tmp's allocation. */
724 tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
729 /* Make sure the pointer can actually be on the widget returned. */
730 if (tx < 0 || tx >= tmp->allocation.width ||
731 ty < 0 || ty >= tmp->allocation.height)
743 tooltip_browse_mode_expired (gpointer data)
747 tooltip = GTK_TOOLTIP (data);
749 tooltip->browse_mode_enabled = FALSE;
750 tooltip->browse_mode_timeout_id = 0;
752 /* destroy tooltip */
753 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
754 "gdk-display-current-tooltip", NULL);
760 gtk_tooltip_display_closed (GdkDisplay *display,
764 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
768 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
771 if (tooltip->last_window)
772 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
773 (gpointer *) &tooltip->last_window);
775 tooltip->last_window = window;
778 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
779 (gpointer *) &tooltip->last_window);
783 gtk_tooltip_run_requery (GtkWidget **widget,
788 gboolean has_tooltip = FALSE;
789 gboolean return_value = FALSE;
791 gtk_tooltip_reset (tooltip);
795 g_object_get (*widget,
796 "has-tooltip", &has_tooltip,
800 g_signal_emit_by_name (*widget,
803 tooltip->keyboard_mode_enabled,
809 GtkWidget *parent = (*widget)->parent;
812 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
821 /* If the custom widget was not reset in the query-tooltip
822 * callback, we clear it here.
824 if (!tooltip->custom_was_reset)
825 gtk_tooltip_set_custom (tooltip, NULL);
831 gtk_tooltip_position (GtkTooltip *tooltip,
833 GtkWidget *new_tooltip_widget)
838 tooltip->tooltip_widget = new_tooltip_widget;
840 /* Position the tooltip */
841 /* FIXME: should we swap this when RTL is enabled? */
842 if (tooltip->keyboard_mode_enabled)
844 gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
845 if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
847 x += new_tooltip_widget->allocation.x;
848 y += new_tooltip_widget->allocation.y;
851 /* For keyboard mode we position the tooltip below the widget,
852 * right of the center of the widget.
854 x += new_tooltip_widget->allocation.width / 2;
855 y += new_tooltip_widget->allocation.height + 4;
864 /* For mouse mode, we position the tooltip right of the cursor,
865 * a little below the cursor's center.
867 cursor_size = gdk_display_get_default_cursor_size (display);
868 x += cursor_size / 2;
869 y += cursor_size / 2;
872 screen = gtk_widget_get_screen (new_tooltip_widget);
875 if (tooltip->current_window)
878 GdkRectangle monitor;
879 GtkRequisition requisition;
881 gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
884 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
885 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
887 if (x + requisition.width > monitor.x + monitor.width)
888 x -= x - (monitor.x + monitor.width) + requisition.width;
889 else if (x < monitor.x)
892 if (y + requisition.height > monitor.y + monitor.height)
893 y -= y - (monitor.y + monitor.height) + requisition.height;
895 if (!tooltip->keyboard_mode_enabled)
897 /* don't pop up under the pointer */
898 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
899 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
900 y = tooltip->last_y - requisition.height - 2;
903 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
904 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
909 gtk_tooltip_show_tooltip (GdkDisplay *display)
915 GtkWidget *tooltip_widget;
916 GtkWidget *pointer_widget;
918 gboolean has_tooltip;
919 gboolean return_value = FALSE;
921 tooltip = g_object_get_data (G_OBJECT (display),
922 "gdk-display-current-tooltip");
924 if (tooltip->keyboard_mode_enabled)
927 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
931 window = tooltip->last_window;
933 if (!GDK_IS_WINDOW (window))
936 gdk_window_get_origin (window, &x, &y);
937 x = tooltip->last_x - x;
938 y = tooltip->last_y - y;
940 pointer_widget = tooltip_widget = find_widget_under_pointer (window,
947 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
949 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
953 if (!tooltip->current_window)
955 if (gtk_widget_get_tooltip_window (tooltip_widget))
956 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
958 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
961 screen = gtk_widget_get_screen (tooltip_widget);
963 /* FIXME: should use tooltip->current_window iso tooltip->window */
964 if (screen != gtk_widget_get_screen (tooltip->window))
966 g_signal_handlers_disconnect_by_func (display,
967 gtk_tooltip_display_closed,
970 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
972 g_signal_connect (display, "closed",
973 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
976 gtk_tooltip_position (tooltip, display, tooltip_widget);
978 /* Now a tooltip is visible again on the display, make sure browse
981 tooltip->browse_mode_enabled = TRUE;
982 if (tooltip->browse_mode_timeout_id)
984 g_source_remove (tooltip->browse_mode_timeout_id);
985 tooltip->browse_mode_timeout_id = 0;
990 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
995 if (tooltip->timeout_id)
997 g_source_remove (tooltip->timeout_id);
998 tooltip->timeout_id = 0;
1001 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1004 tooltip->tooltip_widget = NULL;
1006 if (!tooltip->keyboard_mode_enabled)
1009 GtkSettings *settings;
1011 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1013 g_object_get (settings,
1014 "gtk-tooltip-browse-mode-timeout", &timeout,
1017 /* The tooltip is gone, after (by default, should be configurable) 500ms
1018 * we want to turn off browse mode
1020 if (!tooltip->browse_mode_timeout_id)
1021 tooltip->browse_mode_timeout_id =
1022 gdk_threads_add_timeout_full (0, timeout,
1023 tooltip_browse_mode_expired,
1024 g_object_ref (tooltip),
1029 if (tooltip->browse_mode_timeout_id)
1031 g_source_remove (tooltip->browse_mode_timeout_id);
1032 tooltip->browse_mode_timeout_id = 0;
1036 if (tooltip->current_window)
1038 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1039 tooltip->current_window = NULL;
1044 tooltip_popup_timeout (gpointer data)
1046 GdkDisplay *display;
1047 GtkTooltip *tooltip;
1049 display = GDK_DISPLAY_OBJECT (data);
1050 tooltip = g_object_get_data (G_OBJECT (display),
1051 "gdk-display-current-tooltip");
1053 /* This usually does not happen. However, it does occur in language
1054 * bindings were reference counting of objects behaves differently.
1059 gtk_tooltip_show_tooltip (display);
1061 tooltip->timeout_id = 0;
1067 gtk_tooltip_start_delay (GdkDisplay *display)
1070 GtkTooltip *tooltip;
1071 GtkSettings *settings;
1073 tooltip = g_object_get_data (G_OBJECT (display),
1074 "gdk-display-current-tooltip");
1076 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1079 if (tooltip->timeout_id)
1080 g_source_remove (tooltip->timeout_id);
1082 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1084 if (tooltip->browse_mode_enabled)
1085 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1087 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1089 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1090 tooltip_popup_timeout,
1091 g_object_ref (display),
1096 _gtk_tooltip_focus_in (GtkWidget *widget)
1099 gboolean return_value = FALSE;
1100 GdkDisplay *display;
1101 GtkTooltip *tooltip;
1103 /* Get current tooltip for this display */
1104 display = gtk_widget_get_display (widget);
1105 tooltip = g_object_get_data (G_OBJECT (display),
1106 "gdk-display-current-tooltip");
1108 /* Check if keyboard mode is enabled at this moment */
1109 if (!tooltip || !tooltip->keyboard_mode_enabled)
1112 if (tooltip->keyboard_widget)
1113 g_object_unref (tooltip->keyboard_widget);
1115 tooltip->keyboard_widget = g_object_ref (widget);
1117 gdk_window_get_pointer (widget->window, &x, &y, NULL);
1119 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1122 gtk_tooltip_hide_tooltip (tooltip);
1126 if (!tooltip->current_window)
1128 if (gtk_widget_get_tooltip_window (widget))
1129 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1131 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1134 gtk_tooltip_show_tooltip (display);
1138 _gtk_tooltip_focus_out (GtkWidget *widget)
1140 GdkDisplay *display;
1141 GtkTooltip *tooltip;
1143 /* Get current tooltip for this display */
1144 display = gtk_widget_get_display (widget);
1145 tooltip = g_object_get_data (G_OBJECT (display),
1146 "gdk-display-current-tooltip");
1148 if (!tooltip || !tooltip->keyboard_mode_enabled)
1151 if (tooltip->keyboard_widget)
1153 g_object_unref (tooltip->keyboard_widget);
1154 tooltip->keyboard_widget = NULL;
1157 gtk_tooltip_hide_tooltip (tooltip);
1161 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1163 GdkDisplay *display;
1164 GtkTooltip *tooltip;
1166 display = gtk_widget_get_display (widget);
1167 tooltip = g_object_get_data (G_OBJECT (display),
1168 "gdk-display-current-tooltip");
1172 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1173 g_object_set_data_full (G_OBJECT (display),
1174 "gdk-display-current-tooltip",
1175 tooltip, g_object_unref);
1176 g_signal_connect (display, "closed",
1177 G_CALLBACK (gtk_tooltip_display_closed),
1181 tooltip->keyboard_mode_enabled ^= 1;
1183 if (tooltip->keyboard_mode_enabled)
1185 tooltip->keyboard_widget = g_object_ref (widget);
1186 _gtk_tooltip_focus_in (widget);
1190 if (tooltip->keyboard_widget)
1192 g_object_unref (tooltip->keyboard_widget);
1193 tooltip->keyboard_widget = NULL;
1196 gtk_tooltip_hide_tooltip (tooltip);
1201 _gtk_tooltip_hide (GtkWidget *widget)
1203 GtkWidget *toplevel;
1204 GdkDisplay *display;
1205 GtkTooltip *tooltip;
1207 display = gtk_widget_get_display (widget);
1208 tooltip = g_object_get_data (G_OBJECT (display),
1209 "gdk-display-current-tooltip");
1211 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1214 toplevel = gtk_widget_get_toplevel (widget);
1216 if (widget == tooltip->tooltip_widget
1217 || toplevel->window == tooltip->toplevel_window)
1218 gtk_tooltip_hide_tooltip (tooltip);
1222 tooltips_enabled (GdkWindow *window)
1225 gboolean touchscreen;
1227 GtkSettings *settings;
1229 screen = gdk_drawable_get_screen (window);
1230 settings = gtk_settings_get_for_screen (screen);
1232 g_object_get (settings,
1233 "gtk-touchscreen-mode", &touchscreen,
1234 "gtk-enable-tooltips", &enabled,
1237 return (!touchscreen && enabled);
1241 _gtk_tooltip_handle_event (GdkEvent *event)
1244 gboolean return_value = FALSE;
1245 GtkWidget *has_tooltip_widget = NULL;
1246 GdkDisplay *display;
1247 GtkTooltip *current_tooltip;
1249 if (!tooltips_enabled (event->any.window))
1252 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1253 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1254 display = gdk_drawable_get_display (event->any.window);
1255 current_tooltip = g_object_get_data (G_OBJECT (display),
1256 "gdk-display-current-tooltip");
1258 if (current_tooltip)
1260 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1261 gdk_event_get_root_coords (event,
1262 ¤t_tooltip->last_x,
1263 ¤t_tooltip->last_y);
1266 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1268 has_tooltip_widget = current_tooltip->keyboard_widget;
1269 if (!has_tooltip_widget)
1272 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1277 gtk_tooltip_hide_tooltip (current_tooltip);
1279 gtk_tooltip_start_delay (display);
1284 #ifdef DEBUG_TOOLTIP
1285 if (has_tooltip_widget)
1286 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1287 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1288 has_tooltip_widget->allocation.x,
1289 has_tooltip_widget->allocation.y,
1290 has_tooltip_widget->allocation.width,
1291 has_tooltip_widget->allocation.height,
1293 #endif /* DEBUG_TOOLTIP */
1295 /* Always poll for a next motion event */
1296 gdk_event_request_motions (&event->motion);
1298 /* Hide the tooltip when there's no new tooltip widget */
1299 if (!has_tooltip_widget)
1301 if (current_tooltip)
1302 gtk_tooltip_hide_tooltip (current_tooltip);
1307 switch (event->type)
1309 case GDK_BUTTON_PRESS:
1310 case GDK_2BUTTON_PRESS:
1311 case GDK_3BUTTON_PRESS:
1313 case GDK_DRAG_ENTER:
1314 case GDK_GRAB_BROKEN:
1315 gtk_tooltip_hide_tooltip (current_tooltip);
1318 case GDK_MOTION_NOTIFY:
1319 case GDK_ENTER_NOTIFY:
1320 case GDK_LEAVE_NOTIFY:
1322 if (current_tooltip)
1324 gboolean tip_area_set;
1325 GdkRectangle tip_area;
1326 gboolean hide_tooltip;
1328 tip_area_set = current_tooltip->tip_area_set;
1329 tip_area = current_tooltip->tip_area;
1331 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1335 /* Requested to be hidden? */
1336 hide_tooltip = !return_value;
1338 /* Leave notify should override the query function */
1339 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1341 /* Is the pointer above another widget now? */
1342 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1343 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1345 /* Did the pointer move out of the previous "context area"? */
1347 hide_tooltip |= (x <= tip_area.x
1348 || x >= tip_area.x + tip_area.width
1350 || y >= tip_area.y + tip_area.height);
1353 gtk_tooltip_hide_tooltip (current_tooltip);
1355 gtk_tooltip_start_delay (display);
1359 /* Need a new tooltip for this display */
1360 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1361 g_object_set_data_full (G_OBJECT (display),
1362 "gdk-display-current-tooltip",
1363 current_tooltip, g_object_unref);
1364 g_signal_connect (display, "closed",
1365 G_CALLBACK (gtk_tooltip_display_closed),
1368 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1369 gdk_event_get_root_coords (event,
1370 ¤t_tooltip->last_x,
1371 ¤t_tooltip->last_y);
1373 gtk_tooltip_start_delay (display);
1383 #define __GTK_TOOLTIP_C__
1384 #include "gtkaliasdef.c"