gtkbox.c 83 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25 26
/**
 * SECTION:gtkbox
27
 * @Short_description: A container box
28
 * @Title: GtkBox
29
 * @See_also: #GtkFrame, #GtkGrid, #GtkLayout
30
 *
31
 * The GtkBox widget organizes child widgets into a rectangular area.
32 33
 *
 * The rectangular area of a GtkBox is organized into either a single row
34
 * or a single column of child widgets depending upon the orientation.
35 36
 * Thus, all children of a GtkBox are allocated one dimension in common,
 * which is the height of a row, or the width of a column.
37
 *
38
 * GtkBox uses a notion of packing. Packing refers
39 40
 * to adding widgets with reference to a particular position in a
 * #GtkContainer. For a GtkBox, there are two reference positions: the
41
 * start and the end of the box.
42 43 44
 * For a vertical #GtkBox, the start is defined as the top of the box and
 * the end is defined as the bottom. For a horizontal #GtkBox the start
 * is defined as the left side and the end is defined as the right side.
45 46
 *
 * Use repeated calls to gtk_box_pack_start() to pack widgets into a
47 48
 * GtkBox from start to end. Use gtk_box_pack_end() to add widgets from
 * end to start. You may intersperse these calls and add widgets from
49 50
 * both ends of the same GtkBox.
 *
51 52
 * Because GtkBox is a #GtkContainer, you may also use gtk_container_add()
 * to insert widgets into the box, and they will be packed with the default
53
 * values for expand and fill child properties. Use gtk_container_remove()
54
 * to remove widgets from the GtkBox.
55 56 57 58 59
 *
 * Use gtk_box_set_homogeneous() to specify whether or not all children
 * of the GtkBox are forced to get the same amount of space.
 *
 * Use gtk_box_set_spacing() to determine how much space will be
60
 * minimally placed between all children in the GtkBox. Note that
61
 * spacing is added between the children, while
62
 * padding added by gtk_box_pack_start() or gtk_box_pack_end() is added
63
 * on either side of the widget it belongs to.
64 65 66 67
 *
 * Use gtk_box_reorder_child() to move a GtkBox child to a different
 * place in the box.
 *
68 69
 * Use gtk_box_set_child_packing() to reset the expand,
 * fill and padding child properties.
70
 * Use gtk_box_query_child_packing() to query these fields.
71
 *
72 73
 * Note that a single-row or single-column #GtkGrid provides exactly
 * the same functionality as #GtkBox.
74
 *
75
 * # CSS nodes
76
 *
77 78 79 80 81
 * GtkBox uses a single CSS node with name box.
 *
 * In horizontal orientation, the nodes of the children are always arranged
 * from left to right. So :first-child will always select the leftmost child,
 * regardless of text direction.
82 83
 */

84
#include "config.h"
85

Elliot Lee's avatar
Elliot Lee committed
86
#include "gtkbox.h"
87
#include "gtkboxprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
88 89
#include "gtkcontainerprivate.h"
#include "gtkcsscustomgadgetprivate.h"
90
#include "gtkcssnodeprivate.h"
91
#include "gtkintl.h"
92
#include "gtkorientable.h"
93
#include "gtkorientableprivate.h"
94
#include "gtkprivate.h"
95 96 97
#include "gtktypebuiltins.h"
#include "gtksizerequest.h"
#include "gtkwidgetpath.h"
98
#include "gtkwidgetprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
99
#include "a11y/gtkcontaineraccessible.h"
100

Elliot Lee's avatar
Elliot Lee committed
101

102
enum {
103 104
  PROP_0,
  PROP_SPACING,
105
  PROP_HOMOGENEOUS,
106 107 108
  PROP_BASELINE_POSITION,

  /* orientable */
109 110
  PROP_ORIENTATION,
  LAST_PROP = PROP_ORIENTATION
111
};
Elliot Lee's avatar
Elliot Lee committed
112

113
enum {
Tim Janik's avatar
Tim Janik committed
114 115 116 117 118
  CHILD_PROP_0,
  CHILD_PROP_EXPAND,
  CHILD_PROP_FILL,
  CHILD_PROP_PADDING,
  CHILD_PROP_PACK_TYPE,
119 120
  CHILD_PROP_POSITION,
  LAST_CHILD_PROP
121 122
};

123 124
typedef struct _GtkBoxChild        GtkBoxChild;

125
struct _GtkBoxPrivate
126
{
127
  GList          *children;
128
  GtkBoxChild    *center;
Benjamin Otte's avatar
Benjamin Otte committed
129
  GtkCssGadget   *gadget;
130

131
  GtkOrientation  orientation;
132 133 134 135 136
  gint16          spacing;

  guint           default_expand : 1;
  guint           homogeneous    : 1;
  guint           spacing_set    : 1;
137
  guint           baseline_pos   : 2;
138 139
};

140
static GParamSpec *props[LAST_PROP] = { NULL, };
141
static GParamSpec *child_props[LAST_CHILD_PROP] = { NULL, };
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
 * GtkBoxChild:
 * @widget: the child widget, packed into the GtkBox.
 * @padding: the number of extra pixels to put between this child and its
 *  neighbors, set when packed, zero by default.
 * @expand: flag indicates whether extra space should be given to this child.
 *  Any extra space given to the parent GtkBox is divided up among all children
 *  with this attribute set to %TRUE; set when packed, %TRUE by default.
 * @fill: flag indicates whether any extra space given to this child due to its
 *  @expand attribute being set is actually allocated to the child, rather than
 *  being used as padding around the widget; set when packed, %TRUE by default.
 * @pack: one of #GtkPackType indicating whether the child is packed with
 *  reference to the start (top/left) or end (bottom/right) of the GtkBox.
 */
struct _GtkBoxChild
{
  GtkWidget *widget;

  guint16    padding;

  guint      expand : 1;
  guint      fill   : 1;
  guint      pack   : 1;
};
167 168 169

static void gtk_box_size_allocate         (GtkWidget              *widget,
                                           GtkAllocation          *allocation);
170 171
static gboolean gtk_box_draw           (GtkWidget        *widget,
                                        cairo_t          *cr);
172

173 174
static void gtk_box_direction_changed  (GtkWidget        *widget,
                                        GtkTextDirection  previous_direction);
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static void gtk_box_set_property       (GObject        *object,
                                        guint           prop_id,
                                        const GValue   *value,
                                        GParamSpec     *pspec);
static void gtk_box_get_property       (GObject        *object,
                                        guint           prop_id,
                                        GValue         *value,
                                        GParamSpec     *pspec);
