gtkoverlay.c 27.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * gtkoverlay.c
 * This file is part of gtk
 *
 * Copyright (C) 2011 - Ignacio Casal Quinteiro, Mike Krüger
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#include "config.h"

#include "gtkoverlay.h"
#include "gtkbuildable.h"
#include "gtkscrolledwindow.h"
#include "gtkmarshalers.h"

#include "gtkprivate.h"
#include "gtkintl.h"

/**
 * SECTION:gtkoverlay
 * @short_description: A container which overlays widgets on top of each other
 * @title: GtkOverlay
 *
 * GtkOverlay is a container which contains a single main child, on top
37 38 39 40 41 42 43 44
 * of which it can place “overlay” widgets. The position of each overlay
 * widget is determined by its #GtkWidget:halign and #GtkWidget:valign
 * properties. E.g. a widget with both alignments set to %GTK_ALIGN_START
 * will be placed at the top left corner of the GtkOverlay container,
 * whereas an overlay with halign set to %GTK_ALIGN_CENTER and valign set
 * to %GTK_ALIGN_END will be placed a the bottom edge of the GtkOverlay,
 * horizontally centered. The position can be adjusted by setting the margin
 * properties of the child to non-zero values.
45 46 47 48
 *
 * More complicated placement of overlays is possible by connecting
 * to the #GtkOverlay::get-child-position signal.
 *
49
 * # GtkOverlay as GtkBuildable
50
 *
51
 * The GtkOverlay implementation of the GtkBuildable interface
52
 * supports placing a child as an overlay by specifying “overlay” as
53
 * the “type” attribute of a `<child>` element.
54 55 56 57 58 59
 *
 * # CSS nodes
 *
 * GtkOverlay has a single CSS node with the name “overlay”. Overlay children
 * whose alignments cause them to be positioned at an edge get the style classes
 * “.left”, “.right”, “.top”, and/or “.bottom” according to their position.
60 61 62 63 64 65 66 67 68 69 70 71 72
 */

struct _GtkOverlayPrivate
{
  GSList *children;
};

typedef struct _GtkOverlayChild GtkOverlayChild;

struct _GtkOverlayChild
{
  GtkWidget *widget;
  GdkWindow *window;
73
  gboolean pass_through;
74 75 76 77 78 79 80
};

enum {
  GET_CHILD_POSITION,
  LAST_SIGNAL
};

81 82 83
enum
{
  CHILD_PROP_0,
84
  CHILD_PROP_PASS_THROUGH,
85 86 87
  CHILD_PROP_INDEX
};

88 89 90 91 92
static guint signals[LAST_SIGNAL] = { 0 };

static void gtk_overlay_buildable_init (GtkBuildableIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkOverlay, gtk_overlay, GTK_TYPE_BIN,
93
                         G_ADD_PRIVATE (GtkOverlay)
94 95 96
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_overlay_buildable_init))

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static void
gtk_overlay_compute_child_allocation (GtkOverlay      *overlay,
				      GtkOverlayChild *child,
				      GtkAllocation *window_allocation,
				      GtkAllocation *widget_allocation)
{
  gint left, right, top, bottom;
  GtkAllocation allocation, overlay_allocation;
  gboolean result;

  g_signal_emit (overlay, signals[GET_CHILD_POSITION],
                 0, child->widget, &allocation, &result);

  gtk_widget_get_allocation (GTK_WIDGET (overlay), &overlay_allocation);

  allocation.x += overlay_allocation.x;
  allocation.y += overlay_allocation.y;

  /* put the margins outside the window; also arrange things
   * so that the adjusted child allocation still ends up at 0, 0
   */
118 119
  left = gtk_widget_get_margin_start (child->widget);
  right = gtk_widget_get_margin_end (child->widget);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  top = gtk_widget_get_margin_top (child->widget);
  bottom = gtk_widget_get_margin_bottom (child->widget);

  if (widget_allocation)
    {
      widget_allocation->x = - left;
      widget_allocation->y = - top;
      widget_allocation->width = allocation.width;
      widget_allocation->height = allocation.height;
    }

  if (window_allocation)
    {
      window_allocation->x = allocation.x + left;
      window_allocation->y = allocation.y + top;
      window_allocation->width = allocation.width - (left + right);
      window_allocation->height = allocation.height - (top + bottom);
    }
}

