gtkbox.c 78.3 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
 */

76
#include "config.h"
77

Elliot Lee's avatar
Elliot Lee committed
78
#include "gtkbox.h"
79
#include "gtkboxprivate.h"
80
#include "gtkintl.h"
81
#include "gtkorientable.h"
82
#include "gtkorientableprivate.h"
83
#include "gtkprivate.h"
84 85 86
#include "gtktypebuiltins.h"
#include "gtksizerequest.h"
#include "gtkwidgetpath.h"
87
#include "gtkwidgetprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
88
#include "a11y/gtkcontaineraccessible.h"
89

Elliot Lee's avatar
Elliot Lee committed
90

91
enum {
92 93
  PROP_0,
  PROP_SPACING,
94
  PROP_HOMOGENEOUS,
95 96 97
  PROP_BASELINE_POSITION,

  /* orientable */
98 99
  PROP_ORIENTATION,
  LAST_PROP = PROP_ORIENTATION
100
};
Elliot Lee's avatar
Elliot Lee committed
101

102
enum {
Tim Janik's avatar
Tim Janik committed
103 104 105 106 107 108
  CHILD_PROP_0,
  CHILD_PROP_EXPAND,
  CHILD_PROP_FILL,
  CHILD_PROP_PADDING,
  CHILD_PROP_PACK_TYPE,
  CHILD_PROP_POSITION
109 110
};

111 112
typedef struct _GtkBoxChild        GtkBoxChild;

113
struct _GtkBoxPrivate
114
{
115
  GList          *children;
116
  GtkBoxChild    *center;
117

118
  GtkOrientation  orientation;
119 120 121 122 123
  gint16          spacing;

  guint           default_expand : 1;
  guint           homogeneous    : 1;
  guint           spacing_set    : 1;
124
  guint           baseline_pos   : 2;
125 126
};

127 128
static GParamSpec *props[LAST_PROP] = { NULL, };

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*
 * 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;
};
153 154 155

static void gtk_box_size_allocate         (GtkWidget              *widget,
                                           GtkAllocation          *allocation);
156 157
static gboolean gtk_box_draw           (GtkWidget        *widget,
                                        cairo_t          *cr);
158

159 160
static void gtk_box_direction_changed  (GtkWidget        *widget,
                                        GtkTextDirection  previous_direction);
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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);
189 190 191
static GtkWidgetPath * gtk_box_get_path_for_child
                                       (GtkContainer   *container,
                                        GtkWidget      *child);
Elliot Lee's avatar
Elliot Lee committed
192

193

194 195 196 197 198 199 200 201 202 203 204 205 206 207
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);
208 209 210 211 212 213
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);
214

215
static void               gtk_box_buildable_init                 (GtkBuildableIface  *iface);
Matthias Clasen's avatar
Matthias Clasen committed
216

217
G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
218
                         G_ADD_PRIVATE (GtkBox)
219 220
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_box_buildable_init))
Elliot Lee's avatar
Elliot Lee committed
221 222 223 224

