gtktooltip.c 43.3 KB
Newer Older
Kristian Rietveld's avatar
Kristian Rietveld committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* gtktooltip.c
 *
 * Copyright (C) 2006-2007 Imendio AB
 * Contact: Kristian Rietveld <kris@imendio.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

22
#include "config.h"
23

24 25
#include "gtktooltip.h"

26 27 28
#include <math.h>
#include <string.h>

Kristian Rietveld's avatar
Kristian Rietveld committed
29 30 31 32 33 34 35
#include "gtkintl.h"
#include "gtkwindow.h"
#include "gtkmain.h"
#include "gtklabel.h"
#include "gtkimage.h"
#include "gtkhbox.h"
#include "gtkalignment.h"
36
#include "gtksizerequest.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
37

Matthias Clasen's avatar
Matthias Clasen committed
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/**
 * SECTION:gtktooltip
 * @Short_description: Add tips to your widgets
 * @Title: GtkTooltip
 *
 * Basic tooltips can be realized simply by using gtk_widget_set_tooltip_text()
 * or gtk_widget_set_tooltip_markup() without any explicit tooltip object.
 *
 * When you need a tooltip with a little more fancy contents, like adding an
 * image, or you want the tooltip to have different contents per #GtkTreeView
 * row or cell, you will have to do a little more work:
 * <itemizedlist>
 * <listitem>
 * <para>
 * Set the #GtkWidget:has-tooltip property to %TRUE, this will make GTK+
 * monitor the widget for motion and related events which are needed to
 * determine when and where to show a tooltip.
 * </para>
 * </listitem>
 * <listitem>
 * <para>
 * Connect to the #GtkWidget::query-tooltip signal.  This signal will be
 * emitted when a tooltip is supposed to be shown. One of the arguments passed
 * to the signal handler is a GtkTooltip object. This is the object that we
 * are about to display as a tooltip, and can be manipulated in your callback
 * using functions like gtk_tooltip_set_icon(). There are functions for setting
 * the tooltip's markup, setting an image from a stock icon, or even putting in
 * a custom widget.
 * </para>
 * </listitem>
 * <listitem>
 * <para>
 * Return %TRUE from your query-tooltip handler. This causes the tooltip to be
 * show. If you return %FALSE, it will not be shown.
 * </para>
 * </listitem>
 * </itemizedlist>
 *
 * In the probably rare case where you want to have even more control over the
 * tooltip that is about to be shown, you can set your own #GtkWindow which
 * will be used as tooltip window.  This works as follows:
 * <itemizedlist>
 * <listitem>
 * <para>
 * Set #GtkWidget:has-tooltip and connect to #GtkWidget::query-tooltip as
 * before.
 * </para>
 * </listitem>
 * <listitem>
 * <para>
 * Use gtk_widget_set_tooltip_window() to set a #GtkWindow created by you as
 * tooltip window.
 * </para>
 * </listitem>
 * <listitem>
 * <para>
 * In the #GtkWidget::query-tooltip callback you can access your window using
 * gtk_widget_get_tooltip_window() and manipulate as you wish. The semantics of
 * the return value are exactly as before, return %TRUE to show the window,
 * %FALSE to not show it.
 * </para>
 * </listitem>
 * </itemizedlist>
 */


105 106
#undef DEBUG_TOOLTIP

Kristian Rietveld's avatar
Kristian Rietveld committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

#define GTK_TOOLTIP_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOOLTIP, GtkTooltipClass))
#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))

typedef struct _GtkTooltipClass   GtkTooltipClass;

struct _GtkTooltip
{
  GObject parent_instance;

  GtkWidget *window;
  GtkWidget *alignment;
  GtkWidget *box;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *custom_widget;

  GtkWindow *current_window;
  GtkWidget *keyboard_widget;

  GtkWidget *tooltip_widget;
  GdkWindow *toplevel_window;

  gdouble last_x;
  gdouble last_y;
  GdkWindow *last_window;

  guint timeout_id;
  guint browse_mode_timeout_id;

138 139
  GdkRectangle tip_area;

Kristian Rietveld's avatar
Kristian Rietveld committed
140 141
  guint browse_mode_enabled : 1;
  guint keyboard_mode_enabled : 1;
142
  guint tip_area_set : 1;
143
  guint custom_was_reset : 1;
Kristian Rietveld's avatar
Kristian Rietveld committed
144 145 146 147 148 149 150
};

struct _GtkTooltipClass
{
  GObjectClass parent_class;
};

151
#define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
Kristian Rietveld's avatar
Kristian Rietveld committed
152 153 154 155


static void       gtk_tooltip_class_init           (GtkTooltipClass *klass);
static void       gtk_tooltip_init                 (GtkTooltip      *tooltip);
Paolo Borelli's avatar
Paolo Borelli committed
156
static void       gtk_tooltip_dispose              (GObject         *object);
Kristian Rietveld's avatar
Kristian Rietveld committed
157

158
static void       gtk_tooltip_window_style_updated (GtkTooltip      *tooltip);
159 160
static gboolean   gtk_tooltip_paint_window         (GtkTooltip      *tooltip,
                                                    cairo_t         *cr);
Kristian Rietveld's avatar
Kristian Rietveld committed
161 162 163 164 165
static void       gtk_tooltip_window_hide          (GtkWidget       *widget,
						    gpointer         user_data);
static void       gtk_tooltip_display_closed       (GdkDisplay      *display,
						    gboolean         was_error,
						    GtkTooltip      *tooltip);
166 167
static void       gtk_tooltip_set_last_window      (GtkTooltip      *tooltip,
						    GdkWindow       *window);
Kristian Rietveld's avatar
Kristian Rietveld committed
168 169 170 171 172 173 174 175 176 177 178


G_DEFINE_TYPE (GtkTooltip, gtk_tooltip, G_TYPE_OBJECT);

static void
gtk_tooltip_class_init (GtkTooltipClass *klass)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (klass);

Paolo Borelli's avatar
Paolo Borelli committed
179
  object_class->dispose = gtk_tooltip_dispose;
Kristian Rietveld's avatar
Kristian Rietveld committed
180 181 182 183 184
}

