gtktooltip.c 35 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"
Kristian Rietveld's avatar
Kristian Rietveld committed
23 24 25 26 27 28 29 30 31
#include "gtktooltip.h"
#include "gtkintl.h"
#include "gtkwindow.h"
#include "gtkmain.h"
#include "gtklabel.h"
#include "gtkimage.h"
#include "gtkhbox.h"
#include "gtkalignment.h"

Matthias Clasen's avatar
Matthias Clasen committed
32 33
#include "gtkalias.h"

Kristian Rietveld's avatar
Kristian Rietveld committed
34 35
#include <string.h>

36 37
#undef DEBUG_TOOLTIP

Kristian Rietveld's avatar
Kristian Rietveld 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

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

69 70
  GdkRectangle tip_area;

Kristian Rietveld's avatar
Kristian Rietveld committed
71 72
  guint browse_mode_enabled : 1;
  guint keyboard_mode_enabled : 1;
73
  guint tip_area_set : 1;
74
  guint custom_was_reset : 1;
Kristian Rietveld's avatar
Kristian Rietveld committed
75 76 77 78 79 80 81 82 83 84 85 86
};

struct _GtkTooltipClass
{
  GObjectClass parent_class;
};

#define GTK_TOOLTIP_VISIBLE(tooltip) ((tooltip)->current_window && GTK_WIDGET_VISIBLE ((tooltip)->current_window))


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

89
static void       gtk_tooltip_window_style_set     (GtkTooltip      *tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
90 91 92 93 94 95
static gboolean   gtk_tooltip_paint_window         (GtkTooltip      *tooltip);
static void       gtk_tooltip_window_hide          (GtkWidget       *widget,
						    gpointer         user_data);
static void       gtk_tooltip_display_closed       (GdkDisplay      *display,
						    gboolean         was_error,
						    GtkTooltip      *tooltip);
96 97
static void       gtk_tooltip_set_last_window      (GtkTooltip      *tooltip,
						    GdkWindow       *window);
Kristian Rietveld's avatar
Kristian Rietveld committed
98 99 100 101 102 103 104 105 106 107 108


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
109
  object_class->dispose = gtk_tooltip_dispose;
Kristian Rietveld's avatar
Kristian Rietveld committed
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 138 139 140 141 142 143 144 145 146
}

static void
gtk_tooltip_init (GtkTooltip *tooltip)
{
  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);

  tooltip->alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
			     tooltip->window->style->ythickness,
			     tooltip->window->style->ythickness,
			     tooltip->window->style->xthickness,
			     tooltip->window->style->xthickness);
  gtk_container_add (GTK_CONTAINER (tooltip->window), tooltip->alignment);
  gtk_widget_show (tooltip->alignment);

147
  g_signal_connect_swapped (tooltip->window, "style-set",
148
			    G_CALLBACK (gtk_tooltip_window_style_set), tooltip);
149
  g_signal_connect_swapped (tooltip->window, "expose-event",
Kristian Rietveld's avatar
Kristian Rietveld committed
150 151 152 153 154 155 156 157 158 159 160
			    G_CALLBACK (gtk_tooltip_paint_window), tooltip);

  tooltip->box = gtk_hbox_new (FALSE, tooltip->window->style->xthickness);
  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 ("");
161
  gtk_label_set_line_wrap (GTK_LABEL (tooltip->label), TRUE);
Kristian Rietveld's avatar
Kristian Rietveld committed
162 163 164 165 166 167 168
  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
169
gtk_tooltip_dispose (GObject *object)
Kristian Rietveld's avatar
Kristian Rietveld committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
{
  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;
    }

185
  gtk_tooltip_set_custom (tooltip, NULL);
186 187
  gtk_tooltip_set_last_window (tooltip, NULL);

Kristian Rietveld's avatar
Kristian Rietveld committed
188 189 190 191 192 193 194 195 196
  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
197
      tooltip->window = NULL;
Kristian Rietveld's avatar
Kristian Rietveld committed
198
    }
Michael Natterer's avatar
Michael Natterer committed
199

Paolo Borelli's avatar
Paolo Borelli committed
200
  G_OBJECT_CLASS (gtk_tooltip_parent_class)->dispose (object);