static void
gtk_box_class_init (GtkBoxClass *class)
{
225 226
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
Tim Janik's avatar
Tim Janik committed
227
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
228

229 230 231
  object_class->set_property = gtk_box_set_property;
  object_class->get_property = gtk_box_get_property;

232
  widget_class->draw                           = gtk_box_draw;
233 234 235 236
  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;
237
  widget_class->get_preferred_height_and_baseline_for_width = gtk_box_get_preferred_height_and_baseline_for_width;
238
  widget_class->get_preferred_width_for_height = gtk_box_get_preferred_width_for_height;
239
  widget_class->direction_changed              = gtk_box_direction_changed;
240

Tim Janik's avatar
Tim Janik committed
241 242 243 244 245 246
  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;
247
  container_class->get_path_for_child = gtk_box_get_path_for_child;
248
  gtk_container_class_handle_border_width (container_class);
Tim Janik's avatar
Tim Janik committed
249

250 251 252 253
  g_object_class_override_property (object_class,
                                    PROP_ORIENTATION,
                                    "orientation");

254 255 256 257 258
  props[PROP_SPACING] =
    g_param_spec_int ("spacing",
                      P_("Spacing"),
                      P_("The amount of space between children"),
                      0, G_MAXINT, 0,
259
                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
260 261 262 263 264 265

  props[PROP_HOMOGENEOUS] =
    g_param_spec_boolean ("homogeneous",
                          P_("Homogeneous"),
                          P_("Whether the children should all be the same size"),
                          FALSE,
266
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
267 268 269 270 271 272 273

  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,
274
                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
275 276

  g_object_class_install_properties (object_class, LAST_PROP, props);
277

278 279 280 281 282 283 284 285
  /**
   * 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.
286 287 288 289
   *
   * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
   * and #GtkWidget:vexpand properties are the preferred way to influence
   * child size allocation in containers.
290
   *
291
   * In contrast to #GtkWidget:hexpand, the expand child property does
292
   * not cause the box to expand itself.
293
   */
Tim Janik's avatar
Tim Janik committed
294 295
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_EXPAND,
296 297
					      g_param_spec_boolean ("expand",
								    P_("Expand"),
298
								    P_("Whether the child should receive extra space when the parent grows"),
299
								    FALSE,
300
								    GTK_PARAM_READWRITE));
301 302 303 304 305

  /**
   * GtkBox:fill:
   *
   * Whether the child should receive extra space when the parent grows.
306 307 308 309
   *
   * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
   * and #GtkWidget:vexpand properties are the preferred way to influence
   * child size allocation in containers.
310
   */
Tim Janik's avatar
Tim Janik committed
311 312
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_FILL,
313 314
					      g_param_spec_boolean ("fill",
								    P_("Fill"),
315
								    P_("Whether extra space given to the child should be allocated to the child or used as padding"),
316
								    TRUE,
317
								    GTK_PARAM_READWRITE));
318

Tim Janik's avatar
Tim Janik committed
319 320
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_PADDING,
321 322
					      g_param_spec_uint ("padding",
								 P_("Padding"),
323
								 P_("Extra space to put between the child and its neighbors, in pixels"),
Tim Janik's avatar
Tim Janik committed
324
								 0, G_MAXINT, 0,
325
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
326 327
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_PACK_TYPE,
328
					      g_param_spec_enum ("pack-type",
329
								 P_("Pack type"),
330
								 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
Tim Janik's avatar
Tim Janik committed
331
								 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
332
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
333 334
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_POSITION,
335 336
					      g_param_spec_int ("position",
								P_("Position"),
337
								P_("The index of the child in the parent"),
Tim Janik's avatar
Tim Janik committed
338
								-1, G_MAXINT, 0,
339
								GTK_PARAM_READWRITE));
340

Matthias Clasen's avatar
Matthias Clasen committed
341
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILLER);
Elliot Lee's avatar
Elliot Lee committed
342 343 344 345 346
}

static void
gtk_box_init (GtkBox *box)
{
347
  GtkBoxPrivate *private;
348

349
  box->priv = gtk_box_get_instance_private (box);
350
  private = box->priv;
351

352
  gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
353
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
354 355

  private->orientation = GTK_ORIENTATION_HORIZONTAL;
356 357
  private->children = NULL;

358
  private->default_expand = FALSE;
359 360
  private->homogeneous = FALSE;
  private->spacing = 0;
361
  private->spacing_set = FALSE;
362
  private->baseline_pos = GTK_BASELINE_POSITION_CENTER;
363 364

  _gtk_orientable_set_style_classes (GTK_ORIENTABLE (box));
Elliot Lee's avatar
Elliot Lee committed
365 366
}

367 368 369 370 371
static void
gtk_box_set_property (GObject      *object,
                      guint         prop_id,
                      const GValue *value,
                      GParamSpec   *pspec)