static void gtk_box_add                (GtkContainer   *container,
                                        GtkWidget      *widget);
static void gtk_box_remove             (GtkContainer   *container,
                                        GtkWidget      *widget);
static void gtk_box_forall             (GtkContainer   *container,
                                        gboolean        include_internals,
                                        GtkCallback     callback,
                                        gpointer        callback_data);
static void gtk_box_set_child_property (GtkContainer   *container,
                                        GtkWidget      *child,
                                        guint           property_id,
                                        const GValue   *value,
                                        GParamSpec     *pspec);
static void gtk_box_get_child_property (GtkContainer   *container,
                                        GtkWidget      *child,
                                        guint           property_id,
                                        GValue         *value,
                                        GParamSpec     *pspec);
static GType gtk_box_child_type        (GtkContainer   *container);
203 204 205
static GtkWidgetPath * gtk_box_get_path_for_child
                                       (GtkContainer   *container,
                                        GtkWidget      *child);
Elliot Lee's avatar
Elliot Lee committed
206

207

208 209 210 211 212 213 214 215 216 217 218 219 220 221
static void               gtk_box_get_preferred_width            (GtkWidget           *widget,
                                                                  gint                *minimum_size,
                                                                  gint                *natural_size);
static void               gtk_box_get_preferred_height           (GtkWidget           *widget,
                                                                  gint                *minimum_size,
                                                                  gint                *natural_size);
static void               gtk_box_get_preferred_width_for_height (GtkWidget           *widget,
                                                                  gint                 height,
                                                                  gint                *minimum_width,
                                                                  gint                *natural_width);
static void               gtk_box_get_preferred_height_for_width (GtkWidget           *widget,
                                                                  gint                 width,
                                                                  gint                *minimum_height,
                                                                  gint                *natural_height);
222 223 224 225 226 227
static void  gtk_box_get_preferred_height_and_baseline_for_width (GtkWidget           *widget,
								  gint                 width,
								  gint                *minimum_height,
								  gint                *natural_height,
								  gint                *minimum_baseline,
								  gint                *natural_baseline);
228

229
static void               gtk_box_buildable_init                 (GtkBuildableIface  *iface);
Matthias Clasen's avatar
Matthias Clasen committed
230

231
G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
232
                         G_ADD_PRIVATE (GtkBox)
233 234
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_box_buildable_init))
Elliot Lee's avatar
Elliot Lee committed
235

Benjamin Otte's avatar
Benjamin Otte committed
236 237 238 239 240 241 242 243 244 245 246
static void
gtk_box_dispose (GObject *object)
{
  GtkBox *box = GTK_BOX (object);
  GtkBoxPrivate *priv = box->priv;

  g_clear_object (&priv->gadget);

  G_OBJECT_CLASS (gtk_box_parent_class)->dispose (object);
}

