* Boston, MA 02111-1307, USA.
*/
-#include <config.h>
+#include "config.h"
#include "gtktooltip.h"
#include "gtkintl.h"
#include "gtkwindow.h"
#include "gtkhbox.h"
#include "gtkalignment.h"
+#include "gtkalias.h"
+
#include <string.h>
+#undef DEBUG_TOOLTIP
+
-#define GTK_TOOLTIP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_TOOLTIP, GtkTooltip))
#define GTK_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
-#define GTK_IS_TOOLTIP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_TOOLTIP))
#define GTK_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TOOLTIP))
#define GTK_TOOLTIP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TOOLTIP, GtkTooltipClass))
guint timeout_id;
guint browse_mode_timeout_id;
+ GdkRectangle tip_area;
+
guint browse_mode_enabled : 1;
guint keyboard_mode_enabled : 1;
+ guint tip_area_set : 1;
+ guint custom_was_reset : 1;
};
struct _GtkTooltipClass
static void gtk_tooltip_class_init (GtkTooltipClass *klass);
static void gtk_tooltip_init (GtkTooltip *tooltip);
-static void gtk_tooltip_finalize (GObject *object);
+static void gtk_tooltip_dispose (GObject *object);
+static void gtk_tooltip_window_style_set (GtkTooltip *tooltip);
static gboolean gtk_tooltip_paint_window (GtkTooltip *tooltip);
static void gtk_tooltip_window_hide (GtkWidget *widget,
gpointer user_data);
static void gtk_tooltip_display_closed (GdkDisplay *display,
gboolean was_error,
GtkTooltip *tooltip);
+static void gtk_tooltip_set_last_window (GtkTooltip *tooltip,
+ GdkWindow *window);
G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);
object_class = G_OBJECT_CLASS (klass);
- object_class->finalize = gtk_tooltip_finalize;
+ object_class->dispose = gtk_tooltip_dispose;
}
static void
gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
gtk_widget_show (tooltip->alignment);
- g_signal_connect_swapped (tooltip->window, "expose_event",
+ g_signal_connect_swapped (tooltip->window, "style-set",
+ G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
+ g_signal_connect_swapped (tooltip->window, "expose-event",
G_CALLBACK (gtk_tooltip_paint_window), tooltip);
tooltip->box = gtk_hbox_new (FALSE, tooltip->window->style->xthickness);
FALSE, FALSE, 0);
tooltip->label = gtk_label_new ("");
+ gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
FALSE, FALSE, 0);
}
static void
-gtk_tooltip_finalize (GObject *object)
+gtk_tooltip_dispose (GObject *object)
{
GtkTooltip *tooltip = GTK_TOOLTIP (object);
tooltip->browse_mode_timeout_id = 0;
}
+ gtk_tooltip_set_custom (tooltip, NULL);
+ gtk_tooltip_set_last_window (tooltip, NULL);
+
if (tooltip->window)
{
GdkDisplay *display;
gtk_tooltip_display_closed,
tooltip);
gtk_widget_destroy (tooltip->window);
+ tooltip->window = NULL;
}
+
+ G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
}
/* public API */
/**
* gtk_tooltip_set_markup:
- * @label: a #GtkTooltip
+ * @tooltip: a #GtkTooltip
* @markup: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
*
- * Sets the text of the tooltip to be @str, which is marked up
+ * Sets the text of the tooltip to be @markup, which is marked up
* with the <link
* linkend="PangoMarkupFormat">Pango text markup language</link>.
* If @markup is %NULL, the label will be hidden.
gtk_widget_hide (tooltip->label);
}
+/**
+ * gtk_tooltip_set_text:
+ * @tooltip: a #GtkTooltip
+ * @text: a text string or %NULL
+ *
+ * Sets the text of the tooltip to be @text. If @text is %NULL, the label
+ * will be hidden. See also gtk_tooltip_set_markup().
+ *
+ * Since: 2.12
+ */
+void
+gtk_tooltip_set_text (GtkTooltip *tooltip,
+ const gchar *text)
+{
+ g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
+
+ gtk_label_set_text (GTK_LABEL (tooltip->label), text);
+
+ if (text)
+ gtk_widget_show (tooltip->label);
+ else
+ gtk_widget_hide (tooltip->label);
+}
+
/**
* gtk_tooltip_set_icon:
* @tooltip: a #GtkTooltip
/**
* gtk_tooltip_set_icon_from_stock:
* @tooltip: a #GtkTooltip
- * @stock_id: a stock icon name, or %NULL
- * @size: a stock icon size
+ * @stock_id: a stock id, or %NULL
+ * @size: (type int): a stock icon size
*
* Sets the icon of the tooltip (which is in front of the text) to be
* the stock item indicated by @stock_id with the size indicated
gtk_widget_hide (tooltip->image);
}
+/**
+ * gtk_tooltip_set_icon_from_icon_name:
+ * @tooltip: a #GtkTooltip
+ * @icon_name: an icon name, or %NULL
+ * @size: (type int): a stock icon size
+ *
+ * Sets the icon of the tooltip (which is in front of the text) to be
+ * the icon indicated by @icon_name with the size indicated
+ * by @size. If @icon_name is %NULL, the image will be hidden.
+ *
+ * Since: 2.14
+ */
+void
+gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
+ const gchar *icon_name,
+ GtkIconSize size)
+{
+ g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
+
+ gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
+
+ if (icon_name)
+ gtk_widget_show (tooltip->image);
+ else
+ gtk_widget_hide (tooltip->image);
+}
+
+/**
+ * gtk_tooltip_set_icon_from_gicon:
+ * @tooltip: a #GtkTooltip
+ * @gicon: a #GIcon representing the icon, or %NULL
+ * @size: (type int): a stock icon size
+ *
+ * Sets the icon of the tooltip (which is in front of the text)
+ * to be the icon indicated by @gicon with the size indicated
+ * by @size. If @gicon is %NULL, the image will be hidden.
+ *
+ * Since: 2.20
+ */
+void
+gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
+ GIcon *gicon,
+ GtkIconSize size)
+{
+ g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
+
+ gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
+
+ if (gicon)
+ gtk_widget_show (tooltip->image);
+ else
+ gtk_widget_hide (tooltip->image);
+}
+
/**
* gtk_tooltip_set_custom:
- * tooltip: a #GtkTooltip
- * custom_widget: a #GtkWidget
+ * @tooltip: a #GtkTooltip
+ * @custom_widget: a #GtkWidget, or %NULL to unset the old custom widget.
*
- * Replaces the widget packed into the tooltip with @custom_widget. By
- * default a box with a #GtkImage and #GtkLabel is embedded in the tooltip,
- * which can be configured using gtk_tooltip_set_markup() and
- * gtk_tooltip_set_icon().
+ * Replaces the widget packed into the tooltip with
+ * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
+ * away.
+ * By default a box with a #GtkImage and #GtkLabel is embedded in
+ * the tooltip, which can be configured using gtk_tooltip_set_markup()
+ * and gtk_tooltip_set_icon().
+
*
* Since: 2.12
*/
if (custom_widget)
g_return_if_fail (GTK_IS_WIDGET (custom_widget));
+ /* The custom widget has been updated from the query-tooltip
+ * callback, so we do not want to reset the custom widget later on.
+ */
+ tooltip->custom_was_reset = TRUE;
+
+ /* No need to do anything if the custom widget stays the same */
+ if (tooltip->custom_widget == custom_widget)
+ return;
+
if (tooltip->custom_widget)
{
- gtk_container_remove (GTK_CONTAINER (tooltip->box),
- tooltip->custom_widget);
- g_object_unref (tooltip->custom_widget);
+ GtkWidget *custom = tooltip->custom_widget;
+ /* Note: We must reset tooltip->custom_widget first,
+ * since gtk_container_remove() will recurse into
+ * gtk_tooltip_set_custom()
+ */
+ tooltip->custom_widget = NULL;
+ gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
+ g_object_unref (custom);
}
if (custom_widget)
gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
gtk_widget_show (custom_widget);
}
+}
+
+/**
+ * gtk_tooltip_set_tip_area:
+ * @tooltip: a #GtkTooltip
+ * @rect: a #GdkRectangle
+ *
+ * Sets the area of the widget, where the contents of this tooltip apply,
+ * to be @rect (in widget coordinates). This is especially useful for
+ * properly setting tooltips on #GtkTreeView rows and cells, #GtkIconViews,
+ * etc.
+ *
+ * For setting tooltips on #GtkTreeView, please refer to the convenience
+ * functions for this: gtk_tree_view_set_tooltip_row() and
+ * gtk_tree_view_set_tooltip_cell().
+ *
+ * Since: 2.12
+ */
+void
+gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
+ const GdkRectangle *rect)
+{
+ g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
+
+ if (!rect)
+ tooltip->tip_area_set = FALSE;
else
- tooltip->custom_widget = NULL;
+ {
+ tooltip->tip_area_set = TRUE;
+ tooltip->tip_area = *rect;
+ }
}
/**
* gtk_tooltip_trigger_tooltip_query:
- * @display: a #GtkDisplay
+ * @display: a #GdkDisplay
*
* Triggers a new tooltip query on @display, in order to update the current
* visible tooltip, or to show/hide the current tooltip. This function is
event.motion.y = y;
event.motion.is_hint = FALSE;
+ gdk_window_get_origin (window, &x, &y);
+ event.motion.x_root = event.motion.x + x;
+ event.motion.y_root = event.motion.y + y;
+
_gtk_tooltip_handle_event (&event);
}
{
gtk_tooltip_set_markup (tooltip, NULL);
gtk_tooltip_set_icon (tooltip, NULL);
- gtk_tooltip_set_custom (tooltip, NULL);
+ gtk_tooltip_set_tip_area (tooltip, NULL);
+
+ /* See if the custom widget is again set from the query-tooltip
+ * callback.
+ */
+ tooltip->custom_was_reset = FALSE;
+}
+
+static void
+gtk_tooltip_window_style_set (GtkTooltip *tooltip)
+{
+ gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
+ tooltip->window->style->ythickness,
+ tooltip->window->style->ythickness,
+ tooltip->window->style->xthickness,
+ tooltip->window->style->xthickness);
+ gtk_box_set_spacing (GTK_BOX (tooltip->box),
+ tooltip->window->style->xthickness);
+
+ gtk_widget_queue_draw (tooltip->window);
}
static gboolean
gtk_tooltip_paint_window (GtkTooltip *tooltip)
{
- GtkRequisition req;
-
- gtk_widget_size_request (tooltip->window, &req);
gtk_paint_flat_box (tooltip->window->style,
tooltip->window->window,
GTK_STATE_NORMAL,
{
GtkTooltip *tooltip = GTK_TOOLTIP (user_data);
- if (tooltip->custom_widget)
- gtk_tooltip_set_custom (tooltip, NULL);
+ gtk_tooltip_set_custom (tooltip, NULL);
}
/* event handling, etc */
child_location_foreach (GtkWidget *child,
gpointer data)
{
+ gint x, y;
struct ChildLocation *child_loc = data;
- if (!child_loc->child)
- {
- gint x, y;
+ /* Ignore invisible widgets */
+ if (!GTK_WIDGET_DRAWABLE (child))
+ return;
+
+ x = 0;
+ y = 0;
+
+ /* (child_loc->x, child_loc->y) are relative to
+ * child_loc->container's allocation.
+ */
+ if (!child_loc->child &&
gtk_widget_translate_coordinates (child_loc->container, child,
child_loc->x, child_loc->y,
- &x, &y);
-
+ &x, &y))
+ {
+#ifdef DEBUG_TOOLTIP
+ g_print ("candidate: %s alloc=[(%d,%d) %dx%d] (%d, %d)->(%d, %d)\n",
+ gtk_widget_get_name (child),
+ child->allocation.x,
+ child->allocation.y,
+ child->allocation.width,
+ child->allocation.height,
+ child_loc->x, child_loc->y,
+ x, y);
+#endif /* DEBUG_TOOLTIP */
+
+ /* (x, y) relative to child's allocation. */
if (x >= 0 && x < child->allocation.width
&& y >= 0 && y < child->allocation.height)
{
{
struct ChildLocation tmp = { NULL, NULL, 0, 0 };
+ /* Take (x, y) relative the child's allocation and
+ * recurse.
+ */
tmp.x = x;
tmp.y = y;
tmp.container = child;
- gtk_container_foreach (GTK_CONTAINER (child),
- child_location_foreach, &tmp);
+ gtk_container_forall (GTK_CONTAINER (child),
+ child_location_foreach, &tmp);
if (tmp.child)
child_loc->child = tmp.child;
}
}
+/* Translates coordinates from dest_widget->window relative (src_x, src_y),
+ * to allocation relative (dest_x, dest_y) of dest_widget.
+ */
static void
window_to_alloc (GtkWidget *dest_widget,
gint src_x,
gint *dest_y)
{
/* Translate from window relative to allocation relative */
- if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
+ if (gtk_widget_get_has_window (dest_widget) && dest_widget->parent)
{
gint wx, wy;
gdk_window_get_position (dest_widget->window, &wx, &wy);
+ /* Offset coordinates if widget->window is smaller than
+ * widget->allocation.
+ */
src_x += wx - dest_widget->allocation.x;
src_y += wy - dest_widget->allocation.y;
}
*dest_y = src_y;
}
+/* Translates coordinates from window relative (x, y) to
+ * allocation relative (x, y) of the returned widget.
+ */
static GtkWidget *
find_widget_under_pointer (GdkWindow *window,
gint *x,
GtkWidget *event_widget;
struct ChildLocation child_loc = { NULL, NULL, 0, 0 };
+ gdk_window_get_user_data (window, (void **)&event_widget);
+
+ if (!event_widget)
+ return NULL;
+
+#ifdef DEBUG_TOOLTIP
+ g_print ("event window %p (belonging to %p (%s)) (%d, %d)\n",
+ window, event_widget, gtk_widget_get_name (event_widget),
+ *x, *y);
+#endif
+
+ /* Coordinates are relative to event window */
child_loc.x = *x;
child_loc.y = *y;
- gdk_window_get_user_data (window, (void **)&event_widget);
+ /* We go down the window hierarchy to the widget->window,
+ * coordinates stay relative to the current window.
+ * We end up with window == widget->window, coordinates relative to that.
+ */
+ while (window && window != event_widget->window)
+ {
+ gint px, py;
+
+ gdk_window_get_position (window, &px, &py);
+ child_loc.x += px;
+ child_loc.y += py;
+
+ window = gdk_window_get_parent (window);
+ }
+
+ /* Failing to find widget->window can happen for e.g. a detached handle box;
+ * chaining ::query-tooltip up to its parent probably makes little sense,
+ * and users better implement tooltips on handle_box->child.
+ * so we simply ignore the event for tooltips here.
+ */
+ if (!window)
+ return NULL;
+
+ /* Convert the window relative coordinates to allocation
+ * relative coordinates.
+ */
+ window_to_alloc (event_widget,
+ child_loc.x, child_loc.y,
+ &child_loc.x, &child_loc.y);
+
if (GTK_IS_CONTAINER (event_widget))
{
- window_to_alloc (event_widget,
- child_loc.x, child_loc.y,
- &child_loc.x, &child_loc.y);
+ GtkWidget *container = event_widget;
child_loc.container = event_widget;
child_loc.child = NULL;
- gtk_container_foreach (GTK_CONTAINER (event_widget),
- child_location_foreach, &child_loc);
+ gtk_container_forall (GTK_CONTAINER (event_widget),
+ child_location_foreach, &child_loc);
+
+ /* Here we have a widget, with coordinates relative to
+ * child_loc.container's allocation.
+ */
if (child_loc.child)
event_widget = child_loc.child;
else if (child_loc.container)
event_widget = child_loc.container;
+
+ /* Translate to event_widget's allocation */
+ gtk_widget_translate_coordinates (container, event_widget,
+ child_loc.x, child_loc.y,
+ &child_loc.x, &child_loc.y);
+
}
+ /* We return (x, y) relative to the allocation of event_widget. */
if (x)
*x = child_loc.x;
if (y)
return event_widget;
}
+/* Ignores (x, y) on input, translates event coordinates to
+ * allocation relative (x, y) of the returned widget.
+ */
static GtkWidget *
find_topmost_widget_coords_from_event (GdkEvent *event,
gint *x,
gint tx, ty;
gdouble dx, dy;
GtkWidget *tmp;
- GtkWidget *orig;
- gboolean has_tooltip;
gdk_event_get_coords (event, &dx, &dy);
tx = dx;
ty = dy;
- orig = tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
+ /* Returns coordinates relative to tmp's allocation. */
+ tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
- g_object_get (tmp, "has-tooltip", &has_tooltip, NULL);
+ if (!tmp)
+ return NULL;
- if (tmp && (x != NULL || y != NULL))
- {
- if (tmp != orig)
- gtk_widget_translate_coordinates (orig, tmp, tx, ty, x, y);
- else
- {
- if (x)
- *x = tx;
- if (y)
- *y = ty;
- }
- }
+ /* Make sure the pointer can actually be on the widget returned. */
+ if (tx < 0 || tx >= tmp->allocation.width ||
+ ty < 0 || ty >= tmp->allocation.height)
+ return NULL;
+
+ if (x)
+ *x = tx;
+ if (y)
+ *y = ty;
return tmp;
}
{
GtkTooltip *tooltip;
- GDK_THREADS_ENTER ();
-
tooltip = GTK_TOOLTIP (data);
tooltip->browse_mode_enabled = FALSE;
g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
"gdk-display-current-tooltip", NULL);
- GDK_THREADS_LEAVE ();
-
return FALSE;
}
gboolean was_error,
GtkTooltip *tooltip)
{
- g_object_set (display, "gdk-display-current-tooltip", NULL);
+ g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
+}
+
+static void
+gtk_tooltip_set_last_window (GtkTooltip *tooltip,
+ GdkWindow *window)
+{
+ if (tooltip->last_window == window)
+ return;
+
+ if (tooltip->last_window)
+ g_object_remove_weak_pointer (G_OBJECT (tooltip->last_window),
+ (gpointer *) &tooltip->last_window);
+
+ tooltip->last_window = window;
+
+ if (window)
+ g_object_add_weak_pointer (G_OBJECT (tooltip->last_window),
+ (gpointer *) &tooltip->last_window);
}
static gboolean
}
while (*widget);
+ /* If the custom widget was not reset in the query-tooltip
+ * callback, we clear it here.
+ */
+ if (!tooltip->custom_was_reset)
+ gtk_tooltip_set_custom (tooltip, NULL);
+
return return_value;
}
+static void
+gtk_tooltip_position (GtkTooltip *tooltip,
+ GdkDisplay *display,
+ GtkWidget *new_tooltip_widget)
+{
+ gint x, y;
+ GdkScreen *screen;
+
+ tooltip->tooltip_widget = new_tooltip_widget;
+
+ /* Position the tooltip */
+ /* FIXME: should we swap this when RTL is enabled? */
+ if (tooltip->keyboard_mode_enabled)
+ {
+ gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
+ if (!gtk_widget_get_has_window (new_tooltip_widget))
+ {
+ x += new_tooltip_widget->allocation.x;
+ y += new_tooltip_widget->allocation.y;
+ }
+
+ /* For keyboard mode we position the tooltip below the widget,
+ * right of the center of the widget.
+ */
+ x += new_tooltip_widget->allocation.width / 2;
+ y += new_tooltip_widget->allocation.height + 4;
+ }
+ else
+ {
+ guint cursor_size;
+
+ x = tooltip->last_x;
+ y = tooltip->last_y;
+
+ /* For mouse mode, we position the tooltip right of the cursor,
+ * a little below the cursor's center.
+ */
+ cursor_size = gdk_display_get_default_cursor_size (display);
+ x += cursor_size / 2;
+ y += cursor_size / 2;
+ }
+
+ screen = gtk_widget_get_screen (new_tooltip_widget);
+
+ /* Show it */
+ if (tooltip->current_window)
+ {
+ gint monitor_num;
+ GdkRectangle monitor;
+ GtkRequisition requisition;
+
+ gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
+ &requisition);
+
+ monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
+ gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
+
+ if (x + requisition.width > monitor.x + monitor.width)
+ x -= x - (monitor.x + monitor.width) + requisition.width;
+ else if (x < monitor.x)
+ x = monitor.x;
+
+ if (y + requisition.height > monitor.y + monitor.height)
+ y -= y - (monitor.y + monitor.height) + requisition.height;
+
+ if (!tooltip->keyboard_mode_enabled)
+ {
+ /* don't pop up under the pointer */
+ if (x <= tooltip->last_x && tooltip->last_x < x + requisition.width &&
+ y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
+ y = tooltip->last_y - requisition.height - 2;
+ }
+
+ gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
+ gtk_widget_show (GTK_WIDGET (tooltip->current_window));
+ }
+}
+
static void
gtk_tooltip_show_tooltip (GdkDisplay *display)
{
gint x, y;
- gint w, h;
- gint monitor_num;
GdkScreen *screen;
- GdkRectangle monitor;
GdkWindow *window;
GtkWidget *tooltip_widget;
if (tooltip->keyboard_mode_enabled)
{
+ x = y = -1;
pointer_widget = tooltip_widget = tooltip->keyboard_widget;
}
else
{
window = tooltip->last_window;
+ if (!GDK_IS_WINDOW (window))
+ return;
+
gdk_window_get_origin (window, &x, &y);
x = tooltip->last_x - x;
y = tooltip->last_y - y;
- if (!window)
- return;
-
pointer_widget = tooltip_widget = find_widget_under_pointer (window,
&x, &y);
}
g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);
- g_assert (tooltip != NULL);
-
return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
if (!return_value)
return;
tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
}
- /* Position the tooltip */
- /* FIXME: should we swap this when RTL is enabled? */
- if (tooltip->keyboard_mode_enabled)
- {
- gdk_window_get_origin (tooltip_widget->window, &x, &y);
- if (GTK_WIDGET_NO_WINDOW (tooltip_widget))
- {
- x += tooltip_widget->allocation.x;
- y += tooltip_widget->allocation.y;
- }
-
- /* For keyboard mode we position the tooltip below the widget,
- * right of the center of the widget.
- */
- x += tooltip_widget->allocation.width / 2;
- y += tooltip_widget->allocation.height + 4;
- }
- else
- {
- guint cursor_size;
-
- x = tooltip->last_x;
- y = tooltip->last_y;
-
- /* For mouse mode, we position the tooltip right of the cursor,
- * a little below the cursor's center.
- */
- cursor_size = gdk_display_get_default_cursor_size (display);
- x += cursor_size / 2;
- y += cursor_size / 2;
- }
-
- if (tooltip->current_window)
- {
- GtkRequisition requisition;
-
- gtk_widget_size_request (GTK_WIDGET (tooltip->current_window), &requisition);
- w = requisition.width;
- h = requisition.height;
- }
-
screen = gtk_widget_get_screen (tooltip_widget);
+ /* FIXME: should use tooltip->current_window iso tooltip->window */
if (screen != gtk_widget_get_screen (tooltip->window))
{
g_signal_handlers_disconnect_by_func (display,
G_CALLBACK (gtk_tooltip_display_closed), tooltip);
}
- tooltip->tooltip_widget = tooltip_widget;
-
- monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
- gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
-
- if (x + w > monitor.x + monitor.width)
- x -= x - (monitor.x + monitor.width) + w;
- else if (x < monitor.x)
- x = monitor.x;
-
- if (y + h > monitor.y + monitor.height)
- y -= y - (monitor.y + monitor.height) + h;
-
- /* Show it */
- if (tooltip->current_window)
- {
- gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
- gtk_widget_show (GTK_WIDGET (tooltip->current_window));
- }
+ gtk_tooltip_position (tooltip, display, tooltip_widget);
/* Now a tooltip is visible again on the display, make sure browse
* mode is enabled.
static void
gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
{
- if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip))
+ if (!tooltip)
return;
- tooltip->tooltip_widget = NULL;
-
if (tooltip->timeout_id)
{
g_source_remove (tooltip->timeout_id);
tooltip->timeout_id = 0;
}
+ if (!GTK_TOOLTIP_VISIBLE (tooltip))
+ return;
+
+ tooltip->tooltip_widget = NULL;
+
if (!tooltip->keyboard_mode_enabled)
{
guint timeout;
*/
if (!tooltip->browse_mode_timeout_id)
tooltip->browse_mode_timeout_id =
- g_timeout_add_full (0, timeout,
- tooltip_browse_mode_expired,
- g_object_ref (tooltip),
- g_object_unref);
+ gdk_threads_add_timeout_full (0, timeout,
+ tooltip_browse_mode_expired,
+ g_object_ref (tooltip),
+ g_object_unref);
}
else
{
GdkDisplay *display;
GtkTooltip *tooltip;
- GDK_THREADS_ENTER ();
-
display = GDK_DISPLAY_OBJECT (data);
+ tooltip = g_object_get_data (G_OBJECT (display),
+ "gdk-display-current-tooltip");
+
+ /* This usually does not happen. However, it does occur in language
+ * bindings were reference counting of objects behaves differently.
+ */
+ if (!tooltip)
+ return FALSE;
gtk_tooltip_show_tooltip (display);
- tooltip = g_object_get_data (G_OBJECT (display),
- "gdk-display-current-tooltip");
tooltip->timeout_id = 0;
- GDK_THREADS_LEAVE ();
-
return FALSE;
}
tooltip = g_object_get_data (G_OBJECT (display),
"gdk-display-current-tooltip");
- if (tooltip && GTK_TOOLTIP_VISIBLE (tooltip))
+ if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
return;
if (tooltip->timeout_id)
else
g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
- tooltip->timeout_id = g_timeout_add_full (0, timeout,
- tooltip_popup_timeout,
- g_object_ref (display),
- g_object_unref);
+ tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
+ tooltip_popup_timeout,
+ g_object_ref (display),
+ g_object_unref);
}
void
g_object_set_data_full (G_OBJECT (display),
"gdk-display-current-tooltip",
tooltip, g_object_unref);
+ g_signal_connect (display, "closed",
+ G_CALLBACK (gtk_tooltip_display_closed),
+ tooltip);
}
tooltip->keyboard_mode_enabled ^= 1;
gtk_tooltip_hide_tooltip (tooltip);
}
+static gboolean
+tooltips_enabled (GdkWindow *window)
+{
+ gboolean enabled;
+ gboolean touchscreen;
+ GdkScreen *screen;
+ GtkSettings *settings;
+
+ screen = gdk_drawable_get_screen (window);
+ settings = gtk_settings_get_for_screen (screen);
+
+ g_object_get (settings,
+ "gtk-touchscreen-mode", &touchscreen,
+ "gtk-enable-tooltips", &enabled,
+ NULL);
+
+ return (!touchscreen && enabled);
+}
+
void
_gtk_tooltip_handle_event (GdkEvent *event)
{
GdkDisplay *display;
GtkTooltip *current_tooltip;
+ if (!tooltips_enabled (event->any.window))
+ return;
+
+ /* Returns coordinates relative to has_tooltip_widget's allocation. */
has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
display = gdk_drawable_get_display (event->any.window);
current_tooltip = g_object_get_data (G_OBJECT (display),
if (current_tooltip)
{
- current_tooltip->last_window = event->any.window;
+ gtk_tooltip_set_last_window (current_tooltip, event->any.window);
gdk_event_get_root_coords (event,
¤t_tooltip->last_x,
¤t_tooltip->last_y);
return;
}
+#ifdef DEBUG_TOOLTIP
+ if (has_tooltip_widget)
+ g_print ("%p (%s) at (%d, %d) %dx%d pointer: (%d, %d)\n",
+ has_tooltip_widget, gtk_widget_get_name (has_tooltip_widget),
+ has_tooltip_widget->allocation.x,
+ has_tooltip_widget->allocation.y,
+ has_tooltip_widget->allocation.width,
+ has_tooltip_widget->allocation.height,
+ x, y);
+#endif /* DEBUG_TOOLTIP */
+
/* Always poll for a next motion event */
- if (event->type == GDK_MOTION_NOTIFY && event->motion.is_hint)
- gdk_window_get_pointer (event->any.window, NULL, NULL, NULL);
+ gdk_event_request_motions (&event->motion);
/* Hide the tooltip when there's no new tooltip widget */
if (!has_tooltip_widget)
{
- if (current_tooltip && GTK_TOOLTIP_VISIBLE (current_tooltip))
+ if (current_tooltip)
gtk_tooltip_hide_tooltip (current_tooltip);
return;
case GDK_SCROLL:
if (current_tooltip)
{
+ gboolean tip_area_set;
+ GdkRectangle tip_area;
+ gboolean hide_tooltip;
+
+ tip_area_set = current_tooltip->tip_area_set;
+ tip_area = current_tooltip->tip_area;
+
return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
current_tooltip,
&x, &y);
- if (!return_value)
+ /* Requested to be hidden? */
+ hide_tooltip = !return_value;
+
+ /* Leave notify should override the query function */
+ hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);
+
+ /* Is the pointer above another widget now? */
+ if (GTK_TOOLTIP_VISIBLE (current_tooltip))
+ hide_tooltip |= has_tooltip_widget != current_tooltip->tooltip_widget;
+
+ /* Did the pointer move out of the previous "context area"? */
+ if (tip_area_set)
+ hide_tooltip |= (x <= tip_area.x
+ || x >= tip_area.x + tip_area.width
+ || y <= tip_area.y
+ || y >= tip_area.y + tip_area.height);
+
+ if (hide_tooltip)
gtk_tooltip_hide_tooltip (current_tooltip);
else
gtk_tooltip_start_delay (display);
g_object_set_data_full (G_OBJECT (display),
"gdk-display-current-tooltip",
current_tooltip, g_object_unref);
+ g_signal_connect (display, "closed",
+ G_CALLBACK (gtk_tooltip_display_closed),
+ current_tooltip);
- current_tooltip->last_window = event->any.window;
+ gtk_tooltip_set_last_window (current_tooltip, event->any.window);
gdk_event_get_root_coords (event,
¤t_tooltip->last_x,
¤t_tooltip->last_y);
break;
}
}
+
+
+#define __GTK_TOOLTIP_C__
+#include "gtkaliasdef.c"