372
{
373
  GtkBox *box = GTK_BOX (object);
374
  GtkBoxPrivate *private = box->priv;
375

376
  switch (prop_id)
377
    {
378
    case PROP_ORIENTATION:
379 380 381 382 383 384 385 386 387 388
      {
        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");
          }
      }
389
      break;
390 391
    case PROP_SPACING:
      gtk_box_set_spacing (box, g_value_get_int (value));
392
      break;
393 394 395
    case PROP_BASELINE_POSITION:
      gtk_box_set_baseline_position (box, g_value_get_enum (value));
      break;
396 397
    case PROP_HOMOGENEOUS:
      gtk_box_set_homogeneous (box, g_value_get_boolean (value));
398
      break;
Tim Janik's avatar
Tim Janik committed
399
    default:
400
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
401
      break;
402 403 404
    }
}

405 406 407 408 409
static void
gtk_box_get_property (GObject    *object,
                      guint       prop_id,
                      GValue     *value,
                      GParamSpec *pspec)
410
{
411
  GtkBox *box = GTK_BOX (object);
412
  GtkBoxPrivate *private = box->priv;
413

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

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static gboolean
gtk_box_draw (GtkWidget *widget,
              cairo_t   *cr)
{
  GtkStyleContext *context;
  GtkAllocation alloc;

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

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

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

450
static void
451 452 453
count_expand_children (GtkBox *box,
                       gint *visible_children,
                       gint *expand_children)
454
{
455
  GtkBoxPrivate  *private = box->priv;
456
  GList       *children;
457 458
  GtkBoxChild *child;

459
  *visible_children = *expand_children = 0;
460

461
  for (children = private->children; children; children = children->next)
462 463 464
    {
      child = children->data;

465
      if (gtk_widget_get_visible (child->widget))
466
	{
467
	  *visible_children += 1;
468
	  if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
469
	    *expand_children += 1;
470 471
	}
    }
472
}
473 474

static void
475 476
gtk_box_size_allocate_no_center (GtkWidget     *widget,
                                 GtkAllocation *allocation)
477 478
{
  GtkBox *box = GTK_BOX (widget);
479
  GtkBoxPrivate *private = box->priv;
480 481
  GtkBoxChild *child;
  GList *children;
482 483
  gint nvis_children;
  gint nexpand_children;
484

485 486 487
  GtkTextDirection direction;
  GtkAllocation child_allocation;
  GtkRequestedSize *sizes;
488 489 490 491 492
  gint child_minimum_baseline, child_natural_baseline;
  gint minimum_above, natural_above;
  gint minimum_below, natural_below;
  gboolean have_baseline;
  gint baseline;
493 494 495 496 497 498 499 500 501 502

  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;


503
  gtk_widget_set_allocation (widget, allocation);
504

505
  count_expand_children (box, &nvis_children, &nexpand_children);
506

507 508 509 510 511 512 513 514
  /* 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)
515
    size = allocation->width - (nvis_children - 1) * private->spacing;
516
  else
517
    size = allocation->height - (nvis_children - 1) * private->spacing;
518

519 520 521 522
  have_baseline = FALSE;
  minimum_above = natural_above = 0;
  minimum_below = natural_below = 0;

523 524
  /* Retrieve desired size for visible children. */
  for (i = 0, children = private->children; children; children = children->next)
525
    {
526
      child = children->data;
527

528 529
      if (!gtk_widget_get_visible (child->widget))
	continue;
530

531
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
532 533 534 535
	gtk_widget_get_preferred_width_for_height (child->widget,
                                                   allocation->height,
                                                   &sizes[i].minimum_size,
                                                   &sizes[i].natural_size);
536
      else
537 538 539 540 541
	gtk_widget_get_preferred_height_and_baseline_for_width (child->widget,
								allocation->width,
								&sizes[i].minimum_size,
								&sizes[i].natural_size,
								NULL, NULL);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

      /* 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++;
    }
568

569 570 571 572 573
  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
       */
574
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
575
	size = allocation->width - (nvis_children - 1) * private->spacing;
576
      else
577
	size = allocation->height - (nvis_children - 1) * private->spacing;
578

579 580 581 582 583 584
      extra = size / nvis_children;
      n_extra_widgets = size % nvis_children;
    }
  else
    {
      /* Bring children up to size first */
585
      size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
586

587 588 589 590 591 592 593
      /* 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;
594
	}
595 596 597
      else
	extra = 0;
    }
598

599
  /* Allocate child sizes. */
600 601 602 603 604 605 606
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
    {
      for (i = 0, children = private->children;
	   children;
	   children = children->next)
	{
	  child = children->data;
607

608 609
	  /* If widget is not visible, skip it. */
	  if (!gtk_widget_get_visible (child->widget))
610
	    continue;
611

612 613 614 615 616 617 618 619 620
	  /* 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;
	    }

621 622
	  /* Assign the child's size. */
	  if (private->homogeneous)
623
	    {
624 625 626 627 628 629 630 631 632 633 634 635
	      child_size = extra;

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

636
	      if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
637 638 639 640 641 642 643 644 645 646 647
		{
		  child_size += extra;

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

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 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 744 745 746
	  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. */
	  if (!gtk_widget_get_visible (child->widget))
	    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;

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

775
	    }
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	  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;
		}
	    }
800
	  gtk_widget_size_allocate_with_baseline (child->widget, &child_allocation, baseline);
801 802

	  i++;
803 804
	}
    }
805

806
  _gtk_widget_set_simple_clip (widget, NULL);
807 808
}

809 810 811 812 813 814 815 816 817 818 819 820 821 822 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 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
static void
gtk_box_size_allocate_with_center (GtkWidget     *widget,
                                   GtkAllocation *allocation)
{
  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;

  gtk_widget_set_allocation (widget, allocation);

  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 &&
          gtk_widget_get_visible (child->widget))
        {
          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;

      if (!gtk_widget_get_visible (child->widget))
	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;
906 907

          idx[child->pack] += 1;
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 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 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 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 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        }

      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. */
          if (!gtk_widget_get_visible (child->widget))
            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);
          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 = priv->children; children; children = children->next)
        {
          child = children->data;

          /* If widget is not visible, skip it. */
          if (!gtk_widget_get_visible (child->widget))
            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;
                }

              if (packing == GTK_PACK_START)
                {
                  x += child_size + priv->spacing;
                }
              else
                {
                  x -= child_size + priv->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;

            }
          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 */