Elliot Lee's avatar
Elliot Lee committed
247 248 249
static void
gtk_box_class_init (GtkBoxClass *class)
{
250 251
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
Tim Janik's avatar
Tim Janik committed
252
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
253

254 255
  object_class->set_property = gtk_box_set_property;
  object_class->get_property = gtk_box_get_property;
Benjamin Otte's avatar
Benjamin Otte committed
256
  object_class->dispose = gtk_box_dispose;
257

258
  widget_class->draw                           = gtk_box_draw;
259 260 261 262
  widget_class->size_allocate                  = gtk_box_size_allocate;
  widget_class->get_preferred_width            = gtk_box_get_preferred_width;
  widget_class->get_preferred_height           = gtk_box_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_box_get_preferred_height_for_width;
263
  widget_class->get_preferred_height_and_baseline_for_width = gtk_box_get_preferred_height_and_baseline_for_width;
264
  widget_class->get_preferred_width_for_height = gtk_box_get_preferred_width_for_height;
265
  widget_class->direction_changed              = gtk_box_direction_changed;
266

Tim Janik's avatar
Tim Janik committed
267 268 269 270 271 272
  container_class->add = gtk_box_add;
  container_class->remove = gtk_box_remove;
  container_class->forall = gtk_box_forall;
  container_class->child_type = gtk_box_child_type;
  container_class->set_child_property = gtk_box_set_child_property;
  container_class->get_child_property = gtk_box_get_child_property;
273
  container_class->get_path_for_child = gtk_box_get_path_for_child;
274
  gtk_container_class_handle_border_width (container_class);
Tim Janik's avatar
Tim Janik committed
275

276 277 278 279
  g_object_class_override_property (object_class,
                                    PROP_ORIENTATION,
                                    "orientation");

280 281 282 283 284
  props[PROP_SPACING] =
    g_param_spec_int ("spacing",
                      P_("Spacing"),
                      P_("The amount of space between children"),
                      0, G_MAXINT, 0,
285
                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
286 287 288 289 290 291

  props[PROP_HOMOGENEOUS] =
    g_param_spec_boolean ("homogeneous",
                          P_("Homogeneous"),
                          P_("Whether the children should all be the same size"),
                          FALSE,
292
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
293 294 295 296 297 298 299

  props[PROP_BASELINE_POSITION] =
    g_param_spec_enum ("baseline-position",
                       P_("Baseline position"),
                       P_("The position of the baseline aligned widgets if extra space is available"),
                       GTK_TYPE_BASELINE_POSITION,
                       GTK_BASELINE_POSITION_CENTER,
300
                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
301 302

  g_object_class_install_properties (object_class, LAST_PROP, props);
303

304 305 306 307 308 309 310 311
  /**
   * GtkBox:expand:
   *
   * Whether the child should receive extra space when the parent grows.
   *
   * Note that the default value for this property is %FALSE for GtkBox,
   * but #GtkHBox, #GtkVBox and other subclasses use the old default
   * of %TRUE.
312 313 314 315
   *
   * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
   * and #GtkWidget:vexpand properties are the preferred way to influence
   * child size allocation in containers.
316
   *
317
   * In contrast to #GtkWidget:hexpand, the expand child property does
318
   * not cause the box to expand itself.
319
   */
320 321 322 323 324 325
  child_props[CHILD_PROP_EXPAND] =
      g_param_spec_boolean ("expand",
                            P_("Expand"),
                            P_("Whether the child should receive extra space when the parent grows"),
                            FALSE,
                            GTK_PARAM_READWRITE);
326 327 328 329 330

  /**
   * GtkBox:fill:
   *
   * Whether the child should receive extra space when the parent grows.
331 332 333 334
   *
   * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
   * and #GtkWidget:vexpand properties are the preferred way to influence
   * child size allocation in containers.
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
  child_props[CHILD_PROP_FILL] =
      g_param_spec_boolean ("fill",
                            P_("Fill"),
                            P_("Whether extra space given to the child should be allocated to the child or used as padding"),
                            TRUE,
                            GTK_PARAM_READWRITE);

  child_props[CHILD_PROP_PADDING] =
      g_param_spec_uint ("padding",
                         P_("Padding"),
                         P_("Extra space to put between the child and its neighbors, in pixels"),
                         0, G_MAXINT,
                         0,
                         GTK_PARAM_READWRITE);

  child_props[CHILD_PROP_PACK_TYPE] =
      g_param_spec_enum ("pack-type",
                         P_("Pack type"),
                         P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
                         GTK_TYPE_PACK_TYPE, GTK_PACK_START,
                         GTK_PARAM_READWRITE);

  child_props[CHILD_PROP_POSITION] =
      g_param_spec_int ("position",
                        P_("Position"),
                        P_("The index of the child in the parent"),
                        -1, G_MAXINT,
                        0,
                        GTK_PARAM_READWRITE);

  gtk_container_class_install_child_properties (container_class, LAST_CHILD_PROP, child_props);
367

Matthias Clasen's avatar
Matthias Clasen committed
368
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILLER);
369
  gtk_widget_class_set_css_name (widget_class, "box");
Elliot Lee's avatar
Elliot Lee committed
370 371
}

372 373 374 375 376
static void
gtk_box_set_property (GObject      *object,
                      guint         prop_id,
                      const GValue *value,
                      GParamSpec   *pspec)
377
{
378
  GtkBox *box = GTK_BOX (object);
379
  GtkBoxPrivate *private = box->priv;
380

381
  switch (prop_id)
382
    {
383
    case PROP_ORIENTATION:
384 385 386 387 388 389 390 391 392 393
      {
        GtkOrientation orientation = g_value_get_enum (value);
        if (private->orientation != orientation)
          {
            private->orientation = orientation;
            _gtk_orientable_set_style_classes (GTK_ORIENTABLE (box));
            gtk_widget_queue_resize (GTK_WIDGET (box));
            g_object_notify (object, "orientation");
          }
      }
394
      break;
395 396
    case PROP_SPACING:
      gtk_box_set_spacing (box, g_value_get_int (value));
397
      break;
398 399 400
    case PROP_BASELINE_POSITION:
      gtk_box_set_baseline_position (box, g_value_get_enum (value));
      break;
401 402
    case PROP_HOMOGENEOUS:
      gtk_box_set_homogeneous (box, g_value_get_boolean (value));
403
      break;
Tim Janik's avatar
Tim Janik committed
404
    default:
405
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
406
      break;
407 408 409
    }
}

410 411 412 413 414
static void
gtk_box_get_property (GObject    *object,
                      guint       prop_id,
                      GValue     *value,
                      GParamSpec *pspec)
415
{
416
  GtkBox *box = GTK_BOX (object);
417
  GtkBoxPrivate *private = box->priv;
418

419
  switch (prop_id)
420
    {
421 422 423
    case PROP_ORIENTATION:
      g_value_set_enum (value, private->orientation);
      break;
424
    case PROP_SPACING:
425
      g_value_set_int (value, private->spacing);
426
      break;
427 428 429
    case PROP_BASELINE_POSITION:
      g_value_set_enum (value, private->baseline_pos);
      break;
430
    case PROP_HOMOGENEOUS:
431
      g_value_set_boolean (value, private->homogeneous);
432 433
      break;
    default:
434
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 436 437 438
      break;
    }
}

439
static gboolean
Benjamin Otte's avatar
Benjamin Otte committed
440 441 442 443 444 445 446
gtk_box_draw_contents (GtkCssGadget *gadget,
                       cairo_t      *cr,
                       int           x,
                       int           y,
                       int           width,
                       int           height,
                       gpointer      unused)
447
{
Benjamin Otte's avatar
Benjamin Otte committed
448
  GTK_WIDGET_CLASS (gtk_box_parent_class)->draw (gtk_css_gadget_get_owner (gadget), cr);
449

Benjamin Otte's avatar
Benjamin Otte committed
450 451
  return FALSE;
}
452

Benjamin Otte's avatar
Benjamin Otte committed
453 454 455 456 457
static gboolean
gtk_box_draw (GtkWidget *widget,
              cairo_t   *cr)
{
  gtk_css_gadget_draw (GTK_BOX (widget)->priv->gadget, cr);
458

Benjamin Otte's avatar
Benjamin Otte committed
459
  return FALSE;
460
}
461

Benjamin Otte's avatar
Benjamin Otte committed
462

463
static void
464 465 466
count_expand_children (GtkBox *box,
                       gint *visible_children,
                       gint *expand_children)
467
{
468
  GtkBoxPrivate  *private = box->priv;
469
  GList       *children;
470 471
  GtkBoxChild *child;

472
  *visible_children = *expand_children = 0;
473

474
  for (children = private->children; children; children = children->next)
475 476 477
    {
      child = children->data;

478
      if (_gtk_widget_get_visible (child->widget))
479
	{
480
	  *visible_children += 1;
481
	  if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
482
	    *expand_children += 1;
483 484
	}
    }
485
}
486 487

static void
Benjamin Otte's avatar
Benjamin Otte committed
488 489
gtk_box_size_allocate_no_center (GtkWidget           *widget,
                                 const GtkAllocation *allocation)
490 491
{
  GtkBox *box = GTK_BOX (widget);
492
  GtkBoxPrivate *private = box->priv;
493 494
  GtkBoxChild *child;
  GList *children;
495 496
  gint nvis_children;
  gint nexpand_children;
497

498 499 500
  GtkTextDirection direction;
  GtkAllocation child_allocation;
  GtkRequestedSize *sizes;
501 502 503 504 505
  gint child_minimum_baseline, child_natural_baseline;
  gint minimum_above, natural_above;
  gint minimum_below, natural_below;
  gboolean have_baseline;
  gint baseline;
506 507 508 509 510 511 512 513 514 515

  GtkPackType packing;

  gint size;
  gint extra;
  gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
  gint x = 0, y = 0, i;
  gint child_size;


516
  count_expand_children (box, &nvis_children, &nexpand_children);
517

518 519 520 521 522 523 524 525
  /* If there is no visible child, simply return. */
  if (nvis_children <= 0)
    return;

  direction = gtk_widget_get_direction (widget);
  sizes = g_newa (GtkRequestedSize, nvis_children);

  if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
526
    size = allocation->width - (nvis_children - 1) * private->spacing;
527
  else
528
    size = allocation->height - (nvis_children - 1) * private->spacing;
529

530 531 532 533
  have_baseline = FALSE;
  minimum_above = natural_above = 0;
  minimum_below = natural_below = 0;

534 535
  /* Retrieve desired size for visible children. */
  for (i = 0, children = private->children; children; children = children->next)
536
    {
537
      child = children->data;
538

539
      if (!_gtk_widget_get_visible (child->widget))
540
	continue;
541

542
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
543 544 545 546
	gtk_widget_get_preferred_width_for_height (child->widget,
                                                   allocation->height,
                                                   &sizes[i].minimum_size,
                                                   &sizes[i].natural_size);
547
      else
548 549 550 551 552
	gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
								allocation->width,
								&sizes[i].minimum_size,
								&sizes[i].natural_size,
								NULL, NULL);
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

      /* Assert the api is working properly */
      if (sizes[i].minimum_size < 0)
	g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
		 gtk_widget_get_name (GTK_WIDGET (child->widget)),
		 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
		 sizes[i].minimum_size,
		 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
		 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);

      if (sizes[i].natural_size < sizes[i].minimum_size)
	g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
		 gtk_widget_get_name (GTK_WIDGET (child->widget)),
		 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
		 sizes[i].natural_size,
		 sizes[i].minimum_size,
		 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
		 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);

      size -= sizes[i].minimum_size;
      size -= child->padding * 2;

      sizes[i].data = child;

      i++;
    }
579

580 581 582 583 584
  if (private->homogeneous)
    {
      /* If were homogenous we still need to run the above loop to get the
       * minimum sizes for children that are not going to fill
       */
585
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
586
	size = allocation->width - (nvis_children - 1) * private->spacing;
587
      else
588
	size = allocation->height - (nvis_children - 1) * private->spacing;
589

590 591 592 593 594 595
      extra = size / nvis_children;
      n_extra_widgets = size % nvis_children;
    }
  else
    {
      /* Bring children up to size first */
596
      size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
597

598 599 600 601 602 603 604
      /* Calculate space which hasn't distributed yet,
       * and is available for expanding children.
       */
      if (nexpand_children > 0)
	{
	  extra = size / nexpand_children;
	  n_extra_widgets = size % nexpand_children;
605
	}
606 607 608
      else
	extra = 0;
    }
609

610
  /* Allocate child sizes. */
611 612 613 614 615 616 617
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
    {
      for (i = 0, children = private->children;
	   children;
	   children = children->next)
	{
	  child = children->data;
618

619
	  /* If widget is not visible, skip it. */
620
	  if (!_gtk_widget_get_visible (child->widget))
621
	    continue;
622

623 624 625 626 627 628 629 630 631
	  /* If widget is packed differently skip it, but still increment i,
	   * since widget is visible and will be handled in next loop iteration.
	   */
	  if (child->pack != packing)
	    {
	      i++;
	      continue;
	    }

632 633
	  /* Assign the child's size. */
	  if (private->homogeneous)
634
	    {
635 636 637 638 639 640 641 642 643 644 645 646
	      child_size = extra;

	      if (n_extra_widgets > 0)
		{
		  child_size++;
		  n_extra_widgets--;
		}
	    }
	  else
	    {
	      child_size = sizes[i].minimum_size + child->padding * 2;

647
	      if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
648 649 650 651 652 653 654 655 656 657 658
		{
		  child_size += extra;

		  if (n_extra_widgets > 0)
		    {
		      child_size++;
		      n_extra_widgets--;
		    }
		}
	    }

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 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 735 736 737 738 739 740 741 742 743
	  sizes[i].natural_size = child_size;

	  if (private->orientation == GTK_ORIENTATION_HORIZONTAL &&
	      gtk_widget_get_valign_with_baseline (child->widget) == GTK_ALIGN_BASELINE)
	    {
	      int child_allocation_width;
	      int child_minimum_height, child_natural_height;

	      if (child->fill)
		child_allocation_width = MAX (1, child_size - child->padding * 2);
	      else
		child_allocation_width = sizes[i].minimum_size;

	      child_minimum_baseline = -1;
	      child_natural_baseline = -1;
	      gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
								      child_allocation_width,
								      &child_minimum_height, &child_natural_height,
								      &child_minimum_baseline, &child_natural_baseline);

	      if (child_minimum_baseline >= 0)
		{
		  have_baseline = TRUE;
		  minimum_below = MAX (minimum_below, child_minimum_height - child_minimum_baseline);
		  natural_below = MAX (natural_below, child_natural_height - child_natural_baseline);
		  minimum_above = MAX (minimum_above, child_minimum_baseline);
		  natural_above = MAX (natural_above, child_natural_baseline);
		}
	    }

	  i++;
	}
    }

  baseline = gtk_widget_get_allocated_baseline (widget);
  if (baseline == -1 && have_baseline)
    {
      gint height = MAX (1, allocation->height);

      /* TODO: This is purely based on the minimum baseline, when things fit we should
	 use the natural one? */

      switch (private->baseline_pos)
	{
	case GTK_BASELINE_POSITION_TOP:
	  baseline = minimum_above;
	  break;
	case GTK_BASELINE_POSITION_CENTER:
	  baseline = minimum_above + (height - (minimum_above + minimum_below)) / 2;
	  break;
	case GTK_BASELINE_POSITION_BOTTOM:
	  baseline = height - minimum_below;
	  break;
	}
    }

  /* Allocate child positions. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
    {
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
	{
	  child_allocation.y = allocation->y;
	  child_allocation.height = MAX (1, allocation->height);
	  if (packing == GTK_PACK_START)
	    x = allocation->x;
	  else
	    x = allocation->x + allocation->width;
	}
      else
	{
	  child_allocation.x = allocation->x;
	  child_allocation.width = MAX (1, allocation->width);
	  if (packing == GTK_PACK_START)
	    y = allocation->y;
	  else
	    y = allocation->y + allocation->height;
	}

      for (i = 0, children = private->children;
	   children;
	   children = children->next)
	{
	  child = children->data;

	  /* If widget is not visible, skip it. */