140 141
static GdkWindow *
gtk_overlay_create_child_window (GtkOverlay *overlay,
142
                                 GtkOverlayChild *child)
143 144 145 146 147 148 149
{
  GtkWidget *widget = GTK_WIDGET (overlay);
  GtkAllocation allocation;
  GdkWindow *window;
  GdkWindowAttr attributes;
  gint attributes_mask;

150
  gtk_overlay_compute_child_allocation (overlay, child, &allocation, NULL);
151 152 153 154 155 156 157

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
  attributes.x = allocation.x;
  attributes.y = allocation.y;
158 159
  attributes.visual = gtk_widget_get_visual (widget);
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
160
  attributes.event_mask = gtk_widget_get_events (widget);
161 162 163

  window = gdk_window_new (gtk_widget_get_window (widget),
                           &attributes, attributes_mask);
164
  gtk_widget_register_window (widget, window);
165

166 167
  gdk_window_set_pass_through (window, child->pass_through);

168 169
  gtk_widget_set_parent_window (child->widget, window);
  
170 171 172
  return window;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static GtkAlign
effective_align (GtkAlign         align,
                 GtkTextDirection direction)
{
  switch (align)
    {
    case GTK_ALIGN_START:
      return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_END : GTK_ALIGN_START;
    case GTK_ALIGN_END:
      return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END;
    default:
      return align;
    }
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201
static void
gtk_overlay_get_main_widget_allocation (GtkOverlay *overlay,
                                        GtkAllocation *main_alloc_out)
{
  GtkWidget *main_widget;
  GtkAllocation main_alloc;

  main_widget = gtk_bin_get_child (GTK_BIN (overlay));

  /* special-case scrolled windows */
  if (GTK_IS_SCROLLED_WINDOW (main_widget))
    {
      GtkWidget *grandchild;
      gint x, y;
202
      gboolean res;
203 204 205 206

      grandchild = gtk_bin_get_child (GTK_BIN (main_widget));
      res = gtk_widget_translate_coordinates (grandchild, main_widget, 0, 0, &x, &y);

207 208 209 210 211 212 213 214 215 216 217
      if (res)
        {
          main_alloc.x = x;
          main_alloc.y = y;
        }
      else
        {
          main_alloc.x = 0;
          main_alloc.y = 0;
        }

218 219 220
      main_alloc.width = gtk_widget_get_allocated_width (grandchild);
      main_alloc.height = gtk_widget_get_allocated_height (grandchild);
    }
221 222 223 224
  else
    {
      main_alloc.x = 0;
      main_alloc.y = 0;
225 226
      main_alloc.width = gtk_widget_get_allocated_width (GTK_WIDGET (overlay));
      main_alloc.height = gtk_widget_get_allocated_height (GTK_WIDGET (overlay));
227
    }
228 229 230 231 232

  if (main_alloc_out)
    *main_alloc_out = main_alloc;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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 284 285 286 287 288 289 290 291 292 293 294 295
static void
gtk_overlay_child_update_style_classes (GtkOverlay *overlay,
                                        GtkWidget *child,
                                        GtkAllocation *child_allocation)
{
  GtkAllocation overlay_allocation, main_allocation;
  GtkAlign valign, halign;
  gboolean is_left, is_right, is_top, is_bottom;
  gboolean has_left, has_right, has_top, has_bottom;
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (child);
  has_left = gtk_style_context_has_class (context, GTK_STYLE_CLASS_LEFT);
  has_right = gtk_style_context_has_class (context, GTK_STYLE_CLASS_RIGHT);
  has_top = gtk_style_context_has_class (context, GTK_STYLE_CLASS_TOP);
  has_bottom = gtk_style_context_has_class (context, GTK_STYLE_CLASS_BOTTOM);

  is_left = is_right = is_top = is_bottom = FALSE;

  gtk_overlay_get_main_widget_allocation (overlay, &main_allocation);
  gtk_widget_get_allocation (GTK_WIDGET (overlay), &overlay_allocation);

  main_allocation.x += overlay_allocation.x;
  main_allocation.y += overlay_allocation.y;

  halign = effective_align (gtk_widget_get_halign (child),
                            gtk_widget_get_direction (child));

  if (halign == GTK_ALIGN_START)
    is_left = (child_allocation->x == main_allocation.x);
  else if (halign == GTK_ALIGN_END)
    is_right = (child_allocation->x + child_allocation->width ==
                main_allocation.x + main_allocation.width);

  valign = gtk_widget_get_valign (child);

  if (valign == GTK_ALIGN_START)
    is_top = (child_allocation->y == main_allocation.y);
  else if (valign == GTK_ALIGN_END)
    is_bottom = (child_allocation->y + child_allocation->height ==
                 main_allocation.y + main_allocation.height);

  if (has_left && !is_left)
    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_LEFT);
  else if (!has_left && is_left)
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_LEFT);

  if (has_right && !is_right)
    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_RIGHT);
  else if (!has_right && is_right)
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_RIGHT);

  if (has_top && !is_top)
    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_TOP);
  else if (!has_top && is_top)
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOP);

  if (has_bottom && !is_bottom)
    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BOTTOM);
  else if (!has_bottom && is_bottom)
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_BOTTOM);
}