1153 1154 1155 1156 1157
  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;

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
  if (center_pos < side[GTK_PACK_START])
    center_pos = side[GTK_PACK_START];
  else if (center_pos + center_size > side[GTK_PACK_END])
    center_pos = side[GTK_PACK_END] - center_size;

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

1175
  _gtk_widget_set_simple_clip (widget, NULL);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
}

static void
gtk_box_size_allocate (GtkWidget     *widget,
                       GtkAllocation *allocation)
{
  GtkBox *box = GTK_BOX (widget);

  if (box->priv->center &&
      gtk_widget_get_visible (box->priv->center->widget))
    gtk_box_size_allocate_with_center (widget, allocation);
  else
    gtk_box_size_allocate_no_center (widget, allocation);
}

Manish Singh's avatar
Manish Singh committed
1191 1192
static GType
gtk_box_child_type (GtkContainer   *container)
1193 1194 1195 1196 1197
{
  return GTK_TYPE_WIDGET;
}

static void
1198 1199 1200 1201 1202
gtk_box_set_child_property (GtkContainer *container,
                            GtkWidget    *child,
                            guint         property_id,
                            const GValue *value,
                            GParamSpec   *pspec)
1203 1204 1205 1206 1207 1208
{
  gboolean expand = 0;
  gboolean fill = 0;
  guint padding = 0;
  GtkPackType pack_type = 0;

Tim Janik's avatar
Tim Janik committed
1209
  if (property_id != CHILD_PROP_POSITION)
1210 1211 1212 1213 1214 1215
    gtk_box_query_child_packing (GTK_BOX (container),
				 child,
				 &expand,
				 &fill,
				 &padding,
				 &pack_type);
Tim Janik's avatar
Tim Janik committed
1216
  switch (property_id)
1217
    {
Tim Janik's avatar
Tim Janik committed
1218
    case CHILD_PROP_EXPAND:
1219 1220
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
Tim Janik's avatar
Tim Janik committed
1221
				 g_value_get_boolean (value),
1222 1223 1224 1225
				 fill,
				 padding,
				 pack_type);
      break;
Tim Janik's avatar
Tim Janik committed
1226
    case CHILD_PROP_FILL:
1227 1228 1229
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
				 expand,
Tim Janik's avatar
Tim Janik committed
1230
				 g_value_get_boolean (value),
1231 1232 1233
				 padding,
				 pack_type);
      break;
