gtktooltip.c 42 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
/* 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
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Kristian Rietveld's avatar
Kristian Rietveld committed
18 19
 */

20
#include "config.h"
21

22 23
#include "gtktooltip.h"

24 25 26
#include <math.h>
#include <string.h>

Kristian Rietveld's avatar
Kristian Rietveld committed
27 28 29 30 31
#include "gtkintl.h"
#include "gtkwindow.h"
#include "gtkmain.h"
#include "gtklabel.h"
#include "gtkimage.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "gtkbox.h"
33
#include "gtksettings.h"
34
#include "gtksizerequest.h"
35
#include "gtkstylecontext.h"
36
#include "gtkwindowprivate.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
37

Matthias Clasen's avatar
Matthias Clasen committed
38

39 40 41 42 43
#ifdef GDK_WINDOWING_WAYLAND
#include "wayland/gdkwayland.h"
#endif


44 45 46 47 48 49 50 51 52 53 54
/**
 * 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:
Matthias Clasen's avatar
Matthias Clasen committed
55 56 57 58 59 60 61 62 63 64
 * 
 * - 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.
 *
 * - 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
65
 *   the tooltip’s markup, setting an image from a named icon, or even putting in
Matthias Clasen's avatar
Matthias Clasen committed
66 67 68 69
 *   a custom widget.
 *
 *   Return %TRUE from your query-tooltip handler. This causes the tooltip to be
 *   show. If you return %FALSE, it will not be shown.
70 71 72 73
 *
 * 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:
Matthias Clasen's avatar
Matthias Clasen committed
74 75 76 77 78 79 80 81 82
 * 
 * - Set #GtkWidget:has-tooltip and connect to #GtkWidget::query-tooltip as before.
 *   Use gtk_widget_set_tooltip_window() to set a #GtkWindow created by you as
 *   tooltip window.
 *
 * - 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.
83 84 85
 */


86 87
#undef DEBUG_TOOLTIP

88 89 90
#define HOVER_TIMEOUT          500
#define BROWSE_TIMEOUT         60
#define BROWSE_DISABLE_TIMEOUT 500
Kristian Rietveld's avatar
Kristian Rietveld committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

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

121 122
  GdkRectangle tip_area;

Kristian Rietveld's avatar
Kristian Rietveld committed
123 124
  guint browse_mode_enabled : 1;
  guint keyboard_mode_enabled : 1;
125
  guint tip_area_set : 1;
126
  guint custom_was_reset : 1;
Kristian Rietveld's avatar
Kristian Rietveld committed
127 128 129 130 131 132 133
};

struct _GtkTooltipClass
{
  GObjectClass parent_class;
};

134
#define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && gtk_widget_get_visible (GTK_WIDGET((tooltip)->current_window)))
Kristian Rietveld's avatar
Kristian Rietveld committed
135 136 137 138


static void       gtk_tooltip_class_init           (GtkTooltipClass *klass);
static void       gtk_tooltip_init                 (GtkTooltip      *tooltip);
Paolo Borelli's avatar
Paolo Borelli committed
139
static void       gtk_tooltip_dispose              (GObject         *object);
Kristian Rietveld's avatar
Kristian Rietveld committed
140 141 142 143 144 145

static void       gtk_tooltip_window_hide          (GtkWidget       *widget,
						    gpointer         user_data);
static void       gtk_tooltip_display_closed       (GdkDisplay      *display,
						    gboolean         was_error,
						    GtkTooltip      *tooltip);
146 147
static void       gtk_tooltip_set_last_window      (GtkTooltip      *tooltip,
						    GdkWindow       *window);
Kristian Rietveld's avatar
Kristian Rietveld committed
148 149 150 151 152 153 154 155 156 157 158


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
159
  object_class->dispose = gtk_tooltip_dispose;
Kristian Rietveld's avatar
Kristian Rietveld committed
160 161 162 163 164
}

static void
gtk_tooltip_init (GtkTooltip *tooltip)
{
165
  GtkStyleContext *context;
Matthias Clasen's avatar
Matthias Clasen committed
166 167 168 169
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *image;
  GtkWidget *label;
170

Kristian Rietveld's avatar
Kristian Rietveld committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184
  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;

185
  window = gtk_window_new (GTK_WINDOW_POPUP);
Matthias Clasen's avatar
Matthias Clasen committed
186 187 188 189
  gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  g_signal_connect (window, "hide",
                    G_CALLBACK (gtk_tooltip_window_hide), tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
190

Matthias Clasen's avatar
Matthias Clasen committed
191
  context = gtk_widget_get_style_context (window);
192 193
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);

Matthias Clasen's avatar
Matthias Clasen committed
194 195
  /* FIXME: don't hardcode the padding */
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
196 197
  gtk_widget_set_margin_start (box, 6);
  gtk_widget_set_margin_end (box, 6);
Matthias Clasen's avatar
Matthias Clasen committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  gtk_widget_set_margin_top (box, 6);
  gtk_widget_set_margin_bottom (box, 6);
  gtk_container_add (GTK_CONTAINER (window), box);
  gtk_widget_show (box);

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

  label = gtk_label_new ("");
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);

  tooltip->window = window;
  tooltip->box = box;
  tooltip->image = image;
  tooltip->label = label;