296
static void
Benjamin Otte's avatar
Benjamin Otte committed
297 298
gtk_overlay_child_allocate (GtkOverlay      *overlay,
                            GtkOverlayChild *child)
299
{
300
  GtkAllocation window_allocation, child_allocation;
Benjamin Otte's avatar
Benjamin Otte committed
301 302 303

  if (gtk_widget_get_mapped (GTK_WIDGET (overlay)))
    {
304 305 306
      /* Note: This calls show every size allocation, which makes
       * us keep the z-order of the chilren, as gdk_window_show()
       * does an implicit raise. */
Benjamin Otte's avatar
Benjamin Otte committed
307
      if (gtk_widget_get_visible (child->widget))
308
        gdk_window_show (child->window);
Benjamin Otte's avatar
Benjamin Otte committed
309 310 311 312 313 314 315
      else if (gdk_window_is_visible (child->window))
        gdk_window_hide (child->window);
    }

  if (!gtk_widget_get_visible (child->widget))
    return;

316
  gtk_overlay_compute_child_allocation (overlay, child, &window_allocation, &child_allocation);
317 318 319

  if (child->window)
    gdk_window_move_resize (child->window,
320 321
                            window_allocation.x, window_allocation.y,
                            window_allocation.width, window_allocation.height);
322

323
  gtk_overlay_child_update_style_classes (overlay, child->widget, &window_allocation);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  gtk_widget_size_allocate (child->widget, &child_allocation);
}

static void
gtk_overlay_size_allocate (GtkWidget     *widget,
                           GtkAllocation *allocation)
{
  GtkOverlay *overlay = GTK_OVERLAY (widget);
  GtkOverlayPrivate *priv = overlay->priv;
  GSList *children;
  GtkWidget *main_widget;

  GTK_WIDGET_CLASS (gtk_overlay_parent_class)->size_allocate (widget, allocation);

  main_widget = gtk_bin_get_child (GTK_BIN (overlay));
339 340
  if (main_widget && gtk_widget_get_visible (main_widget))
    gtk_widget_size_allocate (main_widget, allocation);
341 342

  for (children = priv->children; children; children = children->next)
343
    gtk_overlay_child_allocate (overlay, children->data);
344 345 346 347 348 349 350 351
}