Tim Janik's avatar
Tim Janik committed
1234
    case CHILD_PROP_PADDING:
1235 1236 1237 1238
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
				 expand,
				 fill,
Tim Janik's avatar
Tim Janik committed
1239
				 g_value_get_uint (value),
1240 1241
				 pack_type);
      break;
Tim Janik's avatar
Tim Janik committed
1242
    case CHILD_PROP_PACK_TYPE:
1243 1244 1245 1246 1247
      gtk_box_set_child_packing (GTK_BOX (container),
				 child,
				 expand,
				 fill,
				 padding,
Tim Janik's avatar
Tim Janik committed
1248
				 g_value_get_enum (value));
1249
      break;
Tim Janik's avatar
Tim Janik committed
1250
    case CHILD_PROP_POSITION:
1251 1252
      gtk_box_reorder_child (GTK_BOX (container),
			     child,
Tim Janik's avatar
Tim Janik committed
1253
			     g_value_get_int (value));
1254 1255
      break;
    default:
Tim Janik's avatar
Tim Janik committed
1256
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1257 1258 1259 1260 1261
      break;
    }
}

static void
Tim Janik's avatar
Tim Janik committed
1262 1263 1264 1265 1266
gtk_box_get_child_property (GtkContainer *container,
			    GtkWidget    *child,
			    guint         property_id,
			    GValue       *value,
			    GParamSpec   *pspec)
1267
{
1268 1269
  gboolean expand = FALSE;
  gboolean fill = FALSE;
1270 1271 1272 1273
  guint padding = 0;
  GtkPackType pack_type = 0;
  GList *list;

Tim Janik's avatar
Tim Janik committed
1274
  if (property_id != CHILD_PROP_POSITION)
1275 1276 1277 1278 1279 1280
    gtk_box_query_child_packing (GTK_BOX (container),
				 child,
				 &expand,
				 &fill,
				 &padding,
				 &pack_type);
Tim Janik's avatar
Tim Janik committed
1281
  switch (property_id)
1282
    {
Tim Janik's avatar
Tim Janik committed
1283 1284 1285
      guint i;
    case CHILD_PROP_EXPAND:
      g_value_set_boolean (value, expand);
1286
      break;
Tim Janik's avatar
Tim Janik committed
1287 1288
    case CHILD_PROP_FILL:
      g_value_set_boolean (value, fill);
1289
      break;
Tim Janik's avatar
Tim Janik committed
1290 1291
    case CHILD_PROP_PADDING:
      g_value_set_uint (value, padding);
1292
      break;
Tim Janik's avatar
Tim Janik committed
1293 1294
    case CHILD_PROP_PACK_TYPE:
      g_value_set_enum (value, pack_type);
1295
      break;
Tim Janik's avatar
Tim Janik committed
1296 1297
    case CHILD_PROP_POSITION:
      i = 0;
1298
      for (list = GTK_BOX (container)->priv->children; list; list = list->next)
1299 1300 1301 1302 1303 1304
	{
	  GtkBoxChild *child_entry;

	  child_entry = list->data;
	  if (child_entry->widget == child)
	    break;
Tim Janik's avatar
Tim Janik committed
1305
	  i++;
1306
	}
Tim Janik's avatar
Tim Janik committed
1307
      g_value_set_int (value, list ? i : -1);
1308 1309
      break;
    default:
Tim Janik's avatar
Tim Janik committed
1310
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1311 1312 1313 1314
      break;
    }
}

