gtkgrid.c 69.3 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Red Hat, Inc.
 * Author: Matthias Clasen
 *
 * 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
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Matthias Clasen's avatar
Matthias Clasen committed
17 18 19 20
 */

#include "config.h"

21 22
#include <string.h>

Matthias Clasen's avatar
Matthias Clasen committed
23 24
#include "gtkgrid.h"

25
#include "gtkorientableprivate.h"
26
#include "gtkrender.h"
Matthias Clasen's avatar
Matthias Clasen committed
27
#include "gtksizerequest.h"
28
#include "gtkwidgetprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
29 30 31 32 33 34 35 36 37 38 39 40
#include "gtkprivate.h"
#include "gtkintl.h"


/**
 * SECTION:gtkgrid
 * @Short_description: Pack widgets in a rows and columns
 * @Title: GtkGrid
 * @See_also: #GtkTable, #GtkHBox, #GtkVBox
 *
 * GtkGrid is a container which arranges its child widgets in
 * rows and columns. It is a very similar to #GtkTable and #GtkBox,
41
 * but it consistently uses #GtkWidget’s #GtkWidget:margin and #GtkWidget:expand
42
 * properties instead of custom child properties, and it fully supports
43
 * [height-for-width geometry management][geometry-management].
Matthias Clasen's avatar
Matthias Clasen committed
44 45 46
 *
 * Children are added using gtk_grid_attach(). They can span multiple
 * rows or columns. It is also possible to add a child next to an
47 48
 * existing child, using gtk_grid_attach_next_to(). The behaviour of
 * GtkGrid when several children occupy the same grid cell is undefined.
Matthias Clasen's avatar
Matthias Clasen committed
49 50 51
 *
 * GtkGrid can be used like a #GtkBox by just using gtk_container_add(),
 * which will place children next to each other in the direction determined
52
 * by the #GtkOrientable:orientation property.
Matthias Clasen's avatar
Matthias Clasen committed
53 54 55 56
 */

typedef struct _GtkGridChild GtkGridChild;
typedef struct _GtkGridChildAttach GtkGridChildAttach;
57
typedef struct _GtkGridRowProperties GtkGridRowProperties;
Matthias Clasen's avatar
Matthias Clasen committed
58 59 60 61 62 63 64 65 66 67 68
typedef struct _GtkGridLine GtkGridLine;
typedef struct _GtkGridLines GtkGridLines;
typedef struct _GtkGridLineData GtkGridLineData;
typedef struct _GtkGridRequest GtkGridRequest;

struct _GtkGridChildAttach
{
  gint pos;
  gint span;
};

69 70 71 72 73 74 75 76 77 78 79
struct _GtkGridRowProperties
{
  gint row;
  GtkBaselinePosition baseline_position;
};

static const GtkGridRowProperties gtk_grid_row_properties_default = {
  0,
  GTK_BASELINE_POSITION_CENTER
};

Matthias Clasen's avatar
Matthias Clasen committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
struct _GtkGridChild
{
  GtkWidget *widget;
  GtkGridChildAttach attach[2];
};

#define CHILD_LEFT(child)    ((child)->attach[GTK_ORIENTATION_HORIZONTAL].pos)
#define CHILD_WIDTH(child)   ((child)->attach[GTK_ORIENTATION_HORIZONTAL].span)
#define CHILD_TOP(child)     ((child)->attach[GTK_ORIENTATION_VERTICAL].pos)
#define CHILD_HEIGHT(child)  ((child)->attach[GTK_ORIENTATION_VERTICAL].span)

/* A GtkGridLineData struct contains row/column specific parts
 * of the grid.
 */
struct _GtkGridLineData
{
  gint16 spacing;
  guint homogeneous : 1;
};

struct _GtkGridPrivate
{
  GList *children;
103
  GList *row_properties;
Matthias Clasen's avatar
Matthias Clasen committed
104 105

  GtkOrientation orientation;
106
  gint baseline_row;
Matthias Clasen's avatar
Matthias Clasen committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120

  GtkGridLineData linedata[2];
};

#define ROWS(priv)    (&(priv)->linedata[GTK_ORIENTATION_HORIZONTAL])
#define COLUMNS(priv) (&(priv)->linedata[GTK_ORIENTATION_VERTICAL])

/* A GtkGridLine struct represents a single row or column
 * during size requests
 */
struct _GtkGridLine
{
  gint minimum;
  gint natural;
121 122 123 124 125
  gint minimum_above;
  gint minimum_below;
  gint natural_above;
  gint natural_below;

Matthias Clasen's avatar
Matthias Clasen committed
126 127
  gint position;
  gint allocation;
128
  gint allocated_baseline;
Matthias Clasen's avatar
Matthias Clasen committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  guint need_expand : 1;
  guint expand      : 1;
  guint empty       : 1;
};

struct _GtkGridLines
{
  GtkGridLine *lines;
  gint min, max;
};

struct _GtkGridRequest
{
  GtkGrid *grid;
  GtkGridLines lines[2];
};


enum
{
  PROP_0,
  PROP_ROW_SPACING,
  PROP_COLUMN_SPACING,
  PROP_ROW_HOMOGENEOUS,
154
  PROP_COLUMN_HOMOGENEOUS,
155
  PROP_BASELINE_ROW,
156 157
  N_PROPERTIES,
  PROP_ORIENTATION
Matthias Clasen's avatar
Matthias Clasen committed
158 159
};

160 161
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

Matthias Clasen's avatar
Matthias Clasen committed
162 163 164 165 166 167
enum
{
  CHILD_PROP_0,
  CHILD_PROP_LEFT_ATTACH,
  CHILD_PROP_TOP_ATTACH,
  CHILD_PROP_WIDTH,
168 169
  CHILD_PROP_HEIGHT,
  N_CHILD_PROPERTIES
Matthias Clasen's avatar
Matthias Clasen committed
170 171
};

172 173
static GParamSpec *child_properties[N_CHILD_PROPERTIES] = { NULL, };

Matthias Clasen's avatar
Matthias Clasen committed
174
G_DEFINE_TYPE_WITH_CODE (GtkGrid, gtk_grid, GTK_TYPE_CONTAINER,
175
                         G_ADD_PRIVATE (GtkGrid)
Matthias Clasen's avatar
Matthias Clasen committed
176 177 178
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))


179 180
static void gtk_grid_row_properties_free (GtkGridRowProperties *props);