static gboolean
gtk_overlay_get_child_position (GtkOverlay    *overlay,
                                GtkWidget     *widget,
                                GtkAllocation *alloc)
{
  GtkAllocation main_alloc;
352
  GtkRequisition min, req;
353 354 355
  GtkAlign halign;
  GtkTextDirection direction;

356
  gtk_overlay_get_main_widget_allocation (overlay, &main_alloc);
357
  gtk_widget_get_preferred_size (widget, &min, &req);
358 359

  alloc->x = main_alloc.x;
360
  alloc->width = MAX (min.width, MIN (main_alloc.width, req.width));
361 362 363 364 365 366 367 368 369 370

  direction = gtk_widget_get_direction (widget);

  halign = gtk_widget_get_halign (widget);
  switch (effective_align (halign, direction))
    {
    case GTK_ALIGN_START:
      /* nothing to do */
      break;
    case GTK_ALIGN_FILL:
371
      alloc->width = MAX (alloc->width, main_alloc.width);
372 373
      break;
    case GTK_ALIGN_CENTER:
374
      alloc->x += main_alloc.width / 2 - alloc->width / 2;
375 376
      break;
    case GTK_ALIGN_END:
377
      alloc->x += main_alloc.width - alloc->width;
378
      break;
379 380 381 382
    case GTK_ALIGN_BASELINE:
    default:
      g_assert_not_reached ();
      break;
383 384 385
    }

  alloc->y = main_alloc.y;
386
  alloc->height = MAX  (min.height, MIN (main_alloc.height, req.height));
387 388 389 390 391 392 393

  switch (gtk_widget_get_valign (widget))
    {
    case GTK_ALIGN_START:
      /* nothing to do */
      break;
    case GTK_ALIGN_FILL:
394
      alloc->height = MAX (alloc->height, main_alloc.height);
395 396
      break;
    case GTK_ALIGN_CENTER:
397
      alloc->y += main_alloc.height / 2 - alloc->height / 2;
398 399
      break;
    case GTK_ALIGN_END:
400
      alloc->y += main_alloc.height - alloc->height;
401
      break;
402 403 404 405
    case GTK_ALIGN_BASELINE:
    default:
      g_assert_not_reached ();
      break;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    }

  return TRUE;
}

static void
gtk_overlay_realize (GtkWidget *widget)
{
  GtkOverlay *overlay = GTK_OVERLAY (widget);
  GtkOverlayPrivate *priv = overlay->priv;
  GtkOverlayChild *child;
  GSList *children;

  GTK_WIDGET_CLASS (gtk_overlay_parent_class)->realize (widget);

  for (children = priv->children; children; children = children->next)
    {
      child = children->data;

      if (child->window == NULL)
426
	child->window = gtk_overlay_create_child_window (overlay, child);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    }
}

static void
gtk_overlay_unrealize (GtkWidget *widget)
{
  GtkOverlay *overlay = GTK_OVERLAY (widget);
  GtkOverlayPrivate *priv = overlay->priv;
  GtkOverlayChild *child;
  GSList *children;

  for (children = priv->children; children; children = children->next)
    {
      child = children->data;

      gtk_widget_set_parent_window (child->widget, NULL);
443
      gtk_widget_unregister_window (widget, child->window);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
      gdk_window_destroy (child->window);
      child->window = NULL;
    }

  GTK_WIDGET_CLASS (gtk_overlay_parent_class)->unrealize (widget);
}