744
	  if (!_gtk_widget_get_visible (child->widget))
745 746 747 748 749 750 751 752 753 754 755 756 757
	    continue;

	  /* If widget is packed differently skip it, but still increment i,
	   * since widget is visible and will be handled in next loop iteration.
	   */
	  if (child->pack != packing)
	    {
	      i++;
	      continue;
	    }

	  child_size = sizes[i].natural_size;

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
	  /* Assign the child's position. */
	  if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
	    {
	      if (child->fill)
		{
		  child_allocation.width = MAX (1, child_size - child->padding * 2);
		  child_allocation.x = x + child->padding;
		}
	      else
		{
		  child_allocation.width = sizes[i].minimum_size;
		  child_allocation.x = x + (child_size - child_allocation.width) / 2;
		}

	      if (packing == GTK_PACK_START)
		{
		  x += child_size + private->spacing;
		}
	      else
		{
		  x -= child_size + private->spacing;

		  child_allocation.x -= child_size;
		}

	      if (direction == GTK_TEXT_DIR_RTL)
		child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;

786
	    }
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	  else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
	    {
	      if (child->fill)
		{
		  child_allocation.height = MAX (1, child_size - child->padding * 2);
		  child_allocation.y = y + child->padding;
		}
	      else
		{
		  child_allocation.height = sizes[i].minimum_size;
		  child_allocation.y = y + (child_size - child_allocation.height) / 2;
		}

	      if (packing == GTK_PACK_START)
		{
		  y += child_size + private->spacing;
		}
	      else
		{
		  y -= child_size + private->spacing;

		  child_allocation.y -= child_size;
		}
	    }