Matthias Clasen's avatar
Matthias Clasen committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static void
gtk_grid_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  GtkGrid *grid = GTK_GRID (object);
  GtkGridPrivate *priv = grid->priv;

  switch (prop_id)
    {
    case PROP_ORIENTATION:
      g_value_set_enum (value, priv->orientation);
      break;

    case PROP_ROW_SPACING:
197
      g_value_set_int (value, COLUMNS (priv)->spacing);
Matthias Clasen's avatar
Matthias Clasen committed
198 199 200
      break;

    case PROP_COLUMN_SPACING:
201
      g_value_set_int (value, ROWS (priv)->spacing);
Matthias Clasen's avatar
Matthias Clasen committed
202 203 204
      break;

    case PROP_ROW_HOMOGENEOUS:
205
      g_value_set_boolean (value, COLUMNS (priv)->homogeneous);
Matthias Clasen's avatar
Matthias Clasen committed
206 207 208
      break;

    case PROP_COLUMN_HOMOGENEOUS:
209
      g_value_set_boolean (value, ROWS (priv)->homogeneous);
Matthias Clasen's avatar
Matthias Clasen committed
210 211
      break;

212 213 214 215
    case PROP_BASELINE_ROW:
      g_value_set_int (value, priv->baseline_row);
      break;

Matthias Clasen's avatar
Matthias Clasen committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_grid_set_orientation (GtkGrid        *grid,
                          GtkOrientation  orientation)
{
  GtkGridPrivate *priv = grid->priv;

  if (priv->orientation != orientation)
    {
      priv->orientation = orientation;
231
      _gtk_orientable_set_style_classes (GTK_ORIENTABLE (grid));
Matthias Clasen's avatar
Matthias Clasen committed
232 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

      g_object_notify (G_OBJECT (grid), "orientation");
    }
}

static void
gtk_grid_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  GtkGrid *grid = GTK_GRID (object);

  switch (prop_id)
    {
    case PROP_ORIENTATION:
      gtk_grid_set_orientation (grid, g_value_get_enum (value));
      break;

    case PROP_ROW_SPACING:
      gtk_grid_set_row_spacing (grid, g_value_get_int (value));
      break;

    case PROP_COLUMN_SPACING:
      gtk_grid_set_column_spacing (grid, g_value_get_int (value));
      break;

    case PROP_ROW_HOMOGENEOUS:
      gtk_grid_set_row_homogeneous (grid, g_value_get_boolean (value));
      break;

    case PROP_COLUMN_HOMOGENEOUS:
      gtk_grid_set_column_homogeneous (grid, g_value_get_boolean (value));
      break;

267 268 269 270
    case PROP_BASELINE_ROW:
      gtk_grid_set_baseline_row (grid, g_value_get_int (value));
      break;

Matthias Clasen's avatar
Matthias Clasen committed
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static GtkGridChild *
find_grid_child (GtkGrid   *grid,
                 GtkWidget *widget)
{
  GtkGridPrivate *priv = grid->priv;
  GtkGridChild *child;
  GList *list;

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

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

  return NULL;
}

static void
gtk_grid_get_child_property (GtkContainer *container,
                             GtkWidget    *child,
                             guint         property_id,
                             GValue       *value,
                             GParamSpec   *pspec)
{
  GtkGrid *grid = GTK_GRID (container);
  GtkGridChild *grid_child;

  grid_child = find_grid_child (grid, child);

  if (grid_child == NULL)
    {
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      return;
    }

  switch (property_id)
    {
    case CHILD_PROP_LEFT_ATTACH:
      g_value_set_int (value, CHILD_LEFT (grid_child));
      break;

    case CHILD_PROP_TOP_ATTACH:
      g_value_set_int (value, CHILD_TOP (grid_child));
      break;

    case CHILD_PROP_WIDTH:
      g_value_set_int (value, CHILD_WIDTH (grid_child));
      break;

    case CHILD_PROP_HEIGHT:
      g_value_set_int (value, CHILD_HEIGHT (grid_child));
      break;

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

static void
gtk_grid_set_child_property (GtkContainer *container,
                             GtkWidget    *child,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkGrid *grid = GTK_GRID (container);
  GtkGridChild *grid_child;

  grid_child = find_grid_child (grid, child);

  if (grid_child == NULL)
    {
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      return;
    }

  switch (property_id)
    {
    case CHILD_PROP_LEFT_ATTACH:
      CHILD_LEFT (grid_child) = g_value_get_int (value);
      break;

    case CHILD_PROP_TOP_ATTACH:
      CHILD_TOP (grid_child) = g_value_get_int (value);
      break;

   case CHILD_PROP_WIDTH:
      CHILD_WIDTH (grid_child) = g_value_get_int (value);
      break;

    case CHILD_PROP_HEIGHT:
      CHILD_HEIGHT (grid_child) = g_value_get_int (value);
      break;

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

379 380
  if (_gtk_widget_get_visible (child) &&
      _gtk_widget_get_visible (GTK_WIDGET (grid)))
Matthias Clasen's avatar
Matthias Clasen committed
381 382 383 384 385 386 387 388
    gtk_widget_queue_resize (child);
}

static void
gtk_grid_init (GtkGrid *grid)
{
  GtkGridPrivate *priv;

389
  grid->priv = gtk_grid_get_instance_private (grid);
Matthias Clasen's avatar
Matthias Clasen committed
390 391 392 393 394 395 396
  priv = grid->priv;

  gtk_widget_set_has_window (GTK_WIDGET (grid), FALSE);
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (grid), FALSE);

  priv->children = NULL;
  priv->orientation = GTK_ORIENTATION_HORIZONTAL;
397
  priv->baseline_row = 0;
Matthias Clasen's avatar
Matthias Clasen committed
398 399 400 401 402 403

  priv->linedata[0].spacing = 0;
  priv->linedata[1].spacing = 0;

  priv->linedata[0].homogeneous = FALSE;
  priv->linedata[1].homogeneous = FALSE;
404 405

  _gtk_orientable_set_style_classes (GTK_ORIENTABLE (grid));
Matthias Clasen's avatar
Matthias Clasen committed
406 407
}

408 409 410 411 412 413 414 415 416 417 418
static void
gtk_grid_finalize (GObject *object)
{
  GtkGrid *grid = GTK_GRID (object);
  GtkGridPrivate *priv = grid->priv;

  g_list_free_full (priv->row_properties, (GDestroyNotify)gtk_grid_row_properties_free);

  G_OBJECT_CLASS (gtk_grid_parent_class)->finalize (object);
}

Matthias Clasen's avatar
Matthias Clasen committed
419
static void
420 421 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 448 449 450 451 452 453 454
grid_attach (GtkGrid   *grid,
             GtkWidget *widget,
             gint       left,
             gint       top,
             gint       width,
             gint       height)
{
  GtkGridPrivate *priv = grid->priv;
  GtkGridChild *child;

  child = g_slice_new (GtkGridChild);
  child->widget = widget;
  CHILD_LEFT (child) = left;
  CHILD_TOP (child) = top;
  CHILD_WIDTH (child) = width;
  CHILD_HEIGHT (child) = height;

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

  gtk_widget_set_parent (widget, GTK_WIDGET (grid));
}

/* Find the position 'touching' existing
 * children. @orientation and @max determine
 * from which direction to approach (horizontal
 * + max = right, vertical + !max = top, etc).
 * @op_pos, @op_span determine the rows/columns
 * in which the touching has to happen.
 */
static gint
find_attach_position (GtkGrid         *grid,
                      GtkOrientation   orientation,
                      gint             op_pos,
                      gint             op_span,
                      gboolean         max)
Matthias Clasen's avatar
Matthias Clasen committed
455 456 457 458 459 460 461
{
  GtkGridPrivate *priv = grid->priv;
  GtkGridChild *grid_child;
  GtkGridChildAttach *attach;
  GtkGridChildAttach *opposite;
  GList *list;
  gint pos;
462 463 464 465 466 467 468 469
  gboolean hit;

  if (max)
    pos = -G_MAXINT;
  else
    pos = G_MAXINT;

  hit = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
470 471 472 473 474

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

475 476 477 478 479 480 481
      attach = &grid_child->attach[orientation];
      opposite = &grid_child->attach[1 - orientation];

      /* check if the ranges overlap */
      if (opposite->pos <= op_pos + op_span && op_pos <= opposite->pos + opposite->span)
        {
          hit = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
482

483 484 485 486 487
          if (max)
            pos = MAX (pos, attach->pos + attach->span);
          else
            pos = MIN (pos, attach->pos);
        }
Matthias Clasen's avatar
Matthias Clasen committed
488 489
     }

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  if (!hit)
    pos = 0;

  return pos;
}

static void
gtk_grid_add (GtkContainer *container,
              GtkWidget    *child)
{
  GtkGrid *grid = GTK_GRID (container);
  GtkGridPrivate *priv = grid->priv;
  gint pos[2] = { 0, 0 };

  pos[priv->orientation] = find_attach_position (grid, priv->orientation, 0, 1, TRUE);
  grid_attach (grid, child, pos[0], pos[1], 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
}

static void
gtk_grid_remove (GtkContainer *container,
                 GtkWidget    *child)
{
  GtkGrid *grid = GTK_GRID (container);
  GtkGridPrivate *priv = grid->priv;
  GtkGridChild *grid_child;
  GList *list;

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

      if (grid_child->widget == child)
        {
523
          gboolean was_visible = _gtk_widget_get_visible (child);
Matthias Clasen's avatar
Matthias Clasen committed
524 525 526 527 528 529 530

          gtk_widget_unparent (child);

          priv->children = g_list_remove (priv->children, grid_child);

          g_slice_free (GtkGridChild, grid_child);

531
          if (was_visible && _gtk_widget_get_visible (GTK_WIDGET (grid)))
Matthias Clasen's avatar
Matthias Clasen committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
            gtk_widget_queue_resize (GTK_WIDGET (grid));

          break;
        }
    }
}

static void
gtk_grid_forall (GtkContainer *container,
                 gboolean      include_internals,
                 GtkCallback   callback,
                 gpointer      callback_data)
{
  GtkGrid *grid = GTK_GRID (container);
  GtkGridPrivate *priv = grid->priv;
  GtkGridChild *child;
  GList *list;

550 551
  list = priv->children;
  while (list)
Matthias Clasen's avatar
Matthias Clasen committed
552 553
    {
      child = list->data;
554
      list  = list->next;
Matthias Clasen's avatar
Matthias Clasen committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

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

static GType
gtk_grid_child_type (GtkContainer *container)
{
  return GTK_TYPE_WIDGET;
}

/* Calculates the min and max numbers for both orientations.
 */
static void
gtk_grid_request_count_lines (GtkGridRequest *request)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridChild *child;
  GtkGridChildAttach *attach;
  GList *list;
  gint min[2];
  gint max[2];

  min[0] = min[1] = G_MAXINT;
  max[0] = max[1] = G_MININT;

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

      min[0] = MIN (min[0], attach[0].pos);
      max[0] = MAX (max[0], attach[0].pos + attach[0].span);
      min[1] = MIN (min[1], attach[1].pos);
      max[1] = MAX (max[1], attach[1].pos + attach[1].span);
    }

  request->lines[0].min = min[0];
  request->lines[0].max = max[0];
  request->lines[1].min = min[1];
  request->lines[1].max = max[1];
}

/* Sets line sizes to 0 and marks lines as expand
 * if they have a non-spanning expanding child.
 */
static void
gtk_grid_request_init (GtkGridRequest *request,
                       GtkOrientation  orientation)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridChild *child;
  GtkGridChildAttach *attach;
  GtkGridLines *lines;
  GList *list;
  gint i;

  lines = &request->lines[orientation];

  for (i = 0; i < lines->max - lines->min; i++)
    {
      lines->lines[i].minimum = 0;
      lines->lines[i].natural = 0;
618 619 620 621
      lines->lines[i].minimum_above = -1;
      lines->lines[i].minimum_below = -1;
      lines->lines[i].natural_above = -1;
      lines->lines[i].natural_below = -1;
Matthias Clasen's avatar
Matthias Clasen committed
622
      lines->lines[i].expand = FALSE;
623
      lines->lines[i].empty = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
624 625 626 627 628 629 630
    }

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

      attach = &child->attach[orientation];
631
      if (attach->span == 1 && gtk_widget_compute_expand (child->widget, orientation))
Matthias Clasen's avatar
Matthias Clasen committed
632 633 634 635 636 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
        lines->lines[attach->pos - lines->min].expand = TRUE;
    }
}

/* Sums allocations for lines spanned by child and their spacing.
 */
static gint
compute_allocation_for_child (GtkGridRequest *request,
                              GtkGridChild   *child,
                              GtkOrientation  orientation)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  GtkGridLine *line;
  GtkGridChildAttach *attach;
  gint size;
  gint i;

  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];
  attach = &child->attach[orientation];

  size = (attach->span - 1) * linedata->spacing;
  for (i = 0; i < attach->span; i++)
    {
      line = &lines->lines[attach->pos - lines->min + i];
      size += line->allocation;
    }

  return size;
}