1315 1316 1317 1318
typedef struct _CountingData CountingData;
struct _CountingData {
  GtkWidget *widget;
  gboolean found;
1319 1320
  guint before;
  guint after;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
};

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

  if (!gtk_widget_get_visible (widget))
    return;

  if (count->widget == widget)
1333
    count->found = TRUE;
1334 1335 1336
  else if (count->found)
    count->after++;
  else
1337
    count->before++;
1338 1339
}

1340 1341
static gint
gtk_box_get_visible_position (GtkBox    *box,
1342 1343
                              GtkWidget *child)
{
1344
  CountingData count = { child, FALSE, 0, 0 };
1345

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
  /* 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;
1357

1358 1359 1360 1361 1362
  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;
1363 1364 1365 1366 1367 1368
}

static GtkWidgetPath *
gtk_box_get_path_for_child (GtkContainer *container,
                            GtkWidget    *child)
{
1369
  GtkWidgetPath *path, *sibling_path;
1370
  GtkBox *box;
1371
  GtkBoxPrivate *private;
1372
  GList *list, *children;
1373 1374

  box = GTK_BOX (container);
1375
  private = box->priv;
1376

1377
  path = _gtk_widget_create_path (GTK_WIDGET (container));
1378 1379

  if (gtk_widget_get_visible (child))
1380
    {
1381 1382
      gint position;

1383
      sibling_path = gtk_widget_path_new ();
1384 1385 1386

      /* get_children works in visible order */
      children = gtk_container_get_children (container);
1387 1388 1389
      if (private->orientation == GTK_ORIENTATION_HORIZONTAL &&
          gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
        children = g_list_reverse (children);
1390 1391 1392 1393

      for (list = children; list; list = list->next)
        {
          if (!gtk_widget_get_visible (list->data))
1394 1395
            continue;

1396
          gtk_widget_path_append_for_widget (sibling_path, list->data);
1397
        }
1398

1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
      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);
1409
    }
1410 1411 1412 1413 1414 1415
  else
    gtk_widget_path_append_for_widget (path, child);

  return path;
}

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
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;
}

1436 1437 1438 1439 1440 1441
static void
gtk_box_invalidate_order_foreach (GtkWidget *widget)
{
  _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
}

1442 1443 1444
static void
gtk_box_invalidate_order (GtkBox *box)
{
1445
  gtk_container_foreach (GTK_CONTAINER (box),
1446
                         (GtkCallback) gtk_box_invalidate_order_foreach,
1447
                         NULL);
1448 1449
}

1450 1451 1452 1453 1454 1455 1456
static void
gtk_box_direction_changed (GtkWidget        *widget,
                           GtkTextDirection  previous_direction)
{
  gtk_box_invalidate_order (GTK_BOX (widget));
}

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
static void
box_child_visibility_notify_cb (GObject *obj,
                                GParamSpec *pspec,
                                gpointer user_data)
{
  GtkBox *box = user_data;

  gtk_box_invalidate_order (box);
}

