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"
37 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
38 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
39 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
41 typedef struct _GtkTooltipClass GtkTooltipClass;
45 GObject parent_instance;
52 GtkWidget *custom_widget;
54 GtkWindow *current_window;
55 GtkWidget *keyboard_widget;
57 GtkWidget *tooltip_widget;
58 GdkWindow *toplevel_window;
62 GdkWindow *last_window;
65 guint browse_mode_timeout_id;
67 GdkRectangle tip_area;
69 guint browse_mode_enabled : 1;
70 guint keyboard_mode_enabled : 1;
71 guint tip_area_set : 1;
74 struct _GtkTooltipClass
76 GObjectClass parent_class;
79 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && GTK_WIDGET_VISIBLE ((tooltip)->current_window))
82 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
83 static void gtk_tooltip_init (GtkTooltip *tooltip);
84 static void gtk_tooltip_finalize (GObject *object);
86 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip);
87 static void gtk_tooltip_window_hide (GtkWidget *widget,
89 static void gtk_tooltip_display_closed (GdkDisplay *display,
94 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
97 gtk_tooltip_class_init (GtkTooltipClass *klass)
99 GObjectClass *object_class;
101 object_class = G_OBJECT_CLASS (klass);
103 object_class->finalize = gtk_tooltip_finalize;
107 gtk_tooltip_init (GtkTooltip *tooltip)
109 tooltip->timeout_id = 0;
110 tooltip->browse_mode_timeout_id = 0;
112 tooltip->browse_mode_enabled = FALSE;
113 tooltip->keyboard_mode_enabled = FALSE;
115 tooltip->current_window = NULL;
116 tooltip->keyboard_widget = NULL;
118 tooltip->tooltip_widget = NULL;
119 tooltip->toplevel_window = NULL;
121 tooltip->last_window = NULL;
123 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
124 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
125 GDK_WINDOW_TYPE_HINT_TOOLTIP);
126 gtk_widget_set_app_paintable (tooltip->window, TRUE);
127 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
128 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
129 g_signal_connect (tooltip->window, "hide",
130 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
132 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
133 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
134 tooltip->window->style->ythickness,
135 tooltip->window->style->ythickness,
136 tooltip->window->style->xthickness,
137 tooltip->window->style->xthickness);
138 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
139 gtk_widget_show (tooltip->alignment);
141 g_signal_connect_swapped (tooltip->window, "expose_event",
142 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
144 tooltip->box = gtk_hbox_new (FALSE, tooltip->window->style->xthickness);
145 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
146 gtk_widget_show (tooltip->box);
148 tooltip->image = gtk_image_new ();
149 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
152 tooltip->label = gtk_label_new ("");
153 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
154 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
157 tooltip->custom_widget = NULL;
161 gtk_tooltip_finalize (GObject *object)
163 GtkTooltip *tooltip = GTK_TOOLTIP (object);
165 if (tooltip->timeout_id)
167 g_source_remove (tooltip->timeout_id);
168 tooltip->timeout_id = 0;
171 if (tooltip->browse_mode_timeout_id)
173 g_source_remove (tooltip->browse_mode_timeout_id);
174 tooltip->browse_mode_timeout_id = 0;
181 display = gtk_widget_get_display (tooltip->window);
182 g_signal_handlers_disconnect_by_func (display,
183 gtk_tooltip_display_closed,
185 gtk_widget_destroy (tooltip->window);
188 G_OBJECT_CLASS (gtk_tooltip_parent_class)->finalize (object);
194 * gtk_tooltip_set_markup:
195 * @tooltip: a #GtkTooltip
196 * @markup: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
198 * Sets the text of the tooltip to be @markup, which is marked up
200 * linkend="PangoMarkupFormat">Pango text markup language</link>.
201 * If @markup is %NULL, the label will be hidden.
206 gtk_tooltip_set_markup (GtkTooltip *tooltip,
209 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
211 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
214 gtk_widget_show (tooltip->label);
216 gtk_widget_hide (tooltip->label);
220 * gtk_tooltip_set_text:
221 * @tooltip: a #GtkTooltip
222 * @text: a text string or %NULL
224 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
225 * will be hidden. See also gtk_tooltip_set_markup().
230 gtk_tooltip_set_text (GtkTooltip *tooltip,
233 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
235 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
238 gtk_widget_show (tooltip->label);
240 gtk_widget_hide (tooltip->label);
244 * gtk_tooltip_set_icon:
245 * @tooltip: a #GtkTooltip
246 * @pixbuf: a #GdkPixbuf, or %NULL
248 * Sets the icon of the tooltip (which is in front of the text) to be
249 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
254 gtk_tooltip_set_icon (GtkTooltip *tooltip,
257 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
259 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
261 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
264 gtk_widget_show (tooltip->image);
266 gtk_widget_hide (tooltip->image);
270 * gtk_tooltip_set_icon_from_stock:
271 * @tooltip: a #GtkTooltip
272 * @stock_id: a stock icon name, or %NULL
273 * @size: a stock icon size
275 * Sets the icon of the tooltip (which is in front of the text) to be
276 * the stock item indicated by @stock_id with the size indicated
277 * by @size. If @stock_id is %NULL, the image will be hidden.
282 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
283 const gchar *stock_id,
286 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
288 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
291 gtk_widget_show (tooltip->image);
293 gtk_widget_hide (tooltip->image);
297 * gtk_tooltip_set_custom:
298 * @tooltip: a #GtkTooltip
299 * @custom_widget: a #GtkWidget
301 * Replaces the widget packed into the tooltip with @custom_widget.
302 * By default a box with a #GtkImage and #GtkLabel is embedded in
303 * the tooltip, which can be configured using gtk_tooltip_set_markup()
304 * and gtk_tooltip_set_icon().
309 gtk_tooltip_set_custom (GtkTooltip *tooltip,
310 GtkWidget *custom_widget)
312 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
314 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
316 if (tooltip->custom_widget)
318 GtkWidget *custom = tooltip->custom_widget;
319 /* Note: We must reset tooltip->custom_widget first,
320 * since gtk_container_remove() will recurse into
321 * gtk_tooltip_set_custom()
323 tooltip->custom_widget = NULL;
324 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
325 g_object_unref (custom);
330 tooltip->custom_widget = g_object_ref (custom_widget);
332 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
333 gtk_widget_show (custom_widget);
338 * gtk_tooltip_set_tip_area:
339 * @tooltip: a #GtkTooltip
340 * @rect: a #GdkRectangle
342 * Sets the area of the widget, where the contents of this tooltip apply,
343 * to be @rect (in widget coordinates). This is especially useful for
344 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
347 * For setting tooltips on #GtkTreeView, please refer to the convenience
348 * functions for this: gtk_tree_view_set_tooltip_row() and
349 * gtk_tree_view_set_tooltip_cell().
354 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
357 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
360 tooltip->tip_area_set = FALSE;
363 tooltip->tip_area_set = TRUE;
364 tooltip->tip_area = *rect;
369 * gtk_tooltip_trigger_tooltip_query:
370 * @display: a #GdkDisplay
372 * Triggers a new tooltip query on @display, in order to update the current
373 * visible tooltip, or to show/hide the current tooltip. This function is
374 * useful to call when, for example, the state of the widget changed by a
380 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
386 /* Trigger logic as if the mouse moved */
387 window = gdk_display_get_window_at_pointer (display, &x, &y);
391 event.type = GDK_MOTION_NOTIFY;
392 event.motion.window = window;
395 event.motion.is_hint = FALSE;
397 _gtk_tooltip_handle_event (&event);
400 /* private functions */
403 gtk_tooltip_reset (GtkTooltip *tooltip)
405 gtk_tooltip_set_markup (tooltip, NULL);
406 gtk_tooltip_set_icon (tooltip, NULL);
407 gtk_tooltip_set_custom (tooltip, NULL);
408 gtk_tooltip_set_tip_area (tooltip, NULL);
412 gtk_tooltip_paint_window (GtkTooltip *tooltip)
414 gtk_paint_flat_box (tooltip->window->style,
415 tooltip->window->window,
422 tooltip->window->allocation.width,
423 tooltip->window->allocation.height);
429 gtk_tooltip_window_hide (GtkWidget *widget,
432 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
434 gtk_tooltip_set_custom (tooltip, NULL);
437 /* event handling, etc */
442 GtkWidget *container;
449 child_location_foreach (GtkWidget *child,
453 struct ChildLocation *child_loc = data;
455 /* Ignore invisible widgets */
456 if (!GTK_WIDGET_DRAWABLE (child))
459 if (!child_loc->child &&
460 gtk_widget_translate_coordinates (child_loc->container, child,
461 child_loc->x, child_loc->y,
464 if (x >= 0 && x < child->allocation.width
465 && y >= 0 && y < child->allocation.height)
467 if (GTK_IS_CONTAINER (child))
469 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
473 tmp.container = child;
475 gtk_container_forall (GTK_CONTAINER (child),
476 child_location_foreach, &tmp);
479 child_loc->child = tmp.child;
481 child_loc->child = child;
484 child_loc->child = child;
490 window_to_alloc (GtkWidget *dest_widget,
496 /* Translate from window relative to allocation relative */
497 if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
500 gdk_window_get_position (dest_widget->window, &wx, &wy);
502 src_x += wx - dest_widget->allocation.x;
503 src_y += wy - dest_widget->allocation.y;
507 src_x -= dest_widget->allocation.x;
508 src_y -= dest_widget->allocation.y;
518 find_widget_under_pointer (GdkWindow *window,
522 GtkWidget *event_widget;
523 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
525 gdk_window_get_user_data (window, (void **)&event_widget);
533 while (window && window != event_widget->window)
537 gdk_window_get_position (window, &px, &py);
541 window = gdk_window_get_parent (window);
544 if (GTK_WIDGET_NO_WINDOW (event_widget))
546 child_loc.x += event_widget->allocation.x;
547 child_loc.y += event_widget->allocation.y;
550 /* Failing to find widget->window can happen for e.g. a detached handle box;
551 * chaining ::query-tooltip up to its parent probably makes little sense,
552 * and users better implement tooltips on handle_box->child.
553 * so we simply ignore the event for tooltips here.
558 if (GTK_IS_CONTAINER (event_widget))
560 window_to_alloc (event_widget,
561 child_loc.x, child_loc.y,
562 &child_loc.x, &child_loc.y);
564 child_loc.container = event_widget;
565 child_loc.child = NULL;
567 gtk_container_forall (GTK_CONTAINER (event_widget),
568 child_location_foreach, &child_loc);
571 event_widget = child_loc.child;
572 else if (child_loc.container)
573 event_widget = child_loc.container;
585 find_topmost_widget_coords_from_event (GdkEvent *event,
593 gdk_event_get_coords (event, &dx, &dy);
597 tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
602 /* Make sure the pointer can actually be on the widget returned */
603 if (GTK_WIDGET_NO_WINDOW (tmp))
605 if (tx < tmp->allocation.x ||
606 tx >= tmp->allocation.x + tmp->allocation.width ||
607 ty < tmp->allocation.y ||
608 ty >= tmp->allocation.y + tmp->allocation.height)
613 if (tx < 0 || tx >= tmp->allocation.width ||
614 ty < 0 || ty >= tmp->allocation.height)
627 tooltip_browse_mode_expired (gpointer data)
631 tooltip = GTK_TOOLTIP (data);
633 tooltip->browse_mode_enabled = FALSE;
634 tooltip->browse_mode_timeout_id = 0;
636 /* destroy tooltip */
637 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
638 "gdk-display-current-tooltip", NULL);
644 gtk_tooltip_display_closed (GdkDisplay *display,
648 g_object_set (display, "gdk-display-current-tooltip", NULL);
652 gtk_tooltip_run_requery (GtkWidget **widget,
657 gboolean has_tooltip = FALSE;
658 gboolean return_value = FALSE;
660 gtk_tooltip_reset (tooltip);
664 g_object_get (*widget,
665 "has-tooltip", &has_tooltip,
669 g_signal_emit_by_name (*widget,
672 tooltip->keyboard_mode_enabled,
678 GtkWidget *parent = (*widget)->parent;
681 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
694 gtk_tooltip_position (GtkTooltip *tooltip,
696 GtkWidget *new_tooltip_widget)
701 tooltip->tooltip_widget = new_tooltip_widget;
703 /* Position the tooltip */
704 /* FIXME: should we swap this when RTL is enabled? */
705 if (tooltip->keyboard_mode_enabled)
707 gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
708 if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
710 x += new_tooltip_widget->allocation.x;
711 y += new_tooltip_widget->allocation.y;
714 /* For keyboard mode we position the tooltip below the widget,
715 * right of the center of the widget.
717 x += new_tooltip_widget->allocation.width / 2;
718 y += new_tooltip_widget->allocation.height + 4;
727 /* For mouse mode, we position the tooltip right of the cursor,
728 * a little below the cursor's center.
730 cursor_size = gdk_display_get_default_cursor_size (display);
731 x += cursor_size / 2;
732 y += cursor_size / 2;
735 screen = gtk_widget_get_screen (new_tooltip_widget);
738 if (tooltip->current_window)
741 GdkRectangle monitor;
742 GtkRequisition requisition;
744 gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
747 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
748 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
750 if (x + requisition.width > monitor.x + monitor.width)
751 x -= x - (monitor.x + monitor.width) + requisition.width;
752 else if (x < monitor.x)
755 if (y + requisition.height > monitor.y + monitor.height)
756 y -= y - (monitor.y + monitor.height) + requisition.height;
758 if (!tooltip->keyboard_mode_enabled)
760 /* don't pop up under the pointer */
761 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
762 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
763 y = tooltip->last_y - requisition.height - 2;
766 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
767 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
772 gtk_tooltip_show_tooltip (GdkDisplay *display)
778 GtkWidget *tooltip_widget;
779 GtkWidget *pointer_widget;
781 gboolean has_tooltip;
782 gboolean return_value = FALSE;
784 tooltip = g_object_get_data (G_OBJECT (display),
785 "gdk-display-current-tooltip");
787 if (tooltip->keyboard_mode_enabled)
789 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
793 window = tooltip->last_window;
795 if (!GDK_IS_WINDOW (window))
798 gdk_window_get_origin (window, &x, &y);
799 x = tooltip->last_x - x;
800 y = tooltip->last_y - y;
802 pointer_widget = tooltip_widget = find_widget_under_pointer (window,
809 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
811 g_assert (tooltip != NULL);
813 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
817 if (!tooltip->current_window)
819 if (gtk_widget_get_tooltip_window (tooltip_widget))
820 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
822 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
825 screen = gtk_widget_get_screen (tooltip_widget);
827 /* FIXME: should use tooltip->current_window iso tooltip->window */
828 if (screen != gtk_widget_get_screen (tooltip->window))
830 g_signal_handlers_disconnect_by_func (display,
831 gtk_tooltip_display_closed,
834 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
836 g_signal_connect (display, "closed",
837 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
840 gtk_tooltip_position (tooltip, display, tooltip_widget);
842 /* Now a tooltip is visible again on the display, make sure browse
845 tooltip->browse_mode_enabled = TRUE;
846 if (tooltip->browse_mode_timeout_id)
848 g_source_remove (tooltip->browse_mode_timeout_id);
849 tooltip->browse_mode_timeout_id = 0;
854 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
859 if (tooltip->timeout_id)
861 g_source_remove (tooltip->timeout_id);
862 tooltip->timeout_id = 0;
865 if (!GTK_TOOLTIP_VISIBLE (tooltip))
868 tooltip->tooltip_widget = NULL;
870 if (!tooltip->keyboard_mode_enabled)
873 GtkSettings *settings;
875 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
877 g_object_get (settings,
878 "gtk-tooltip-browse-mode-timeout", &timeout,
881 /* The tooltip is gone, after (by default, should be configurable) 500ms
882 * we want to turn off browse mode
884 if (!tooltip->browse_mode_timeout_id)
885 tooltip->browse_mode_timeout_id =
886 gdk_threads_add_timeout_full (0, timeout,
887 tooltip_browse_mode_expired,
888 g_object_ref (tooltip),
893 if (tooltip->browse_mode_timeout_id)
895 g_source_remove (tooltip->browse_mode_timeout_id);
896 tooltip->browse_mode_timeout_id = 0;
900 if (tooltip->current_window)
902 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
903 tooltip->current_window = NULL;
908 tooltip_popup_timeout (gpointer data)
913 display = GDK_DISPLAY_OBJECT (data);
915 gtk_tooltip_show_tooltip (display);
917 tooltip = g_object_get_data (G_OBJECT (display),
918 "gdk-display-current-tooltip");
919 tooltip->timeout_id = 0;
925 gtk_tooltip_start_delay (GdkDisplay *display)
929 GtkSettings *settings;
931 tooltip = g_object_get_data (G_OBJECT (display),
932 "gdk-display-current-tooltip");
934 if (tooltip && GTK_TOOLTIP_VISIBLE (tooltip))
937 if (tooltip->timeout_id)
938 g_source_remove (tooltip->timeout_id);
940 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
942 if (tooltip->browse_mode_enabled)
943 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
945 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
947 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
948 tooltip_popup_timeout,
949 g_object_ref (display),
954 _gtk_tooltip_focus_in (GtkWidget *widget)
957 gboolean return_value = FALSE;
961 /* Get current tooltip for this display */
962 display = gtk_widget_get_display (widget);
963 tooltip = g_object_get_data (G_OBJECT (display),
964 "gdk-display-current-tooltip");
966 /* Check if keyboard mode is enabled at this moment */
967 if (!tooltip || !tooltip->keyboard_mode_enabled)
970 if (tooltip->keyboard_widget)
971 g_object_unref (tooltip->keyboard_widget);
973 tooltip->keyboard_widget = g_object_ref (widget);
975 gdk_window_get_pointer (widget->window, &x, &y, NULL);
977 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
980 gtk_tooltip_hide_tooltip (tooltip);
984 if (!tooltip->current_window)
986 if (gtk_widget_get_tooltip_window (widget))
987 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
989 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
992 gtk_tooltip_show_tooltip (display);
996 _gtk_tooltip_focus_out (GtkWidget *widget)
1001 /* Get current tooltip for this display */
1002 display = gtk_widget_get_display (widget);
1003 tooltip = g_object_get_data (G_OBJECT (display),
1004 "gdk-display-current-tooltip");
1006 if (!tooltip || !tooltip->keyboard_mode_enabled)
1009 if (tooltip->keyboard_widget)
1011 g_object_unref (tooltip->keyboard_widget);
1012 tooltip->keyboard_widget = NULL;
1015 gtk_tooltip_hide_tooltip (tooltip);
1019 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1021 GdkDisplay *display;
1022 GtkTooltip *tooltip;
1024 display = gtk_widget_get_display (widget);
1025 tooltip = g_object_get_data (G_OBJECT (display),
1026 "gdk-display-current-tooltip");
1030 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1031 g_object_set_data_full (G_OBJECT (display),
1032 "gdk-display-current-tooltip",
1033 tooltip, g_object_unref);
1036 tooltip->keyboard_mode_enabled ^= 1;
1038 if (tooltip->keyboard_mode_enabled)
1040 tooltip->keyboard_widget = g_object_ref (widget);
1041 _gtk_tooltip_focus_in (widget);
1045 if (tooltip->keyboard_widget)
1047 g_object_unref (tooltip->keyboard_widget);
1048 tooltip->keyboard_widget = NULL;
1051 gtk_tooltip_hide_tooltip (tooltip);
1056 _gtk_tooltip_hide (GtkWidget *widget)
1058 GtkWidget *toplevel;
1059 GdkDisplay *display;
1060 GtkTooltip *tooltip;
1062 display = gtk_widget_get_display (widget);
1063 tooltip = g_object_get_data (G_OBJECT (display),
1064 "gdk-display-current-tooltip");
1066 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1069 toplevel = gtk_widget_get_toplevel (widget);
1071 if (widget == tooltip->tooltip_widget
1072 || toplevel->window == tooltip->toplevel_window)
1073 gtk_tooltip_hide_tooltip (tooltip);
1077 _gtk_tooltip_handle_event (GdkEvent *event)
1080 gboolean return_value = FALSE;
1081 GtkWidget *has_tooltip_widget = NULL;
1082 GdkDisplay *display;
1083 GtkTooltip *current_tooltip;
1085 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1086 display = gdk_drawable_get_display (event->any.window);
1087 current_tooltip = g_object_get_data (G_OBJECT (display),
1088 "gdk-display-current-tooltip");
1090 if (current_tooltip)
1092 current_tooltip->last_window = event->any.window;
1093 gdk_event_get_root_coords (event,
1094 ¤t_tooltip->last_x,
1095 ¤t_tooltip->last_y);
1098 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1100 has_tooltip_widget = current_tooltip->keyboard_widget;
1101 if (!has_tooltip_widget)
1104 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1109 gtk_tooltip_hide_tooltip (current_tooltip);
1111 gtk_tooltip_start_delay (display);
1116 /* Always poll for a next motion event */
1117 gdk_event_request_motions (&event->motion);
1119 /* Hide the tooltip when there's no new tooltip widget */
1120 if (!has_tooltip_widget)
1122 if (current_tooltip)
1123 gtk_tooltip_hide_tooltip (current_tooltip);
1128 switch (event->type)
1130 case GDK_BUTTON_PRESS:
1131 case GDK_2BUTTON_PRESS:
1132 case GDK_3BUTTON_PRESS:
1134 case GDK_DRAG_ENTER:
1135 case GDK_GRAB_BROKEN:
1136 gtk_tooltip_hide_tooltip (current_tooltip);
1139 case GDK_MOTION_NOTIFY:
1140 case GDK_ENTER_NOTIFY:
1141 case GDK_LEAVE_NOTIFY:
1143 if (current_tooltip)
1145 gboolean tip_area_set;
1146 GdkRectangle tip_area;
1147 gboolean hide_tooltip;
1149 tip_area_set = current_tooltip->tip_area_set;
1150 tip_area = current_tooltip->tip_area;
1152 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1156 /* Requested to be hidden? */
1157 hide_tooltip = !return_value;
1159 /* Is the pointer above another widget now? */
1160 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1161 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1163 /* Did the pointer move out of the previous "context area"? */
1165 hide_tooltip |= (x <= tip_area.x
1166 || x >= tip_area.x + tip_area.width
1168 || y >= tip_area.y + tip_area.height);
1171 gtk_tooltip_hide_tooltip (current_tooltip);
1173 gtk_tooltip_start_delay (display);
1177 /* Need a new tooltip for this display */
1178 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1179 g_object_set_data_full (G_OBJECT (display),
1180 "gdk-display-current-tooltip",
1181 current_tooltip, g_object_unref);
1183 current_tooltip->last_window = event->any.window;
1184 gdk_event_get_root_coords (event,
1185 ¤t_tooltip->last_x,
1186 ¤t_tooltip->last_y);
1188 gtk_tooltip_start_delay (display);
1198 #define __GTK_TOOLTIP_C__
1199 #include "gtkaliasdef.c"