static void
compute_request_for_child (GtkGridRequest *request,
                           GtkGridChild   *child,
                           GtkOrientation  orientation,
                           gboolean        contextual,
                           gint           *minimum,
671 672 673
                           gint           *natural,
			   gint           *minimum_baseline,
                           gint           *natural_baseline)
Matthias Clasen's avatar
Matthias Clasen committed
674
{
675 676 677 678
  if (minimum_baseline)
    *minimum_baseline = -1;
  if (natural_baseline)
    *natural_baseline = -1;
Matthias Clasen's avatar
Matthias Clasen committed
679 680 681 682 683 684 685 686 687 688
  if (contextual)
    {
      gint size;

      size = compute_allocation_for_child (request, child, 1 - orientation);
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
        gtk_widget_get_preferred_width_for_height (child->widget,
                                                   size,
                                                   minimum, natural);
      else
689 690 691 692
        gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
								size,
								minimum, natural,
								minimum_baseline, natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
693 694 695 696 697 698
    }
  else
    {
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
        gtk_widget_get_preferred_width (child->widget, minimum, natural);
      else
699 700 701 702
        gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
								-1,
								minimum, natural,
								minimum_baseline, natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    }
}

/* Sets requisition to max. of non-spanning children.
 * If contextual is TRUE, requires allocations of
 * lines in the opposite orientation to be set.
 */
static void
gtk_grid_request_non_spanning (GtkGridRequest *request,
                               GtkOrientation  orientation,
                               gboolean        contextual)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridChild *child;
  GtkGridChildAttach *attach;
  GtkGridLines *lines;
  GtkGridLine *line;
  GList *list;
721 722 723 724
  gint i;
  GtkBaselinePosition baseline_pos;
  gint minimum, minimum_baseline;
  gint natural, natural_baseline;
Matthias Clasen's avatar
Matthias Clasen committed
725 726 727 728 729 730 731

  lines = &request->lines[orientation];

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

732
      if (!_gtk_widget_get_visible (child->widget))
Matthias Clasen's avatar
Matthias Clasen committed
733 734 735 736 737 738
        continue;

      attach = &child->attach[orientation];
      if (attach->span != 1)
        continue;

739
      compute_request_for_child (request, child, orientation, contextual, &minimum, &natural, &minimum_baseline, &natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
740 741

      line = &lines->lines[attach->pos - lines->min];
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

      if (minimum_baseline != -1)
	{
	  line->minimum_above = MAX (line->minimum_above, minimum_baseline);
	  line->minimum_below = MAX (line->minimum_below, minimum - minimum_baseline);
	  line->natural_above = MAX (line->natural_above, natural_baseline);
	  line->natural_below = MAX (line->natural_below, natural - natural_baseline);
	}
      else
	{
	  line->minimum = MAX (line->minimum, minimum);
	  line->natural = MAX (line->natural, natural);
	}
    }

  for (i = 0; i < lines->max - lines->min; i++)
    {
      line = &lines->lines[i];

      if (line->minimum_above != -1)
	{
	  line->minimum = MAX (line->minimum, line->minimum_above + line->minimum_below);
	  line->natural = MAX (line->natural, line->natural_above + line->natural_below);

	  baseline_pos = gtk_grid_get_row_baseline_position (request->grid, i + lines->min);

	  switch (baseline_pos)
	    {
	    case GTK_BASELINE_POSITION_TOP:
	      line->minimum_above += 0;
	      line->minimum_below += line->minimum - (line->minimum_above + line->minimum_below);
	      line->natural_above += 0;
	      line->natural_below += line->natural - (line->natural_above + line->natural_below);
	      break;
	    case GTK_BASELINE_POSITION_CENTER:
	      line->minimum_above += (line->minimum - (line->minimum_above + line->minimum_below))/2;
	      line->minimum_below += (line->minimum - (line->minimum_above + line->minimum_below))/2;
	      line->natural_above += (line->natural - (line->natural_above + line->natural_below))/2;
	      line->natural_below += (line->natural - (line->natural_above + line->natural_below))/2;
	      break;
	    case GTK_BASELINE_POSITION_BOTTOM:
	      line->minimum_above += line->minimum - (line->minimum_above + line->minimum_below);
	      line->minimum_below += 0;
	      line->natural_above += line->natural - (line->natural_above + line->natural_below);
	      line->natural_below += 0;
	      break;
	    }
	}
Matthias Clasen's avatar
Matthias Clasen committed
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 821 822 823
    }
}