Kristian Rietveld's avatar
Kristian Rietveld committed
214 215 216 217
  tooltip->custom_widget = NULL;
}

static void
Paolo Borelli's avatar
Paolo Borelli committed
218
gtk_tooltip_dispose (GObject *object)
Kristian Rietveld's avatar
Kristian Rietveld committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
{
  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;
    }

234
  gtk_tooltip_set_custom (tooltip, NULL);
235 236
  gtk_tooltip_set_last_window (tooltip, NULL);

Kristian Rietveld's avatar
Kristian Rietveld committed
237 238 239 240 241 242 243 244 245
  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
246
      tooltip->window = NULL;
Kristian Rietveld's avatar
Kristian Rietveld committed
247
    }
Michael Natterer's avatar
Michael Natterer committed
248

Paolo Borelli's avatar
Paolo Borelli committed
249
  G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
Kristian Rietveld's avatar
Kristian Rietveld committed
250 251 252 253 254 255
}

/* public API */

/**
 * gtk_tooltip_set_markup:
Matthias Clasen's avatar
Matthias Clasen committed
256
 * @tooltip: a #GtkTooltip
257
 * @markup: (allow-none): a markup string (see [Pango markup format][PangoMarkupFormat]) or %NULL
Kristian Rietveld's avatar
Kristian Rietveld committed
258
 *
Matthias Clasen's avatar
Matthias Clasen committed
259
 * Sets the text of the tooltip to be @markup, which is marked up
260
 * with the [Pango text markup language][PangoMarkupFormat].
Kristian Rietveld's avatar
Kristian Rietveld committed
261 262 263 264 265 266 267 268 269 270 271
 * 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));

  if (markup)
Matthias Clasen's avatar
Matthias Clasen committed
272 273 274 275
    {
      gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
      gtk_widget_show (tooltip->label);
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
276
  else
Matthias Clasen's avatar
Matthias Clasen committed
277 278 279
    {
      gtk_widget_hide (tooltip->label);
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
280 281
}

282 283 284
/**
 * gtk_tooltip_set_text:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
285
 * @text: (allow-none): a text string or %NULL
286 287 288 289 290 291 292 293 294 295 296 297 298
 *
 * 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));

  if (text)
Matthias Clasen's avatar
Matthias Clasen committed
299 300 301 302
    {
      gtk_label_set_text (GTK_LABEL (tooltip->label), text);
      gtk_widget_show (tooltip->label);
    }
303
  else
Matthias Clasen's avatar
Matthias Clasen committed
304 305 306
    {
      gtk_widget_hide (tooltip->label);
    }
307 308
}

Kristian Rietveld's avatar
Kristian Rietveld committed
309 310 311
/**
 * gtk_tooltip_set_icon:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
312
 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
Kristian Rietveld's avatar
Kristian Rietveld committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
 *
 * 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));

  if (pixbuf)
Matthias Clasen's avatar
Matthias Clasen committed
328 329 330 331
    {
      gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
      gtk_widget_show (tooltip->image);
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
332
  else
Matthias Clasen's avatar
Matthias Clasen committed
333 334 335
    {
      gtk_widget_hide (tooltip->image);
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
336 337 338 339 340
}

/**
 * gtk_tooltip_set_icon_from_stock:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
341
 * @stock_id: (allow-none): a stock id, or %NULL
342
 * @size: (type int): a stock icon size
Kristian Rietveld's avatar
Kristian Rietveld committed
343 344 345 346 347 348
 *
 * 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
349 350
 *
 * Deprecated: 3.10: Use gtk_tooltip_set_icon_from_icon_name() instead.
Kristian Rietveld's avatar
Kristian Rietveld committed
351 352 353 354 355 356 357 358 359
 */
void
gtk_tooltip_set_icon_from_stock (GtkTooltip  *tooltip,
				 const gchar *stock_id,
				 GtkIconSize  size)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  if (stock_id)