static void
gtk_tooltip_init (GtkTooltip *tooltip)
{
185 186
  GtkStyleContext *context;
  GtkBorder padding, border;
187

Kristian Rietveld's avatar
Kristian Rietveld committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  tooltip->timeout_id = 0;
  tooltip->browse_mode_timeout_id = 0;

  tooltip->browse_mode_enabled = FALSE;
  tooltip->keyboard_mode_enabled = FALSE;

  tooltip->current_window = NULL;
  tooltip->keyboard_widget = NULL;

  tooltip->tooltip_widget = NULL;
  tooltip->toplevel_window = NULL;

  tooltip->last_window = NULL;

  tooltip->window = g_object_ref (gtk_window_new (GTK_WINDOW_POPUP));
  gtk_window_set_type_hint (GTK_WINDOW (tooltip->window),
			    GDK_WINDOW_TYPE_HINT_TOOLTIP);
  gtk_widget_set_app_paintable (tooltip->window, TRUE);
  gtk_window_set_resizable (GTK_WINDOW (tooltip->window), FALSE);
  gtk_widget_set_name (tooltip->window, "gtk-tooltip");
  g_signal_connect (tooltip->window, "hide",
		    G_CALLBACK (gtk_tooltip_window_hide), tooltip);

211 212 213 214 215
  context = gtk_widget_get_style_context (tooltip->window);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);

  gtk_style_context_get_padding (context, 0, &padding);
  gtk_style_context_get_border (context, 0, &border);
216

Kristian Rietveld's avatar
Kristian Rietveld committed
217 218
  tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
219 220 221 222
                             border.top + padding.top,
                             border.bottom + padding.bottom,
                             border.left + padding.left,
                             border.right + padding.right);
Kristian Rietveld's avatar
Kristian Rietveld committed
223 224 225
  gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
  gtk_widget_show (tooltip->alignment);

226 227
  g_signal_connect_swapped (tooltip->window, "style-updated",
			    G_CALLBACK (gtk_tooltip_window_style_updated), tooltip);
228
  g_signal_connect_swapped (tooltip->window, "draw",
Kristian Rietveld's avatar
Kristian Rietveld committed
229 230
			    G_CALLBACK (gtk_tooltip_paint_window), tooltip);

231
  tooltip->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, padding.left);
Kristian Rietveld's avatar
Kristian Rietveld committed
232 233 234 235 236 237 238 239
  gtk_container_add (GTK_CONTAINER (tooltip->alignment), tooltip->box);
  gtk_widget_show (tooltip->box);

  tooltip->image = gtk_image_new ();
  gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->image,
		      FALSE, FALSE, 0);

  tooltip->label = gtk_label_new ("");
240
  gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
Kristian Rietveld's avatar
Kristian Rietveld committed
241 242 243 244 245 246 247
  gtk_box_pack_start (GTK_BOX (tooltip->box), tooltip->label,
		      FALSE, FALSE, 0);

  tooltip->custom_widget = NULL;
}

static void
Paolo Borelli's avatar
Paolo Borelli committed
248
gtk_tooltip_dispose (GObject *object)
Kristian Rietveld's avatar
Kristian Rietveld committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
{
  GtkTooltip *tooltip = GTK_TOOLTIP (object);

  if (tooltip->timeout_id)
    {
      g_source_remove (tooltip->timeout_id);
      tooltip->timeout_id = 0;
    }

  if (tooltip->browse_mode_timeout_id)
    {
      g_source_remove (tooltip->browse_mode_timeout_id);
      tooltip->browse_mode_timeout_id = 0;
    }

264
  gtk_tooltip_set_custom (tooltip, NULL);
265 266
  gtk_tooltip_set_last_window (tooltip, NULL);

Kristian Rietveld's avatar
Kristian Rietveld committed
267 268 269 270 271 272 273 274 275
  if (tooltip->window)
    {
      GdkDisplay *display;

      display = gtk_widget_get_display (tooltip->window);
      g_signal_handlers_disconnect_by_func (display,
					    gtk_tooltip_display_closed,
					    tooltip);
      gtk_widget_destroy (tooltip->window);
Paolo Borelli's avatar
Paolo Borelli committed
276
      tooltip->window = NULL;
Kristian Rietveld's avatar
Kristian Rietveld committed
277
    }
Michael Natterer's avatar
Michael Natterer committed
278

Paolo Borelli's avatar
Paolo Borelli committed
279
  G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
Kristian Rietveld's avatar
Kristian Rietveld committed
280 281 282 283 284 285
}

/* public API */

/**
 * gtk_tooltip_set_markup:
Matthias Clasen's avatar
Matthias Clasen committed
286
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
287
 * @markup: (allow-none): a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
Kristian Rietveld's avatar
Kristian Rietveld committed
288
 *
Matthias Clasen's avatar
Matthias Clasen committed
289
 * Sets the text of the tooltip to be @markup, which is marked up
Kristian Rietveld's avatar
Kristian Rietveld committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
 * with the <link
 * linkend="PangoMarkupFormat">Pango text markup language</link>.
 * If @markup is %NULL, the label will be hidden.
 *
 * Since: 2.12
 */
void
gtk_tooltip_set_markup (GtkTooltip  *tooltip,
			const gchar *markup)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);

  if (markup)
    gtk_widget_show (tooltip->label);
  else
    gtk_widget_hide (tooltip->label);
}

310 311 312
/**
 * gtk_tooltip_set_text:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
313
 * @text: (allow-none): a text string or %NULL
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
 *
 * 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);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
334 335 336
/**
 * gtk_tooltip_set_icon:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
337
 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
Kristian Rietveld's avatar
Kristian Rietveld committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
 *
 * Sets the icon of the tooltip (which is in front of the text) to be
 * @pixbuf.  If @pixbuf is %NULL, the image will be hidden.
 *
 * Since: 2.12
 */
void
gtk_tooltip_set_icon (GtkTooltip *tooltip,
		      GdkPixbuf  *pixbuf)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
  if (pixbuf)
    g_return_if_fail (GDK_IS_PIXBUF (pixbuf));

  gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);

  if (pixbuf)
    gtk_widget_show (tooltip->image);
  else
    gtk_widget_hide (tooltip->image);
}

/**
 * gtk_tooltip_set_icon_from_stock:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
363
 * @stock_id: (allow-none): a stock id, or %NULL
364
 * @size: (type int): a stock icon size
Kristian Rietveld's avatar
Kristian Rietveld committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
 *
 * 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
 * by @size.  If @stock_id is %NULL, the image will be hidden.
 *
 * Since: 2.12
 */
void
gtk_tooltip_set_icon_from_stock (GtkTooltip  *tooltip,
				 const gchar *stock_id,
				 GtkIconSize  size)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);

  if (stock_id)
    gtk_widget_show (tooltip->image);
  else
    gtk_widget_hide (tooltip->image);
}

Matthias Clasen's avatar
Matthias Clasen committed
387 388 389
/**
 * gtk_tooltip_set_icon_from_icon_name:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
390
 * @icon_name: (allow-none): an icon name, or %NULL
391
 * @size: (type int): a stock icon size
Matthias Clasen's avatar
Matthias Clasen committed
392 393 394 395 396
 *
 * 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.
 *
397
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
398 399
 */