Kristian Rietveld's avatar
Kristian Rietveld committed
201 202 203 204 205 206
}

/* public API */

/**
 * gtk_tooltip_set_markup:
Matthias Clasen's avatar
Matthias Clasen committed
207
 * @tooltip: a #GtkTooltip
Kristian Rietveld's avatar
Kristian Rietveld committed
208 209
 * @markup: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>) or %NULL
 *
Matthias Clasen's avatar
Matthias Clasen committed
210
 * Sets the text of the tooltip to be @markup, which is marked up
Kristian Rietveld's avatar
Kristian Rietveld committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
 * 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);
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/**
 * gtk_tooltip_set_text:
 * @tooltip: a #GtkTooltip
 * @text: a text string or %NULL
 *
 * Sets the text of the tooltip to be @text. If @text is %NULL, the label
 * will be hidden. See also gtk_tooltip_set_markup().
 *
 * Since: 2.12
 */
void
gtk_tooltip_set_text (GtkTooltip  *tooltip,
                      const gchar *text)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  gtk_label_set_text (GTK_LABEL (tooltip->label), text);

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

Kristian Rietveld's avatar
Kristian Rietveld committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/**
 * gtk_tooltip_set_icon:
 * @tooltip: a #GtkTooltip
 * @pixbuf: a #GdkPixbuf, or %NULL
 *
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
284
 * @stock_id: a stock id, or %NULL
Kristian Rietveld's avatar
Kristian Rietveld committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
 * @size: a stock icon size
 *
 * Sets the icon of the tooltip (which is in front of the text) to be
 * the stock item indicated by @stock_id with the size indicated
 * 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
308 309 310 311 312 313 314 315 316 317
/**
 * gtk_tooltip_set_icon_from_icon_name:
 * @tooltip: a #GtkTooltip
 * @icon_name: an icon name, or %NULL
 * @size: a stock icon size
 *
 * Sets the icon of the tooltip (which is in front of the text) to be
 * the icon indicated by @icon_name with the size indicated
 * by @size.  If @icon_name is %NULL, the image will be hidden.
 *
318
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
 */
void
gtk_tooltip_set_icon_from_icon_name(GtkTooltip  *tooltip,
				    const gchar *icon_name,
				    GtkIconSize  size)
{
  g_return_if_fail (GTK_IS_TOOLTIP (tooltip));

  gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);

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

Kristian Rietveld's avatar
Kristian Rietveld committed
335 336
/**
 * gtk_tooltip_set_custom:
Matthias Clasen's avatar
Matthias Clasen committed
337
 * @tooltip: a #GtkTooltip
338
 * @custom_widget: a #GtkWidget, or %NULL to unset the old custom widget.
Kristian Rietveld's avatar
Kristian Rietveld committed
339
 *
340 341 342
 * 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
343 344 345
 * 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().
346

Kristian Rietveld's avatar
Kristian Rietveld committed
347 348 349 350 351 352 353 354 355 356 357
 *
 * 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));

358 359 360 361 362 363 364 365 366
  /* 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
367 368
  if (tooltip->custom_widget)
    {
369 370 371 372 373 374 375 376
      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
377 378 379 380 381 382 383 384 385 386 387
    }

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

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/**
 * 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
405 406
gtk_tooltip_set_tip_area (GtkTooltip         *tooltip,
			  const GdkRectangle *rect)
407 408 409 410 411 412 413 414 415 416 417 418
{
  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
419 420
/**
 * gtk_tooltip_trigger_tooltip_query:
421
 * @display: a #GdkDisplay
Kristian Rietveld's avatar
Kristian Rietveld committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
 *
 * 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;

  /* Trigger logic as if the mouse moved */
  window = gdk_display_get_window_at_pointer (display, &x, &y);
  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;

448 449 450 451
  gdk_window_get_origin (window, &x, &y);
  event.motion.x_root = event.motion.x + x;
  event.motion.y_root = event.motion.y + y;

Kristian Rietveld's avatar
Kristian Rietveld committed
452 453 454 455 456 457 458 459 460 461
  _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);
462
  gtk_tooltip_set_tip_area (tooltip, NULL);