811
	  gtk_widget_size_allocate_with_baseline (child->widget, &child_allocation, baseline);
812 813

	  i++;
814 815
	}
    }
816

817
  _gtk_widget_set_simple_clip (widget, NULL);
818 819
}

820
static void
Benjamin Otte's avatar
Benjamin Otte committed
821 822
gtk_box_size_allocate_with_center (GtkWidget           *widget,
                                   const GtkAllocation *allocation)
823 824 825 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
{
  GtkBox *box = GTK_BOX (widget);
  GtkBoxPrivate *priv = box->priv;
  GtkBoxChild *child;
  GList *children;
  gint nvis[2];
  gint nexp[2];
  GtkTextDirection direction;
  GtkAllocation child_allocation;
  GtkRequestedSize *sizes[2];
  GtkRequestedSize center_req;
  gint child_minimum_baseline, child_natural_baseline;
  gint minimum_above, natural_above;
  gint minimum_below, natural_below;
  gboolean have_baseline;
  gint baseline;
  gint idx[2];
  gint center_pos;
  gint center_size;
  gint box_size;
  gint side[2];
  GtkPackType packing;
  gint min_size[2];
  gint nat_size[2];
  gint extra[2];
  gint n_extra_widgets[2];
  gint x = 0, y = 0, i;
  gint child_size;

  nvis[0] = nvis[1] = 0;
  nexp[0] = nexp[1] = 0;
  for (children = priv->children; children; children = children->next)
    {
      child = children->data;

      if (child != priv->center &&
859
          _gtk_widget_get_visible (child->widget))
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
        {
          nvis[child->pack] += 1;
          if (child->expand || gtk_widget_compute_expand (child->widget, priv->orientation))
            nexp[child->pack] += 1;
        }
    }

  direction = gtk_widget_get_direction (widget);
  sizes[0] = g_newa (GtkRequestedSize, nvis[0]);
  sizes[1] = g_newa (GtkRequestedSize, nvis[1]);

  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
    box_size = allocation->width;
  else
    box_size = allocation->height;

  have_baseline = FALSE;
  minimum_above = natural_above = 0;
  minimum_below = natural_below = 0;

  min_size[0] = nat_size[0] = nvis[0] * priv->spacing;
  min_size[1] = nat_size[1] = nvis[1] * priv->spacing;

  /* Retrieve desired size for visible children. */
  idx[0] = idx[1] = 0;
  for (children = priv->children; children; children = children->next)
    {
      GtkRequestedSize *req;

      child = children->data;

891
      if (!_gtk_widget_get_visible (child->widget))
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	continue;

      if (child == priv->center)
        req = &center_req;
      else
        req = &(sizes[child->pack][idx[child->pack]]);

      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        gtk_widget_get_preferred_width_for_height (child->widget,
                                                   allocation->height,
                                                   &req->minimum_size,
                                                   &req->natural_size);
      else
        gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
                                                                allocation->width,
                                                                &req->minimum_size,
                                                                &req->natural_size,
                                                                NULL, NULL);

      if (child != priv->center)
        {
          min_size[child->pack] += req->minimum_size + 2 * child->padding;
          nat_size[child->pack] += req->natural_size + 2 * child->padding;
915 916

          idx[child->pack] += 1;
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
        }

      req->data = child;
    }

  /* Determine size of center */
  if (priv->center->expand)
    center_size = MAX (box_size - 2 * MAX (nat_size[0], nat_size[1]), center_req.minimum_size);
  else
    center_size = MAX (MIN (center_req.natural_size, box_size - min_size[0] - min_size[1]), center_req.minimum_size);

  if (priv->homogeneous)
    {
      extra[0] = ((box_size - center_size) / 2 - nvis[0] * priv->spacing) / nvis[0];
      extra[1] = ((box_size - center_size) / 2 - nvis[1] * priv->spacing) / nvis[1];
      extra[0] = MIN (extra[0], extra[1]);
      n_extra_widgets[0] = 0;
    }
  else
    {
      for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++)
        {
          gint s;
          /* Distribute the remainder naturally on each side */
          s = MIN ((box_size - center_size) / 2 - min_size[packing], box_size - center_size - min_size[0] - min_size[1]);
          s = gtk_distribute_natural_allocation (MAX (0, s), nvis[packing], sizes[packing]);

          /* Calculate space which hasn't distributed yet,
           * and is available for expanding children.
           */
          if (nexp[packing] > 0)
            {
              extra[packing] = s / nexp[packing];
              n_extra_widgets[packing] = s % nexp[packing];
            }
           else
	     extra[packing] = 0;
        }
    }

  /* Allocate child sizes. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
    {
      for (i = 0, children = priv->children; children; children = children->next)
        {
          child = children->data;

          /* If widget is not visible, skip it. */