static void
gtk_overlay_map (GtkWidget *widget)
{
  GtkOverlay *overlay = GTK_OVERLAY (widget);
  GtkOverlayPrivate *priv = overlay->priv;
  GtkOverlayChild *child;
  GSList *children;

  GTK_WIDGET_CLASS (gtk_overlay_parent_class)->map (widget);

  for (children = priv->children; children; children = children->next)
    {
      child = children->data;

      if (child->window != NULL &&
          gtk_widget_get_visible (child->widget) &&
          gtk_widget_get_child_visible (child->widget))
        gdk_window_show (child->window);
    }
}

static void
gtk_overlay_unmap (GtkWidget *widget)
{
  GtkOverlay *overlay = GTK_OVERLAY (widget);
  GtkOverlayPrivate *priv = overlay->priv;
  GtkOverlayChild *child;
  GSList *children;

  for (children = priv->children; children; children = children->next)
    {
      child = children->data;

      if (child->window != NULL &&
          gdk_window_is_visible (child->window))
        gdk_window_hide (child->window);
    }

  GTK_WIDGET_CLASS (gtk_overlay_parent_class)->unmap (widget);
}

static void
gtk_overlay_remove (GtkContainer *container,
                    GtkWidget    *widget)
{
  GtkOverlayPrivate *priv = GTK_OVERLAY (container)->priv;
  GtkOverlayChild *child;
498
  GSList *children, *next;
499
  gboolean removed;
500

501
  removed = FALSE;
502
  for (children = priv->children; children; children = next)
503 504
    {
      child = children->data;
505
      next = children->next;
506 507 508 509 510

      if (child->widget == widget)
        {
          if (child->window != NULL)
            {
511
              gtk_widget_unregister_window (GTK_WIDGET (container), child->window);
512 513 514 515 516 517 518 519
              gdk_window_destroy (child->window);
            }

          gtk_widget_unparent (widget);

          priv->children = g_slist_delete_link (priv->children, children);
          g_slice_free (GtkOverlayChild, child);

520
          removed = TRUE;
521
        }
522 523
      else if (removed)
        gtk_widget_child_notify (child->widget, "index");
524 525
    }

526 527
  if (!removed)
    GTK_CONTAINER_CLASS (gtk_overlay_parent_class)->remove (container, widget);
528 529
}

530 531 532 533
/**
 * gtk_overlay_reorder_overlay:
 * @overlay: a #GtkOverlay
 * @child: the overlaid #GtkWidget to move
534
 * @position: the new index for @child in the list of overlay children
535 536 537 538 539 540 541 542 543 544
 *   of @overlay, starting from 0. If negative, indicates the end of
 *   the list
 *
 * Moves @child to a new @index in the list of @overlay children.
 * The list contains overlays in the order that these were
 * added to @overlay.
 *
 * A widget’s index in the @overlay children list determines which order
 * the children are drawn if they overlap. The first child is drawn at
 * the bottom. It also affects the default focus chain order.
545 546
 *
 * Since: 3.18
547 548
 */
void
549 550 551
gtk_overlay_reorder_overlay (GtkOverlay *overlay,
                             GtkWidget  *child,
                             gint        position)
552 553 554 555 556 557 558
{
  GtkOverlayPrivate *priv;
  GSList *old_link;
  GSList *new_link;
  GSList *l;
  GtkOverlayChild *child_info = NULL;
  gint old_index, i;
559
  gint index;
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

  g_return_if_fail (GTK_IS_OVERLAY (overlay));
  g_return_if_fail (GTK_IS_WIDGET (child));

  priv = GTK_OVERLAY (overlay)->priv;

  old_link = priv->children;
  old_index = 0;
  while (old_link)
    {
      child_info = old_link->data;
      if (child_info->widget == child)
	break;

      old_link = old_link->next;
      old_index++;
    }

  g_return_if_fail (old_link != NULL);

580
  if (position < 0)
581 582 583 584 585 586
    {
      new_link = NULL;
      index = g_slist_length (priv->children) - 1;
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
587
      new_link = g_slist_nth (priv->children, position);
588
      index = MIN (position, g_slist_length (priv->children) - 1);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    }

  if (index == old_index)
    return;

  priv->children = g_slist_delete_link (priv->children, old_link);
  priv->children = g_slist_insert_before (priv->children, new_link, child_info);

  for (i = 0, l = priv->children; l != NULL; l = l->next, i++)
    {
      GtkOverlayChild *info = l->data;
      if ((i < index && i < old_index) ||
          (i > index && i > old_index))
        continue;
      gtk_widget_child_notify (info->widget, "index");
    }

  if (gtk_widget_get_visible (child) &&
      gtk_widget_get_visible (GTK_WIDGET (overlay)))
    gtk_widget_queue_resize (GTK_WIDGET (overlay));
}