/* Enforce homogeneous sizes.
 */
static void
gtk_grid_request_homogeneous (GtkGridRequest *request,
                              GtkOrientation  orientation)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  gint minimum, natural;
  gint i;

  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];

  if (!linedata->homogeneous)
    return;

  minimum = 0;
  natural = 0;

  for (i = 0; i < lines->max - lines->min; i++)
    {
      minimum = MAX (minimum, lines->lines[i].minimum);
      natural = MAX (natural, lines->lines[i].natural);
    }

  for (i = 0; i < lines->max - lines->min; i++)
    {
      lines->lines[i].minimum = minimum;
      lines->lines[i].natural = natural;
824 825
      /* TODO: Do we want to adjust the baseline here too?
	 And if so, also in the homogenous resize. */
Matthias Clasen's avatar
Matthias Clasen committed
826 827 828 829 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
    }
}

/* Deals with spanning children.
 * Requires expand fields of lines to be set for
 * non-spanning children.
 */
static void
gtk_grid_request_spanning (GtkGridRequest *request,
                           GtkOrientation  orientation,
                           gboolean        contextual)
{
  GtkGridPrivate *priv = request->grid->priv;
  GList *list;
  GtkGridChild *child;
  GtkGridChildAttach *attach;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  GtkGridLine *line;
  gint minimum;
  gint natural;
  gint span_minimum;
  gint span_natural;
  gint span_expand;
  gboolean force_expand;
  gint extra;
  gint expand;
  gint line_extra;
  gint i;

  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];

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

863
      if (!_gtk_widget_get_visible (child->widget))
Matthias Clasen's avatar
Matthias Clasen committed
864 865 866 867 868 869
        continue;

      attach = &child->attach[orientation];
      if (attach->span == 1)
        continue;

870 871
      /* We ignore baselines for spanning children */
      compute_request_for_child (request, child, orientation, contextual, &minimum, &natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

      span_minimum = (attach->span - 1) * linedata->spacing;
      span_natural = (attach->span - 1) * linedata->spacing;
      span_expand = 0;
      force_expand = FALSE;
      for (i = 0; i < attach->span; i++)
        {
          line = &lines->lines[attach->pos - lines->min + i];
          span_minimum += line->minimum;
          span_natural += line->natural;
          if (line->expand)
            span_expand += 1;
        }
      if (span_expand == 0)
        {
          span_expand = attach->span;
          force_expand = TRUE;
        }

      /* If we need to request more space for this child to fill
       * its requisition, then divide up the needed space amongst the
       * lines it spans, favoring expandable lines if any.
894 895 896 897 898
       *
       * When doing homogeneous allocation though, try to keep the
       * line allocations even, since we're going to force them to
       * be the same anyway, and we don't want to introduce unnecessary
       * extra space.
Matthias Clasen's avatar
Matthias Clasen committed
899 900 901
       */
      if (span_minimum < minimum)
        {
902
          if (linedata->homogeneous)
Matthias Clasen's avatar
Matthias Clasen committed
903
            {
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
              gint total, m;

              total = minimum - (attach->span - 1) * linedata->spacing;
              m = total / attach->span + (total % attach->span ? 1 : 0);
              for (i = 0; i < attach->span; i++)
                {
                  line = &lines->lines[attach->pos - lines->min + i];
                  line->minimum = MAX(line->minimum, m);
                }
            }
          else
            {
              extra = minimum - span_minimum;
              expand = span_expand;
              for (i = 0; i < attach->span; i++)
Matthias Clasen's avatar
Matthias Clasen committed
919
                {
920 921 922 923 924 925 926 927
                  line = &lines->lines[attach->pos - lines->min + i];
                  if (force_expand || line->expand)
                    {
                      line_extra = extra / expand;
                      line->minimum += line_extra;
                      extra -= line_extra;
                      expand -= 1;
                    }
Matthias Clasen's avatar
Matthias Clasen committed
928 929 930 931 932 933
                }
            }
        }

      if (span_natural < natural)
        {
934 935 936 937 938 939 940 941 942 943 944 945 946
          if (linedata->homogeneous)
            {
              gint total, n;

              total = natural - (attach->span - 1) * linedata->spacing;
              n = total / attach->span + (total % attach->span ? 1 : 0);
              for (i = 0; i < attach->span; i++)
                {
                  line = &lines->lines[attach->pos - lines->min + i];
                  line->natural = MAX(line->natural, n);
                }
            }
          else
Matthias Clasen's avatar
Matthias Clasen committed
947
            {
948 949 950
              extra = natural - span_natural;
              expand = span_expand;
              for (i = 0; i < attach->span; i++)
Matthias Clasen's avatar
Matthias Clasen committed
951
                {
952 953 954 955 956 957 958 959
                  line = &lines->lines[attach->pos - lines->min + i];
                  if (force_expand || line->expand)
                    {
                      line_extra = extra / expand;
                      line->natural += line_extra;
                      extra -= line_extra;
                      expand -= 1;
                    }
Matthias Clasen's avatar
Matthias Clasen committed
960 961 962 963 964 965 966 967 968 969 970
                }
            }
        }
    }
}

/* Marks empty and expanding lines and counts them.
 */
static void
gtk_grid_request_compute_expand (GtkGridRequest *request,
                                 GtkOrientation  orientation,
971 972
				 gint            min,
				 gint            max,
Matthias Clasen's avatar
Matthias Clasen committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
                                 gint           *nonempty_lines,
                                 gint           *expand_lines)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridChild *child;
  GtkGridChildAttach *attach;
  GList *list;
  gint i;
  GtkGridLines *lines;
  GtkGridLine *line;
  gboolean has_expand;
  gint expand;
  gint empty;

  lines = &request->lines[orientation];

989 990 991 992
  min = MAX (min, lines->min);
  max = MIN (max, lines->max);

  for (i = min - lines->min; i < max - lines->min; i++)
Matthias Clasen's avatar
Matthias Clasen committed
993 994 995 996 997 998 999 1000 1001 1002
    {
      lines->lines[i].need_expand = FALSE;
      lines->lines[i].expand = FALSE;
      lines->lines[i].empty = TRUE;
    }

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

1003
      if (!_gtk_widget_get_visible (child->widget))
Matthias Clasen's avatar
Matthias Clasen committed
1004 1005 1006 1007 1008 1009
        continue;

      attach = &child->attach[orientation];
      if (attach->span != 1)
        continue;

1010 1011 1012
      if (attach->pos >= max || attach->pos < min)
	continue;

Matthias Clasen's avatar
Matthias Clasen committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
      line = &lines->lines[attach->pos - lines->min];
      line->empty = FALSE;
      if (gtk_widget_compute_expand (child->widget, orientation))
        line->expand = TRUE;
    }

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

1023
      if (!_gtk_widget_get_visible (child->widget))
Matthias Clasen's avatar
Matthias Clasen committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        continue;

      attach = &child->attach[orientation];
      if (attach->span == 1)
        continue;

      has_expand = FALSE;
      for (i = 0; i < attach->span; i++)
        {
          line = &lines->lines[attach->pos - lines->min + i];
1034 1035 1036 1037 1038 1039 1040

          if (line->expand)
            has_expand = TRUE;

	  if (attach->pos + i >= max || attach->pos + 1 < min)
	    continue;

Matthias Clasen's avatar
Matthias Clasen committed
1041 1042 1043 1044 1045 1046 1047
          line->empty = FALSE;
        }

      if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
        {
          for (i = 0; i < attach->span; i++)
            {
1048 1049 1050
	      if (attach->pos + i >= max || attach->pos + 1 < min)
		continue;

Matthias Clasen's avatar
Matthias Clasen committed
1051 1052 1053 1054 1055 1056 1057 1058
              line = &lines->lines[attach->pos - lines->min + i];
              line->need_expand = TRUE;
            }
        }
    }

  empty = 0;
  expand = 0;
1059
  for (i = min - lines->min; i < max - lines->min; i++)