Matthias Clasen's avatar
Matthias Clasen committed
360 361 362 363 364 365
    {
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
 G_GNUC_END_IGNORE_DEPRECATIONS;
      gtk_widget_show (tooltip->image);
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
366
  else
Matthias Clasen's avatar
Matthias Clasen committed
367 368 369
    {
      gtk_widget_hide (tooltip->image);
    }
Kristian Rietveld's avatar
Kristian Rietveld committed
370 371
}

Matthias Clasen's avatar
Matthias Clasen committed
372 373 374
/**
 * gtk_tooltip_set_icon_from_icon_name:
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
375
 * @icon_name: (allow-none): an icon name, or %NULL
376
 * @size: (type int): a stock icon size
Matthias Clasen's avatar
Matthias Clasen committed
377 378 379 380 381
 *
 * 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.
 *
382
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
383 384
 */
void
385 386 387
gtk_tooltip_set_icon_from_icon_name (GtkTooltip  *tooltip,
				     const gchar *icon_name,
				     GtkIconSize  size)
Matthias Clasen's avatar
Matthias Clasen committed
388 389 390 391
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  if (icon_name)
Matthias Clasen's avatar
Matthias Clasen committed
392 393 394 395
    {
      gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
      gtk_widget_show (tooltip->image);
    }
396
  else
Matthias Clasen's avatar
Matthias Clasen committed
397 398 399
    {
      gtk_widget_hide (tooltip->image);
    }
400 401 402
}

/**
403
 * gtk_tooltip_set_icon_from_gicon:
404
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
405
 * @gicon: (allow-none): a #GIcon representing the icon, or %NULL
406
 * @size: (type int): a stock icon size
407 408 409 410
 *
 * 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.
411 412 413 414 415 416 417 418 419 420 421
 *
 * Since: 2.20
 */
void
gtk_tooltip_set_icon_from_gicon (GtkTooltip  *tooltip,
				 GIcon       *gicon,
				 GtkIconSize  size)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  if (gicon)
Matthias Clasen's avatar
Matthias Clasen committed
422 423 424 425
    {
      gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
      gtk_widget_show (tooltip->image);
    }
Matthias Clasen's avatar
Matthias Clasen committed
426
  else
Matthias Clasen's avatar
Matthias Clasen committed
427 428 429
    {
      gtk_widget_hide (tooltip->image);
    }
Matthias Clasen's avatar
Matthias Clasen committed
430 431
}

Kristian Rietveld's avatar
Kristian Rietveld committed
432 433
/**
 * gtk_tooltip_set_custom:
Matthias Clasen's avatar
Matthias Clasen committed
434
 * @tooltip: a #GtkTooltip
Johan Dahlin's avatar
Johan Dahlin committed
435
 * @custom_widget: (allow-none): a #GtkWidget, or %NULL to unset the old custom widget.
Kristian Rietveld's avatar
Kristian Rietveld committed
436
 *
437 438 439
 * 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
440 441 442
 * 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().
443

Kristian Rietveld's avatar
Kristian Rietveld committed
444 445 446 447 448 449 450 451 452 453 454
 *
 * 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));

455 456 457 458 459 460 461 462 463
  /* 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
464 465
  if (tooltip->custom_widget)
    {
466 467 468 469 470 471 472 473
      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
474 475 476 477 478 479 480 481 482 483 484
    }

  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);
    }
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/**
 * 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
502 503
gtk_tooltip_set_tip_area (GtkTooltip         *tooltip,
			  const GdkRectangle *rect)
504 505 506 507 508 509 510 511 512 513 514 515
{
  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
516 517
/**
 * gtk_tooltip_trigger_tooltip_query:
518
 * @display: a #GdkDisplay
Kristian Rietveld's avatar
Kristian Rietveld committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532
 *
 * 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;
533
  GdkDevice *device;
Kristian Rietveld's avatar
Kristian Rietveld committed
534 535

  /* Trigger logic as if the mouse moved */
536
  device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
537
  window = gdk_device_get_window_at_position (device, &x, &y);
Kristian Rietveld's avatar
Kristian Rietveld committed
538 539 540 541 542 543 544 545 546
  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;

547 548 549
  gdk_window_get_root_coords (window, x, y, &x, &y);
  event.motion.x_root = x;
  event.motion.y_root = y;
550

Kristian Rietveld's avatar
Kristian Rietveld committed
551 552 553 554 555 556 557 558 559 560
  _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);
561
  gtk_tooltip_set_tip_area (tooltip, NULL);
562 563 564 565 566

  /* 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
567 568 569 570 571 572 573 574
}

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

575
  gtk_tooltip_set_custom (tooltip, NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
576 577 578 579 580 581 582 583 584 585 586 587 588
}

/* event handling, etc */

struct ChildLocation
{
  GtkWidget *child;
  GtkWidget *container;