965
          if (!_gtk_widget_get_visible (child->widget))
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
            continue;

          /* Skip the center widget */
          if (child == priv->center)
            continue;

          /* If widget is packed differently, skip it. */
          if (child->pack != packing)
            continue;

          /* Assign the child's size. */
          if (priv->homogeneous)
            {
              child_size = extra[0];

              if (n_extra_widgets[0] > 0)
                {
                  child_size++;
                  n_extra_widgets[0]--;
                }
            }
          else
            {
              child_size = sizes[packing][i].minimum_size + child->padding * 2;

              if (child->expand || gtk_widget_compute_expand (child->widget, priv->orientation))
                {
                  child_size += extra[packing];

                  if (n_extra_widgets[packing] > 0)
                    {
                      child_size++;
                      n_extra_widgets[packing]--;
                    }
                }
            }

          sizes[packing][i].natural_size = child_size;

          if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
              gtk_widget_get_valign_with_baseline (child->widget) == GTK_ALIGN_BASELINE)
            {
              gint child_allocation_width;
	      gint child_minimum_height, child_natural_height;

              if (child->fill)
                child_allocation_width = MAX (1, child_size - child->padding * 2);
              else
                child_allocation_width = sizes[packing][i].minimum_size;

              child_minimum_baseline = -1;
              child_natural_baseline = -1;
              gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
                                                                      child_allocation_width,
                                                                      &child_minimum_height, &child_natural_height,
                                                                      &child_minimum_baseline, &child_natural_baseline);

              if (child_minimum_baseline >= 0)
                {
                  have_baseline = TRUE;
                  minimum_below = MAX (minimum_below, child_minimum_height - child_minimum_baseline);
                  natural_below = MAX (natural_below, child_natural_height - child_natural_baseline);
                  minimum_above = MAX (minimum_above, child_minimum_baseline);
                  natural_above = MAX (natural_above, child_natural_baseline);
                }
            }

          i++;
        }
    }

  baseline = gtk_widget_get_allocated_baseline (widget);
  if (baseline == -1 && have_baseline)
    {
      gint height = MAX (1, allocation->height);

     /* TODO: This is purely based on the minimum baseline, when things fit we should
      * use the natural one?
      */
      switch (priv->baseline_pos)
        {
        case GTK_BASELINE_POSITION_TOP:
          baseline = minimum_above;
          break;
        case GTK_BASELINE_POSITION_CENTER:
          baseline = minimum_above + (height - (minimum_above + minimum_below)) / 2;
          break;
        case GTK_BASELINE_POSITION_BOTTOM:
          baseline = height - minimum_below;
          break;
        }
    }

  /* Allocate child positions. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          child_allocation.y = allocation->y;
          child_allocation.height = MAX (1, allocation->height);
1066 1067
          if ((packing == GTK_PACK_START && direction == GTK_TEXT_DIR_LTR) ||
              (packing == GTK_PACK_END && direction == GTK_TEXT_DIR_RTL))
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
            x = allocation->x;
          else
            x = allocation->x + allocation->width;
        }
      else
        {
          child_allocation.x = allocation->x;
          child_allocation.width = MAX (1, allocation->width);
          if (packing == GTK_PACK_START)
            y = allocation->y;
          else
            y = allocation->y + allocation->height;
        }

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

          /* If widget is not visible, skip it. */
1087
          if (!_gtk_widget_get_visible (child->widget))
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
            continue;

          /* Skip the center widget */
          if (child == priv->center)
            continue;

          /* If widget is packed differently, skip it. */
          if (child->pack != packing)
            continue;

          child_size = sizes[packing][i].natural_size;

          /* Assign the child's position. */
          if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
            {
              if (child->fill)
                {
                  child_allocation.width = MAX (1, child_size - child->padding * 2);
                  child_allocation.x = x + child->padding;
                }
              else
                {
                  child_allocation.width = sizes[packing][i].minimum_size;
                  child_allocation.x = x + (child_size - child_allocation.width) / 2;
                }

1114 1115
              if ((packing == GTK_PACK_START && direction == GTK_TEXT_DIR_LTR) ||
                  (packing == GTK_PACK_END && direction == GTK_TEXT_DIR_RTL))
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
                {
                  x += child_size + priv->spacing;
                }
              else
                {
                  x -= child_size + priv->spacing;
                  child_allocation.x -= child_size;
                }
            }
          else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
            {
              if (child->fill)
                {
                  child_allocation.height = MAX (1, child_size - child->padding * 2);
                  child_allocation.y = y + child->padding;
                }
              else
                {
                  child_allocation.height = sizes[packing][i].minimum_size;
                  child_allocation.y = y + (child_size - child_allocation.height) / 2;
                }

              if (packing == GTK_PACK_START)
                {
                  y += child_size + priv->spacing;
                }
              else
                {
                  y -= child_size + priv->spacing;
                  child_allocation.y -= child_size;
                }
            }
          gtk_widget_size_allocate_with_baseline (child->widget, &child_allocation, baseline);

          i++;
        }

      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        side[packing] = x;
      else
        side[packing] = y;
    }

  /* Allocate the center widget */
1160 1161 1162 1163 1164
  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
    center_pos = allocation->x + (box_size - center_size) / 2;
  else
    center_pos = allocation->y + (box_size - center_size) / 2;

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
      direction == GTK_TEXT_DIR_RTL)
    packing = GTK_PACK_END;
  else
    packing = GTK_PACK_START;

  if (center_pos < side[packing])
    center_pos = side[packing];
  else if (center_pos + center_size > side[1 - packing])
    center_pos = side[1 - packing] - center_size;
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      child_allocation.x = center_pos;
      child_allocation.width = center_size;
    }
  else
    {
      child_allocation.y = center_pos;
      child_allocation.height = center_size;
    }
  gtk_widget_size_allocate_with_baseline (priv->center->widget, &child_allocation, baseline);
1187

1188
  _gtk_widget_set_simple_clip (widget, NULL);
1189 1190 1191
}

static void
Benjamin Otte's avatar
Benjamin Otte committed
1192 1193 1194 1195 1196
gtk_box_allocate_contents (GtkCssGadget        *gadget,
                           const GtkAllocation *allocation,
                           int                  baseline,
                           GtkAllocation       *out_clip,
                           gpointer             unused)
1197
{
Benjamin Otte's avatar
Benjamin Otte committed
1198
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
1199 1200 1201
  GtkBox *box = GTK_BOX (widget);

  if (box->priv->center &&
1202
      _gtk_widget_get_visible (box->priv->center->widget))
1203 1204 1205
    gtk_box_size_allocate_with_center (widget, allocation);
  else
    gtk_box_size_allocate_no_center (widget, allocation);
Benjamin Otte's avatar
Benjamin Otte committed
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

  gtk_container_get_children_clip (GTK_CONTAINER (box), out_clip);
}

static void
gtk_box_size_allocate (GtkWidget     *widget,
                       GtkAllocation *allocation)
{
  GtkBoxPrivate *priv = GTK_BOX (widget)->priv;
  GtkAllocation clip;

  gtk_widget_set_allocation (widget, allocation);

  gtk_css_gadget_allocate (priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);
  
  gtk_widget_set_clip (widget, &clip);
1225 1226
}