Matthias Clasen's avatar
Matthias Clasen committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    {
      line = &lines->lines[i];

      if (line->need_expand)
        line->expand = TRUE;

      if (line->empty)
        empty += 1;

      if (line->expand)
        expand += 1;
    }

  if (nonempty_lines)
1074
    *nonempty_lines = max - min - empty;
Matthias Clasen's avatar
Matthias Clasen committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

  if (expand_lines)
    *expand_lines = expand;
}

/* Sums the minimum and natural fields of lines and their spacing.
 */
static void
gtk_grid_request_sum (GtkGridRequest *request,
                      GtkOrientation  orientation,
                      gint           *minimum,
1086 1087 1088
                      gint           *natural,
		      gint           *minimum_baseline,
		      gint           *natural_baseline)
Matthias Clasen's avatar
Matthias Clasen committed
1089 1090 1091 1092 1093 1094 1095 1096
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  gint i;
  gint min, nat;
  gint nonempty;

1097
  gtk_grid_request_compute_expand (request, orientation, G_MININT, G_MAXINT, &nonempty, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1098 1099 1100 1101

  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];

1102 1103
  min = 0;
  nat = 0;
Matthias Clasen's avatar
Matthias Clasen committed
1104 1105
  for (i = 0; i < lines->max - lines->min; i++)
    {
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
      if (orientation == GTK_ORIENTATION_VERTICAL &&
	  lines->min + i == priv->baseline_row &&
	  lines->lines[i].minimum_above != -1)
	{
	  if (minimum_baseline)
	    *minimum_baseline = min + lines->lines[i].minimum_above;
	  if (natural_baseline)
	    *natural_baseline = nat + lines->lines[i].natural_above;
	}

Matthias Clasen's avatar
Matthias Clasen committed
1116 1117
      min += lines->lines[i].minimum;
      nat += lines->lines[i].natural;
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130

      if (!lines->lines[i].empty)
	{
	  min += linedata->spacing;
	  nat += linedata->spacing;
	}
    }

  /* Remove last spacing, if any was applied */
  if (nonempty > 0)
    {
      min -= linedata->spacing;
      nat -= linedata->spacing;
Matthias Clasen's avatar
Matthias Clasen committed
1131 1132
    }

1133 1134
  *minimum = min;
  *natural = nat;
Matthias Clasen's avatar
Matthias Clasen committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
}

/* Computes minimum and natural fields of lines.
 * When contextual is TRUE, requires allocation of
 * lines in the opposite orientation to be set.
 */
static void
gtk_grid_request_run (GtkGridRequest *request,
                      GtkOrientation  orientation,
                      gboolean        contextual)
{
  gtk_grid_request_init (request, orientation);
  gtk_grid_request_non_spanning (request, orientation, contextual);
  gtk_grid_request_homogeneous (request, orientation);
  gtk_grid_request_spanning (request, orientation, contextual);
  gtk_grid_request_homogeneous (request, orientation);
}

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 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 1221 1222 1223
static void
gtk_grid_distribute_non_homogeneous (GtkGridLines *lines,
				     gint nonempty,
				     gint expand,
				     gint size,
				     gint min,
				     gint max)
{
  GtkRequestedSize *sizes;
  GtkGridLine *line;
  gint extra;
  gint rest;
  int i, j;

  if (nonempty == 0)
    return;

  sizes = g_newa (GtkRequestedSize, nonempty);

  j = 0;
  for (i = min - lines->min; i < max - lines->min; i++)
    {
      line = &lines->lines[i];
      if (line->empty)
	continue;

      size -= line->minimum;

      sizes[j].minimum_size = line->minimum;
      sizes[j].natural_size = line->natural;
      sizes[j].data = line;
      j++;
    }

  size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);

  if (expand > 0)
    {
      extra = size / expand;
      rest = size % expand;
    }
  else
    {
      extra = 0;
      rest = 0;
    }

  j = 0;
  for (i = min - lines->min; i < max - lines->min; i++)
    {
      line = &lines->lines[i];
      if (line->empty)
	continue;

      g_assert (line == sizes[j].data);

      line->allocation = sizes[j].minimum_size;
      if (line->expand)
	{
	  line->allocation += extra;
	  if (rest > 0)
	    {
	      line->allocation += 1;
	      rest -= 1;
	    }
	}

      j++;
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
/* Requires that the minimum and natural fields of lines
 * have been set, computes the allocation field of lines
 * by distributing total_size among lines.
 */
static void
gtk_grid_request_allocate (GtkGridRequest *request,
                           GtkOrientation  orientation,
                           gint            total_size)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  GtkGridLine *line;
1237 1238 1239 1240 1241 1242
  gint nonempty1, nonempty2;
  gint expand1, expand2;
  gint i;
  GtkBaselinePosition baseline_pos;
  gint baseline;
  gint extra, extra2;
Matthias Clasen's avatar
Matthias Clasen committed
1243
  gint rest;
1244 1245
  gint size1, size2;
  gint split, split_pos;
1246

Matthias Clasen's avatar
Matthias Clasen committed
1247 1248 1249
  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
  baseline = gtk_widget_get_allocated_baseline (GTK_WIDGET (request->grid));

  if (orientation == GTK_ORIENTATION_VERTICAL && baseline != -1 &&
      priv->baseline_row >= lines->min && priv->baseline_row < lines->max &&
      lines->lines[priv->baseline_row - lines->min].minimum_above != -1)
    {
      split = priv->baseline_row;
      split_pos = baseline - lines->lines[priv->baseline_row - lines->min].minimum_above;
      gtk_grid_request_compute_expand (request, orientation, lines->min, split, &nonempty1, &expand1);
      gtk_grid_request_compute_expand (request, orientation, split, lines->max, &nonempty2, &expand2);

      if (nonempty2 > 0)
	{
	  size1 = split_pos - (nonempty1) * linedata->spacing;
	  size2 = (total_size - split_pos) - (nonempty2 - 1) * linedata->spacing;
	}
      else
	{
	  size1 = total_size - (nonempty1 - 1) * linedata->spacing;
	  size2 = 0;
	}
    }
  else
    {
      gtk_grid_request_compute_expand (request, orientation, lines->min, lines->max, &nonempty1, &expand1);
      nonempty2 = expand2 = 0;
      split = lines->max;

      size1 = total_size - (nonempty1 - 1) * linedata->spacing;
      size2 = 0;
    }

  if (nonempty1 == 0 && nonempty2 == 0)
    return;
Matthias Clasen's avatar
Matthias Clasen committed
1284 1285 1286

  if (linedata->homogeneous)
    {
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
      if (nonempty1 > 0)
	{
	  extra = size1 / nonempty1;
	  rest = size1 % nonempty1;
	}
      else
	{
	  extra = 0;
	  rest = 0;
	}
      if (nonempty2 > 0)
	{
	  extra2 = size2 / nonempty2;
	  if (extra2 < extra || nonempty1 == 0)
	    {
	      extra = extra2;
	      rest = size2 % nonempty2;
	    }
	}
Matthias Clasen's avatar
Matthias Clasen committed
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

      for (i = 0; i < lines->max - lines->min; i++)
        {
          line = &lines->lines[i];
          if (line->empty)
            continue;

          line->allocation = extra;
          if (rest > 0)
            {
              line->allocation += 1;
              rest -= 1;
            }
        }
    }
  else
    {
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
      gtk_grid_distribute_non_homogeneous (lines,
					   nonempty1,
					   expand1,
					   size1,
					   lines->min,
					   split);
      gtk_grid_distribute_non_homogeneous (lines,
					   nonempty2,
					   expand2,
					   size2,
					   split,
					   lines->max);
    }
Matthias Clasen's avatar
Matthias Clasen committed
1336

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
  for (i = 0; i < lines->max - lines->min; i++)
    {
      line = &lines->lines[i];
      if (line->empty)
	continue;

      if (line->minimum_above != -1)
	{
	  /* Note: This is overridden in gtk_grid_request_position for the allocated baseline */
	  baseline_pos = gtk_grid_get_row_baseline_position (request->grid, i + lines->min);

	  switch (baseline_pos)
	    {
	    case GTK_BASELINE_POSITION_TOP:
	      line->allocated_baseline =
		line->minimum_above;
	      break;
	    case GTK_BASELINE_POSITION_CENTER:
	      line->allocated_baseline =
		line->minimum_above +
		(line->allocation - (line->minimum_above + line->minimum_below)) / 2;
	      break;
	    case GTK_BASELINE_POSITION_BOTTOM:
	      line->allocated_baseline =
		line->allocation - line->minimum_below;
	      break;
	    }
	}
Matthias Clasen's avatar
Matthias Clasen committed
1365
      else
1366
	line->allocated_baseline = -1;
Matthias Clasen's avatar
Matthias Clasen committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    }
}