  gint x;
  gint y;
};

589 590 591 592 593 594 595 596 597
static void
prepend_and_ref_widget (GtkWidget *widget,
                        gpointer   data)
{
  GSList **slist_p = data;

  *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
}

Kristian Rietveld's avatar
Kristian Rietveld committed
598 599 600 601
static void
child_location_foreach (GtkWidget *child,
			gpointer   data)
{
602
  GtkAllocation child_allocation;
603
  gint x, y;
Kristian Rietveld's avatar
Kristian Rietveld committed
604 605
  struct ChildLocation *child_loc = data;

606
  /* Ignore invisible widgets */
607
  if (!gtk_widget_is_drawable (child))
608 609
    return;

610 611
  gtk_widget_get_allocation (child, &child_allocation);

612 613 614
  x = 0;
  y = 0;

615 616 617 618
  /* (child_loc->x, child_loc->y) are relative to
   * child_loc->container's allocation.
   */

619
  if (!child_loc->child &&
Kristian Rietveld's avatar
Kristian Rietveld committed
620 621
      gtk_widget_translate_coordinates (child_loc->container, child,
					child_loc->x, child_loc->y,
622 623
					&x, &y))
    {
624 625 626
#ifdef DEBUG_TOOLTIP
      g_print ("candidate: %s  alloc=[(%d,%d)  %dx%d]     (%d, %d)->(%d, %d)\n",
	       gtk_widget_get_name (child),
627 628 629 630
               child_allocation.x,
               child_allocation.y,
               child_allocation.width,
               child_allocation.height,
631 632 633 634 635
	       child_loc->x, child_loc->y,
	       x, y);
#endif /* DEBUG_TOOLTIP */

      /* (x, y) relative to child's allocation. */
636 637
      if (x >= 0 && x < child_allocation.width
	  && y >= 0 && y < child_allocation.height)
Kristian Rietveld's avatar
Kristian Rietveld committed
638 639 640 641
        {
	  if (GTK_IS_CONTAINER (child))
	    {
	      struct ChildLocation tmp = { NULL, NULL, 0, 0 };
642
              GSList *children = NULL, *tmp_list;
Kristian Rietveld's avatar
Kristian Rietveld committed
643

644 645 646
	      /* Take (x, y) relative the child's allocation and
	       * recurse.
	       */
Kristian Rietveld's avatar
Kristian Rietveld committed
647 648 649 650
	      tmp.x = x;
	      tmp.y = y;
	      tmp.container = child;

651
	      gtk_container_forall (GTK_CONTAINER (child),
652 653 654 655 656 657 658
				    prepend_and_ref_widget, &children);

              for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
                {
                  child_location_foreach (tmp_list->data, &tmp);
                  g_object_unref (tmp_list->data);
                }
Kristian Rietveld's avatar
Kristian Rietveld committed
659 660 661 662 663

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

              g_slist_free (children);
Kristian Rietveld's avatar
Kristian Rietveld committed
666 667 668 669 670 671 672
	    }
	  else
	    child_loc->child = child;
	}
    }
}

673 674 675
/* 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
676 677 678 679 680 681 682
static void
window_to_alloc (GtkWidget *dest_widget,
		 gint       src_x,
		 gint       src_y,
		 gint      *dest_x,
		 gint      *dest_y)
{
683 684 685 686
  GtkAllocation allocation;

  gtk_widget_get_allocation (dest_widget, &allocation);

Kristian Rietveld's avatar
Kristian Rietveld committed
687
  /* Translate from window relative to allocation relative */
688 689
  if (gtk_widget_get_has_window (dest_widget) &&
      gtk_widget_get_parent (dest_widget))
Kristian Rietveld's avatar
Kristian Rietveld committed
690 691
    {
      gint wx, wy;
692 693
      gdk_window_get_position (gtk_widget_get_window (dest_widget),
                               &wx, &wy);
Kristian Rietveld's avatar
Kristian Rietveld committed
694

695 696 697
      /* Offset coordinates if widget->window is smaller than
       * widget->allocation.
       */
698 699
      src_x += wx - allocation.x;
      src_y += wy - allocation.y;
Kristian Rietveld's avatar
Kristian Rietveld committed
700 701 702
    }
  else
    {
703 704
      src_x -= allocation.x;
      src_y -= allocation.y;
Kristian Rietveld's avatar
Kristian Rietveld committed
705 706 707 708 709 710 711 712
    }

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

713 714 715
/* Translates coordinates from window relative (x, y) to
 * allocation relative (x, y) of the returned widget.
 */