void
400 401 402
gtk_tooltip_set_icon_from_icon_name (GtkTooltip  *tooltip,
				     const gchar *icon_name,
				     GtkIconSize  size)
Matthias Clasen's avatar
Matthias Clasen committed
403 404 405 406 407 408 409
{
  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);
410 411 412 413 414
  else
    gtk_widget_hide (tooltip->image);
}

/**
415
 * gtk_tooltip_set_icon_from_gicon:
416
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
417
 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
418
 * @size: (type int): a stock icon size
419 420 421 422
 *
 * 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.
423 424 425 426 427 428 429 430 431 432 433 434 435 436
 *
 * 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);
Matthias Clasen's avatar
Matthias Clasen committed
437 438 439 440
  else
    gtk_widget_hide (tooltip->image);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
441 442
/**
 * gtk_tooltip_set_custom:
Matthias Clasen's avatar
Matthias Clasen committed
443
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
444
 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
Kristian Rietveld's avatar
Kristian Rietveld committed
445
 *
446 447 448
 * Replaces the widget packed into the tooltip with
 * @custom_widget. @custom_widget does not get destroyed when the tooltip goes
 * away.
Matthias Clasen's avatar
Matthias Clasen committed
449 450 451
 * 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().
452

Kristian Rietveld's avatar
Kristian Rietveld committed
453 454 455 456 457 458 459 460 461 462 463
 *
 * Since: 2.12
 */
void
gtk_tooltip_set_custom (GtkTooltip *tooltip,
			GtkWidget  *custom_widget)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
  if (custom_widget)
    g_return_if_fail (GTK_IS_WIDGET (custom_widget));

464 465 466 467 468 469 470 471 472
  /* 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;

Kristian Rietveld's avatar
Kristian Rietveld committed
473 474
  if (tooltip->custom_widget)
    {
475 476 477 478 479 480 481 482
      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);
Kristian Rietveld's avatar
Kristian Rietveld committed
483 484 485 486 487 488 489 490 491 492 493
    }

  if (custom_widget)
    {
      tooltip->custom_widget = g_object_ref (custom_widget);

      gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
      gtk_widget_show (custom_widget);
    }
}

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/**
 * 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
511 512
gtk_tooltip_set_tip_area (GtkTooltip         *tooltip,
			  const GdkRectangle *rect)
513 514 515 516 517 518 519 520 521 522 523 524
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  if (!rect)
    tooltip->tip_area_set = FALSE;
  else
    {
      tooltip->tip_area_set = TRUE;
      tooltip->tip_area = *rect;
    }
}

Kristian Rietveld's avatar
Kristian Rietveld committed
525 526
/**
 * gtk_tooltip_trigger_tooltip_query:
527
 * @display: a #GdkDisplay
Kristian Rietveld's avatar
Kristian Rietveld committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541
 *
 * 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
 * useful to call when, for example, the state of the widget changed by a
 * key press.
 *
 * Since: 2.12
 */
void
gtk_tooltip_trigger_tooltip_query (GdkDisplay *display)
{
  gint x, y;
  GdkWindow *window;
  GdkEvent event;
542
  GdkDevice *device;
Kristian Rietveld's avatar
Kristian Rietveld committed
543 544

  /* Trigger logic as if the mouse moved */
545
  device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
546
  window = gdk_device_get_window_at_position (device, &x, &y);
Kristian Rietveld's avatar
Kristian Rietveld committed
547 548 549 550 551 552 553 554 555
  if (!window)
    return;

  event.type = GDK_MOTION_NOTIFY;
  event.motion.window = window;
  event.motion.x = x;
  event.motion.y = y;
  event.motion.is_hint = FALSE;

556 557 558
  gdk_window_get_root_coords (window, x, y, &x, &y);
  event.motion.x_root = x;
  event.motion.y_root = y;
559

Kristian Rietveld's avatar
Kristian Rietveld committed
560 561 562 563 564 565 566 567 568 569
  _gtk_tooltip_handle_event (&event);
}

/* private functions */

static void
gtk_tooltip_reset (GtkTooltip *tooltip)
{
  gtk_tooltip_set_markup (tooltip, NULL);
  gtk_tooltip_set_icon (tooltip, NULL);
570
  gtk_tooltip_set_tip_area (tooltip, NULL);
571 572 573 574 575

  /* See if the custom widget is again set from the query-tooltip
   * callback.
   */
  tooltip->custom_was_reset = FALSE;
Kristian Rietveld's avatar
Kristian Rietveld committed
576 577
}

578
static void
579
gtk_tooltip_window_style_updated (GtkTooltip *tooltip)
580
{
581 582
  GtkStyleContext *context;
  GtkBorder padding, border;
583

584 585 586
  context = gtk_widget_get_style_context (tooltip->window);
  gtk_style_context_get_padding (context, 0, &padding);
  gtk_style_context_get_border (context, 0, &border);
587

588
  gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
589 590 591 592
                             border.top + padding.top,
                             border.bottom + padding.bottom,
                             border.left + padding.left,
                             border.right + padding.right);
593

594
  gtk_box_set_spacing (GTK_BOX (tooltip->box), padding.left);
595 596 597 598

  gtk_widget_queue_draw (tooltip->window);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
599
static gboolean
600 601
gtk_tooltip_paint_window (GtkTooltip *tooltip,
                          cairo_t    *cr)
Kristian Rietveld's avatar
Kristian Rietveld committed
602
{
603 604 605 606 607 608 609 610 611 612
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (tooltip->window);

  gtk_render_background (context, cr, 0, 0,
			 gtk_widget_get_allocated_width (tooltip->window),
			 gtk_widget_get_allocated_height (tooltip->window));
  gtk_render_frame (context, cr, 0, 0,
                    gtk_widget_get_allocated_width (tooltip->window),
                    gtk_widget_get_allocated_height (tooltip->window));
Kristian Rietveld's avatar
Kristian Rietveld committed
613 614 615 616 617 618 619 620 621 622

  return FALSE;
}