463 464 465 466 467

  /* 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
468 469
}

470 471 472 473 474 475 476 477 478 479 480 481
static void
gtk_tooltip_window_style_set (GtkTooltip *tooltip)
{
  gtk_alignment_set_padding (GTK_ALIGNMENT (tooltip->alignment),
			     tooltip->window->style->ythickness,
			     tooltip->window->style->ythickness,
			     tooltip->window->style->xthickness,
			     tooltip->window->style->xthickness);

  gtk_widget_queue_draw (tooltip->window);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
static gboolean
gtk_tooltip_paint_window (GtkTooltip *tooltip)
{
  gtk_paint_flat_box (tooltip->window->style,
		      tooltip->window->window,
		      GTK_STATE_NORMAL,
		      GTK_SHADOW_OUT,
		      NULL,
		      tooltip->window,
		      "tooltip",
		      0, 0,
		      tooltip->window->allocation.width,
		      tooltip->window->allocation.height);

  return FALSE;
}

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

505
  gtk_tooltip_set_custom (tooltip, NULL);
Kristian Rietveld's avatar
Kristian Rietveld committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
}

/* event handling, etc */

struct ChildLocation
{
  GtkWidget *child;
  GtkWidget *container;

  gint x;
  gint y;
};

static void
child_location_foreach (GtkWidget *child,
			gpointer   data)
{
523
  gint x, y;
Kristian Rietveld's avatar
Kristian Rietveld committed
524 525
  struct ChildLocation *child_loc = data;

526 527 528 529
  /* Ignore invisible widgets */
  if (!GTK_WIDGET_DRAWABLE (child))
    return;

530 531 532
  x = 0;
  y = 0;

533 534 535 536
  /* (child_loc->x, child_loc->y) are relative to
   * child_loc->container's allocation.
   */

537
  if (!child_loc->child &&
Kristian Rietveld's avatar
Kristian Rietveld committed
538 539
      gtk_widget_translate_coordinates (child_loc->container, child,
					child_loc->x, child_loc->y,
540 541
					&x, &y))
    {
542 543 544 545 546 547 548 549 550 551 552 553
#ifdef DEBUG_TOOLTIP
      g_print ("candidate: %s  alloc=[(%d,%d)  %dx%d]     (%d, %d)->(%d, %d)\n",
	       gtk_widget_get_name (child),
	       child->allocation.x,
	       child->allocation.y,
	       child->allocation.width,
	       child->allocation.height,
	       child_loc->x, child_loc->y,
	       x, y);
#endif /* DEBUG_TOOLTIP */

      /* (x, y) relative to child's allocation. */
Kristian Rietveld's avatar
Kristian Rietveld committed
554 555 556 557 558 559 560
      if (x >= 0 && x < child->allocation.width
	  && y >= 0 && y < child->allocation.height)
        {
	  if (GTK_IS_CONTAINER (child))
	    {
	      struct ChildLocation tmp = { NULL, NULL, 0, 0 };

561 562 563
	      /* Take (x, y) relative the child's allocation and
	       * recurse.
	       */
Kristian Rietveld's avatar
Kristian Rietveld committed
564 565 566 567
	      tmp.x = x;
	      tmp.y = y;
	      tmp.container = child;

568 569
	      gtk_container_forall (GTK_CONTAINER (child),
				    child_location_foreach, &tmp);
Kristian Rietveld's avatar
Kristian Rietveld committed
570 571 572 573 574 575 576 577 578 579 580 581

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

582 583 584
/* 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
585 586 587 588 589 590 591 592 593 594 595 596 597
static void
window_to_alloc (GtkWidget *dest_widget,
		 gint       src_x,
		 gint       src_y,
		 gint      *dest_x,
		 gint      *dest_y)
{
  /* Translate from window relative to allocation relative */
  if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
    {
      gint wx, wy;
      gdk_window_get_position (dest_widget->window, &wx, &wy);

598 599 600
      /* Offset coordinates if widget->window is smaller than
       * widget->allocation.
       */
Kristian Rietveld's avatar
Kristian Rietveld committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
      src_x += wx - dest_widget->allocation.x;
      src_y += wy - dest_widget->allocation.y;
    }
  else
    {
      src_x -= dest_widget->allocation.x;
      src_y -= dest_widget->allocation.y;
    }

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

616 617 618
/* Translates coordinates from window relative (x, y) to
 * allocation relative (x, y) of the returned widget.
 */
Kristian Rietveld's avatar
Kristian Rietveld committed
619 620 621 622 623 624 625 626
static GtkWidget *
find_widget_under_pointer (GdkWindow *window,
			   gint      *x,
			   gint      *y)
{
  GtkWidget *event_widget;
  struct ChildLocation child_loc = { NULL, NULL, 0, 0 };

627 628 629 630 631
  gdk_window_get_user_data (window, (void **)&event_widget);

  if (!event_widget)
    return NULL;

632 633 634 635 636 637 638
#ifdef DEBUG_TOOLTIP
  g_print ("event window %p (belonging to %p (%s))  (%d, %d)\n",
	   window, event_widget, gtk_widget_get_name (event_widget),
	   *x, *y);
#endif

  /* Coordinates are relative to event window */
Kristian Rietveld's avatar
Kristian Rietveld committed
639 640 641
  child_loc.x = *x;
  child_loc.y = *y;

642 643 644 645
  /* 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.
   */
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  while (window && window != event_widget->window)
    {
      gint px, py;

      gdk_window_get_position (window, &px, &py);
      child_loc.x += px;
      child_loc.y += py;

      window = gdk_window_get_parent (window);
    }

  /* Failing to find widget->window can happen for e.g. a detached handle box;
   * chaining ::query-tooltip up to its parent probably makes little sense,
   * and users better implement tooltips on handle_box->child.
   * so we simply ignore the event for tooltips here.
   */
  if (!window)
    return NULL;

665 666 667 668 669 670 671
  /* 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
672 673
  if (GTK_IS_CONTAINER (event_widget))
    {
674
      GtkWidget *container = event_widget;
Kristian Rietveld's avatar
Kristian Rietveld committed
675 676 677 678

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

679 680
      gtk_container_forall (GTK_CONTAINER (event_widget),
			    child_location_foreach, &child_loc);
Kristian Rietveld's avatar
Kristian Rietveld committed
681

682 683 684 685
      /* Here we have a widget, with coordinates relative to
       * child_loc.container's allocation.
       */

Kristian Rietveld's avatar
Kristian Rietveld committed
686 687 688 689
      if (child_loc.child)
	event_widget = child_loc.child;
      else if (child_loc.container)
	event_widget = child_loc.container;
690 691 692 693 694 695

      /* 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
696 697
    }

698
  /* We return (x, y) relative to the allocation of event_widget. */
Kristian Rietveld's avatar
Kristian Rietveld committed
699 700 701 702 703 704 705 706
  if (x)
    *x = child_loc.x;
  if (y)
    *y = child_loc.y;

  return event_widget;
}

707 708 709
/* Ignores (x, y) on input, translates event coordinates to
 * allocation relative (x, y) of the returned widget.
 */
Kristian Rietveld's avatar
Kristian Rietveld committed
710 711 712 713 714 715 716 717 718 719 720 721 722
static GtkWidget *
find_topmost_widget_coords_from_event (GdkEvent *event,
				       gint     *x,
				       gint     *y)
{
  gint tx, ty;
  gdouble dx, dy;
  GtkWidget *tmp;

  gdk_event_get_coords (event, &dx, &dy);
  tx = dx;
  ty = dy;

723
  /* Returns coordinates relative to tmp's allocation. */
724
  tmp = find_widget_under_pointer (event->any.window, &tx, &ty);
Kristian Rietveld's avatar
Kristian Rietveld committed
725

726
  if (!tmp)
727 728
    return NULL;

729 730 731 732
  /* Make sure the pointer can actually be on the widget returned. */
  if (tx < 0 || tx >= tmp->allocation.width ||
      ty < 0 || ty >= tmp->allocation.height)
    return NULL;
733

734 735 736 737
  if (x)
    *x = tx;
  if (y)
    *y = ty;
Kristian Rietveld's avatar
Kristian Rietveld committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

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

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
static void
gtk_tooltip_set_last_window (GtkTooltip *tooltip,
			     GdkWindow  *window)
{
  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
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
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)
        {
	  GtkWidget *parent = (*widget)->parent;

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

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

821 822 823 824 825 826
  /* 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
827 828 829
  return return_value;
}

830 831 832 833 834 835 836 837 838 839 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 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
static void
gtk_tooltip_position (GtkTooltip *tooltip,
		      GdkDisplay *display,
		      GtkWidget  *new_tooltip_widget)
{
  gint x, y;
  GdkScreen *screen;

  tooltip->tooltip_widget = new_tooltip_widget;

  /* Position the tooltip */
  /* FIXME: should we swap this when RTL is enabled? */
  if (tooltip->keyboard_mode_enabled)
    {
      gdk_window_get_origin (new_tooltip_widget->window, &x, &y);
      if (GTK_WIDGET_NO_WINDOW (new_tooltip_widget))
        {
	  x += new_tooltip_widget->allocation.x;
	  y += new_tooltip_widget->allocation.y;
	}

      /* For keyboard mode we position the tooltip below the widget,
       * right of the center of the widget.
       */
      x += new_tooltip_widget->allocation.width / 2;
      y += new_tooltip_widget->allocation.height + 4;
    }
  else
    {
      guint cursor_size;

      x = tooltip->last_x;
      y = tooltip->last_y;

      /* For mouse mode, we position the tooltip right of the cursor,
       * a little below the cursor's center.
       */
      cursor_size = gdk_display_get_default_cursor_size (display);
      x += cursor_size / 2;
      y += cursor_size / 2;
    }

  screen = gtk_widget_get_screen (new_tooltip_widget);

  /* Show it */
  if (tooltip->current_window)
    {
      gint monitor_num;
      GdkRectangle monitor;
      GtkRequisition requisition;

      gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
                               &requisition);

      monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
      gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

      if (x + requisition.width > monitor.x + monitor.width)
        x -= x - (monitor.x + monitor.width) + requisition.width;
      else if (x < monitor.x)
        x = monitor.x;

      if (y + requisition.height > monitor.y + monitor.height)
        y -= y - (monitor.y + monitor.height) + requisition.height;
894 895 896 897 898 899 900 901 902
  
      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;
        }
  
903 904 905 906 907
      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
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
static void
gtk_tooltip_show_tooltip (GdkDisplay *display)
{
  gint x, y;
  GdkScreen *screen;

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

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

  if (tooltip->keyboard_mode_enabled)
    {
926
      x = y = -1;
Kristian Rietveld's avatar
Kristian Rietveld committed
927 928 929 930 931 932
      pointer_widget = tooltip_widget = tooltip->keyboard_widget;
    }
  else
    {
      window = tooltip->last_window;

933 934 935
      if (!GDK_IS_WINDOW (window))
	return;

Kristian Rietveld's avatar
Kristian Rietveld committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
      gdk_window_get_origin (window, &x, &y);
      x = tooltip->last_x - x;
      y = tooltip->last_y - y;

      pointer_widget = tooltip_widget = find_widget_under_pointer (window,
								   &x, &y);
    }

  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))
	tooltip->current_window = gtk_widget_get_tooltip_window (tooltip_widget);
      else
	tooltip->current_window = GTK_WINDOW (GTK_TOOLTIP (tooltip)->window);
    }

  screen = gtk_widget_get_screen (tooltip_widget);

963
  /* FIXME: should use tooltip->current_window iso tooltip->window */
Kristian Rietveld's avatar
Kristian Rietveld committed
964 965 966 967 968 969 970 971 972 973 974 975
  if (screen != gtk_widget_get_screen (tooltip->window))
    {
      g_signal_handlers_disconnect_by_func (display,
					    gtk_tooltip_display_closed,
					    tooltip);

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

      g_signal_connect (display, "closed",
			G_CALLBACK (gtk_tooltip_display_closed), tooltip);
    }

976
  gtk_tooltip_position (tooltip, display, tooltip_widget);
Kristian Rietveld's avatar
Kristian Rietveld committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

  /* 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)
{
992
  if (!tooltip)
Kristian Rietveld's avatar
Kristian Rietveld committed
993 994 995 996 997 998 999 1000
    return;

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

1001 1002 1003 1004 1005
  if (!GTK_TOOLTIP_VISIBLE (tooltip))
    return;

  tooltip->tooltip_widget = NULL;

Kristian Rietveld's avatar
Kristian Rietveld committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  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
1022 1023 1024 1025
	  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
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    }
  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;

  display = GDK_DISPLAY_OBJECT (data);
1050 1051 1052 1053 1054 1055 1056 1057
  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
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

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

1076
  if (!tooltip || GTK_TOOLTIP_VISIBLE (tooltip))
Kristian Rietveld's avatar
Kristian Rietveld committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    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
1089 1090 1091 1092
  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
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
}

void
_gtk_tooltip_focus_in (GtkWidget *widget)
{
  gint x, y;
  gboolean return_value = FALSE;
  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");

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

  if (tooltip->keyboard_widget)
    g_object_unref (tooltip->keyboard_widget);

  tooltip->keyboard_widget = g_object_ref (widget);

  gdk_window_get_pointer (widget->window, &x, &y, NULL);

  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);
1176 1177 1178
      g_signal_connect (display, "closed",
			G_CALLBACK (gtk_tooltip_display_closed),
			tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
    }

  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
      || toplevel->window == tooltip->toplevel_window)
    gtk_tooltip_hide_tooltip (tooltip);
}

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
static gboolean
tooltips_enabled (GdkWindow *window)
{
  gboolean enabled;
  gboolean touchscreen;
  GdkScreen *screen;
  GtkSettings *settings;

  screen = gdk_drawable_get_screen (window);
  settings = gtk_settings_get_for_screen (screen);

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

  return (!touchscreen && enabled);
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1240 1241 1242 1243 1244 1245 1246 1247
void
_gtk_tooltip_handle_event (GdkEvent *event)
{
  gint x, y;
  gboolean return_value = FALSE;
  GtkWidget *has_tooltip_widget = NULL;
  GdkDisplay *display;
  GtkTooltip *current_tooltip;
1248

1249
  if (!tooltips_enabled (event->any.window))
1250
    return;
Kristian Rietveld's avatar
Kristian Rietveld committed
1251

1252
  /* Returns coordinates relative to has_tooltip_widget's allocation. */
Kristian Rietveld's avatar
Kristian Rietveld committed
1253 1254 1255 1256 1257 1258 1259
  has_tooltip_widget = find_topmost_widget_coords_from_event (event, &x, &y);
  display = gdk_drawable_get_display (event->any.window);
  current_tooltip = g_object_get_data (G_OBJECT (display),
				       "gdk-display-current-tooltip");

  if (current_tooltip)
    {
1260
      gtk_tooltip_set_last_window (current_tooltip, event->any.window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
      gdk_event_get_root_coords (event,
				&current_tooltip->last_x,
				&current_tooltip->last_y);
    }

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

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
#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
1295
  /* Always poll for a next motion event */
1296
  gdk_event_request_motions (&event->motion);
Kristian Rietveld's avatar
Kristian Rietveld committed
1297 1298 1299 1300

  /* Hide the tooltip when there's no new tooltip widget */
  if (!has_tooltip_widget)
    {
1301
      if (current_tooltip)
Kristian Rietveld's avatar
Kristian Rietveld committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	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)
	  {
1324 1325 1326 1327 1328 1329 1330
	    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
1331 1332 1333 1334
	    return_value = gtk_tooltip_run_requery (&has_tooltip_widget,
						    current_tooltip,
						    &x, &y);

1335 1336 1337
	    /* Requested to be hidden? */
	    hide_tooltip = !return_value;

1338 1339 1340
	    /* Leave notify should override the query function */
	    hide_tooltip = (event->type == GDK_LEAVE_NOTIFY);

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	    /* 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
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
	      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);
1364 1365 1366
	    g_signal_connect (display, "closed",
			      G_CALLBACK (gtk_tooltip_display_closed),
			      current_tooltip);
Kristian Rietveld's avatar
Kristian Rietveld committed
1367

1368
	    gtk_tooltip_set_last_window (current_tooltip, event->any.window);
Kristian Rietveld's avatar
Kristian Rietveld committed
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	    gdk_event_get_root_coords (event,
				       &current_tooltip->last_x,
				       &current_tooltip->last_y);

	    gtk_tooltip_start_delay (display);
	  }
	break;

      default:
	break;
    }
}
Matthias Clasen's avatar
Matthias Clasen committed
1381 1382 1383 1384


#define __GTK_TOOLTIP_C__
#include "gtkaliasdef.c"