/* Computes the position fields from allocation and spacing.
 */
static void
gtk_grid_request_position (GtkGridRequest *request,
                           GtkOrientation  orientation)
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  GtkGridLine *line;
1380 1381 1382
  gint position, old_position;
  int allocated_baseline;
  gint i, j;
Matthias Clasen's avatar
Matthias Clasen committed
1383 1384 1385 1386

  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];

1387 1388
  allocated_baseline = gtk_widget_get_allocated_baseline (GTK_WIDGET(request->grid));

Matthias Clasen's avatar
Matthias Clasen committed
1389 1390 1391 1392
  position = 0;
  for (i = 0; i < lines->max - lines->min; i++)
    {
      line = &lines->lines[i];
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

      if (orientation == GTK_ORIENTATION_VERTICAL &&
	  i + lines->min == priv->baseline_row &&
	  allocated_baseline != -1 &&
	  lines->lines[i].minimum_above != -1)
	{
	  old_position = position;
	  position = allocated_baseline - line->minimum_above;

	  /* Back-patch previous rows */
	  for (j = 0; j < i; j++)
	    {
	      if (!lines->lines[j].empty)
		lines->lines[j].position += position - old_position;
	    }
	}

Matthias Clasen's avatar
Matthias Clasen committed
1410 1411 1412 1413
      if (!line->empty)
        {
          line->position = position;
          position += line->allocation + linedata->spacing;
1414 1415 1416 1417 1418 1419

	  if (orientation == GTK_ORIENTATION_VERTICAL &&
	      i + lines->min == priv->baseline_row &&
	      allocated_baseline != -1 &&
	      lines->lines[i].minimum_above != -1)
	    line->allocated_baseline = allocated_baseline - line->position;
Matthias Clasen's avatar
Matthias Clasen committed
1420 1421 1422 1423 1424 1425 1426 1427
        }
    }
}

static void
gtk_grid_get_size (GtkGrid        *grid,
                   GtkOrientation  orientation,
                   gint           *minimum,
1428 1429 1430
                   gint           *natural,
		   gint           *minimum_baseline,
		   gint           *natural_baseline)
Matthias Clasen's avatar
Matthias Clasen committed
1431 1432 1433 1434
{
  GtkGridRequest request;
  GtkGridLines *lines;

1435 1436
  *minimum = 0;
  *natural = 0;
1437

1438 1439 1440 1441 1442 1443
  if (minimum_baseline)
    *minimum_baseline = -1;

  if (natural_baseline)
    *natural_baseline = -1;

1444 1445 1446
  if (grid->priv->children == NULL)
    return;

Matthias Clasen's avatar
Matthias Clasen committed
1447 1448 1449 1450
  request.grid = grid;
  gtk_grid_request_count_lines (&request);
  lines = &request.lines[orientation];
  lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1451
  memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
Matthias Clasen's avatar
Matthias Clasen committed
1452 1453

  gtk_grid_request_run (&request, orientation, FALSE);
1454 1455
  gtk_grid_request_sum (&request, orientation, minimum, natural,
			minimum_baseline, natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
1456 1457 1458 1459 1460 1461 1462
}

static void
gtk_grid_get_size_for_size (GtkGrid        *grid,
                            GtkOrientation  orientation,
                            gint            size,
                            gint           *minimum,
1463 1464 1465
                            gint           *natural,
			    gint           *minimum_baseline,
                            gint           *natural_baseline)
Matthias Clasen's avatar
Matthias Clasen committed
1466 1467 1468
{
  GtkGridRequest request;
  GtkGridLines *lines;
1469
  gint min_size, nat_size;
Matthias Clasen's avatar
Matthias Clasen committed
1470

1471 1472 1473 1474 1475 1476
  if (minimum)
    *minimum = 0;

  if (natural)
    *natural = 0;

1477 1478 1479 1480 1481 1482
  if (minimum_baseline)
    *minimum_baseline = -1;

  if (natural_baseline)
    *natural_baseline = -1;

1483 1484 1485
  if (grid->priv->children == NULL)
    return;

Matthias Clasen's avatar
Matthias Clasen committed
1486 1487 1488 1489
  request.grid = grid;
  gtk_grid_request_count_lines (&request);
  lines = &request.lines[0];
  lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1490
  memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
Matthias Clasen's avatar
Matthias Clasen committed
1491 1492
  lines = &request.lines[1];
  lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1493
  memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
Matthias Clasen's avatar
Matthias Clasen committed
1494 1495

  gtk_grid_request_run (&request, 1 - orientation, FALSE);
1496
  gtk_grid_request_sum (&request, 1 - orientation, &min_size, &nat_size, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1497 1498 1499
  gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));

  gtk_grid_request_run (&request, orientation, TRUE);
1500
  gtk_grid_request_sum (&request, orientation, minimum, natural, minimum_baseline, natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
1501 1502 1503 1504 1505 1506 1507 1508 1509
}

static void
gtk_grid_get_preferred_width (GtkWidget *widget,
                              gint      *minimum,
                              gint      *natural)
{
  GtkGrid *grid = GTK_GRID (widget);

1510
  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1511
    gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1512
  else
1513
    gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1514 1515 1516 1517 1518 1519 1520 1521 1522
}

static void
gtk_grid_get_preferred_height (GtkWidget *widget,
                               gint      *minimum,
                               gint      *natural)
{
  GtkGrid *grid = GTK_GRID (widget);

1523
  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1524
    gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1525
  else
1526
    gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}

static void
gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
                                         gint       height,
                                         gint      *minimum,
                                         gint      *natural)
{
  GtkGrid *grid = GTK_GRID (widget);

1537
  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1538
    gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1539
  else
1540
    gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
}

static void
gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
                                         gint       width,
                                         gint      *minimum,
                                         gint      *natural)
{
  GtkGrid *grid = GTK_GRID (widget);

1551
  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
    gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural, NULL, NULL);
  else
    gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural, NULL, NULL);
}

static void
gtk_grid_get_preferred_height_and_baseline_for_width (GtkWidget *widget,
						      gint       width,
						      gint      *minimum,
						      gint      *natural,
						      gint      *minimum_baseline,
						      gint      *natural_baseline)
{
  GtkGrid *grid = GTK_GRID (widget);

  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH && width != -1)
    gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural, minimum_baseline, natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
1569
  else
1570
    gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural, minimum_baseline, natural_baseline);
Matthias Clasen's avatar
Matthias Clasen committed
1571 1572
}

1573

Matthias Clasen's avatar
Matthias Clasen committed
1574 1575 1576 1577 1578
static void
allocate_child (GtkGridRequest *request,
                GtkOrientation  orientation,
                GtkGridChild   *child,
                gint           *position,
1579 1580
                gint           *size,
		gint           *baseline)
Matthias Clasen's avatar
Matthias Clasen committed
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
{
  GtkGridPrivate *priv = request->grid->priv;
  GtkGridLineData *linedata;
  GtkGridLines *lines;
  GtkGridLine *line;
  GtkGridChildAttach *attach;
  gint i;

  linedata = &priv->linedata[orientation];
  lines = &request->lines[orientation];
  attach = &child->attach[orientation];

  *position = lines->lines[attach->pos - lines->min].position;
1594 1595 1596 1597
  if (attach->span == 1)
    *baseline = lines->lines[attach->pos - lines->min].allocated_baseline;
  else
    *baseline = -1;
Matthias Clasen's avatar
Matthias Clasen committed
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614

  *size = (attach->span - 1) * linedata->spacing;
  for (i = 0; i < attach->span; i++)
    {
      line = &lines->lines[attach->pos - lines->min + i];
      *size += line->allocation;
    }
}

