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.
27 #include "gtktooltip.h"
29 #include "gtkwindow.h"
34 #include "gtkalignment.h"
41 #define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
42 #define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
43 #define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
45 typedef struct _GtkTooltipClass GtkTooltipClass;
49 GObject parent_instance;
56 GtkWidget *custom_widget;
58 GtkWindow *current_window;
59 GtkWidget *keyboard_widget;
61 GtkWidget *tooltip_widget;
62 GdkWindow *toplevel_window;
66 GdkWindow *last_window;
69 guint browse_mode_timeout_id;
71 GdkRectangle tip_area;
73 guint browse_mode_enabled : 1;
74 guint keyboard_mode_enabled : 1;
75 guint tip_area_set : 1;
76 guint custom_was_reset : 1;
79 struct _GtkTooltipClass
81 GObjectClass parent_class;
84 #define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
87 static void gtk_tooltip_class_init (GtkTooltipClass *klass);
88 static void gtk_tooltip_init (GtkTooltip *tooltip);
89 static void gtk_tooltip_dispose (GObject *object);
91 static void gtk_tooltip_window_style_set (GtkTooltip *tooltip);
92 static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip);
93 static void gtk_tooltip_window_hide (GtkWidget *widget,
95 static void gtk_tooltip_display_closed (GdkDisplay *display,
98 static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
102 G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
105 gtk_tooltip_class_init (GtkTooltipClass *klass)
107 GObjectClass *object_class;
109 object_class = G_OBJECT_CLASS (klass);
111 object_class->dispose = gtk_tooltip_dispose;
115 gtk_tooltip_init (GtkTooltip *tooltip)
117 tooltip->timeout_id = 0;
118 tooltip->browse_mode_timeout_id = 0;
120 tooltip->browse_mode_enabled = FALSE;
121 tooltip->keyboard_mode_enabled = FALSE;
123 tooltip->current_window = NULL;
124 tooltip->keyboard_widget = NULL;
126 tooltip->tooltip_widget = NULL;
127 tooltip->toplevel_window = NULL;
129 tooltip->last_window = NULL;
131 tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
132 gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
133 GDK_WINDOW_TYPE_HINT_TOOLTIP);
134 gtk_widget_set_app_paintable (tooltip->window, TRUE);
135 gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
136 gtk_widget_set_name (tooltip->window, "gtk-tooltip");
137 g_signal_connect (tooltip->window, "hide",
138 G_CALLBACK (gtk_tooltip_window_hide), tooltip);
140 tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
141 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
142 tooltip->window->style->ythickness,
143 tooltip->window->style->ythickness,
144 tooltip->window->style->xthickness,
145 tooltip->window->style->xthickness);
146 gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
147 gtk_widget_show (tooltip->alignment);
149 g_signal_connect_swapped (tooltip->window, "style-set",
150 G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
151 g_signal_connect_swapped (tooltip->window, "expose-event",
152 G_CALLBACK (gtk_tooltip_paint_window), tooltip);
154 tooltip->box = gtk_hbox_new (FALSE, tooltip->window->style->xthickness);
155 gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
156 gtk_widget_show (tooltip->box);
158 tooltip->image = gtk_image_new ();
159 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
162 tooltip->label = gtk_label_new ("");
163 gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
164 gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
167 tooltip->custom_widget = NULL;
171 gtk_tooltip_dispose (GObject *object)
173 GtkTooltip *tooltip = GTK_TOOLTIP (object);
175 if (tooltip->timeout_id)
177 g_source_remove (tooltip->timeout_id);
178 tooltip->timeout_id = 0;
181 if (tooltip->browse_mode_timeout_id)
183 g_source_remove (tooltip->browse_mode_timeout_id);
184 tooltip->browse_mode_timeout_id = 0;
187 gtk_tooltip_set_custom (tooltip, NULL);
188 gtk_tooltip_set_last_window (tooltip, NULL);
194 display = gtk_widget_get_display (tooltip->window);
195 g_signal_handlers_disconnect_by_func (display,
196 gtk_tooltip_display_closed,
198 gtk_widget_destroy (tooltip->window);
199 tooltip->window = NULL;
202 G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
208 * gtk_tooltip_set_markup:
209 * @tooltip: a #GtkTooltip
210 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
212 * Sets the text of the tooltip to be @markup, which is marked up
214 * linkend="PangoMarkupFormat">Pango text markup language</link>.
215 * If @markup is %NULL, the label will be hidden.
220 gtk_tooltip_set_markup (GtkTooltip *tooltip,
223 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
225 gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
228 gtk_widget_show (tooltip->label);
230 gtk_widget_hide (tooltip->label);
234 * gtk_tooltip_set_text:
235 * @tooltip: a #GtkTooltip
236 * @text: (allow-none): a text string or %NULL
238 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
239 * will be hidden. See also gtk_tooltip_set_markup().
244 gtk_tooltip_set_text (GtkTooltip *tooltip,
247 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
249 gtk_label_set_text (GTK_LABEL (tooltip->label), text);
252 gtk_widget_show (tooltip->label);
254 gtk_widget_hide (tooltip->label);
258 * gtk_tooltip_set_icon:
259 * @tooltip: a #GtkTooltip
260 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
262 * Sets the icon of the tooltip (which is in front of the text) to be
263 * @pixbuf. If @pixbuf is %NULL, the image will be hidden.
268 gtk_tooltip_set_icon (GtkTooltip *tooltip,
271 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
273 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
275 gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
278 gtk_widget_show (tooltip->image);
280 gtk_widget_hide (tooltip->image);
284 * gtk_tooltip_set_icon_from_stock:
285 * @tooltip: a #GtkTooltip
286 * @stock_id: (allow-none): a stock id, or %NULL
287 * @size: (type int): a stock icon size
289 * Sets the icon of the tooltip (which is in front of the text) to be
290 * the stock item indicated by @stock_id with the size indicated
291 * by @size. If @stock_id is %NULL, the image will be hidden.
296 gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
297 const gchar *stock_id,
300 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
302 gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
305 gtk_widget_show (tooltip->image);
307 gtk_widget_hide (tooltip->image);
311 * gtk_tooltip_set_icon_from_icon_name:
312 * @tooltip: a #GtkTooltip
313 * @icon_name: (allow-none): an icon name, or %NULL
314 * @size: (type int): a stock icon size
316 * Sets the icon of the tooltip (which is in front of the text) to be
317 * the icon indicated by @icon_name with the size indicated
318 * by @size. If @icon_name is %NULL, the image will be hidden.
323 gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
324 const gchar *icon_name,
327 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
329 gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
332 gtk_widget_show (tooltip->image);
334 gtk_widget_hide (tooltip->image);
338 * gtk_tooltip_set_icon_from_gicon:
339 * @tooltip: a #GtkTooltip
340 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
341 * @size: (type int): a stock icon size
343 * Sets the icon of the tooltip (which is in front of the text)
344 * to be the icon indicated by @gicon with the size indicated
345 * by @size. If @gicon is %NULL, the image will be hidden.
350 gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
354 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
356 gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
359 gtk_widget_show (tooltip->image);
361 gtk_widget_hide (tooltip->image);
365 * gtk_tooltip_set_custom:
366 * @tooltip: a #GtkTooltip
367 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
369 * Replaces the widget packed into the tooltip with
370 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
372 * By default a box with a #GtkImage and #GtkLabel is embedded in
373 * the tooltip, which can be configured using gtk_tooltip_set_markup()
374 * and gtk_tooltip_set_icon().
380 gtk_tooltip_set_custom (GtkTooltip *tooltip,
381 GtkWidget *custom_widget)
383 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
385 g_return_if_fail (GTK_IS_WIDGET (custom_widget));
387 /* The custom widget has been updated from the query-tooltip
388 * callback, so we do not want to reset the custom widget later on.
390 tooltip->custom_was_reset = TRUE;
392 /* No need to do anything if the custom widget stays the same */
393 if (tooltip->custom_widget == custom_widget)
396 if (tooltip->custom_widget)
398 GtkWidget *custom = tooltip->custom_widget;
399 /* Note: We must reset tooltip->custom_widget first,
400 * since gtk_container_remove() will recurse into
401 * gtk_tooltip_set_custom()
403 tooltip->custom_widget = NULL;
404 gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
405 g_object_unref (custom);
410 tooltip->custom_widget = g_object_ref (custom_widget);
412 gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
413 gtk_widget_show (custom_widget);
418 * gtk_tooltip_set_tip_area:
419 * @tooltip: a #GtkTooltip
420 * @rect: a #GdkRectangle
422 * Sets the area of the widget, where the contents of this tooltip apply,
423 * to be @rect (in widget coordinates). This is especially useful for
424 * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
427 * For setting tooltips on #GtkTreeView, please refer to the convenience
428 * functions for this: gtk_tree_view_set_tooltip_row() and
429 * gtk_tree_view_set_tooltip_cell().
434 gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
435 const GdkRectangle *rect)
437 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
440 tooltip->tip_area_set = FALSE;
443 tooltip->tip_area_set = TRUE;
444 tooltip->tip_area = *rect;
449 * gtk_tooltip_trigger_tooltip_query:
450 * @display: a #GdkDisplay
452 * Triggers a new tooltip query on @display, in order to update the current
453 * visible tooltip, or to show/hide the current tooltip. This function is
454 * useful to call when, for example, the state of the widget changed by a
460 gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
466 /* Trigger logic as if the mouse moved */
467 window = gdk_display_get_window_at_pointer (display, &x, &y);
471 event.type = GDK_MOTION_NOTIFY;
472 event.motion.window = window;
475 event.motion.is_hint = FALSE;
477 gdk_window_get_root_coords (window, x, y, &x, &y);
478 event.motion.x_root = x;
479 event.motion.y_root = y;
481 _gtk_tooltip_handle_event (&event);
484 /* private functions */
487 gtk_tooltip_reset (GtkTooltip *tooltip)
489 gtk_tooltip_set_markup (tooltip, NULL);
490 gtk_tooltip_set_icon (tooltip, NULL);
491 gtk_tooltip_set_tip_area (tooltip, NULL);
493 /* See if the custom widget is again set from the query-tooltip
496 tooltip->custom_was_reset = FALSE;
500 gtk_tooltip_window_style_set (GtkTooltip *tooltip)
502 gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
503 tooltip->window->style->ythickness,
504 tooltip->window->style->ythickness,
505 tooltip->window->style->xthickness,
506 tooltip->window->style->xthickness);
507 gtk_box_set_spacing (GTK_BOX (tooltip->box),
508 tooltip->window->style->xthickness);
510 gtk_widget_queue_draw (tooltip->window);
514 gtk_tooltip_paint_window (GtkTooltip *tooltip)
516 gtk_paint_flat_box (tooltip->window->style,
517 tooltip->window->window,
524 tooltip->window->allocation.width,
525 tooltip->window->allocation.height);
531 gtk_tooltip_window_hide (GtkWidget *widget,
534 GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
536 gtk_tooltip_set_custom (tooltip, NULL);
539 /* event handling, etc */
544 GtkWidget *container;
551 child_location_foreach (GtkWidget *child,
555 struct ChildLocation *child_loc = data;
557 /* Ignore invisible widgets */
558 if (!gtk_widget_is_drawable (child))
564 /* (child_loc->x, child_loc->y) are relative to
565 * child_loc->container's allocation.
568 if (!child_loc->child &&
569 gtk_widget_translate_coordinates (child_loc->container, child,
570 child_loc->x, child_loc->y,
574 g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
575 gtk_widget_get_name (child),
578 child->allocation.width,
579 child->allocation.height,
580 child_loc->x, child_loc->y,
582 #endif /* DEBUG_TOOLTIP */
584 /* (x, y) relative to child's allocation. */
585 if (x >= 0 && x < child->allocation.width
586 && y >= 0 && y < child->allocation.height)
588 if (GTK_IS_CONTAINER (child))
590 struct ChildLocation tmp = { NULL, NULL, 0, 0 };
592 /* Take (x, y) relative the child's allocation and
597 tmp.container = child;
599 gtk_container_forall (GTK_CONTAINER (child),
600 child_location_foreach, &tmp);
603 child_loc->child = tmp.child;
605 child_loc->child = child;
608 child_loc->child = child;
613 /* Translates coordinates from dest_widget->window relative (src_x, src_y),
614 * to allocation relative (dest_x, dest_y) of dest_widget.
617 window_to_alloc (GtkWidget *dest_widget,
623 /* Translate from window relative to allocation relative */
624 if (gtk_widget_get_has_window (dest_widget) && dest_widget->parent)
627 gdk_window_get_position (dest_widget->window, &wx, &wy);
629 /* Offset coordinates if widget->window is smaller than
630 * widget->allocation.
632 src_x += wx - dest_widget->allocation.x;
633 src_y += wy - dest_widget->allocation.y;
637 src_x -= dest_widget->allocation.x;
638 src_y -= dest_widget->allocation.y;
647 /* Translates coordinates from window relative (x, y) to
648 * allocation relative (x, y) of the returned widget.
651 find_widget_under_pointer (GdkWindow *window,
655 GtkWidget *event_widget;
656 struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
658 gdk_window_get_user_data (window, (void **)&event_widget);
664 g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
665 window, event_widget, gtk_widget_get_name (event_widget),
669 /* Coordinates are relative to event window */
673 /* We go down the window hierarchy to the widget->window,
674 * coordinates stay relative to the current window.
675 * We end up with window == widget->window, coordinates relative to that.
677 while (window && window != event_widget->window)
681 gdk_window_coords_to_parent (window,
682 child_loc.x, child_loc.y,
687 window = gdk_window_get_effective_parent (window);
690 /* Failing to find widget->window can happen for e.g. a detached handle box;
691 * chaining ::query-tooltip up to its parent probably makes little sense,
692 * and users better implement tooltips on handle_box->child.
693 * so we simply ignore the event for tooltips here.
698 /* Convert the window relative coordinates to allocation
699 * relative coordinates.
701 window_to_alloc (event_widget,
702 child_loc.x, child_loc.y,
703 &child_loc.x, &child_loc.y);
705 if (GTK_IS_CONTAINER (event_widget))
707 GtkWidget *container = event_widget;
709 child_loc.container = event_widget;
710 child_loc.child = NULL;
712 gtk_container_forall (GTK_CONTAINER (event_widget),
713 child_location_foreach, &child_loc);
715 /* Here we have a widget, with coordinates relative to
716 * child_loc.container's allocation.
720 event_widget = child_loc.child;
721 else if (child_loc.container)
722 event_widget = child_loc.container;
724 /* Translate to event_widget's allocation */
725 gtk_widget_translate_coordinates (container, event_widget,
726 child_loc.x, child_loc.y,
727 &child_loc.x, &child_loc.y);
731 /* We return (x, y) relative to the allocation of event_widget. */
740 /* Ignores (x, y) on input, translates event coordinates to
741 * allocation relative (x, y) of the returned widget.
744 find_topmost_widget_coords_from_event (GdkEvent *event,
752 gdk_event_get_coords (event, &dx, &dy);
756 /* Returns coordinates relative to tmp's allocation. */
757 tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
762 /* Make sure the pointer can actually be on the widget returned. */
763 if (tx < 0 || tx >= tmp->allocation.width ||
764 ty < 0 || ty >= tmp->allocation.height)
776 tooltip_browse_mode_expired (gpointer data)
780 tooltip = GTK_TOOLTIP (data);
782 tooltip->browse_mode_enabled = FALSE;
783 tooltip->browse_mode_timeout_id = 0;
785 /* destroy tooltip */
786 g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
787 "gdk-display-current-tooltip", NULL);
793 gtk_tooltip_display_closed (GdkDisplay *display,
797 g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
801 gtk_tooltip_set_last_window (GtkTooltip *tooltip,
804 if (tooltip->last_window == window)
807 if (tooltip->last_window)
808 g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
809 (gpointer *) &tooltip->last_window);
811 tooltip->last_window = window;
814 g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
815 (gpointer *) &tooltip->last_window);
819 gtk_tooltip_run_requery (GtkWidget **widget,
824 gboolean has_tooltip = FALSE;
825 gboolean return_value = FALSE;
827 gtk_tooltip_reset (tooltip);
831 g_object_get (*widget,
832 "has-tooltip", &has_tooltip,
836 g_signal_emit_by_name (*widget,
839 tooltip->keyboard_mode_enabled,
845 GtkWidget *parent = (*widget)->parent;
848 gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);
857 /* If the custom widget was not reset in the query-tooltip
858 * callback, we clear it here.
860 if (!tooltip->custom_was_reset)
861 gtk_tooltip_set_custom (tooltip, NULL);
867 get_bounding_box (GtkWidget *widget,
868 GdkRectangle *bounds)
878 window = gtk_widget_get_parent_window (widget);
880 x = widget->allocation.x;
881 y = widget->allocation.y;
882 w = widget->allocation.width;
883 h = widget->allocation.height;
885 gdk_window_get_root_coords (window, x, y, &x1, &y1);
886 gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
887 gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
888 gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);
890 #define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
891 #define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))
893 bounds->x = floor (MIN4 (x1, x2, x3, x4));
894 bounds->y = floor (MIN4 (y1, y2, y3, y4));
895 bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
896 bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
900 gtk_tooltip_position (GtkTooltip *tooltip,
902 GtkWidget *new_tooltip_widget)
907 GdkRectangle monitor;
908 GtkRequisition requisition;
912 #define MAX_DISTANCE 32
914 tooltip->tooltip_widget = new_tooltip_widget;
916 screen = gtk_widget_get_screen (new_tooltip_widget);
918 gtk_widget_size_request (GTK_WIDGET (tooltip->current_window), &requisition);
920 monitor_num = gdk_screen_get_monitor_at_point (screen,
923 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
925 get_bounding_box (new_tooltip_widget, &bounds);
927 /* Position the tooltip */
929 cursor_size = gdk_display_get_default_cursor_size (display);
932 x = bounds.x + bounds.width / 2 - requisition.width / 2;
933 y = bounds.y + bounds.height + 4;
935 if (y + requisition.height <= monitor.y + monitor.height)
937 if (tooltip->keyboard_mode_enabled)
940 if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
942 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
943 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
944 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
945 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
952 x = bounds.x + bounds.width / 2 - requisition.width / 2;
953 y = bounds.y - requisition.height - 4;
957 if (tooltip->keyboard_mode_enabled)
960 if (y + requisition.height >= tooltip->last_y - MAX_DISTANCE)
962 if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
963 x = tooltip->last_x + cursor_size + MAX_DISTANCE;
964 else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
965 x = tooltip->last_x - MAX_DISTANCE - requisition.width;
971 /* Try right FIXME: flip on rtl ? */
972 x = bounds.x + bounds.width + 4;
973 y = bounds.y + bounds.height / 2 - requisition.height / 2;
975 if (x + requisition.width <= monitor.x + monitor.width)
977 if (tooltip->keyboard_mode_enabled)
980 if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
982 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
983 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
984 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
985 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
991 /* Try left FIXME: flip on rtl ? */
992 x = bounds.x - requisition.width - 4;
993 y = bounds.y + bounds.height / 2 - requisition.height / 2;
997 if (tooltip->keyboard_mode_enabled)
1000 if (x + requisition.width >= tooltip->last_x - MAX_DISTANCE)
1002 if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
1003 y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1004 else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
1005 y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1012 if (tooltip->keyboard_mode_enabled)
1014 x = bounds.x + bounds.width / 2 - requisition.width / 2;
1015 y = bounds.y + bounds.height + 4;
1020 x = tooltip->last_x + cursor_size * 3 / 4;
1021 y = tooltip->last_y + cursor_size * 3 / 4;
1026 if (tooltip->current_window)
1028 if (x + requisition.width > monitor.x + monitor.width)
1029 x -= x - (monitor.x + monitor.width) + requisition.width;
1030 else if (x < monitor.x)
1033 if (y + requisition.height > monitor.y + monitor.height)
1034 y -= y - (monitor.y + monitor.height) + requisition.height;
1035 else if (y < monitor.y)
1038 if (!tooltip->keyboard_mode_enabled)
1040 /* don't pop up under the pointer */
1041 if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
1042 y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
1043 y = tooltip->last_y - requisition.height - 2;
1046 gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
1047 gtk_widget_show (GTK_WIDGET (tooltip->current_window));
1052 gtk_tooltip_show_tooltip (GdkDisplay *display)
1058 GtkWidget *tooltip_widget;
1059 GtkWidget *pointer_widget;
1060 GtkTooltip *tooltip;
1061 gboolean has_tooltip;
1062 gboolean return_value = FALSE;
1064 tooltip = g_object_get_data (G_OBJECT (display),
1065 "gdk-display-current-tooltip");
1067 if (tooltip->keyboard_mode_enabled)
1070 pointer_widget = tooltip_widget = tooltip->keyboard_widget;
1076 window = tooltip->last_window;
1078 if (!GDK_IS_WINDOW (window))
1081 gdk_window_get_pointer (window, &x, &y, NULL);
1083 gdk_window_get_root_coords (window, x, y, &tx, &ty);
1084 tooltip->last_x = tx;
1085 tooltip->last_y = ty;
1087 pointer_widget = tooltip_widget = find_widget_under_pointer (window,
1091 if (!tooltip_widget)
1094 g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
1096 return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
1100 if (!tooltip->current_window)
1102 if (gtk_widget_get_tooltip_window (tooltip_widget))
1103 tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
1105 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1108 screen = gtk_widget_get_screen (tooltip_widget);
1110 /* FIXME: should use tooltip->current_window iso tooltip->window */
1111 if (screen != gtk_widget_get_screen (tooltip->window))
1113 g_signal_handlers_disconnect_by_func (display,
1114 gtk_tooltip_display_closed,
1117 gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);
1119 g_signal_connect (display, "closed",
1120 G_CALLBACK (gtk_tooltip_display_closed), tooltip);
1123 gtk_tooltip_position (tooltip, display, tooltip_widget);
1125 /* Now a tooltip is visible again on the display, make sure browse
1128 tooltip->browse_mode_enabled = TRUE;
1129 if (tooltip->browse_mode_timeout_id)
1131 g_source_remove (tooltip->browse_mode_timeout_id);
1132 tooltip->browse_mode_timeout_id = 0;
1137 gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
1142 if (tooltip->timeout_id)
1144 g_source_remove (tooltip->timeout_id);
1145 tooltip->timeout_id = 0;
1148 if (!GTK_TOOLTIP_VISIBLE (tooltip))
1151 tooltip->tooltip_widget = NULL;
1153 if (!tooltip->keyboard_mode_enabled)
1156 GtkSettings *settings;
1158 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1160 g_object_get (settings,
1161 "gtk-tooltip-browse-mode-timeout", &timeout,
1164 /* The tooltip is gone, after (by default, should be configurable) 500ms
1165 * we want to turn off browse mode
1167 if (!tooltip->browse_mode_timeout_id)
1168 tooltip->browse_mode_timeout_id =
1169 gdk_threads_add_timeout_full (0, timeout,
1170 tooltip_browse_mode_expired,
1171 g_object_ref (tooltip),
1176 if (tooltip->browse_mode_timeout_id)
1178 g_source_remove (tooltip->browse_mode_timeout_id);
1179 tooltip->browse_mode_timeout_id = 0;
1183 if (tooltip->current_window)
1185 gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
1186 tooltip->current_window = NULL;
1191 tooltip_popup_timeout (gpointer data)
1193 GdkDisplay *display;
1194 GtkTooltip *tooltip;
1196 display = GDK_DISPLAY_OBJECT (data);
1197 tooltip = g_object_get_data (G_OBJECT (display),
1198 "gdk-display-current-tooltip");
1200 /* This usually does not happen. However, it does occur in language
1201 * bindings were reference counting of objects behaves differently.
1206 gtk_tooltip_show_tooltip (display);
1208 tooltip->timeout_id = 0;
1214 gtk_tooltip_start_delay (GdkDisplay *display)
1217 GtkTooltip *tooltip;
1218 GtkSettings *settings;
1220 tooltip = g_object_get_data (G_OBJECT (display),
1221 "gdk-display-current-tooltip");
1223 if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
1226 if (tooltip->timeout_id)
1227 g_source_remove (tooltip->timeout_id);
1229 settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));
1231 if (tooltip->browse_mode_enabled)
1232 g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
1234 g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
1236 tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
1237 tooltip_popup_timeout,
1238 g_object_ref (display),
1243 _gtk_tooltip_focus_in (GtkWidget *widget)
1246 gboolean return_value = FALSE;
1247 GdkDisplay *display;
1248 GtkTooltip *tooltip;
1251 /* Get current tooltip for this display */
1252 display = gtk_widget_get_display (widget);
1253 tooltip = g_object_get_data (G_OBJECT (display),
1254 "gdk-display-current-tooltip");
1256 /* Check if keyboard mode is enabled at this moment */
1257 if (!tooltip || !tooltip->keyboard_mode_enabled)
1260 device = gtk_get_current_event_device ();
1262 if (device && device->source == GDK_SOURCE_KEYBOARD)
1263 device = gdk_device_get_associated_device (device);
1265 /* This function should be called by either a focus in event,
1266 * or a key binding. In either case there should be a device.
1271 if (tooltip->keyboard_widget)
1272 g_object_unref (tooltip->keyboard_widget);
1274 tooltip->keyboard_widget = g_object_ref (widget);
1276 gdk_window_get_device_position (widget->window, device, &x, &y, NULL);
1278 return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
1281 gtk_tooltip_hide_tooltip (tooltip);
1285 if (!tooltip->current_window)
1287 if (gtk_widget_get_tooltip_window (widget))
1288 tooltip->current_window = gtk_widget_get_tooltip_window (widget);
1290 tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
1293 gtk_tooltip_show_tooltip (display);
1297 _gtk_tooltip_focus_out (GtkWidget *widget)
1299 GdkDisplay *display;
1300 GtkTooltip *tooltip;
1302 /* Get current tooltip for this display */
1303 display = gtk_widget_get_display (widget);
1304 tooltip = g_object_get_data (G_OBJECT (display),
1305 "gdk-display-current-tooltip");
1307 if (!tooltip || !tooltip->keyboard_mode_enabled)
1310 if (tooltip->keyboard_widget)
1312 g_object_unref (tooltip->keyboard_widget);
1313 tooltip->keyboard_widget = NULL;
1316 gtk_tooltip_hide_tooltip (tooltip);
1320 _gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
1322 GdkDisplay *display;
1323 GtkTooltip *tooltip;
1325 display = gtk_widget_get_display (widget);
1326 tooltip = g_object_get_data (G_OBJECT (display),
1327 "gdk-display-current-tooltip");
1331 tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1332 g_object_set_data_full (G_OBJECT (display),
1333 "gdk-display-current-tooltip",
1334 tooltip, g_object_unref);
1335 g_signal_connect (display, "closed",
1336 G_CALLBACK (gtk_tooltip_display_closed),
1340 tooltip->keyboard_mode_enabled ^= 1;
1342 if (tooltip->keyboard_mode_enabled)
1344 tooltip->keyboard_widget = g_object_ref (widget);
1345 _gtk_tooltip_focus_in (widget);
1349 if (tooltip->keyboard_widget)
1351 g_object_unref (tooltip->keyboard_widget);
1352 tooltip->keyboard_widget = NULL;
1355 gtk_tooltip_hide_tooltip (tooltip);
1360 _gtk_tooltip_hide (GtkWidget *widget)
1362 GtkWidget *toplevel;
1363 GdkDisplay *display;
1364 GtkTooltip *tooltip;
1366 display = gtk_widget_get_display (widget);
1367 tooltip = g_object_get_data (G_OBJECT (display),
1368 "gdk-display-current-tooltip");
1370 if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
1373 toplevel = gtk_widget_get_toplevel (widget);
1375 if (widget == tooltip->tooltip_widget
1376 || toplevel->window == tooltip->toplevel_window)
1377 gtk_tooltip_hide_tooltip (tooltip);
1381 tooltips_enabled (GdkWindow *window)
1384 gboolean touchscreen;
1386 GtkSettings *settings;
1388 screen = gdk_drawable_get_screen (window);
1389 settings = gtk_settings_get_for_screen (screen);
1391 g_object_get (settings,
1392 "gtk-touchscreen-mode", &touchscreen,
1393 "gtk-enable-tooltips", &enabled,
1396 return (!touchscreen && enabled);
1400 _gtk_tooltip_handle_event (GdkEvent *event)
1403 gboolean return_value = FALSE;
1404 GtkWidget *has_tooltip_widget = NULL;
1405 GdkDisplay *display;
1406 GtkTooltip *current_tooltip;
1408 if (!tooltips_enabled (event->any.window))
1411 /* Returns coordinates relative to has_tooltip_widget's allocation. */
1412 has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1413 display = gdk_drawable_get_display (event->any.window);
1414 current_tooltip = g_object_get_data (G_OBJECT (display),
1415 "gdk-display-current-tooltip");
1417 if (current_tooltip)
1419 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1422 if (current_tooltip && current_tooltip->keyboard_mode_enabled)
1424 has_tooltip_widget = current_tooltip->keyboard_widget;
1425 if (!has_tooltip_widget)
1428 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1433 gtk_tooltip_hide_tooltip (current_tooltip);
1435 gtk_tooltip_start_delay (display);
1440 #ifdef DEBUG_TOOLTIP
1441 if (has_tooltip_widget)
1442 g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
1443 has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
1444 has_tooltip_widget->allocation.x,
1445 has_tooltip_widget->allocation.y,
1446 has_tooltip_widget->allocation.width,
1447 has_tooltip_widget->allocation.height,
1449 #endif /* DEBUG_TOOLTIP */
1451 /* Always poll for a next motion event */
1452 gdk_event_request_motions (&event->motion);
1454 /* Hide the tooltip when there's no new tooltip widget */
1455 if (!has_tooltip_widget)
1457 if (current_tooltip)
1458 gtk_tooltip_hide_tooltip (current_tooltip);
1463 switch (event->type)
1465 case GDK_BUTTON_PRESS:
1466 case GDK_2BUTTON_PRESS:
1467 case GDK_3BUTTON_PRESS:
1469 case GDK_DRAG_ENTER:
1470 case GDK_GRAB_BROKEN:
1471 gtk_tooltip_hide_tooltip (current_tooltip);
1474 case GDK_MOTION_NOTIFY:
1475 case GDK_ENTER_NOTIFY:
1476 case GDK_LEAVE_NOTIFY:
1478 if (current_tooltip)
1480 gboolean tip_area_set;
1481 GdkRectangle tip_area;
1482 gboolean hide_tooltip;
1484 tip_area_set = current_tooltip->tip_area_set;
1485 tip_area = current_tooltip->tip_area;
1487 return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
1491 /* Requested to be hidden? */
1492 hide_tooltip = !return_value;
1494 /* Leave notify should override the query function */
1495 hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
1497 /* Is the pointer above another widget now? */
1498 if (GTK_TOOLTIP_VISIBLE (current_tooltip))
1499 hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
1501 /* Did the pointer move out of the previous "context area"? */
1503 hide_tooltip |= (x <= tip_area.x
1504 || x >= tip_area.x + tip_area.width
1506 || y >= tip_area.y + tip_area.height);
1509 gtk_tooltip_hide_tooltip (current_tooltip);
1511 gtk_tooltip_start_delay (display);
1515 /* Need a new tooltip for this display */
1516 current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
1517 g_object_set_data_full (G_OBJECT (display),
1518 "gdk-display-current-tooltip",
1519 current_tooltip, g_object_unref);
1520 g_signal_connect (display, "closed",
1521 G_CALLBACK (gtk_tooltip_display_closed),
1524 gtk_tooltip_set_last_window (current_tooltip, event->any.window);
1526 gtk_tooltip_start_delay (display);
1536 #define __GTK_TOOLTIP_C__
1537 #include "gtkaliasdef.c"