716 717 718 719 720 721
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
722 723 724 725
{
  GtkWidget *event_widget;
  struct ChildLocation child_loc = { NULL, NULL, 0, 0 };

726 727
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

728 729 730 731 732
  gdk_window_get_user_data (window, (void **)&event_widget);

  if (!event_widget)
    return NULL;

733 734 735
#ifdef DEBUG_TOOLTIP
  g_print ("event window %p (belonging to %p (%s))  (%d, %d)\n",
	   window, event_widget, gtk_widget_get_name (event_widget),
736
	   window_x, window_y);
737 738 739
#endif

  /* Coordinates are relative to event window */
740 741
  child_loc.x = window_x;
  child_loc.y = window_y;
Kristian Rietveld's avatar
Kristian Rietveld committed
742

743 744 745 746
  /* 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.
   */
747
  while (window && window != gtk_widget_get_window (event_widget))
748
    {
749
      gdouble px, py;
750

751 752 753 754 755
      gdk_window_coords_to_parent (window,
                                   child_loc.x, child_loc.y,
                                   &px, &py);
      child_loc.x = px;
      child_loc.y = py;
756

757
      window = gdk_window_get_effective_parent (window);
758 759 760 761 762 763 764 765 766 767
    }

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

768 769 770 771 772 773 774
  /* 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
775 776
  if (GTK_IS_CONTAINER (event_widget))
    {
777
      GtkWidget *container = event_widget;
Kristian Rietveld's avatar
Kristian Rietveld committed
778 779 780 781

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

782 783
      gtk_container_forall (GTK_CONTAINER (event_widget),
			    child_location_foreach, &child_loc);
Kristian Rietveld's avatar
Kristian Rietveld committed
784

785 786 787 788
      /* Here we have a widget, with coordinates relative to
       * child_loc.container's allocation.
       */

Kristian Rietveld's avatar
Kristian Rietveld committed
789 790 791 792
      if (child_loc.child)
	event_widget = child_loc.child;
      else if (child_loc.container)
	event_widget = child_loc.container;
793 794 795 796 797

      /* 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
798 799
    }

800
  /* We return (x, y) relative to the allocation of event_widget. */
801 802 803 804
  if (widget_x)
    *widget_x = child_loc.x;
  if (widget_y)
    *widget_y = child_loc.y;
Kristian Rietveld's avatar
Kristian Rietveld committed
805 806 807 808

  return event_widget;
}

809 810 811
/* Ignores (x, y) on input, translates event coordinates to
 * allocation relative (x, y) of the returned widget.
 */