static void
gtk_grid_request_allocate_children (GtkGridRequest *request)
{
  GtkGridPrivate *priv = request->grid->priv;
  GList *list;
  GtkGridChild *child;
  GtkAllocation allocation;
  GtkAllocation child_allocation;
1615
  gint x, y, width, height, baseline, ignore;
Matthias Clasen's avatar
Matthias Clasen committed
1616 1617 1618 1619 1620 1621 1622

  gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);

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

1623
      if (!_gtk_widget_get_visible (child->widget))
Matthias Clasen's avatar
Matthias Clasen committed
1624 1625
        continue;

1626 1627
      allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width, &ignore);
      allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height, &baseline);
Matthias Clasen's avatar
Matthias Clasen committed
1628 1629 1630 1631 1632 1633

      child_allocation.x = allocation.x + x;
      child_allocation.y = allocation.y + y;
      child_allocation.width = MAX (1, width);
      child_allocation.height = MAX (1, height);

1634 1635 1636 1637
      if (gtk_widget_get_direction (GTK_WIDGET (request->grid)) == GTK_TEXT_DIR_RTL)
        child_allocation.x = allocation.x + allocation.width
                             - (child_allocation.x - allocation.x) - child_allocation.width;

1638
      gtk_widget_size_allocate_with_baseline (child->widget, &child_allocation, baseline);
Matthias Clasen's avatar
Matthias Clasen committed
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
    }
}

#define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)