Manish Singh's avatar
Manish Singh committed
1227 1228
static GType
gtk_box_child_type (GtkContainer   *container)
1229 1230 1231 1232 1233
{
  return GTK_TYPE_WIDGET;
}

static void
1234 1235 1236 1237 1238
gtk_box_set_child_property (GtkContainer *container,
                            GtkWidget    *child,
                            guint         property_id,
                            const GValue *value,
                            GParamSpec   *pspec)
1239 1240 1241 1242 1243 1244
{
  gboolean expand = 0;
  gboolean fill = 0;
  guint padding = 0;
  GtkPackType pack_type = 0;

Tim Janik's avatar
Tim Janik committed
1245
  if (property_id != CHILD_PROP_POSITION)
1246 1247 1248 1249 1250 1251
    gtk_box_query_child_packing (GTK_BOX (container),
				 child,
				 &expand,
				 &fill,
				 &padding,
				 &pack_type);
Tim Janik's avatar
Tim Janik committed
1252
  switch (property_id)
1253
    {
Tim Janik's avatar
Tim Janik committed
1254
    case CHILD_PROP_EXPAND:
1255 1256
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
Tim Janik's avatar
Tim Janik committed
1257
				 g_value_get_boolean (value),
1258 1259 1260 1261
				 fill,
				 padding,
				 pack_type);
      break;
Tim Janik's avatar
Tim Janik committed
1262
    case CHILD_PROP_FILL:
1263 1264 1265
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
				 expand,
Tim Janik's avatar
Tim Janik committed
1266
				 g_value_get_boolean (value),
1267 1268 1269
				 padding,
				 pack_type);
      break;
Tim Janik's avatar
Tim Janik committed
1270
    case CHILD_PROP_PADDING:
1271 1272 1273 1274
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
				 expand,
				 fill,
Tim Janik's avatar
Tim Janik committed
1275
				 g_value_get_uint (value),
1276 1277
				 pack_type);
      break;
Tim Janik's avatar
Tim Janik committed
1278
    case CHILD_PROP_PACK_TYPE:
1279 1280 1281 1282 1283
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
				 expand,
				 fill,
				 padding,
Tim Janik's avatar
Tim Janik committed
1284
				 g_value_get_enum (value));
1285
      break;
Tim Janik's avatar
Tim Janik committed
1286
    case CHILD_PROP_POSITION:
1287 1288
      gtk_box_reorder_child (GTK_BOX (container),
			     child,
Tim Janik's avatar
Tim Janik committed
1289
			     g_value_get_int (value));
1290 1291
      break;
    default:
Tim Janik's avatar
Tim Janik committed
1292
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1293 1294 1295 1296 1297
      break;
    }
}

static void
Tim Janik's avatar
Tim Janik committed
1298 1299 1300 1301 1302
gtk_box_get_child_property (GtkContainer *container,
			    GtkWidget    *child,
			    guint         property_id,
			    GValue       *value,
			    GParamSpec   *pspec)
1303
{
1304 1305
  gboolean expand = FALSE;
  gboolean fill = FALSE;
1306 1307 1308 1309
  guint padding = 0;
  GtkPackType pack_type = 0;
  GList *list;

Tim Janik's avatar
Tim Janik committed
1310
  if (property_id != CHILD_PROP_POSITION)
1311 1312 1313 1314 1315 1316
    gtk_box_query_child_packing (GTK_BOX (container),
				 child,
				 &expand,
				 &fill,
				 &padding,
				 &pack_type);
Tim Janik's avatar
Tim Janik committed
1317
  switch (property_id)
1318
    {
Tim Janik's avatar
Tim Janik committed
1319 1320 1321
      guint i;
    case CHILD_PROP_EXPAND:
      g_value_set_boolean (value, expand);
1322
      break;
Tim Janik's avatar
Tim Janik committed
1323 1324
    case CHILD_PROP_FILL:
      g_value_set_boolean (value, fill);
1325
      break;
Tim Janik's avatar
Tim Janik committed
1326 1327
    case CHILD_PROP_PADDING:
      g_value_set_uint (value, padding);
1328
      break;
Tim Janik's avatar
Tim Janik committed
1329 1330
    case CHILD_PROP_PACK_TYPE:
      g_value_set_enum (value, pack_type);
1331
      break;
Tim Janik's avatar
Tim Janik committed
1332 1333
    case CHILD_PROP_POSITION:
      i = 0;
1334
      for (list = GTK_BOX (container)->priv->children; list; list = list->next)
1335 1336 1337 1338 1339 1340
	{
	  GtkBoxChild *child_entry;

	  child_entry = list->data;
	  if (child_entry->widget == child)
	    break;
Tim Janik's avatar
Tim Janik committed
1341
	  i++;
1342
	}
Tim Janik's avatar
Tim Janik committed
1343
      g_value_set_int (value, list ? i : -1);
1344 1345
      break;
    default:
Tim Janik's avatar
Tim Janik committed
1346
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1347 1348 1349 1350
      break;
    }
}

1351 1352 1353 1354
typedef struct _CountingData CountingData;
struct _CountingData {
  GtkWidget *widget;
  gboolean found;
1355 1356
  guint before;
  guint after;
1357 1358 1359 1360 1361 1362 1363 1364
};

static void
count_widget_position (GtkWidget *widget,
                       gpointer   data)
{
  CountingData *count = data;

1365
  if (!_gtk_widget_get_visible (widget))
1366 1367 1368
    return;

  if (count->widget == widget)
1369
    count->found = TRUE;
1370 1371 1372
  else if (count->found)
    count->after++;
  else
1373
    count->before++;
1374 1375
}

