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;
76 struct _GtkTooltipClass
78 GObjectClass parent_class;
81 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && GTK_WIDGET_VISIBLE ((tooltip)->current_window))
84 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
85 static void gtk_tooltip_init (GtkTooltip *tooltip);
86 static void gtk_tooltip_finalize (GObject *object);
88 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip);
89 static void gtk_tooltip_window_hide (GtkWidget *widget,
91 static void gtk_tooltip_display_closed (GdkDisplay *display,
96 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
99 gtk_tooltip_class_init (GtkTooltipClass *klass)
101 GObjectClass *object_class;
103 object_class = G_OBJECT_CLASS (klass);
105 object_class->finalize = gtk_tooltip_finalize;
109 gtk_tooltip_init (GtkTooltip *tooltip)
111 tooltip->timeout_id = 0;
112 tooltip->browse_mode_timeout_id = 0;
114 tooltip->browse_mode_enabled = FALSE;
115 tooltip->keyboard_mode_enabled = FALSE;
117 tooltip->current_window = NULL;
118 tooltip->keyboard_widget = NULL;
120 tooltip->tooltip_widget = NULL;
121 tooltip->toplevel_window = NULL;
123 tooltip->last_window = NULL;
125 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
126 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
127 GDK_WINDOW_TYPE_HINT_TOOLTIP);
128 gtk_widget_set_app_paintable (tooltip->window, TRUE);
129 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
130 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
131 g_signal_connect (tooltip->window, "hide",
132 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
134 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
135 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
136 tooltip->window->style->ythickness,
137 tooltip->window->style->ythickness,
138 tooltip->window->style->xthickness,
139 tooltip->window->style->xthickness);
140 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
141 gtk_widget_show (tooltip->alignment);
143 g_signal_connect_swapped (tooltip->window, "expose_event",
144 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
146 tooltip->box = gtk_hbox_new (FALSE, tooltip->window->style->xthickness);
147 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
148 gtk_widget_show (tooltip->box);
150 tooltip->image = gtk_image_new ();
151 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
154 tooltip->label = gtk_label_new ("");
155 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
156 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
159 tooltip->custom_widget = NULL;
163 gtk_tooltip_finalize (GObject *object)
165 GtkTooltip *tooltip = GTK_TOOLTIP (object);
167 if (tooltip->timeout_id)
169 g_source_remove (tooltip->timeout_id);
170 tooltip->timeout_id = 0;
173 if (tooltip->browse_mode_timeout_id)
175 g_source_remove (tooltip->browse_mode_timeout_id);
176 tooltip->browse_mode_timeout_id = 0;
183 display = gtk_widget_get_display (tooltip->window);
184 g_signal_handlers_disconnect_by_func (display,
185 gtk_tooltip_display_closed,
187 gtk_widget_destroy (tooltip->window);
190 G_OBJECT_CLASS (gtk_tooltip_parent_class)->finalize (object);
196 * gtk_tooltip_set_markup:
197 * @tooltip: a #GtkTooltip
198 * @markup: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
200 * Sets the text of the tooltip to be @markup, which is marked up
202 * linkend="PangoMarkupFormat">Pango text markup language</link>.
203 * If @markup is %NULL, the label will be hidden.
208 gtk_tooltip_set_markup (GtkTooltip *tooltip,
211 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
213 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
216 gtk_widget_show (tooltip->label);
218 gtk_widget_hide (tooltip->label);
222 * gtk_tooltip_set_text:
223 * @tooltip: a #GtkTooltip
224 * @text: a text string or %NULL
226 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
227 * will be hidden. See also gtk_tooltip_set_markup().
232 gtk_tooltip_set_text (GtkTooltip *tooltip,
235 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
237 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
240 gtk_widget_show (tooltip->label);
242 gtk_widget_hide (tooltip->label);
246 * gtk_tooltip_set_icon:
247 * @tooltip: a #GtkTooltip
248 * @pixbuf: a #GdkPixbuf, or %NULL
250 * Sets the icon of the tooltip (which is in front of the text) to be
251 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
256 gtk_tooltip_set_icon (GtkTooltip *tooltip,
259 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
261 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
263 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
266 gtk_widget_show (tooltip->image);
268 gtk_widget_hide (tooltip->image);
272 * gtk_tooltip_set_icon_from_stock:
273 * @tooltip: a #GtkTooltip
274 * @stock_id: a stock icon name, or %NULL
275 * @size: a stock icon size
277 * Sets the icon of the tooltip (which is in front of the text) to be
278 * the stock item indicated by @stock_id with the size indicated
279 * by @size. If @stock_id is %NULL, the image will be hidden.
284 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
285 const gchar *stock_id,
288 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
290 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
293 gtk_widget_show (tooltip->image);
295 gtk_widget_hide (tooltip->image);
299 * gtk_tooltip_set_custom:
300 * @tooltip: a #GtkTooltip
301 * @custom_widget: a #GtkWidget
303 * Replaces the widget packed into the tooltip with @custom_widget.
304 * By default a box with a #GtkImage and #GtkLabel is embedded in
305 * the tooltip, which can be configured using gtk_tooltip_set_markup()
306 * and gtk_tooltip_set_icon().
311 gtk_tooltip_set_custom (GtkTooltip *tooltip,
312 GtkWidget *custom_widget)
314 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
316 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
318 if (tooltip->custom_widget)
320 GtkWidget *custom = tooltip->custom_widget;
321 /* Note: We must reset tooltip->custom_widget first,
322 * since gtk_container_remove() will recurse into
323 * gtk_tooltip_set_custom()
325 tooltip->custom_widget = NULL;
326 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
327 g_object_unref (custom);
332 tooltip->custom_widget = g_object_ref (custom_widget);
334 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
335 gtk_widget_show (custom_widget);
340 * gtk_tooltip_set_tip_area:
341 * @tooltip: a #GtkTooltip
342 * @rect: a #GdkRectangle
344 * Sets the area of the widget, where the contents of this tooltip apply,
345 * to be @rect (in widget coordinates). This is especially useful for
346 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
349 * For setting tooltips on #GtkTreeView, please refer to the convenience
350 * functions for this: gtk_tree_view_set_tooltip_row() and
351 * gtk_tree_view_set_tooltip_cell().
356 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
359 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
362 tooltip->tip_area_set = FALSE;
365 tooltip->tip_area_set = TRUE;
366 tooltip->tip_area = *rect;
371 * gtk_tooltip_trigger_tooltip_query:
372 * @display: a #GdkDisplay
374 * Triggers a new tooltip query on @display, in order to update the current
375 * visible tooltip, or to show/hide the current tooltip. This function is
376 * useful to call when, for example, the state of the widget changed by a
382 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
388 /* Trigger logic as if the mouse moved */
389 window = gdk_display_get_window_at_pointer (display, &x, &y);
393 event.type = GDK_MOTION_NOTIFY;
394 event.motion.window = window;
397 event.motion.is_hint = FALSE;
399 _gtk_tooltip_handle_event (&event);
402 /* private functions */
405 gtk_tooltip_reset (GtkTooltip *tooltip)
407 gtk_tooltip_set_markup (tooltip, NULL);
408 gtk_tooltip_set_icon (tooltip, NULL);
409 gtk_tooltip_set_custom (tooltip, NULL);
410 gtk_tooltip_set_tip_area (tooltip, NULL);
414 gtk_tooltip_paint_window (GtkTooltip *tooltip)
416 gtk_paint_flat_box (tooltip->window->style,
417 tooltip->window->window,
424 tooltip->window->allocation.width,
425 tooltip->window->allocation.height);
431 gtk_tooltip_window_hide (GtkWidget *widget,
434 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
436 gtk_tooltip_set_custom (tooltip, NULL);
439 /* event handling, etc */
444 GtkWidget *container;
451 child_location_foreach (GtkWidget *child,
455 struct ChildLocation *child_loc = data;
457 /* Ignore invisible widgets */
458 if (!GTK_WIDGET_DRAWABLE (child))
461 /* (child_loc->x, child_loc->y) are relative to
462 * child_loc->container's allocation.
465 if (!child_loc->child &&
466 gtk_widget_translate_coordinates (child_loc->container, child,
467 child_loc->x, child_loc->y,
471 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
472 gtk_widget_get_name (child),
475 child->allocation.width,
476 child->allocation.height,
477 child_loc->x, child_loc->y,
479 #endif /* DEBUG_TOOLTIP */
481 /* (x, y) relative to child's allocation. */
482 if (x >= 0 && x < child->allocation.width
483 && y >= 0 && y < child->allocation.height)
485 if (GTK_IS_CONTAINER (child))
487 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
489 /* Take (x, y) relative the child's allocation and
494 tmp.container = child;
496 gtk_container_forall (GTK_CONTAINER (child),
497 child_location_foreach, &tmp);
500 child_loc->child = tmp.child;
502 child_loc->child = child;
505 child_loc->child = child;
510 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
511 * to allocation relative (dest_x, dest_y) of dest_widget.
514 window_to_alloc (GtkWidget *dest_widget,
520 /* Translate from window relative to allocation relative */
521 if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
524 gdk_window_get_position (dest_widget->window, &wx, &wy);
526 /* Offset coordinates if widget->window is smaller than
527 * widget->allocation.
529 src_x += wx - dest_widget->allocation.x;
530 src_y += wy - dest_widget->allocation.y;
534 src_x -= dest_widget->allocation.x;
535 src_y -= dest_widget->allocation.y;
544 /* Translates coordinates from window relative (x, y) to
545 * allocation relative (x, y) of the returned widget.
548 find_widget_under_pointer (GdkWindow *window,
552 GtkWidget *event_widget;
553 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
555 gdk_window_get_user_data (window, (void **)&event_widget);
561 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
562 window, event_widget, gtk_widget_get_name (event_widget),
566 /* Coordinates are relative to event window */
570 /* We go down the window hierarchy to the widget->window,
571 * coordinates stay relative to the current window.
572 * We end up with window == widget->window, coordinates relative to that.
574 while (window && window != event_widget->window)
578 gdk_window_get_position (window, &px, &py);
582 window = gdk_window_get_parent (window);
585 /* Failing to find widget->window can happen for e.g. a detached handle box;
586 * chaining ::query-tooltip up to its parent probably makes little sense,
587 * and users better implement tooltips on handle_box->child.
588 * so we simply ignore the event for tooltips here.
593 /* Convert the window relative coordinates to allocation
594 * relative coordinates.
596 window_to_alloc (event_widget,
597 child_loc.x, child_loc.y,
598 &child_loc.x, &child_loc.y);
600 if (GTK_IS_CONTAINER (event_widget))
602 GtkWidget *container = event_widget;
604 child_loc.container = event_widget;
605 child_loc.child = NULL;
607 gtk_container_forall (GTK_CONTAINER (event_widget),
608 child_location_foreach, &child_loc);
610 /* Here we have a widget, with coordinates relative to
611 * child_loc.container's allocation.
615 event_widget = child_loc.child;
616 else if (child_loc.container)
617 event_widget = child_loc.container;
619 /* Translate to event_widget's allocation */
620 gtk_widget_translate_coordinates (container, event_widget,
621 child_loc.x, child_loc.y,
622 &child_loc.x, &child_loc.y);
626 /* We return (x, y) relative to the allocation of event_widget. */
635 /* Ignores (x, y) on input, translates event coordinates to
636 * allocation relative (x, y) of the returned widget.
639 find_topmost_widget_coords_from_event (GdkEvent *event,
647 gdk_event_get_coords (event, &dx, &dy);
651 /* Returns coordinates relative to tmp's allocation. */
652 tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
657 /* Make sure the pointer can actually be on the widget returned. */
658 if (tx < 0 || tx >= tmp->allocation.width ||
659 ty < 0 || ty >= tmp->allocation.height)
671 tooltip_browse_mode_expired (gpointer data)
675 tooltip = GTK_TOOLTIP (data);
677 tooltip->browse_mode_enabled = FALSE;
678 tooltip->browse_mode_timeout_id = 0;
680 /* destroy tooltip */
681 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
682 "gdk-display-current-tooltip", NULL);
688 gtk_tooltip_display_closed (GdkDisplay *display,
692 g_object_set (display, "gdk-display-current-tooltip", NULL);
696 gtk_tooltip_run_requery (GtkWidget **widget,
701 gboolean has_tooltip = FALSE;
702 gboolean return_value = FALSE;
704 gtk_tooltip_reset (tooltip);
708 g_object_get (*widget,
709 "has-tooltip", &has_tooltip,
713 g_signal_emit_by_name (*widget,
716 tooltip->keyboard_mode_enabled,
722 GtkWidget *parent = (*widget)->parent;
725 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
738 gtk_tooltip_position (GtkTooltip *tooltip,
740 GtkWidget *new_tooltip_widget)
745 tooltip->tooltip_widget = new_tooltip_widget;
747 /* Position the tooltip */
748 /* FIXME: should we swap this when RTL is enabled? */
749 if (tooltip->keyboard_mode_enabled)
751 gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
752 if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
754 x += new_tooltip_widget->allocation.x;
755 y += new_tooltip_widget->allocation.y;
758 /* For keyboard mode we position the tooltip below the widget,
759 * right of the center of the widget.
761 x += new_tooltip_widget->allocation.width / 2;
762 y += new_tooltip_widget->allocation.height + 4;
771 /* For mouse mode, we position the tooltip right of the cursor,
772 * a little below the cursor's center.
774 cursor_size = gdk_display_get_default_cursor_size (display);
775 x += cursor_size / 2;
776 y += cursor_size / 2;
779 screen = gtk_widget_get_screen (new_tooltip_widget);
782 if (tooltip->current_window)
785 GdkRectangle monitor;
786 GtkRequisition requisition;
788 gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
791 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
792 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
794 if (x + requisition.width > monitor.x + monitor.width)
795 x -= x - (monitor.x + monitor.width) + requisition.width;
796 else if (x < monitor.x)
799 if (y + requisition.height > monitor.y + monitor.height)
800 y -= y - (monitor.y + monitor.height) + requisition.height;
802 if (!tooltip->keyboard_mode_enabled)
804 /* don't pop up under the pointer */
805 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
806 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
807 y = tooltip->last_y - requisition.height - 2;
810 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
811 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
816 gtk_tooltip_show_tooltip (GdkDisplay *display)
822 GtkWidget *tooltip_widget;
823 GtkWidget *pointer_widget;
825 gboolean has_tooltip;
826 gboolean return_value = FALSE;
828 tooltip = g_object_get_data (G_OBJECT (display),
829 "gdk-display-current-tooltip");
831 if (tooltip->keyboard_mode_enabled)
833 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
837 window = tooltip->last_window;
839 if (!GDK_IS_WINDOW (window))
842 gdk_window_get_origin (window, &x, &y);
843 x = tooltip->last_x - x;
844 y = tooltip->last_y - y;
846 pointer_widget = tooltip_widget = find_widget_under_pointer (window,
853 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
855 g_assert (tooltip != NULL);
857 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
861 if (!tooltip->current_window)
863 if (gtk_widget_get_tooltip_window (tooltip_widget))
864 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
866 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
869 screen = gtk_widget_get_screen (tooltip_widget);
871 /* FIXME: should use tooltip->current_window iso tooltip->window */
872 if (screen != gtk_widget_get_screen (tooltip->window))
874 g_signal_handlers_disconnect_by_func (display,
875 gtk_tooltip_display_closed,
878 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
880 g_signal_connect (display, "closed",
881 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
884 gtk_tooltip_position (tooltip, display, tooltip_widget);
886 /* Now a tooltip is visible again on the display, make sure browse
889 tooltip->browse_mode_enabled = TRUE;
890 if (tooltip->browse_mode_timeout_id)
892 g_source_remove (tooltip->browse_mode_timeout_id);
893 tooltip->browse_mode_timeout_id = 0;
898 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
903 if (tooltip->timeout_id)
905 g_source_remove (tooltip->timeout_id);
906 tooltip->timeout_id = 0;
909 if (!GTK_TOOLTIP_VISIBLE (tooltip))
912 tooltip->tooltip_widget = NULL;
914 if (!tooltip->keyboard_mode_enabled)
917 GtkSettings *settings;
919 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
921 g_object_get (settings,
922 "gtk-tooltip-browse-mode-timeout", &timeout,
925 /* The tooltip is gone, after (by default, should be configurable) 500ms
926 * we want to turn off browse mode
928 if (!tooltip->browse_mode_timeout_id)
929 tooltip->browse_mode_timeout_id =
930 gdk_threads_add_timeout_full (0, timeout,
931 tooltip_browse_mode_expired,
932 g_object_ref (tooltip),
937 if (tooltip->browse_mode_timeout_id)
939 g_source_remove (tooltip->browse_mode_timeout_id);
940 tooltip->browse_mode_timeout_id = 0;
944 if (tooltip->current_window)
946 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
947 tooltip->current_window = NULL;
952 tooltip_popup_timeout (gpointer data)
957 display = GDK_DISPLAY_OBJECT (data);
959 gtk_tooltip_show_tooltip (display);
961 tooltip = g_object_get_data (G_OBJECT (display),
962 "gdk-display-current-tooltip");
963 tooltip->timeout_id = 0;
969 gtk_tooltip_start_delay (GdkDisplay *display)
973 GtkSettings *settings;
975 tooltip = g_object_get_data (G_OBJECT (display),
976 "gdk-display-current-tooltip");
978 if (tooltip && GTK_TOOLTIP_VISIBLE (tooltip))
981 if (tooltip->timeout_id)
982 g_source_remove (tooltip->timeout_id);
984 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
986 if (tooltip->browse_mode_enabled)
987 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
989 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
991 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
992 tooltip_popup_timeout,
993 g_object_ref (display),
998 _gtk_tooltip_focus_in (GtkWidget *widget)
1001 gboolean return_value = FALSE;
1002 GdkDisplay *display;
1003 GtkTooltip *tooltip;
1005 /* Get current tooltip for this display */
1006 display = gtk_widget_get_display (widget);
1007 tooltip = g_object_get_data (G_OBJECT (display),
1008 "gdk-display-current-tooltip");
1010 /* Check if keyboard mode is enabled at this moment */
1011 if (!tooltip || !tooltip->keyboard_mode_enabled)
1014 if (tooltip->keyboard_widget)
1015 g_object_unref (tooltip->keyboard_widget);
1017 tooltip->keyboard_widget = g_object_ref (widget);
1019 gdk_window_get_pointer (widget->window, &x, &y, NULL);
1021 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1024 gtk_tooltip_hide_tooltip (tooltip);
1028 if (!tooltip->current_window)
1030 if (gtk_widget_get_tooltip_window (widget))
1031 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1033 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1036 gtk_tooltip_show_tooltip (display);
1040 _gtk_tooltip_focus_out (GtkWidget *widget)
1042 GdkDisplay *display;
1043 GtkTooltip *tooltip;
1045 /* Get current tooltip for this display */
1046 display = gtk_widget_get_display (widget);
1047 tooltip = g_object_get_data (G_OBJECT (display),
1048 "gdk-display-current-tooltip");
1050 if (!tooltip || !tooltip->keyboard_mode_enabled)
1053 if (tooltip->keyboard_widget)
1055 g_object_unref (tooltip->keyboard_widget);
1056 tooltip->keyboard_widget = NULL;
1059 gtk_tooltip_hide_tooltip (tooltip);
1063 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1065 GdkDisplay *display;
1066 GtkTooltip *tooltip;
1068 display = gtk_widget_get_display (widget);
1069 tooltip = g_object_get_data (G_OBJECT (display),
1070 "gdk-display-current-tooltip");
1074 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1075 g_object_set_data_full (G_OBJECT (display),
1076 "gdk-display-current-tooltip",
1077 tooltip, g_object_unref);
1080 tooltip->keyboard_mode_enabled ^= 1;
1082 if (tooltip->keyboard_mode_enabled)
1084 tooltip->keyboard_widget = g_object_ref (widget);
1085 _gtk_tooltip_focus_in (widget);
1089 if (tooltip->keyboard_widget)
1091 g_object_unref (tooltip->keyboard_widget);
1092 tooltip->keyboard_widget = NULL;
1095 gtk_tooltip_hide_tooltip (tooltip);
1100 _gtk_tooltip_hide (GtkWidget *widget)
1102 GtkWidget *toplevel;
1103 GdkDisplay *display;
1104 GtkTooltip *tooltip;
1106 display = gtk_widget_get_display (widget);
1107 tooltip = g_object_get_data (G_OBJECT (display),
1108 "gdk-display-current-tooltip");
1110 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1113 toplevel = gtk_widget_get_toplevel (widget);
1115 if (widget == tooltip->tooltip_widget
1116 || toplevel->window == tooltip->toplevel_window)
1117 gtk_tooltip_hide_tooltip (tooltip);
1121 _gtk_tooltip_handle_event (GdkEvent *event)
1124 gboolean return_value = FALSE;
1125 gboolean touchscreen;
1126 GtkWidget *has_tooltip_widget = NULL;
1128 GdkDisplay *display;
1129 GtkTooltip *current_tooltip;
1130 GtkSettings *settings;
1132 /* Disable tooltips in touchscreen mode */
1133 screen = gdk_drawable_get_screen (event->any.window);
1134 settings = gtk_settings_get_for_screen (screen);
1135 g_object_get (settings, "gtk-touchscreen-mode", &touchscreen, NULL);
1140 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1141 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1142 display = gdk_drawable_get_display (event->any.window);
1143 current_tooltip = g_object_get_data (G_OBJECT (display),
1144 "gdk-display-current-tooltip");
1146 if (current_tooltip)
1148 current_tooltip->last_window = event->any.window;
1149 gdk_event_get_root_coords (event,
1150 ¤t_tooltip->last_x,
1151 ¤t_tooltip->last_y);
1154 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1156 has_tooltip_widget = current_tooltip->keyboard_widget;
1157 if (!has_tooltip_widget)
1160 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1165 gtk_tooltip_hide_tooltip (current_tooltip);
1167 gtk_tooltip_start_delay (display);
1172 #ifdef DEBUG_TOOLTIP
1173 if (has_tooltip_widget)
1174 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1175 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1176 has_tooltip_widget->allocation.x,
1177 has_tooltip_widget->allocation.y,
1178 has_tooltip_widget->allocation.width,
1179 has_tooltip_widget->allocation.height,
1181 #endif /* DEBUG_TOOLTIP */
1183 /* Always poll for a next motion event */
1184 gdk_event_request_motions (&event->motion);
1186 /* Hide the tooltip when there's no new tooltip widget */
1187 if (!has_tooltip_widget)
1189 if (current_tooltip)
1190 gtk_tooltip_hide_tooltip (current_tooltip);
1195 switch (event->type)
1197 case GDK_BUTTON_PRESS:
1198 case GDK_2BUTTON_PRESS:
1199 case GDK_3BUTTON_PRESS:
1201 case GDK_DRAG_ENTER:
1202 case GDK_GRAB_BROKEN:
1203 gtk_tooltip_hide_tooltip (current_tooltip);
1206 case GDK_MOTION_NOTIFY:
1207 case GDK_ENTER_NOTIFY:
1208 case GDK_LEAVE_NOTIFY:
1210 if (current_tooltip)
1212 gboolean tip_area_set;
1213 GdkRectangle tip_area;
1214 gboolean hide_tooltip;
1216 tip_area_set = current_tooltip->tip_area_set;
1217 tip_area = current_tooltip->tip_area;
1219 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1223 /* Requested to be hidden? */
1224 hide_tooltip = !return_value;
1226 /* Leave notify should override the query function */
1227 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1229 /* Is the pointer above another widget now? */
1230 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1231 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1233 /* Did the pointer move out of the previous "context area"? */
1235 hide_tooltip |= (x <= tip_area.x
1236 || x >= tip_area.x + tip_area.width
1238 || y >= tip_area.y + tip_area.height);
1241 gtk_tooltip_hide_tooltip (current_tooltip);
1243 gtk_tooltip_start_delay (display);
1247 /* Need a new tooltip for this display */
1248 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1249 g_object_set_data_full (G_OBJECT (display),
1250 "gdk-display-current-tooltip",
1251 current_tooltip, g_object_unref);
1253 current_tooltip->last_window = event->any.window;
1254 gdk_event_get_root_coords (event,
1255 ¤t_tooltip->last_x,
1256 ¤t_tooltip->last_y);
1258 gtk_tooltip_start_delay (display);
1268 #define __GTK_TOOLTIP_C__
1269 #include "gtkaliasdef.c"