static void
gtk_tooltip_window_hide (GtkWidget *widget,
			 gpointer   user_data)
{
  GtkTooltip *tooltip = GTK_TOOLTIP (user_data);

623
  gtk_tooltip_set_custom (tooltip, NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
}

/* event handling, etc */

struct ChildLocation
{
  GtkWidget *child;
  GtkWidget *container;

  gint x;
  gint y;
};

static void
child_location_foreach (GtkWidget *child,
			gpointer   data)
{
641
  GtkAllocation child_allocation;
642
  gint x, y;
Kristian Rietveld's avatar
Kristian Rietveld committed
643 644
  struct ChildLocation *child_loc = data;

645
  /* Ignore invisible widgets */
646
  if (!gtk_widget_is_drawable (child))
647 648
    return;

649 650
  gtk_widget_get_allocation (child, &child_allocation);

651 652 653
  x = 0;
  y = 0;

654 655 656 657
  /* (child_loc->x, child_loc->y) are relative to
   * child_loc->container's allocation.
   */

658
  if (!child_loc->child &&
Kristian Rietveld's avatar
Kristian Rietveld committed
659 660
      gtk_widget_translate_coordinates (child_loc->container, child,
					child_loc->x, child_loc->y,
661 662
					&x, &y))
    {
663 664 665
#ifdef DEBUG_TOOLTIP
      g_print ("candidate: %s  alloc=[(%d,%d)  %dx%d]     (%d, %d)->(%d, %d)\n",
	       gtk_widget_get_name (child),
666 667 668 669
               child_allocation.x,
               child_allocation.y,
               child_allocation.width,
               child_allocation.height,
670 671 672 673 674
	       child_loc->x, child_loc->y,
	       x, y);
#endif /* DEBUG_TOOLTIP */

      /* (x, y) relative to child's allocation. */
675 676
      if (x >= 0 && x < child_allocation.width
	  && y >= 0 && y < child_allocation.height)
Kristian Rietveld's avatar
Kristian Rietveld committed
677 678 679 680 681
        {
	  if (GTK_IS_CONTAINER (child))
	    {
	      struct ChildLocation tmp = { NULL, NULL, 0, 0 };

682 683 684
	      /* Take (x, y) relative the child's allocation and
	       * recurse.
	       */
Kristian Rietveld's avatar
Kristian Rietveld committed
685 686 687 688
	      tmp.x = x;
	      tmp.y = y;
	      tmp.container = child;

689 690
	      gtk_container_forall (GTK_CONTAINER (child),
				    child_location_foreach, &tmp);
Kristian Rietveld's avatar
Kristian Rietveld committed
691 692 693 694 695 696 697 698 699 700 701 702

	      if (tmp.child)
		child_loc->child = tmp.child;
	      else
		child_loc->child = child;
	    }
	  else
	    child_loc->child = child;
	}
    }
}

703 704 705
/* Translates coordinates from dest_widget->window relative (src_x, src_y),
 * to allocation relative (dest_x, dest_y) of dest_widget.
 */
Kristian Rietveld's avatar
Kristian Rietveld committed
706 707 708 709 710 711 712
static void
window_to_alloc (GtkWidget *dest_widget,
		 gint       src_x,
		 gint       src_y,
		 gint      *dest_x,
		 gint      *dest_y)
{
713 714 715 716
  GtkAllocation allocation;

  gtk_widget_get_allocation (dest_widget, &allocation);

Kristian Rietveld's avatar
Kristian Rietveld committed
717
  /* Translate from window relative to allocation relative */
718 719
  if (gtk_widget_get_has_window (dest_widget) &&
      gtk_widget_get_parent (dest_widget))
Kristian Rietveld's avatar
Kristian Rietveld committed
720 721
    {
      gint wx, wy;
722 723
      gdk_window_get_position (gtk_widget_get_window (dest_widget),
                               &wx, &wy);
Kristian Rietveld's avatar
Kristian Rietveld committed
724

725 726 727
      /* Offset coordinates if widget->window is smaller than
       * widget->allocation.
       */
728 729
      src_x += wx - allocation.x;
      src_y += wy - allocation.y;
Kristian Rietveld's avatar
Kristian Rietveld committed
730 731 732
    }
  else
    {
733 734
      src_x -= allocation.x;
      src_y -= allocation.y;
Kristian Rietveld's avatar
Kristian Rietveld committed
735 736 737 738 739 740 741 742
    }

  if (dest_x)
    *dest_x = src_x;
  if (dest_y)
    *dest_y = src_y;
}

743 744 745
/* Translates coordinates from window relative (x, y) to
 * allocation relative (x, y) of the returned widget.
 */
746 747 748 749 750 751
GtkWidget *
_gtk_widget_find_at_coords (GdkWindow *window,
                            gint       window_x,
                            gint       window_y,
                            gint      *widget_x,
                            gint      *widget_y)
Kristian Rietveld's avatar
Kristian Rietveld committed
752 753 754 755
{
  GtkWidget *event_widget;
  struct ChildLocation child_loc = { NULL, NULL, 0, 0 };

756 757
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

758 759 760 761 762
  gdk_window_get_user_data (window, (void **)&event_widget);

  if (!event_widget)
    return NULL;

763 764 765
#ifdef DEBUG_TOOLTIP
  g_print ("event window %p (belonging to %p (%s))  (%d, %d)\n",
	   window, event_widget, gtk_widget_get_name (event_widget),
766
	   window_x, window_y);
767 768 769
#endif

  /* Coordinates are relative to event window */
770 771
  child_loc.x = window_x;
  child_loc.y = window_y;
Kristian Rietveld's avatar
Kristian Rietveld committed
772

773 774 775 776
  /* 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.
   */
777
  while (window && window != gtk_widget_get_window (event_widget))
778
    {
779
      gdouble px, py;
780

781 782 783 784 785
      gdk_window_coords_to_parent (window,
                                   child_loc.x, child_loc.y,
                                   &px, &py);
      child_loc.x = px;
      child_loc.y = py;
786

787
      window = gdk_window_get_effective_parent (window);
788 789 790 791 792 793 794 795 796 797
    }

  /* 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;

798 799 800 801 802 803 804
  /* 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);

Kristian Rietveld's avatar
Kristian Rietveld committed
805 806
  if (GTK_IS_CONTAINER (event_widget))
    {
807
      GtkWidget *container = event_widget;
Kristian Rietveld's avatar
Kristian Rietveld committed
808 809 810 811

      child_loc.container = event_widget;
      child_loc.child = NULL;

812 813
      gtk_container_forall (GTK_CONTAINER (event_widget),
			    child_location_foreach, &child_loc);
Kristian Rietveld's avatar
Kristian Rietveld committed
814

815 816 817 818
      /* Here we have a widget, with coordinates relative to
       * child_loc.container's allocation.
       */

Kristian Rietveld's avatar
Kristian Rietveld committed
819 820 821 822
      if (child_loc.child)
	event_widget = child_loc.child;
      else if (child_loc.container)
	event_widget = child_loc.container;
823 824 825 826 827

      /* 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);
Kristian Rietveld's avatar
Kristian Rietveld committed
828 829
    }

830
  /* We return (x, y) relative to the allocation of event_widget. */
831 832 833 834
  if (widget_x)
    *widget_x = child_loc.x;
  if (widget_y)
    *widget_y = child_loc.y;
Kristian Rietveld's avatar
Kristian Rietveld committed
835 836 837 838

  return event_widget;
}

839 840 841
/* Ignores (x, y) on input, translates event coordinates to
 * allocation relative (x, y) of the returned widget.
 */
Kristian Rietveld's avatar
Kristian Rietveld committed
842 843 844 845 846
static GtkWidget *
find_topmost_widget_coords_from_event (GdkEvent *event,
				       gint     *x,
				       gint     *y)
{
847
  GtkAllocation allocation;
Kristian Rietveld's avatar
Kristian Rietveld committed
848 849 850 851 852 853
  gint tx, ty;
  gdouble dx, dy;
  GtkWidget *tmp;

  gdk_event_get_coords (event, &dx, &dy);

854
  /* Returns coordinates relative to tmp's allocation. */
855
  tmp = _gtk_widget_find_at_coords (event->any.window, dx, dy, &tx, &ty);
Kristian Rietveld's avatar
Kristian Rietveld committed
856

857
  if (!tmp)
858 859
    return NULL;

860
  /* Make sure the pointer can actually be on the widget returned. */
861 862 863
  gtk_widget_get_allocation (tmp, &allocation);
  if (tx < 0 || tx >= allocation.width ||
      ty < 0 || ty >= allocation.height)
864
    return NULL;
865

866 867 868 869
  if (x)
    *x = tx;
  if (y)
    *y = ty;
Kristian Rietveld's avatar
Kristian Rietveld committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

  return tmp;
}

static gint
tooltip_browse_mode_expired (gpointer data)
{
  GtkTooltip *tooltip;

  tooltip = GTK_TOOLTIP (data);

  tooltip->browse_mode_enabled = FALSE;
  tooltip->browse_mode_timeout_id = 0;

  /* destroy tooltip */
  g_object_set_data (G_OBJECT (gtk_widget_get_display (tooltip->window)),
		     "gdk-display-current-tooltip", NULL);

  return FALSE;
}

static void
gtk_tooltip_display_closed (GdkDisplay *display,
			    gboolean    was_error,
			    GtkTooltip *tooltip)
{
896
  g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
897 898
}

899 900 901 902
static void
gtk_tooltip_set_last_window (GtkTooltip *tooltip,
			     GdkWindow  *window)
{
903 904 905
  if (tooltip->last_window == window)
    return;

906 907 908 909 910 911 912 913 914 915 916
  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);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
static gboolean
gtk_tooltip_run_requery (GtkWidget  **widget,
			 GtkTooltip  *tooltip,
			 gint        *x,
			 gint        *y)
{
  gboolean has_tooltip = FALSE;
  gboolean return_value = FALSE;

  gtk_tooltip_reset (tooltip);

  do
    {
      g_object_get (*widget,
		    "has-tooltip", &has_tooltip,
		    NULL);

      if (has_tooltip)
	g_signal_emit_by_name (*widget,
			       "query-tooltip",
			       *x, *y,
			       tooltip->keyboard_mode_enabled,
			       tooltip,
			       &return_value);

      if (!return_value)
        {
944
	  GtkWidget *parent = gtk_widget_get_parent (*widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
945 946 947 948 949 950 951 952 953 954 955

	  if (parent)
	    gtk_widget_translate_coordinates (*widget, parent, *x, *y, x, y);

	  *widget = parent;
	}
      else
	break;
    }
  while (*widget);

956 957 958 959 960 961
  /* 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);

Kristian Rietveld's avatar
Kristian Rietveld committed
962 963 964
  return return_value;
}

965 966 967 968
static void
get_bounding_box (GtkWidget    *widget,
                  GdkRectangle *bounds)
{
969
  GtkAllocation allocation;
970 971 972 973 974 975 976 977 978
  GdkWindow *window;
  gint x, y;
  gint w, h;
  gint x1, y1;
  gint x2, y2;
  gint x3, y3;
  gint x4, y4;

  window = gtk_widget_get_parent_window (widget);
979 980
  if (window == NULL)
    window = gtk_widget_get_window (widget);
981

982 983 984 985 986
  gtk_widget_get_allocation (widget, &allocation);
  x = allocation.x;
  y = allocation.y;
  w = allocation.width;
  h = allocation.height;
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

  gdk_window_get_root_coords (window, x, y, &x1, &y1);
  gdk_window_get_root_coords (window, x + w, y, &x2, &y2);
  gdk_window_get_root_coords (window, x, y + h, &x3, &y3);
  gdk_window_get_root_coords (window, x + w, y + h, &x4, &y4);

#define MIN4(a,b,c,d) MIN(MIN(a,b),MIN(c,d))
#define MAX4(a,b,c,d) MAX(MAX(a,b),MAX(c,d))

  bounds->x = floor (MIN4 (x1, x2, x3, x4));
  bounds->y = floor (MIN4 (y1, y2, y3, y4));
  bounds->width = ceil (MAX4 (x1, x2, x3, x4)) - bounds->x;
  bounds->height = ceil (MAX4 (y1, y2, y3, y4)) - bounds->y;
}

1002 1003 1004 1005 1006 1007 1008
static void
gtk_tooltip_position (GtkTooltip *tooltip,
		      GdkDisplay *display,
		      GtkWidget  *new_tooltip_widget)
{
  gint x, y;
  GdkScreen *screen;
Matthias Clasen's avatar
Matthias Clasen committed
1009 1010 1011 1012 1013 1014 1015
  gint monitor_num;
  GdkRectangle monitor;
  GtkRequisition requisition;
  guint cursor_size;
  GdkRectangle bounds;

#define MAX_DISTANCE 32
1016 1017 1018

  tooltip->tooltip_widget = new_tooltip_widget;

Matthias Clasen's avatar
Matthias Clasen committed
1019 1020
  screen = gtk_widget_get_screen (new_tooltip_widget);

1021 1022
  gtk_widget_get_preferred_size (GTK_WIDGET (tooltip->current_window),
                                 &requisition, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1023 1024 1025 1026 1027 1028 1029 1030

  monitor_num = gdk_screen_get_monitor_at_point (screen,
                                                 tooltip->last_x,
                                                 tooltip->last_y);
  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

  get_bounding_box (new_tooltip_widget, &bounds);

1031
  /* Position the tooltip */
Matthias Clasen's avatar
Matthias Clasen committed
1032 1033 1034 1035 1036 1037 1038 1039

  cursor_size = gdk_display_get_default_cursor_size (display);

  /* Try below */
  x = bounds.x + bounds.width / 2 - requisition.width / 2;
  y = bounds.y + bounds.height + 4;

  if (y + requisition.height <= monitor.y + monitor.height)
1040
    {
Matthias Clasen's avatar
Matthias Clasen committed
1041 1042
      if (tooltip->keyboard_mode_enabled)
        goto found;
1043

Matthias Clasen's avatar
Matthias Clasen committed
1044 1045 1046 1047 1048 1049
      if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
        {
          if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
            x = tooltip->last_x + cursor_size + MAX_DISTANCE;
          else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
            x = tooltip->last_x - MAX_DISTANCE - requisition.width;
1050

Matthias Clasen's avatar
Matthias Clasen committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
          goto found;
        }
   }

  /* Try above */
  x = bounds.x + bounds.width / 2 - requisition.width / 2;
  y = bounds.y - requisition.height - 4;

  if (y >= monitor.y)
    {
      if (tooltip->keyboard_mode_enabled)
        goto found;

      if (y + requisition.height >= tooltip->last_y - MAX_DISTANCE)
        {
          if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
            x = tooltip->last_x + cursor_size + MAX_DISTANCE;
          else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
            x = tooltip->last_x - MAX_DISTANCE - requisition.width;

          goto found;
        }
1073
    }
Matthias Clasen's avatar
Matthias Clasen committed
1074 1075 1076 1077 1078 1079

  /* Try right FIXME: flip on rtl ? */
  x = bounds.x + bounds.width + 4;
  y = bounds.y + bounds.height / 2 - requisition.height / 2;

  if (x + requisition.width <= monitor.x + monitor.width)
1080
    {
Matthias Clasen's avatar
Matthias Clasen committed
1081 1082
      if (tooltip->keyboard_mode_enabled)
        goto found;
1083

Matthias Clasen's avatar
Matthias Clasen committed
1084 1085 1086 1087 1088 1089
      if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
        {
          if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
            y = tooltip->last_y + cursor_size + MAX_DISTANCE;
          else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
            y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1090

Matthias Clasen's avatar
Matthias Clasen committed
1091 1092
          goto found;
        }
1093 1094
    }

Matthias Clasen's avatar
Matthias Clasen committed
1095 1096 1097
  /* Try left FIXME: flip on rtl ? */
  x = bounds.x - requisition.width - 4;
  y = bounds.y + bounds.height / 2 - requisition.height / 2;
1098

Matthias Clasen's avatar
Matthias Clasen committed
1099
  if (x >= monitor.x)
1100
    {
Matthias Clasen's avatar
Matthias Clasen committed
1101 1102
      if (tooltip->keyboard_mode_enabled)
        goto found;
1103

Matthias Clasen's avatar
Matthias Clasen committed
1104 1105 1106 1107 1108 1109
      if (x + requisition.width >= tooltip->last_x - MAX_DISTANCE)
        {
          if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
            y = tooltip->last_y + cursor_size + MAX_DISTANCE;
          else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
            y = tooltip->last_y - MAX_DISTANCE - requisition.height;
1110

Matthias Clasen's avatar
Matthias Clasen committed
1111 1112 1113
          goto found;
        }
    }
1114

Matthias Clasen's avatar
Matthias Clasen committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
   /* Fallback */
  if (tooltip->keyboard_mode_enabled)
    {
      x = bounds.x + bounds.width / 2 - requisition.width / 2;
      y = bounds.y + bounds.height + 4;
    }
  else
    {
      /* At cursor */
      x = tooltip->last_x + cursor_size * 3 / 4;
      y = tooltip->last_y + cursor_size * 3 / 4;
    }

found:
  /* Show it */
  if (tooltip->current_window)
    {
1132 1133 1134 1135 1136 1137 1138
      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;
Matthias Clasen's avatar
Matthias Clasen committed
1139 1140 1141
      else if (y < monitor.y)
        y = monitor.y;

1142 1143 1144 1145 1146 1147 1148
      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;
        }
Matthias Clasen's avatar
Matthias Clasen committed
1149

1150 1151 1152 1153 1154
      gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
      gtk_widget_show (GTK_WIDGET (tooltip->current_window));
    }
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
static void
gtk_tooltip_show_tooltip (GdkDisplay *display)
{
  gint x, y;
  GdkScreen *screen;

  GdkWindow *window;
  GtkWidget *tooltip_widget;
  GtkTooltip *tooltip;
  gboolean has_tooltip;
  gboolean return_value = FALSE;

  tooltip = g_object_get_data (G_OBJECT (display),
Matthias Clasen's avatar
Matthias Clasen committed
1168
                               "gdk-display-current-tooltip");
Kristian Rietveld's avatar
Kristian Rietveld committed
1169 1170 1171

  if (tooltip->keyboard_mode_enabled)
    {
1172
      x = y = -1;
Matthias Clasen's avatar
Matthias Clasen committed
1173
      tooltip_widget = tooltip->keyboard_widget;
Kristian Rietveld's avatar
Kristian Rietveld committed
1174 1175 1176
    }
  else
    {
1177 1178
      gint tx, ty;

Kristian Rietveld's avatar
Kristian Rietveld committed
1179 1180
      window = tooltip->last_window;

1181
      if (!GDK_IS_WINDOW (window))
Matthias Clasen's avatar
Matthias Clasen committed
1182
        return;
1183

1184 1185 1186 1187 1188
      gdk_window_get_pointer (window, &x, &y, NULL);

      gdk_window_get_root_coords (window, x, y, &tx, &ty);
      tooltip->last_x = tx;
      tooltip->last_y = ty;
Kristian Rietveld's avatar
Kristian Rietveld committed
1189

Matthias Clasen's avatar
Matthias Clasen committed
1190
      tooltip_widget = _gtk_widget_find_at_coords (window, x, y, &x, &y);
Kristian Rietveld's avatar
Kristian Rietveld committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    }

  if (!tooltip_widget)
    return;

  g_object_get (tooltip_widget, "has-tooltip", &has_tooltip, NULL);

  return_value = gtk_tooltip_run_requery (&tooltip_widget, tooltip, &x, &y);
  if (!return_value)
    return;

  if (!tooltip->current_window)
    {
      if (gtk_widget_get_tooltip_window (tooltip_widget))
Matthias Clasen's avatar
Matthias Clasen committed
1205
        tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
1206
      else
Matthias Clasen's avatar
Matthias Clasen committed
1207
        tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1208 1209 1210 1211
    }

  screen = gtk_widget_get_screen (tooltip_widget);

1212
  /* FIXME: should use tooltip->current_window iso tooltip->window */
Kristian Rietveld's avatar
Kristian Rietveld committed
1213 1214 1215
  if (screen != gtk_widget_get_screen (tooltip->window))
    {
      g_signal_handlers_disconnect_by_func (display,
Matthias Clasen's avatar
Matthias Clasen committed
1216 1217
                                            gtk_tooltip_display_closed,
                                            tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1218 1219 1220 1221

      gtk_window_set_screen (GTK_WINDOW (tooltip->window), screen);

      g_signal_connect (display, "closed",
Matthias Clasen's avatar
Matthias Clasen committed
1222
                        G_CALLBACK (gtk_tooltip_display_closed), tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1223 1224
    }

1225
  gtk_tooltip_position (tooltip, display, tooltip_widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

  /* Now a tooltip is visible again on the display, make sure browse
   * mode is enabled.
   */
  tooltip->browse_mode_enabled = TRUE;
  if (tooltip->browse_mode_timeout_id)
    {
      g_source_remove (tooltip->browse_mode_timeout_id);
      tooltip->browse_mode_timeout_id = 0;
    }
}

static void
gtk_tooltip_hide_tooltip (GtkTooltip *tooltip)
{
1241
  if (!tooltip)
Kristian Rietveld's avatar
Kristian Rietveld committed
1242 1243 1244 1245 1246 1247 1248 1249
    return;

  if (tooltip->timeout_id)
    {
      g_source_remove (tooltip->timeout_id);
      tooltip->timeout_id = 0;
    }

1250 1251 1252 1253 1254
  if (!GTK_TOOLTIP_VISIBLE (tooltip))
    return;

  tooltip->tooltip_widget = NULL;

Kristian Rietveld's avatar
Kristian Rietveld committed
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
  if (!tooltip->keyboard_mode_enabled)
    {
      guint timeout;
      GtkSettings *settings;

      settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));

      g_object_get (settings,
		    "gtk-tooltip-browse-mode-timeout", &timeout,
		    NULL);

      /* The tooltip is gone, after (by default, should be configurable) 500ms
       * we want to turn off browse mode
       */
      if (!tooltip->browse_mode_timeout_id)
	tooltip->browse_mode_timeout_id =
Kristian Rietveld's avatar
Kristian Rietveld committed
1271 1272 1273 1274
	  gdk_threads_add_timeout_full (0, timeout,
					tooltip_browse_mode_expired,
					g_object_ref (tooltip),
					g_object_unref);
Kristian Rietveld's avatar
Kristian Rietveld committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    }
  else
    {
      if (tooltip->browse_mode_timeout_id)
        {
	  g_source_remove (tooltip->browse_mode_timeout_id);
	  tooltip->browse_mode_timeout_id = 0;
	}
    }

  if (tooltip->current_window)
    {
      gtk_widget_hide (GTK_WIDGET (tooltip->current_window));
      tooltip->current_window = NULL;
    }
}

static gint
tooltip_popup_timeout (gpointer data)
{
  GdkDisplay *display;
  GtkTooltip *tooltip;

1298
  display = GDK_DISPLAY (data);
1299 1300 1301 1302 1303 1304 1305 1306
  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;
Kristian Rietveld's avatar
Kristian Rietveld committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

  gtk_tooltip_show_tooltip (display);

  tooltip->timeout_id = 0;

  return FALSE;
}

static void
gtk_tooltip_start_delay (GdkDisplay *display)
{
  guint timeout;
  GtkTooltip *tooltip;
  GtkSettings *settings;

  tooltip = g_object_get_data (G_OBJECT (display),
			       "gdk-display-current-tooltip");

1325
  if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
Kristian Rietveld's avatar
Kristian Rietveld committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    return;

  if (tooltip->timeout_id)
    g_source_remove (tooltip->timeout_id);

  settings = gtk_widget_get_settings (GTK_WIDGET (tooltip->window));

  if (tooltip->browse_mode_enabled)
    g_object_get (settings, "gtk-tooltip-browse-timeout", &timeout, NULL);
  else
    g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);

Kristian Rietveld's avatar
Kristian Rietveld committed
1338 1339 1340 1341
  tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
						      tooltip_popup_timeout,
						      g_object_ref (display),
						      g_object_unref);
Kristian Rietveld's avatar
Kristian Rietveld committed
1342 1343 1344 1345 1346 1347 1348 1349 1350
}

void
_gtk_tooltip_focus_in (GtkWidget *widget)
{
  gint x, y;
  gboolean return_value = FALSE;
  GdkDisplay *display;
  GtkTooltip *tooltip;
1351
  GdkDevice *device;
Kristian Rietveld's avatar
Kristian Rietveld committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

  /* Get current tooltip for this display */
  display = gtk_widget_get_display (widget);
  tooltip = g_object_get_data (G_OBJECT (display),
			       "gdk-display-current-tooltip");

  /* Check if keyboard mode is enabled at this moment */
  if (!tooltip || !tooltip->keyboard_mode_enabled)
    return;

1362 1363
  device = gtk_get_current_event_device ();

1364
  if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1365 1366 1367 1368 1369 1370 1371 1372
    device = gdk_device_get_associated_device (device);

  /* This function should be called by either a focus in event,
   * or a key binding. In either case there should be a device.
   */
  if (!device)
    return;

Kristian Rietveld's avatar
Kristian Rietveld committed
1373 1374 1375 1376 1377
  if (tooltip->keyboard_widget)
    g_object_unref (tooltip->keyboard_widget);

  tooltip->keyboard_widget = g_object_ref (widget);

1378 1379
  gdk_window_get_device_position (gtk_widget_get_window (widget),
                                  device, &x, &y, NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

  return_value = gtk_tooltip_run_requery (&widget, tooltip, &x, &y);
  if (!return_value)
    {
      gtk_tooltip_hide_tooltip (tooltip);
      return;
    }

  if (!tooltip->current_window)
    {
      if (gtk_widget_get_tooltip_window (widget))
	tooltip->current_window = gtk_widget_get_tooltip_window (widget);
      else
	tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
    }

  gtk_tooltip_show_tooltip (display);
}

void
_gtk_tooltip_focus_out (GtkWidget *widget)
{
  GdkDisplay *display;
  GtkTooltip *tooltip;

  /* Get current tooltip for this display */
  display = gtk_widget_get_display (widget);
  tooltip = g_object_get_data (G_OBJECT (display),
			       "gdk-display-current-tooltip");

  if (!tooltip || !tooltip->keyboard_mode_enabled)
    return;

  if (tooltip->keyboard_widget)
    {
      g_object_unref (tooltip->keyboard_widget);
      tooltip->keyboard_widget = NULL;
    }

  gtk_tooltip_hide_tooltip (tooltip);
}

void
_gtk_tooltip_toggle_keyboard_mode (GtkWidget *widget)
{
  GdkDisplay *display;
  GtkTooltip *tooltip;

  display = gtk_widget_get_display (widget);
  tooltip = g_object_get_data (G_OBJECT (display),
			       "gdk-display-current-tooltip");

  if (!tooltip)
    {
      tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
      g_object_set_data_full (G_OBJECT (display),
			      "gdk-display-current-tooltip",
			      tooltip, g_object_unref);
1438 1439 1440
      g_signal_connect (display, "closed",
			G_CALLBACK (gtk_tooltip_display_closed),
			tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
    }

  tooltip->keyboard_mode_enabled ^= 1;

  if (tooltip->keyboard_mode_enabled)
    {
      tooltip->keyboard_widget = g_object_ref (widget);
      _gtk_tooltip_focus_in (widget);
    }
  else
    {
      if (tooltip->keyboard_widget)
        {
	  g_object_unref (tooltip->keyboard_widget);
	  tooltip->keyboard_widget = NULL;
	}

      gtk_tooltip_hide_tooltip (tooltip);
    }
}

void
_gtk_tooltip_hide (GtkWidget *widget)
{
  GtkWidget *toplevel;
  GdkDisplay *display;
  GtkTooltip *tooltip;

  display = gtk_widget_get_display (widget);
  tooltip = g_object_get_data (G_OBJECT (display),
			       "gdk-display-current-tooltip");

  if (!tooltip || !GTK_TOOLTIP_VISIBLE (tooltip) || !tooltip->tooltip_widget)
    return;

  toplevel = gtk_widget_get_toplevel (widget);

  if (widget == tooltip->tooltip_widget
1479
      || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
Kristian Rietveld's avatar
Kristian Rietveld committed
1480 1481 1482
    gtk_tooltip_hide_tooltip (tooltip);
}

1483 1484 1485 1486 1487 1488 1489 1490
static gboolean
tooltips_enabled (GdkWindow *window)
{
  gboolean enabled;
  gboolean touchscreen;
  GdkScreen *screen;
  GtkSettings *settings;

1491
  screen = gdk_window_get_screen (window);
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
  settings = gtk_settings_get_for_screen (screen);

  g_object_get (settings,
		"gtk-touchscreen-mode", &touchscreen,
		"gtk-enable-tooltips", &enabled,
		NULL);

  return (!touchscreen && enabled);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1502 1503 1504 1505 1506 1507 1508 1509
void
_gtk_tooltip_handle_event (GdkEvent *event)
{
  gint x, y;
  gboolean return_value = FALSE;
  GtkWidget *has_tooltip_widget = NULL;
  GdkDisplay *display;
  GtkTooltip *current_tooltip;
1510

1511
  if (!tooltips_enabled (event->any.window))
1512
    return;
Kristian Rietveld's avatar
Kristian Rietveld committed
1513

1514
  /* Returns coordinates relative to has_tooltip_widget's allocation. */
Kristian Rietveld's avatar
Kristian Rietveld committed
1515
  has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
1516
  display = gdk_window_get_display (event->any.window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1517 1518 1519 1520 1521
  current_tooltip = g_object_get_data (G_OBJECT (display),
				       "gdk-display-current-tooltip");

  if (current_tooltip)
    {
1522
      gtk_tooltip_set_last_window (current_tooltip, event->any.window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    }

  if (current_tooltip && current_tooltip->keyboard_mode_enabled)
    {
      has_tooltip_widget = current_tooltip->keyboard_widget;
      if (!has_tooltip_widget)
	return;

      return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
					      current_tooltip,
					      &x, &y);

      if (!return_value)
	gtk_tooltip_hide_tooltip (current_tooltip);
      else
	gtk_tooltip_start_delay (display);

      return;
    }

1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
#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 */

Kristian Rietveld's avatar
Kristian Rietveld committed
1554
  /* Always poll for a next motion event */
1555
  gdk_event_request_motions (&event->motion);
Kristian Rietveld's avatar
Kristian Rietveld committed
1556 1557 1558 1559

  /* Hide the tooltip when there's no new tooltip widget */
  if (!has_tooltip_widget)
    {
1560
      if (current_tooltip)
Kristian Rietveld's avatar
Kristian Rietveld committed
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	gtk_tooltip_hide_tooltip (current_tooltip);

      return;
    }

  switch (event->type)
    {
      case GDK_BUTTON_PRESS:
      case GDK_2BUTTON_PRESS:
      case GDK_3BUTTON_PRESS:
      case GDK_KEY_PRESS:
      case GDK_DRAG_ENTER:
      case GDK_GRAB_BROKEN:
	gtk_tooltip_hide_tooltip (current_tooltip);
	break;

      case GDK_MOTION_NOTIFY:
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
      case GDK_SCROLL:
	if (current_tooltip)
	  {
1583 1584 1585 1586 1587 1588 1589
	    gboolean tip_area_set;
	    GdkRectangle tip_area;
	    gboolean hide_tooltip;

	    tip_area_set = current_tooltip->tip_area_set;
	    tip_area = current_tooltip->tip_area;

Kristian Rietveld's avatar
Kristian Rietveld committed
1590 1591 1592 1593
	    return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
						    current_tooltip,
						    &x, &y);

1594 1595 1596
	    /* Requested to be hidden? */
	    hide_tooltip = !return_value;

1597 1598 1599
	    /* Leave notify should override the query function */
	    hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
	    /* 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)
Kristian Rietveld's avatar
Kristian Rietveld committed
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	      gtk_tooltip_hide_tooltip (current_tooltip);
	    else
	      gtk_tooltip_start_delay (display);
	  }
	else
	  {
	    /* Need a new tooltip for this display */
	    current_tooltip = g_object_new (GTK_TYPE_TOOLTIP, NULL);
	    g_object_set_data_full (G_OBJECT (display),
				    "gdk-display-current-tooltip",
				    current_tooltip, g_object_unref);
1623 1624 1625
	    g_signal_connect (display, "closed",
			      G_CALLBACK (gtk_tooltip_display_closed),
			      current_tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1626

1627
	    gtk_tooltip_set_last_window (current_tooltip, event->any.window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1628 1629 1630 1631 1632 1633 1634 1635 1636

	    gtk_tooltip_start_delay (display);
	  }
	break;

      default:
	break;
    }
}