1376 1377
static gint
gtk_box_get_visible_position (GtkBox    *box,
1378 1379
                              GtkWidget *child)
{
1380
  CountingData count = { child, FALSE, 0, 0 };
1381

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
  /* foreach iterates in visible order */
  gtk_container_foreach (GTK_CONTAINER (box),
                         count_widget_position,
                         &count);

  /* the child wasn't found, it's likely an internal child of some
   * subclass, return -1 to indicate that there is no sibling relation
   * to the regular box children
   */
  if (!count.found)
    return -1;
1393

1394 1395 1396 1397 1398
  if (box->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
      gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
    return count.after;
  else
    return count.before;
1399 1400 1401 1402 1403 1404
}

static GtkWidgetPath *
gtk_box_get_path_for_child (GtkContainer *container,
                            GtkWidget    *child)
{
1405
  GtkWidgetPath *path, *sibling_path;
1406
  GtkBox *box;
1407
  GtkBoxPrivate *private;
1408
  GList *list, *children;
1409 1410

  box = GTK_BOX (container);
1411
  private = box->priv;
1412

1413
  path = _gtk_widget_create_path (GTK_WIDGET (container));
1414

1415
  if (_gtk_widget_get_visible (child))
1416
    {
1417 1418
      gint position;

1419
      sibling_path = gtk_widget_path_new ();
1420 1421 1422

      /* get_children works in visible order */
      children = gtk_container_get_children (container);
1423 1424 1425
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL &&
          gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
        children = g_list_reverse (children);
1426 1427 1428

      for (list = children; list; list = list->next)
        {
1429
          if (!_gtk_widget_get_visible (list->data))
1430 1431
            continue;

1432
          gtk_widget_path_append_for_widget (sibling_path, list->data);
1433
        }
1434

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
      g_list_free (children);

      position = gtk_box_get_visible_position (box, child);

      if (position >= 0)
        gtk_widget_path_append_with_siblings (path, sibling_path, position);
      else
        gtk_widget_path_append_for_widget (path, child);

      gtk_widget_path_unref (sibling_path);
1445
    }
1446 1447 1448 1449 1450 1451
  else
    gtk_widget_path_append_for_widget (path, child);

  return path;
}

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
static void
gtk_box_buildable_add_child (GtkBuildable *buildable,
                             GtkBuilder   *builder,
                             GObject      *child,
                             const gchar  *type)
{
  if (type && strcmp (type, "center") == 0)
    gtk_box_set_center_widget (GTK_BOX (buildable), GTK_WIDGET (child));
  else if (!type)
    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
  else
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_BOX (buildable), type);
}

static void
gtk_box_buildable_init (GtkBuildableIface *iface)
{
  iface->add_child = gtk_box_buildable_add_child;
}

Benjamin Otte's avatar
Benjamin Otte committed
1472
static void
1473 1474
gtk_box_update_child_css_position (GtkBox      *box,
                                   GtkBoxChild *child_info)
Benjamin Otte's avatar
Benjamin Otte committed
1475
{
1476 1477 1478 1479
  GtkBoxPrivate *priv = box->priv;
  GtkBoxChild *prev;
  gboolean reverse;
  GList *l;
Benjamin Otte's avatar
Benjamin Otte committed
1480

1481 1482 1483 1484
  prev = NULL;
  for (l = priv->children; l->data != child_info; l = l->next)
    {
      GtkBoxChild *cur = l->data;
Benjamin Otte's avatar
Benjamin Otte committed
1485

1486 1487 1488
      if (cur->pack == child_info->pack)
        prev = cur;
    }
1489

1490 1491 1492 1493
  reverse = child_info->pack == GTK_PACK_END;
  if (box->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
      gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
    reverse = !reverse;
Benjamin Otte's avatar
Benjamin Otte committed
1494

1495 1496 1497 1498 1499 1500 1501 1502
  if (reverse)
    gtk_css_node_insert_before (gtk_widget_get_css_node (GTK_WIDGET (box)),
                                gtk_widget_get_css_node (child_info->widget),
                                prev ? gtk_widget_get_css_node (prev->widget) : NULL);
  else
    gtk_css_node_insert_after (gtk_widget_get_css_node (GTK_WIDGET (box)),
                               gtk_widget_get_css_node (child_info->widget),
                               prev ? gtk_widget_get_css_node (prev->widget) : NULL);
1503 1504
}

1505 1506 1507 1508
static void
gtk_box_direction_changed (GtkWidget        *widget,
                           GtkTextDirection  previous_direction)
{
1509 1510 1511 1512
  GtkBox *box = GTK_BOX (widget);

  if (box->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
    gtk_css_node_reverse_children (gtk_widget_get_css_node (widget));
1513 1514
}

1515
static GtkBoxChild *
1516 1517 1518 1519 1520 1521 1522
gtk_box_pack (GtkBox      *box,
              GtkWidget   *child,
              gboolean     expand,
              gboolean     fill,
              guint        padding,
              GtkPackType  pack_type)
{
1523
  GtkContainer *container = GTK_CONTAINER (box);
1524
  GtkBoxPrivate *private = box->priv;
1525 1526
  GtkBoxChild *child_info;

1527 1528
  g_return_val_if_fail (GTK_IS_BOX (box), NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1529
  g_return_val_if_fail (_gtk_widget_get_parent (child) == NULL, NULL);
1530 1531 1532 1533 1534 1535 1536 1537

  child_info = g_new (GtkBoxChild, 1);
  child_info->widget = child;
  child_info->padding = padding;
  child_info->expand = expand ? TRUE : FALSE;
  child_info->fill = fill ? TRUE : FALSE;
  child_info->pack = pack_type;

1538
  private->children = g_list_append (private->children, child_info);
1539
  gtk_box_update_child_css_position (box, child_info);
1540 1541 1542 1543

  gtk_widget_freeze_child_notify (child);

  gtk_widget_set_parent (child, GTK_WIDGET (box));
1544

1545 1546 1547 1548 1549 1550 1551 1552
  if (expand)
    gtk_container_child_notify_by_pspec (container, child, child_props[CHILD_PROP_EXPAND]);
  if (!fill)
    gtk_container_child_notify_by_pspec (container, child, child_props[CHILD_PROP_FILL]);
  if (padding != 0)
    gtk_container_child_notify_by_pspec (container, child, child_props[CHILD_PROP_PADDING]);
  if (pack_type != GTK_PACK_START)
    gtk_container_child_notify_by_pspec (container, child, child_props[CHILD_PROP_PACK_TYPE]);
1553 1554
  gtk_container_child_notify_by_pspec (container, child, child_props[CHILD_PROP_POSITION]);

1555
  gtk_widget_thaw_child_notify (child);
1556 1557

  return child_info;
1558 1559
}

1560
static void
1561 1562 1563
gtk_box_get_size (GtkWidget      *widget,
		  GtkOrientation  orientation,
		  gint           *minimum_size,
1564 1565 1566
		  gint           *natural_size,
		  gint           *minimum_baseline,
		  gint           *natural_baseline)
1567 1568
{
  GtkBox *box;
1569
  GtkBoxPrivate *private;
1570 1571
  GList *children;
  gint nvis_children;
1572
  gint minimum, natural;
1573 1574 1575 1576
  gint minimum_above, natural_above;
  gint minimum_below, natural_below;
  gboolean have_baseline;
  gint min_baseline, nat_baseline;
1577
  gint center_min, center_nat;
1578

1579
  box = GTK_BOX (widget);
1580
  private = box->priv;