612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
static void
gtk_overlay_forall (GtkContainer *overlay,
                    gboolean      include_internals,
                    GtkCallback   callback,
                    gpointer      callback_data)
{
  GtkOverlayPrivate *priv = GTK_OVERLAY (overlay)->priv;
  GtkOverlayChild *child;
  GSList *children;
  GtkWidget *main_widget;

  main_widget = gtk_bin_get_child (GTK_BIN (overlay));
  if (main_widget)
    (* callback) (main_widget, callback_data);

  children = priv->children;
  while (children)
    {
      child = children->data;
      children = children->next;

      (* callback) (child->widget, callback_data);
    }
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
static GtkOverlayChild *
gtk_overlay_get_overlay_child (GtkOverlay *overlay,
			       GtkWidget *child)
{
  GtkOverlayPrivate *priv = GTK_OVERLAY (overlay)->priv;
  GtkOverlayChild *child_info;
  GSList *children;

  for (children = priv->children; children; children = children->next)
    {
      child_info = children->data;

      if (child_info->widget == child)
	return child_info;
    }

  return NULL;
}

static void
gtk_overlay_set_child_property (GtkContainer *container,
				GtkWidget    *child,
				guint         property_id,
				const GValue *value,
				GParamSpec   *pspec)
{
  GtkOverlay *overlay = GTK_OVERLAY (container);
  GtkOverlayChild *child_info;
  GtkWidget *main_widget;

  main_widget = gtk_bin_get_child (GTK_BIN (overlay));
  if (child == main_widget)
    child_info = NULL;
  else
    {
      child_info = gtk_overlay_get_overlay_child (overlay, child);
      if (child_info == NULL)
	{
	  GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
	  return;
	}
    }

  switch (property_id)
    {
682 683 684 685 686 687 688 689 690 691 692 693 694
    case CHILD_PROP_PASS_THROUGH:
      /* Ignore value on main child */
      if (child_info)
	{
	  if (g_value_get_boolean (value) != child_info->pass_through)
	    {
	      child_info->pass_through = g_value_get_boolean (value);
	      if (child_info->window)
		gdk_window_set_pass_through (child_info->window, child_info->pass_through);
	      gtk_container_child_notify (container, child, "pass-through");
	    }
	}
      break;
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    case CHILD_PROP_INDEX:
      if (child_info != NULL)
	gtk_overlay_reorder_overlay (GTK_OVERLAY (container),
				     child,
				     g_value_get_int (value));
      break;

    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}

static void
gtk_overlay_get_child_property (GtkContainer *container,
				GtkWidget    *child,
				guint         property_id,
				GValue       *value,
				GParamSpec   *pspec)
{
  GtkOverlay *overlay = GTK_OVERLAY (container);
  GtkOverlayPrivate *priv = GTK_OVERLAY (overlay)->priv;
  GtkOverlayChild *child_info;
  GtkWidget *main_widget;

  main_widget = gtk_bin_get_child (GTK_BIN (overlay));
  if (child == main_widget)
    child_info = NULL;
  else
    {
      child_info = gtk_overlay_get_overlay_child (overlay, child);
      if (child_info == NULL)
	{
	  GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
	  return;
	}
    }

  switch (property_id)
    {
735 736 737 738 739 740
    case CHILD_PROP_PASS_THROUGH:
      if (child_info)
	g_value_set_boolean (value, child_info->pass_through);
      else
	g_value_set_boolean (value, FALSE);
      break;
741 742 743 744 745 746 747 748 749 750
    case CHILD_PROP_INDEX:
      g_value_set_int (value, g_slist_index (priv->children, child_info));
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}


751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
static void
gtk_overlay_class_init (GtkOverlayClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  widget_class->size_allocate = gtk_overlay_size_allocate;
  widget_class->realize = gtk_overlay_realize;
  widget_class->unrealize = gtk_overlay_unrealize;
  widget_class->map = gtk_overlay_map;
  widget_class->unmap = gtk_overlay_unmap;

  container_class->remove = gtk_overlay_remove;
  container_class->forall = gtk_overlay_forall;
766 767
  container_class->set_child_property = gtk_overlay_set_child_property;
  container_class->get_child_property = gtk_overlay_get_child_property;
768 769 770

  klass->get_child_position = gtk_overlay_get_child_position;

771 772 773 774 775 776 777
  /**
   * GtkOverlay:pass-through:
   *
   * Pass through input, does not affect main child.
   *
   * Since: 3.18
   */
778 779 780 781
  gtk_container_class_install_child_property (container_class, CHILD_PROP_PASS_THROUGH,
      g_param_spec_boolean ("pass-through", P_("Pass Through"), P_("Pass through input, does not affect main child"),
                            FALSE,
                            GTK_PARAM_READWRITE));
782 783 784 785 786 787 788 789

  /**
   * GtkOverlay:index:
   *
   * The index of the overlay in the parent, -1 for the main child.
   *
   * Since: 3.18
   */
790 791 792 793 794 795 796
  gtk_container_class_install_child_property (container_class, CHILD_PROP_INDEX,
					      g_param_spec_int ("index",
								P_("Index"),
								P_("The index of the overlay in the parent, -1 for the main child"),
								-1, G_MAXINT, 0,
								GTK_PARAM_READWRITE));

797 798 799 800
  /**
   * GtkOverlay::get-child-position:
   * @overlay: the #GtkOverlay
   * @widget: the child widget to position
801 802
   * @allocation: (type Gdk.Rectangle) (out caller-allocates): return
   *   location for the allocation
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
   *
   * The ::get-child-position signal is emitted to determine
   * the position and size of any overlay child widgets. A
   * handler for this signal should fill @allocation with
   * the desired position and size for @widget, relative to
   * the 'main' child of @overlay.
   *
   * The default handler for this signal uses the @widget's
   * halign and valign properties to determine the position
   * and gives the widget its natural size (except that an
   * alignment of %GTK_ALIGN_FILL will cause the overlay to
   * be full-width/height). If the main child is a
   * #GtkScrolledWindow, the overlays are placed relative
   * to its contents.
   *
818
   * Returns: %TRUE if the @allocation has been filled
819 820 821 822 823 824 825 826 827 828 829
   */
  signals[GET_CHILD_POSITION] =
    g_signal_new (I_("get-child-position"),
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkOverlayClass, get_child_position),
                  _gtk_boolean_handled_accumulator, NULL,
                  _gtk_marshal_BOOLEAN__OBJECT_BOXED,
                  G_TYPE_BOOLEAN, 2,
                  GTK_TYPE_WIDGET,
                  GDK_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
Timm Bäder's avatar
Timm Bäder committed
830 831

  gtk_widget_class_set_css_name (widget_class, "overlay");
832 833 834 835 836
}

static void
gtk_overlay_init (GtkOverlay *overlay)
{
837
  overlay->priv = gtk_overlay_get_instance_private (overlay);
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

  gtk_widget_set_has_window (GTK_WIDGET (overlay), FALSE);
}

static void
gtk_overlay_buildable_add_child (GtkBuildable *buildable,
                                 GtkBuilder   *builder,
                                 GObject      *child,
                                 const gchar  *type)
{
  if (type && strcmp (type, "overlay") == 0)
    gtk_overlay_add_overlay (GTK_OVERLAY (buildable), GTK_WIDGET (child));
  else if (!type)
    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
  else
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
}

static void
gtk_overlay_buildable_init (GtkBuildableIface *iface)
{
  iface->add_child = gtk_overlay_buildable_add_child;
}

/**
 * gtk_overlay_new:
 *
 * Creates a new #GtkOverlay.
 *
 * Returns: a new #GtkOverlay object.
 *
 * Since: 3.2
 */
GtkWidget *
gtk_overlay_new (void)
{
  return g_object_new (GTK_TYPE_OVERLAY, NULL);
}

/**
 * gtk_overlay_add_overlay:
 * @overlay: a #GtkOverlay
 * @widget: a #GtkWidget to be added to the container
 *
 * Adds @widget to @overlay.
 *
 * The widget will be stacked on top of the main widget
 * added with gtk_container_add().
 *
 * The position at which @widget is placed is determined
888
 * from its #GtkWidget:halign and #GtkWidget:valign properties.
889 890 891 892 893 894 895
 *
 * Since: 3.2
 */
void
gtk_overlay_add_overlay (GtkOverlay *overlay,
                         GtkWidget  *widget)
{
896
  GtkOverlayPrivate *priv;
897 898 899 900 901
  GtkOverlayChild *child;

  g_return_if_fail (GTK_IS_OVERLAY (overlay));
  g_return_if_fail (GTK_IS_WIDGET (widget));

902
  priv = overlay->priv;
903 904 905 906 907 908
  child = g_slice_new0 (GtkOverlayChild);
  child->widget = widget;

  priv->children = g_slist_append (priv->children, child);

  if (gtk_widget_get_realized (GTK_WIDGET (overlay)))
909
    {
910
      child->window = gtk_overlay_create_child_window (overlay, child);
911 912 913 914 915
      gtk_widget_set_parent (widget, GTK_WIDGET (overlay));
    }
  else
    gtk_widget_set_parent (widget, GTK_WIDGET (overlay));

916
  gtk_widget_child_notify (widget, "index");
917
}
918

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
/**
 * gtk_overlay_set_overlay_pass_through:
 * @overlay: a #GtkOverlay
 * @widget: an overlay child of #GtkOverlay
 * @pass_through: whether the child should pass the input through
 *
 * Convenience function to set the value of the #GtkOverlay:pass-through
 * child property for @widget.
 *
 * Since: 3.18
 */
void
gtk_overlay_set_overlay_pass_through (GtkOverlay *overlay,
				      GtkWidget  *widget,
				      gboolean    pass_through)
{
  g_return_if_fail (GTK_IS_OVERLAY (overlay));
  g_return_if_fail (GTK_IS_WIDGET (widget));

  gtk_container_child_set (GTK_CONTAINER (overlay), widget,
			   "pass-through", pass_through,
			   NULL);
}

/**
 * gtk_overlay_get_overlay_pass_through:
 * @overlay: a #GtkOverlay
 * @widget: an overlay child of #GtkOverlay
 *
 * Convenience function to get the value of the #GtkOverlay:pass-through
 * child property for @widget.
 *
 * Returns: whether the widget is a pass through child.
 *
 * Since: 3.18
 */
gboolean
gtk_overlay_get_overlay_pass_through (GtkOverlay *overlay,
				      GtkWidget  *widget)
{
  gboolean pass_through;

  g_return_val_if_fail (GTK_IS_OVERLAY (overlay), FALSE);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);

  gtk_container_child_get (GTK_CONTAINER (overlay), widget,
			   "pass-through", &pass_through,
			   NULL);

  return pass_through;
}