Kristian Rietveld's avatar
Kristian Rietveld committed
812 813 814 815 816
static GtkWidget *
find_topmost_widget_coords_from_event (GdkEvent *event,
				       gint     *x,
				       gint     *y)
{
817
  GtkAllocation allocation;
Kristian Rietveld's avatar
Kristian Rietveld committed
818 819 820 821 822 823
  gint tx, ty;
  gdouble dx, dy;
  GtkWidget *tmp;

  gdk_event_get_coords (event, &dx, &dy);

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

827
  if (!tmp)
828 829
    return NULL;

830
  /* Make sure the pointer can actually be on the widget returned. */
831 832 833
  gtk_widget_get_allocation (tmp, &allocation);
  if (tx < 0 || tx >= allocation.width ||
      ty < 0 || ty >= allocation.height)
834
    return NULL;
835

836 837 838 839
  if (x)
    *x = tx;
  if (y)
    *y = ty;
Kristian Rietveld's avatar
Kristian Rietveld committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

  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)
{
866
  g_object_set_data (G_OBJECT (display), "gdk-display-current-tooltip", NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
867 868
}

869 870 871 872
static void
gtk_tooltip_set_last_window (GtkTooltip *tooltip,
			     GdkWindow  *window)
{
873 874 875
  if (tooltip->last_window == window)
    return;

876 877 878 879 880 881 882 883 884 885 886
  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
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
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)
        {
914
	  GtkWidget *parent = gtk_widget_get_parent (*widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
915 916 917 918 919 920 921 922 923 924 925

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

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

926 927 928 929 930 931
  /* 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
932 933 934
  return return_value;
}

935 936 937 938
static void
get_bounding_box (GtkWidget    *widget,
                  GdkRectangle *bounds)
{
939
  GtkAllocation allocation;
940 941 942 943 944 945 946 947 948
  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);
949 950
  if (window == NULL)
    window = gtk_widget_get_window (widget);
951

952 953 954 955 956
  gtk_widget_get_allocation (widget, &allocation);
  x = allocation.x;
  y = allocation.y;
  w = allocation.width;
  h = allocation.height;
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

  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;
}

972 973 974 975 976
static void
gtk_tooltip_position (GtkTooltip *tooltip,
		      GdkDisplay *display,
		      GtkWidget  *new_tooltip_widget)
{
977
  gint x, y, width, height;
978
  GdkScreen *screen;
Matthias Clasen's avatar
Matthias Clasen committed
979 980 981 982 983 984
  gint monitor_num;
  GdkRectangle monitor;
  guint cursor_size;
  GdkRectangle bounds;

#define MAX_DISTANCE 32
985

986
  gtk_widget_realize (GTK_WIDGET (tooltip->current_window));
987
  gtk_widget_set_visible (GTK_WIDGET (tooltip->current_window), TRUE);
988

989 990
  tooltip->tooltip_widget = new_tooltip_widget;

Matthias Clasen's avatar
Matthias Clasen committed
991 992
  screen = gtk_widget_get_screen (new_tooltip_widget);

993 994
  width = gtk_widget_get_allocated_width (GTK_WIDGET (tooltip->current_window));
  height = gtk_widget_get_allocated_height (GTK_WIDGET (tooltip->current_window));
Matthias Clasen's avatar
Matthias Clasen committed
995 996 997 998

  monitor_num = gdk_screen_get_monitor_at_point (screen,
                                                 tooltip->last_x,
                                                 tooltip->last_y);
999
  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
Matthias Clasen's avatar
Matthias Clasen committed
1000 1001 1002

  get_bounding_box (new_tooltip_widget, &bounds);

1003
  /* Position the tooltip */
Matthias Clasen's avatar
Matthias Clasen committed
1004 1005 1006 1007

  cursor_size = gdk_display_get_default_cursor_size (display);

  /* Try below */
1008
  x = bounds.x + bounds.width / 2 - width / 2;
Matthias Clasen's avatar
Matthias Clasen committed
1009 1010
  y = bounds.y + bounds.height + 4;

1011
  if (y + height <= monitor.y + monitor.height)
1012
    {
Matthias Clasen's avatar
Matthias Clasen committed
1013 1014
      if (tooltip->keyboard_mode_enabled)
        goto found;
1015

Matthias Clasen's avatar
Matthias Clasen committed
1016 1017 1018 1019
      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;
1020 1021
          else if (x + width < tooltip->last_x - MAX_DISTANCE)
            x = tooltip->last_x - MAX_DISTANCE - width;
1022

Matthias Clasen's avatar
Matthias Clasen committed
1023 1024 1025 1026 1027
          goto found;
        }
   }

  /* Try above */
1028 1029
  x = bounds.x + bounds.width / 2 - width / 2;
  y = bounds.y - height - 4;
Matthias Clasen's avatar
Matthias Clasen committed
1030 1031 1032 1033 1034 1035

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

1036
      if (y + height >= tooltip->last_y - MAX_DISTANCE)
Matthias Clasen's avatar
Matthias Clasen committed
1037 1038 1039
        {
          if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
            x = tooltip->last_x + cursor_size + MAX_DISTANCE;
1040 1041
          else if (x + width < tooltip->last_x - MAX_DISTANCE)
            x = tooltip->last_x - MAX_DISTANCE - width;
Matthias Clasen's avatar
Matthias Clasen committed
1042 1043 1044

          goto found;
        }
1045
    }
Matthias Clasen's avatar
Matthias Clasen committed
1046 1047 1048

  /* Try right FIXME: flip on rtl ? */
  x = bounds.x + bounds.width + 4;
1049
  y = bounds.y + bounds.height / 2 - height / 2;
Matthias Clasen's avatar
Matthias Clasen committed
1050

1051
  if (x + width <= monitor.x + monitor.width)
1052
    {
Matthias Clasen's avatar
Matthias Clasen committed
1053 1054
      if (tooltip->keyboard_mode_enabled)
        goto found;
1055

Matthias Clasen's avatar
Matthias Clasen committed
1056 1057 1058 1059
      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;
1060 1061
          else if (y + height < tooltip->last_y - MAX_DISTANCE)
            y = tooltip->last_y - MAX_DISTANCE - height;
1062

Matthias Clasen's avatar
Matthias Clasen committed
1063 1064
          goto found;
        }
1065 1066
    }

Matthias Clasen's avatar
Matthias Clasen committed
1067
  /* Try left FIXME: flip on rtl ? */
1068 1069
  x = bounds.x - width - 4;
  y = bounds.y + bounds.height / 2 - height / 2;
1070

Matthias Clasen's avatar
Matthias Clasen committed
1071
  if (x >= monitor.x)