static void
gtk_grid_size_allocate (GtkWidget     *widget,
                        GtkAllocation *allocation)
{
  GtkGrid *grid = GTK_GRID (widget);
  GtkGridPrivate *priv = grid->priv;
  GtkGridRequest request;
  GtkGridLines *lines;
1652
  GtkOrientation orientation;
Matthias Clasen's avatar
Matthias Clasen committed
1653

1654 1655 1656 1657 1658 1659
  if (priv->children == NULL)
    {
      gtk_widget_set_allocation (widget, allocation);
      return;
    }

Matthias Clasen's avatar
Matthias Clasen committed
1660
  request.grid = grid;
1661

Matthias Clasen's avatar
Matthias Clasen committed
1662 1663 1664
  gtk_grid_request_count_lines (&request);
  lines = &request.lines[0];
  lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1665
  memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
Matthias Clasen's avatar
Matthias Clasen committed
1666 1667
  lines = &request.lines[1];
  lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1668
  memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
Matthias Clasen's avatar
Matthias Clasen committed
1669 1670 1671

  gtk_widget_set_allocation (widget, allocation);

1672 1673 1674 1675 1676 1677 1678 1679
  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
    orientation = GTK_ORIENTATION_HORIZONTAL;
  else
    orientation = GTK_ORIENTATION_VERTICAL;

  gtk_grid_request_run (&request, 1 - orientation, FALSE);
  gtk_grid_request_allocate (&request, 1 - orientation, GET_SIZE (allocation, 1 - orientation));
  gtk_grid_request_run (&request, orientation, TRUE);
1680

1681
  gtk_grid_request_allocate (&request, orientation, GET_SIZE (allocation, orientation));
Matthias Clasen's avatar
Matthias Clasen committed
1682 1683 1684 1685 1686

  gtk_grid_request_position (&request, 0);
  gtk_grid_request_position (&request, 1);

  gtk_grid_request_allocate_children (&request);
1687

1688
  _gtk_widget_set_simple_clip (widget, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1689 1690
}

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
static gboolean
gtk_grid_draw (GtkWidget *widget,
               cairo_t   *cr)
{
  GtkStyleContext *context;
  GtkAllocation allocation;

  context = gtk_widget_get_style_context (widget);
  gtk_widget_get_allocation (widget, &allocation);

  gtk_render_background (context, cr, 0, 0, allocation.width, allocation.height);
  gtk_render_frame (context, cr, 0, 0, allocation.width, allocation.height);

  return GTK_WIDGET_CLASS (gtk_grid_parent_class)->draw (widget, cr);
}

Matthias Clasen's avatar
Matthias Clasen committed
1707 1708 1709 1710 1711 1712 1713 1714 1715
static void
gtk_grid_class_init (GtkGridClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);

  object_class->get_property = gtk_grid_get_property;
  object_class->set_property = gtk_grid_set_property;
1716
  object_class->finalize = gtk_grid_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
1717 1718 1719 1720 1721 1722

  widget_class->size_allocate = gtk_grid_size_allocate;
  widget_class->get_preferred_width = gtk_grid_get_preferred_width;
  widget_class->get_preferred_height = gtk_grid_get_preferred_height;
  widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1723
  widget_class->get_preferred_height_and_baseline_for_width = gtk_grid_get_preferred_height_and_baseline_for_width;
1724
  widget_class->draw = gtk_grid_draw;
Matthias Clasen's avatar
Matthias Clasen committed
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735

  container_class->add = gtk_grid_add;
  container_class->remove = gtk_grid_remove;
  container_class->forall = gtk_grid_forall;
  container_class->child_type = gtk_grid_child_type;
  container_class->set_child_property = gtk_grid_set_child_property;
  container_class->get_child_property = gtk_grid_get_child_property;
  gtk_container_class_handle_border_width (container_class);

  g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");

1736
  obj_properties[PROP_ROW_SPACING] =
Matthias Clasen's avatar
Matthias Clasen committed
1737 1738 1739 1740
    g_param_spec_int ("row-spacing",
                      P_("Row spacing"),
                      P_("The amount of space between two consecutive rows"),
                      0, G_MAXINT16, 0,
1741
                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
Matthias Clasen's avatar
Matthias Clasen committed
1742

1743
  obj_properties[PROP_COLUMN_SPACING] =
Matthias Clasen's avatar
Matthias Clasen committed
1744 1745 1746 1747
    g_param_spec_int ("column-spacing",
                      P_("Column spacing"),
                      P_("The amount of space between two consecutive columns"),
                      0, G_MAXINT16, 0,
1748
                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
Matthias Clasen's avatar
Matthias Clasen committed
1749

1750
  obj_properties[PROP_ROW_HOMOGENEOUS] =
Matthias Clasen's avatar
Matthias Clasen committed
1751 1752 1753 1754
    g_param_spec_boolean ("row-homogeneous",
                          P_("Row Homogeneous"),
                          P_("If TRUE, the rows are all the same height"),
                          FALSE,
1755
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
Matthias Clasen's avatar
Matthias Clasen committed
1756

1757
  obj_properties[PROP_COLUMN_HOMOGENEOUS] =
Matthias Clasen's avatar
Matthias Clasen committed
1758 1759 1760 1761
    g_param_spec_boolean ("column-homogeneous",
                          P_("Column Homogeneous"),
                          P_("If TRUE, the columns are all the same width"),
                          FALSE,
1762
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
1763

1764
  obj_properties[PROP_BASELINE_ROW] =
1765 1766 1767 1768
    g_param_spec_int ("baseline-row",
                      P_("Baseline Row"),
                      P_("The row to align the to the baseline when valign is GTK_ALIGN_BASELINE"),
                      0, G_MAXINT, 0,
1769
                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
1770 1771 1772 1773

  g_object_class_install_properties (object_class,
                                     N_PROPERTIES,
                                     obj_properties);
1774

1775
  child_properties[CHILD_PROP_LEFT_ATTACH] =
Matthias Clasen's avatar
Matthias Clasen committed
1776 1777 1778 1779
    g_param_spec_int ("left-attach",
                      P_("Left attachment"),
                      P_("The column number to attach the left side of the child to"),
                      G_MININT, G_MAXINT, 0,
1780
                      GTK_PARAM_READWRITE);
Matthias Clasen's avatar
Matthias Clasen committed
1781

1782
  child_properties[CHILD_PROP_TOP_ATTACH] =
Matthias Clasen's avatar
Matthias Clasen committed
1783 1784 1785 1786
    g_param_spec_int ("top-attach",
                      P_("Top attachment"),
                      P_("The row number to attach the top side of a child widget to"),
                      G_MININT, G_MAXINT, 0,
1787
                      GTK_PARAM_READWRITE);
Matthias Clasen's avatar
Matthias Clasen committed
1788

1789
  child_properties[CHILD_PROP_WIDTH] =
Matthias Clasen's avatar
Matthias Clasen committed
1790 1791 1792 1793
    g_param_spec_int ("width",
                      P_("Width"),
                      P_("The number of columns that a child spans"),
                      1, G_MAXINT, 1,
1794
                      GTK_PARAM_READWRITE);
Matthias Clasen's avatar
Matthias Clasen committed
1795

1796
  child_properties[CHILD_PROP_HEIGHT] =
Matthias Clasen's avatar
Matthias Clasen committed
1797 1798 1799 1800
    g_param_spec_int ("height",
                      P_("Height"),
                      P_("The number of rows that a child spans"),
                      1, G_MAXINT, 1,
1801 1802 1803
                      GTK_PARAM_READWRITE);

  gtk_container_class_install_child_properties (container_class, N_CHILD_PROPERTIES, child_properties);
Matthias Clasen's avatar
Matthias Clasen committed
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
}

/**
 * gtk_grid_new:
 *
 * Creates a new grid widget.
 *
 * Returns: the new #GtkGrid
 */
GtkWidget *
gtk_grid_new (void)
{
  return g_object_new (GTK_TYPE_GRID, NULL);
}

/**
 * gtk_grid_attach:
 * @grid: a #GtkGrid
 * @child: the widget to add
 * @left: the column number to attach the left side of @child to
 * @top: the row number to attach the top side of @child to
 * @width: the number of columns that @child will span
 * @height: the number of rows that @child will span
 *
 * Adds a widget to the grid.
 *
 * The position of @child is determined by @left and @top. The
1831
 * number of “cells” that @child will occupy is determined by
Matthias Clasen's avatar
Matthias Clasen committed
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
 * @width and @height.
 */
void
gtk_grid_attach (GtkGrid   *grid,
                 GtkWidget *child,
                 gint       left,
                 gint       top,
                 gint       width,
                 gint       height)
{
  g_return_if_fail (GTK_IS_GRID (grid));
  g_return_if_fail (GTK_IS_WIDGET (child));
1844
  g_return_if_fail (_gtk_widget_get_parent (child) == NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
  g_return_if_fail (width > 0);
  g_return_if_fail (height > 0);

  grid_attach (grid, child, left, top, width, height);
}

/**
 * gtk_grid_attach_next_to:
 * @grid: a #GtkGrid
 * @child: the widget to add
1855
 * @sibling: (allow-none): the child of @grid that @child will be placed
1856
 *     next to, or %NULL to place @child at the beginning or end
Matthias Clasen's avatar
Matthias Clasen committed
1857 1858 1859 1860 1861 1862 1863
 * @side: the side of @sibling that @child is positioned next to
 * @width: the number of columns that @child will span
 * @height: the number of rows that @child will span
 *
 * Adds a widget to the grid.
 *
 * The widget is placed next to @sibling, on the side determined by
1864 1865 1866
 * @side. When @sibling is %NULL, the widget is placed in row (for
 * left or right placement) or column 0 (for top or bottom placement),
 * at the end indicated by @side.
1867 1868 1869
 *
 * Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and
 * @side == %GTK_POS_LEFT yields a layout of [3][2][1].
Matthias Clasen's avatar
Matthias Clasen committed
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
 */
void
gtk_grid_attach_next_to (GtkGrid         *grid,
                         GtkWidget       *child,
                         GtkWidget       *sibling,
                         GtkPositionType  side,
                         gint             width,
                         gint             height)
{
  GtkGridChild *grid_sibling;
  gint left, top;

  g_return_if_fail (GTK_IS_GRID (grid));
  g_return_if_fail (GTK_IS_WIDGET (child));
1884 1885
  g_return_if_fail (_gtk_widget_get_parent (child) == NULL);
  g_return_if_fail (sibling == NULL || _gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
Matthias Clasen's avatar
Matthias Clasen committed
1886 1887 1888
  g_return_if_fail (width > 0);
  g_return_if_fail (height > 0);

1889 1890 1891
  if (sibling)
    {
      grid_sibling = find_grid_child (grid, sibling);
Matthias Clasen's avatar
Matthias Clasen committed
1892

1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
      switch (side)
        {
        case GTK_POS_LEFT:
          left = CHILD_LEFT (grid_sibling) - width;
          top = CHILD_TOP (grid_sibling);
          break;
        case GTK_POS_RIGHT:
          left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
          top = CHILD_TOP (grid_sibling);
          break;
        case GTK_POS_TOP:
          left = CHILD_LEFT (grid_sibling);
          top = CHILD_TOP (grid_sibling) - height;
          break;
        case GTK_POS_BOTTOM:
          left = CHILD_LEFT (grid_sibling);
          top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
          break;
        default:
          g_assert_not_reached ();
        }
    }
  else
Matthias Clasen's avatar
Matthias Clasen committed
1916
    {
1917 1918 1919
      switch (side)
        {
        case GTK_POS_LEFT:
1920 1921
          left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, FALSE);
          left -= width;
1922 1923 1924
          top = 0;
          break;
        case GTK_POS_RIGHT:
1925
          left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, TRUE);
1926 1927 1928 1929
          top = 0;
          break;
        case GTK_POS_TOP:
          left = 0;
1930 1931
          top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, FALSE);
          top -= height;
1932 1933 1934
          break;
        case GTK_POS_BOTTOM:
          left = 0;
1935
          top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, TRUE);
1936 1937 1938 1939
          break;
        default:
          g_assert_not_reached ();
        }
Matthias Clasen's avatar
Matthias Clasen committed
1940 1941 1942 1943 1944
    }

  grid_attach (grid, child, left, top, width, height);
}

1945 1946 1947 1948 1949 1950 1951 1952 1953
/**
 * gtk_grid_get_child_at:
 * @grid: a #GtkGrid
 * @left: the left edge of the cell
 * @top: the top edge of the cell
 *
 * Gets the child of @grid whose area covers the grid
 * cell whose upper left corner is at @left, @top.
 *
1954
 * Returns: (transfer none) (nullable): the child at the given position, or %NULL
1955 1956 1957 1958 1959 1960 1961 1962
 *
 * Since: 3.2
 */
GtkWidget *
gtk_grid_get_child_at (GtkGrid *grid,
                       gint     left,
                       gint     top)
{
1963
  GtkGridPrivate *priv;
1964 1965 1966
  GtkGridChild *child;
  GList *list;

1967 1968 1969 1970
  g_return_val_if_fail (GTK_IS_GRID (grid), NULL);

  priv = grid->priv;

1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
  for (list = priv->children; list; list = list->next)
    {
      child = list->data;

      if (CHILD_LEFT (child) <= left &&
          CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
          CHILD_TOP (child) <= top &&
          CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
        return child->widget;
    }

  return NULL;
}

1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
/**
 * gtk_grid_insert_row:
 * @grid: a #GtkGrid
 * @position: the position to insert the row at
 *
 * Inserts a row at the specified position.
 *
 * Children which are attached at or below this position
 * are moved one row down. Children which span across this
 * position are grown to span the new row.
 *
 * Since: 3.2
 */
void
gtk_grid_insert_row (GtkGrid *grid,
                     gint     position)
{
2002
  GtkGridPrivate *priv;
2003 2004 2005 2006 2007 2008
  GtkGridChild *child;
  GList *list;
  gint top, height;

  g_return_if_fail (GTK_IS_GRID (grid));

2009 2010
  priv = grid->priv;

2011 2012 2013 2014 2015 2016 2017 2018 2019