1467
static GtkBoxChild *
1468 1469 1470 1471 1472 1473 1474
gtk_box_pack (GtkBox      *box,
              GtkWidget   *child,
              gboolean     expand,
              gboolean     fill,
              guint        padding,
              GtkPackType  pack_type)
{
1475
  GtkBoxPrivate *private = box->priv;
1476 1477
  GtkBoxChild *child_info;

1478 1479 1480
  g_return_val_if_fail (GTK_IS_BOX (box), NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
  g_return_val_if_fail (gtk_widget_get_parent (child) == NULL, NULL);
1481 1482 1483 1484 1485 1486 1487 1488

  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;

1489
  private->children = g_list_append (private->children, child_info);
1490 1491 1492

  gtk_widget_freeze_child_notify (child);

1493
  gtk_box_invalidate_order (box);
1494
  gtk_widget_set_parent (child, GTK_WIDGET (box));
1495

1496 1497 1498
  g_signal_connect (child, "notify::visible",
                    G_CALLBACK (box_child_visibility_notify_cb), box);

1499 1500 1501 1502 1503 1504
  gtk_widget_child_notify (child, "expand");
  gtk_widget_child_notify (child, "fill");
  gtk_widget_child_notify (child, "padding");
  gtk_widget_child_notify (child, "pack-type");
  gtk_widget_child_notify (child, "position");
  gtk_widget_thaw_child_notify (child);
1505 1506

  return child_info;
1507 1508
}

1509
static void
1510 1511 1512
gtk_box_get_size (GtkWidget      *widget,
		  GtkOrientation  orientation,
		  gint           *minimum_size,
1513 1514 1515
		  gint           *natural_size,
		  gint           *minimum_baseline,
		  gint           *natural_baseline)
1516 1517
{
  GtkBox *box;
1518
  GtkBoxPrivate *private;
1519 1520
  GList *children;
  gint nvis_children;
1521
  gint minimum, natural;
1522 1523 1524 1525
  gint minimum_above, natural_above;
  gint minimum_below, natural_below;
  gboolean have_baseline;
  gint min_baseline, nat_baseline;
1526
  gint center_min, center_nat;
1527

1528
  box = GTK_BOX (widget);
1529
  private = box->priv;
1530

1531
  have_baseline = FALSE;
1532
  minimum = natural = 0;
1533 1534 1535
  minimum_above = natural_above = 0;
  minimum_below = natural_below = 0;
  min_baseline = nat_baseline = -1;
1536 1537 1538

  nvis_children = 0;

1539 1540
  center_min = center_nat = 0;

1541
  for (children = private->children; children; children = children->next)
1542 1543
    {
      GtkBoxChild *child = children->data;
1544 1545 1546

      if (gtk_widget_get_visible (child->widget))
        {
1547
          gint child_minimum, child_natural;
1548
          gint child_minimum_baseline = -1, child_natural_baseline = -1;
1549

1550
	  if (orientation == GTK_ORIENTATION_HORIZONTAL)
1551 1552
	    gtk_widget_get_preferred_width (child->widget,
                                            &child_minimum, &child_natural);
1553
	  else
1554 1555 1556
	    gtk_widget_get_preferred_height_and_baseline_for_width (child->widget, -1,
								    &child_minimum, &child_natural,
								    &child_minimum_baseline, &child_natural_baseline);
1557

1558 1559
          if (private->orientation == orientation)
	    {
1560
              if (private->homogeneous)
1561
                {
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
                  if (child == private->center)
                    {
                      center_min = child_minimum + child->padding * 2;
                      center_nat = child_natural + child->padding * 2;
                    }
                  else
                    {
                      gint largest;

                      largest = child_minimum + child->padding * 2;
                      minimum = MAX (minimum, largest);

                      largest = child_natural + child->padding * 2;
                      natural = MAX (natural, largest);
                    }
1577 1578 1579
                }
              else
                {
1580 1581
                  minimum += child_minimum + child->padding * 2;
                  natural += child_natural + child->padding * 2;
1582
                }