1072
    {
Matthias Clasen's avatar
Matthias Clasen committed
1073 1074
      if (tooltip->keyboard_mode_enabled)
        goto found;
1075

1076
      if (x + width >= tooltip->last_x - MAX_DISTANCE)
Matthias Clasen's avatar
Matthias Clasen committed
1077 1078 1079
        {
          if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
            y = tooltip->last_y + cursor_size + MAX_DISTANCE;
1080 1081
          else if (y + height < tooltip->last_y - MAX_DISTANCE)
            y = tooltip->last_y - MAX_DISTANCE - height;
1082

Matthias Clasen's avatar
Matthias Clasen committed
1083 1084 1085
          goto found;
        }
    }
1086

Matthias Clasen's avatar
Matthias Clasen committed
1087 1088 1089
   /* Fallback */
  if (tooltip->keyboard_mode_enabled)
    {
1090
      x = bounds.x + bounds.width / 2 - width / 2;
Matthias Clasen's avatar
Matthias Clasen committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
      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)
    {
1104 1105
      if (x + width > monitor.x + monitor.width)
        x -= x - (monitor.x + monitor.width) + width;
1106 1107 1108
      else if (x < monitor.x)
        x = monitor.x;

1109 1110
      if (y + height > monitor.y + monitor.height)
        y -= y - (monitor.y + monitor.height) + height;
Matthias Clasen's avatar
Matthias Clasen committed
1111 1112 1113
      else if (y < monitor.y)
        y = monitor.y;

1114 1115 1116
      if (!tooltip->keyboard_mode_enabled)
        {
          /* don't pop up under the pointer */
1117 1118 1119
          if (x <= tooltip->last_x && tooltip->last_x < x + width &&
              y <= tooltip->last_y && tooltip->last_y < y + height)
            y = tooltip->last_y - height - 2;
1120
        }
Matthias Clasen's avatar
Matthias Clasen committed
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
#ifdef GDK_WINDOWING_WAYLAND
      /* set the transient parent on the tooltip when running with the Wayland
       * backend to allow correct positioning of the tooltip windows */
      if (GDK_IS_WAYLAND_DISPLAY (display))
        {
          GtkWidget *toplevel;

          toplevel = gtk_widget_get_toplevel (tooltip->tooltip_widget);
          if (GTK_IS_WINDOW (toplevel))
            gtk_window_set_transient_for (GTK_WINDOW (tooltip->current_window),
                                          GTK_WINDOW (toplevel));
        }
#endif

1136 1137 1138 1139 1140
      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
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
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
1154
                               "gdk-display-current-tooltip");
Kristian Rietveld's avatar
Kristian Rietveld committed
1155 1156 1157

  if (tooltip->keyboard_mode_enabled)
    {
1158
      x = y = -1;
Matthias Clasen's avatar
Matthias Clasen committed
1159
      tooltip_widget = tooltip->keyboard_widget;
Kristian Rietveld's avatar
Kristian Rietveld committed
1160 1161 1162
    }
  else
    {
1163
      GdkDevice *device;
1164 1165
      gint tx, ty;

Kristian Rietveld's avatar
Kristian Rietveld committed
1166 1167
      window = tooltip->last_window;

1168
      if (!GDK_IS_WINDOW (window))
Matthias Clasen's avatar
Matthias Clasen committed
1169
        return;
1170

1171 1172 1173
      device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));

      gdk_window_get_device_position (window, device, &x, &y, NULL);
1174 1175 1176 1177

      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
1178

Matthias Clasen's avatar
Matthias Clasen committed
1179
      tooltip_widget = _gtk_widget_find_at_coords (window, x, y, &x, &y);
Kristian Rietveld's avatar
Kristian Rietveld committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
    }

  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
1194
        tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
1195
      else
Matthias Clasen's avatar
Matthias Clasen committed
1196
        tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1197 1198 1199 1200
    }

  screen = gtk_widget_get_screen (tooltip_widget);

1201
  /* FIXME: should use tooltip->current_window iso tooltip->window */
Kristian Rietveld's avatar
Kristian Rietveld committed
1202 1203 1204
  if (screen != gtk_widget_get_screen (tooltip->window))
    {
      g_signal_handlers_disconnect_by_func (display,
Matthias Clasen's avatar
Matthias Clasen committed
1205 1206
                                            gtk_tooltip_display_closed,
                                            tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1207 1208 1209 1210

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

      g_signal_connect (display, "closed",
Matthias Clasen's avatar
Matthias Clasen committed
1211
                        G_CALLBACK (gtk_tooltip_display_closed), tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1212 1213
    }

1214
  gtk_tooltip_position (tooltip, display, tooltip_widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

  /* 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)
{
1230
  if (!tooltip)
Kristian Rietveld's avatar
Kristian Rietveld committed
1231 1232 1233 1234 1235 1236 1237 1238
    return;

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

1239 1240 1241 1242 1243
  if (!GTK_TOOLTIP_VISIBLE (tooltip))
    return;

  tooltip->tooltip_widget = NULL;

Kristian Rietveld's avatar
Kristian Rietveld committed
1244 1245
  if (!tooltip->keyboard_mode_enabled)
    {
1246
      guint timeout = BROWSE_DISABLE_TIMEOUT;
Kristian Rietveld's avatar
Kristian Rietveld committed
1247 1248 1249 1250 1251

      /* The tooltip is gone, after (by default, should be configurable) 500ms
       * we want to turn off browse mode
       */
      if (!tooltip->browse_mode_timeout_id)
Bastien Nocera's avatar
Bastien Nocera committed
1252 1253 1254 1255 1256 1257 1258 1259
        {
	  tooltip->browse_mode_timeout_id =
	    gdk_threads_add_timeout_full (0, timeout,
					  tooltip_browse_mode_expired,
					  g_object_ref (tooltip),
					  g_object_unref);
	  g_source_set_name_by_id (tooltip->browse_mode_timeout_id, "[gtk+] tooltip_browse_mode_expired");
	}
Kristian Rietveld's avatar
Kristian Rietveld committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    }
  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;

1283
  display = GDK_DISPLAY (data);
1284 1285 1286 1287 1288 1289 1290 1291
  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
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

  gtk_tooltip_show_tooltip (display);

  tooltip->timeout_id = 0;

  return FALSE;
}

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

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

1309
  if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
Kristian Rietveld's avatar
Kristian Rietveld committed
1310 1311 1312 1313 1314 1315
    return;

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

  if (tooltip->browse_mode_enabled)
1316
    timeout = BROWSE_TIMEOUT;
Kristian Rietveld's avatar
Kristian Rietveld committed
1317
  else
1318
    timeout = HOVER_TIMEOUT;
Kristian Rietveld's avatar
Kristian Rietveld committed
1319

Kristian Rietveld's avatar
Kristian Rietveld committed
1320 1321 1322 1323
  tooltip->timeout_id = gdk_threads_add_timeout_full (0, timeout,
						      tooltip_popup_timeout,
						      g_object_ref (display),
						      g_object_unref);
Bastien Nocera's avatar
Bastien Nocera committed
1324
  g_source_set_name_by_id (tooltip->timeout_id, "[gtk+] tooltip_popup_timeout");
Kristian Rietveld's avatar
Kristian Rietveld committed
1325 1326 1327 1328 1329 1330 1331 1332 1333
}

void
_gtk_tooltip_focus_in (GtkWidget *widget)
{
  gint x, y;
  gboolean return_value = FALSE;
  GdkDisplay *display;
  GtkTooltip *tooltip;
1334
  GdkDevice *device;
Kristian Rietveld's avatar
Kristian Rietveld committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344

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

1345 1346
  device = gtk_get_current_event_device ();

1347
  if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1348 1349 1350 1351 1352 1353 1354 1355
    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
1356 1357 1358 1359 1360
  if (tooltip->keyboard_widget)
    g_object_unref (tooltip->keyboard_widget);

  tooltip->keyboard_widget = g_object_ref (widget);

1361 1362
  gdk_window_get_device_position (gtk_widget_get_window (widget),
                                  device, &x, &y, NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 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

  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);
1421 1422 1423
      g_signal_connect (display, "closed",
			G_CALLBACK (gtk_tooltip_display_closed),
			tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
    }

  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
1462
      || gtk_widget_get_window (toplevel) == tooltip->toplevel_window)
Kristian Rietveld's avatar
Kristian Rietveld committed
1463 1464 1465
    gtk_tooltip_hide_tooltip (tooltip);
}

1466
static gboolean
1467
tooltips_enabled (GdkEvent *event)
1468
{
1469 1470
  GdkDevice *source_device;
  GdkInputSource source;
1471

1472 1473 1474 1475 1476 1477
  source_device = gdk_event_get_source_device (event);

  if (!source_device)
    return FALSE;

  source = gdk_device_get_source (source_device);
1478

1479
  if (source != GDK_SOURCE_TOUCHSCREEN)
1480 1481 1482
    return TRUE;

  return FALSE;
1483 1484
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1485 1486 1487 1488 1489 1490 1491 1492
void
_gtk_tooltip_handle_event (GdkEvent *event)
{
  gint x, y;
  gboolean return_value = FALSE;
  GtkWidget *has_tooltip_widget = NULL;
  GdkDisplay *display;
  GtkTooltip *current_tooltip;
1493

1494
  if (!tooltips_enabled (event))
1495
    return;
Kristian Rietveld's avatar
Kristian Rietveld committed
1496