gtkbox.c 17.1 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
/**
Matthias Clasen's avatar
Matthias Clasen committed
26
 * GtkBox:
27
 *
Matthias Clasen's avatar
Matthias Clasen committed
28
 * The `GtkBox` widget arranges child widgets into a single row or column.
29
 *
Matthias Clasen's avatar
Matthias Clasen committed
30
 * ![An example GtkBox](box.png)
31
 *
Matthias Clasen's avatar
Matthias Clasen committed
32
33
34
35
36
 * Whether it is a row or column depends on the value of its
 * [property@Gtk.Orientable:orientation] property. Within the other
 * dimension, all children are allocated the same size. Of course, the
 * [property@Gtk.Widget:halign] and [property@Gtk.Widget:valign] properties
 * can be used on the children to influence their allocation.
37
 *
Matthias Clasen's avatar
Matthias Clasen committed
38
39
40
41
 * Use repeated calls to [method@Gtk.Box.append] to pack widgets into a
 * `GtkBox` from start to end. Use [method@Gtk.Box.remove] to remove widgets
 * from the `GtkBox`. [method@Gtk.Box.insert_child_after] can be used to add
 * a child at a particular position.
42
 *
Matthias Clasen's avatar
Matthias Clasen committed
43
44
45
46
47
48
49
50
 * Use [method@Gtk.Box.set_homogeneous] to specify whether or not all children
 * of the `GtkBox` are forced to get the same amount of space.
 *
 * Use [method@Gtk.Box.set_spacing] to determine how much space will be minimally
 * placed between all children in the `GtkBox`. Note that spacing is added
 * *between* the children.
 *
 * Use [method@Gtk.Box.reorder_child_after] to move a child to a different
51
52
 * place in the box.
 *
Matthias Clasen's avatar
Matthias Clasen committed
53
 * # CSS nodes
Matthias Clasen's avatar
Matthias Clasen committed
54
 *
Matthias Clasen's avatar
Matthias Clasen committed
55
 * `GtkBox` uses a single CSS node with name box.
56
57
58
 *
 * # Accessibility
 *
Matthias Clasen's avatar
Matthias Clasen committed
59
 * `GtkBox` uses the %GTK_ACCESSIBLE_ROLE_GROUP role.
60
61
 */

62
#include "config.h"
63

Elliot Lee's avatar
Elliot Lee committed
64
#include "gtkbox.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
65
#include "gtkboxlayout.h"
Matthias Clasen's avatar
Matthias Clasen committed
66
#include "gtkbuildable.h"
67
#include "gtkintl.h"
68
#include "gtkorientable.h"
69
#include "gtkprivate.h"
70
71
#include "gtktypebuiltins.h"
#include "gtksizerequest.h"
72
#include "gtkwidgetprivate.h"
73

Elliot Lee's avatar
Elliot Lee committed
74

75
enum {
76
77
  PROP_0,
  PROP_SPACING,
78
  PROP_HOMOGENEOUS,
79
80
81
  PROP_BASELINE_POSITION,

  /* orientable */
82
83
  PROP_ORIENTATION,
  LAST_PROP = PROP_ORIENTATION
84
};
Elliot Lee's avatar
Elliot Lee committed
85

Timm Bäder's avatar
Timm Bäder committed
86
typedef struct
87
{
88
89
90
  gint16          spacing;

  guint           homogeneous    : 1;
91
  guint           baseline_pos   : 2;
Timm Bäder's avatar
Timm Bäder committed
92
} GtkBoxPrivate;
93

94
95
static GParamSpec *props[LAST_PROP] = { NULL, };

Matthias Clasen's avatar
Matthias Clasen committed
96
97
98
static void gtk_box_buildable_iface_init (GtkBuildableIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_WIDGET,
99
                         G_ADD_PRIVATE (GtkBox)
Matthias Clasen's avatar
Matthias Clasen committed
100
101
102
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_box_buildable_iface_init))
Elliot Lee's avatar
Elliot Lee committed
103
104


105
106
107
108
109
static void
gtk_box_set_property (GObject      *object,
                      guint         prop_id,
                      const GValue *value,
                      GParamSpec   *pspec)
110
{
111
  GtkBox *box = GTK_BOX (object);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
112
  GtkLayoutManager *box_layout = gtk_widget_get_layout_manager (GTK_WIDGET (box));
113

114
  switch (prop_id)
115
    {
116
    case PROP_ORIENTATION:
117
118
      {
        GtkOrientation orientation = g_value_get_enum (value);
Matthias Clasen's avatar
Matthias Clasen committed
119
        if (gtk_orientable_get_orientation (GTK_ORIENTABLE (box_layout)) != orientation)
120
          {
Matthias Clasen's avatar
Matthias Clasen committed
121
122
123
            gtk_orientable_set_orientation (GTK_ORIENTABLE (box_layout), orientation);
            gtk_widget_update_orientation (GTK_WIDGET (box), orientation);
            g_object_notify_by_pspec (G_OBJECT (box), pspec);
124
125
          }
      }
126
      break;
127
128
    case PROP_SPACING:
      gtk_box_set_spacing (box, g_value_get_int (value));
129
      break;
130
131
132
    case PROP_BASELINE_POSITION:
      gtk_box_set_baseline_position (box, g_value_get_enum (value));
      break;
133
134
    case PROP_HOMOGENEOUS:
      gtk_box_set_homogeneous (box, g_value_get_boolean (value));
135
      break;
Tim Janik's avatar
Tim Janik committed
136
    default:
137
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
138
      break;
139
140
141
    }
}

142
143
144
145
146
static void
gtk_box_get_property (GObject    *object,
                      guint       prop_id,
                      GValue     *value,
                      GParamSpec *pspec)
147
{
148
  GtkBox *box = GTK_BOX (object);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
149
  GtkBoxLayout *box_layout = GTK_BOX_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (box)));
150

151
  switch (prop_id)
152
    {
153
    case PROP_ORIENTATION:
Matthias Clasen's avatar
Matthias Clasen committed
154
      g_value_set_enum (value, gtk_orientable_get_orientation (GTK_ORIENTABLE (box_layout)));
155
      break;
156
    case PROP_SPACING:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
157
      g_value_set_int (value, gtk_box_layout_get_spacing (box_layout));
158
      break;
159
    case PROP_BASELINE_POSITION:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
160
      g_value_set_enum (value, gtk_box_layout_get_baseline_position (box_layout));
161
      break;
162
    case PROP_HOMOGENEOUS:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
163
      g_value_set_boolean (value, gtk_box_layout_get_homogeneous (box_layout));
164
165
      break;
    default:
166
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167
168
169
170
      break;
    }
}

171
static void
Matthias Clasen's avatar
Matthias Clasen committed
172
173
174
gtk_box_compute_expand (GtkWidget *widget,
                        gboolean  *hexpand_p,
                        gboolean  *vexpand_p)
175
{
Matthias Clasen's avatar
Matthias Clasen committed
176
177
178
  GtkWidget *w;
  gboolean hexpand = FALSE;
  gboolean vexpand = FALSE;
179

Matthias Clasen's avatar
Matthias Clasen committed
180
181
182
183
184
185
186
187
188
189
  for (w = gtk_widget_get_first_child (widget);
       w != NULL;
       w = gtk_widget_get_next_sibling (w))
    {
      hexpand = hexpand || gtk_widget_compute_expand (w, GTK_ORIENTATION_HORIZONTAL);
      vexpand = vexpand || gtk_widget_compute_expand (w, GTK_ORIENTATION_VERTICAL);
    }

  *hexpand_p = hexpand;
  *vexpand_p = vexpand;
190
191
}

Matthias Clasen's avatar
Matthias Clasen committed
192
193
static GtkSizeRequestMode
gtk_box_get_request_mode (GtkWidget *widget)
194
{
Matthias Clasen's avatar
Matthias Clasen committed
195
196
  GtkWidget *w;
  int wfh = 0, hfw = 0;
197

Matthias Clasen's avatar
Matthias Clasen committed
198
199
200
  for (w = gtk_widget_get_first_child (widget);
       w != NULL;
       w = gtk_widget_get_next_sibling (w))
201
    {
Matthias Clasen's avatar
Matthias Clasen committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
      GtkSizeRequestMode mode = gtk_widget_get_request_mode (w);

      switch (mode)
        {
        case GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH:
          hfw ++;
          break;
        case GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT:
          wfh ++;
          break;
        case GTK_SIZE_REQUEST_CONSTANT_SIZE:
        default:
          break;
        }
216
217
    }

Matthias Clasen's avatar
Matthias Clasen committed
218
219
220
221
222
223
  if (hfw == 0 && wfh == 0)
    return GTK_SIZE_REQUEST_CONSTANT_SIZE;
  else
    return wfh > hfw ?
        GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
        GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
224
225
}

Matthias Clasen's avatar
Matthias Clasen committed
226
227
static void
gtk_box_dispose (GObject *object)
228
{
Matthias Clasen's avatar
Matthias Clasen committed
229
230
231
232
233
234
  GtkWidget *child;

  while ((child = gtk_widget_get_first_child (GTK_WIDGET (object))))
    gtk_widget_unparent (child);

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

237
238
239
240
241
242
243
244
static void
gtk_box_class_init (GtkBoxClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->set_property = gtk_box_set_property;
  object_class->get_property = gtk_box_get_property;
Matthias Clasen's avatar
Matthias Clasen committed
245
  object_class->dispose = gtk_box_dispose;
246

Matthias Clasen's avatar
Matthias Clasen committed
247
248
249
  widget_class->focus = gtk_widget_focus_child;
  widget_class->compute_expand = gtk_box_compute_expand;
  widget_class->get_request_mode = gtk_box_get_request_mode;
250
251
252
253
254

  g_object_class_override_property (object_class,
                                    PROP_ORIENTATION,
                                    "orientation");

Matthias Clasen's avatar
Matthias Clasen committed
255
256
257
258
259
  /**
   * GtkBox:spacing: (attributes org.gtk.Property.get=gtk_box_get_spacing org.gtk.Property.set=gtk_box_set_spacing)
   *
   * The amount of space between children.
   */
260
  props[PROP_SPACING] =
261
    g_param_spec_int ("spacing", NULL, NULL,
262
263
264
                      0, G_MAXINT, 0,
                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

Matthias Clasen's avatar
Matthias Clasen committed
265
266
267
268
269
  /**
   * GtkBox:homogeneous: (attributes org.gtk.Property.get=gtk_box_get_homogeneous org.gtk.Property.set=gtk_box_set_homogeneous)
   *
   * Whether the children should all be the same size.
   */
270
  props[PROP_HOMOGENEOUS] =
271
    g_param_spec_boolean ("homogeneous", NULL, NULL,
272
273
274
                          FALSE,
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

Matthias Clasen's avatar
Matthias Clasen committed
275
276
277
278
279
  /**
   * GtkBox:baseline-position: (attributes org.gtk.Property.get=gtk_box_get_baseline_position org.gtk.Property.set=gtk_box_set_baseline_position)
   *
   * The position of the baseline aligned widgets if extra space is available.
   */
280
  props[PROP_BASELINE_POSITION] =
281
    g_param_spec_enum ("baseline-position", NULL, NULL,
282
283
284
285
286
287
288
289
                       GTK_TYPE_BASELINE_POSITION,
                       GTK_BASELINE_POSITION_CENTER,
                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);

  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
  gtk_widget_class_set_css_name (widget_class, I_("box"));
290
  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_GROUP);
291
}
Benjamin Otte's avatar
Benjamin Otte committed
292
293
294
static void
gtk_box_init (GtkBox *box)
{
Matthias Clasen's avatar
Matthias Clasen committed
295
  gtk_widget_update_orientation (GTK_WIDGET (box), GTK_ORIENTATION_HORIZONTAL);
Benjamin Otte's avatar
Benjamin Otte committed
296
297
}

Matthias Clasen's avatar
Matthias Clasen committed
298
299
300
301
302
303
static GtkBuildableIface *parent_buildable_iface;

static void
gtk_box_buildable_add_child (GtkBuildable *buildable,
                             GtkBuilder   *builder,
                             GObject      *child,
Benjamin Otte's avatar
Benjamin Otte committed
304
                             const char   *type)
Matthias Clasen's avatar
Matthias Clasen committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
{
  if (GTK_IS_WIDGET (child))
    gtk_box_append (GTK_BOX (buildable), GTK_WIDGET (child));
  else
    parent_buildable_iface->add_child (buildable, builder, child, type);
}

static void
gtk_box_buildable_iface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);

  iface->add_child = gtk_box_buildable_add_child;
}

320
321
/**
 * gtk_box_new:
Andrei Lișiță's avatar
Andrei Lișiță committed
322
323
 * @orientation: the box’s orientation
 * @spacing: the number of pixels to place by default between children
324
 *
Matthias Clasen's avatar
Matthias Clasen committed
325
 * Creates a new `GtkBox`.
326
 *
Matthias Clasen's avatar
Matthias Clasen committed
327
328
 * Returns: a new `GtkBox`.
 */
329
GtkWidget*
330
gtk_box_new (GtkOrientation orientation,
Benjamin Otte's avatar
Benjamin Otte committed
331
             int            spacing)
332
333
334
{
  return g_object_new (GTK_TYPE_BOX,
                       "orientation", orientation,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
335
                       "spacing", spacing,
336
337
338
                       NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
339
/**
Matthias Clasen's avatar
Matthias Clasen committed
340
341
 * gtk_box_set_homogeneous: (attributes org.gtk.Method.set_property=homogeneous)
 * @box: a `GtkBox`
Matthias Clasen's avatar
Matthias Clasen committed
342
343
 * @homogeneous: a boolean value, %TRUE to create equal allotments,
 *   %FALSE for variable allotments
Murray Cumming's avatar
Murray Cumming committed
344
 *
Matthias Clasen's avatar
Matthias Clasen committed
345
 * Sets whether or not all children of @box are given equal space
Matthias Clasen's avatar
Matthias Clasen committed
346
347
 * in the box.
 */
Elliot Lee's avatar
Elliot Lee committed
348
void
349
gtk_box_set_homogeneous (GtkBox  *box,
Timm Bäder's avatar
Timm Bäder committed
350
                         gboolean homogeneous)
Elliot Lee's avatar
Elliot Lee committed
351
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
352
  GtkBoxLayout *box_layout;
353

Elliot Lee's avatar
Elliot Lee committed
354
355
  g_return_if_fail (GTK_IS_BOX (box));

Emmanuele Bassi's avatar
Emmanuele Bassi committed
356
  homogeneous = !!homogeneous;
357

Emmanuele Bassi's avatar
Emmanuele Bassi committed
358
359
360
361
362
363
  box_layout = GTK_BOX_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (box)));
  if (homogeneous == gtk_box_layout_get_homogeneous (box_layout))
    return;

  gtk_box_layout_set_homogeneous (box_layout, homogeneous);
  g_object_notify_by_pspec (G_OBJECT (box), props[PROP_HOMOGENEOUS]);
Elliot Lee's avatar
Elliot Lee committed
364
365
}

366
/**
Matthias Clasen's avatar
Matthias Clasen committed
367
368
 * gtk_box_get_homogeneous: (attributes org.gtk.Method.get_property=homogeneous)
 * @box: a `GtkBox`
369
370
 *
 * Returns whether the box is homogeneous (all children are the
Matthias Clasen's avatar
Matthias Clasen committed
371
 * same size).
372
 *
373
 * Returns: %TRUE if the box is homogeneous.
Matthias Clasen's avatar
Matthias Clasen committed
374
 */
375
376
377
gboolean
gtk_box_get_homogeneous (GtkBox *box)
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
378
  GtkLayoutManager *box_layout;
Timm Bäder's avatar
Timm Bäder committed
379

380
381
  g_return_val_if_fail (GTK_IS_BOX (box), FALSE);

Emmanuele Bassi's avatar
Emmanuele Bassi committed
382
383
384
  box_layout = gtk_widget_get_layout_manager (GTK_WIDGET (box));

  return gtk_box_layout_get_homogeneous (GTK_BOX_LAYOUT (box_layout));
385
386
}

Matthias Clasen's avatar
Matthias Clasen committed
387
/**
Matthias Clasen's avatar
Matthias Clasen committed
388
389
 * gtk_box_set_spacing: (attributes org.gtk.Method.set_property=spacing)
 * @box: a `GtkBox`
Matthias Clasen's avatar
Matthias Clasen committed
390
391
 * @spacing: the number of pixels to put between children
 *
Matthias Clasen's avatar
Matthias Clasen committed
392
 * Sets the number of pixels to place between children of @box.
Matthias Clasen's avatar
Matthias Clasen committed
393
 */
Elliot Lee's avatar
Elliot Lee committed
394
395
void
gtk_box_set_spacing (GtkBox *box,
Timm Bäder's avatar
Timm Bäder committed
396
                     int     spacing)
Elliot Lee's avatar
Elliot Lee committed
397
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
398
  GtkBoxLayout *box_layout;
399

Elliot Lee's avatar
Elliot Lee committed
400
401
  g_return_if_fail (GTK_IS_BOX (box));

Emmanuele Bassi's avatar
Emmanuele Bassi committed
402
403
404
  box_layout = GTK_BOX_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (box)));
  if (spacing == gtk_box_layout_get_spacing (box_layout))
    return;
405

Emmanuele Bassi's avatar
Emmanuele Bassi committed
406
407
  gtk_box_layout_set_spacing (box_layout, spacing);
  g_object_notify_by_pspec (G_OBJECT (box), props[PROP_SPACING]);
Elliot Lee's avatar
Elliot Lee committed
408
409
}

410
/**
Matthias Clasen's avatar
Matthias Clasen committed
411
412
 * gtk_box_get_spacing: (attributes org.gtk.Method.get_property=spacing)
 * @box: a `GtkBox`
Murray Cumming's avatar
Murray Cumming committed
413
 *
414
 * Gets the value set by gtk_box_set_spacing().
Murray Cumming's avatar
Murray Cumming committed
415
 *
416
 * Returns: spacing between children
Matthias Clasen's avatar
Matthias Clasen committed
417
 */
Benjamin Otte's avatar
Benjamin Otte committed
418
int
419
420
gtk_box_get_spacing (GtkBox *box)
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
421
422
  GtkLayoutManager *box_layout;

423
  g_return_val_if_fail (GTK_IS_BOX (box), 0);
424

Emmanuele Bassi's avatar
Emmanuele Bassi committed
425
426
427
  box_layout = gtk_widget_get_layout_manager (GTK_WIDGET (box));

  return gtk_box_layout_get_spacing (GTK_BOX_LAYOUT (box_layout));
428
429
}

430
/**
Matthias Clasen's avatar
Matthias Clasen committed
431
432
433
434
435
436
437
438
439
440
 * gtk_box_set_baseline_position: (attributes org.gtk.Method.set_property=baseline-position)
 * @box: a `GtkBox`
 * @position: a `GtkBaselinePosition`
 *
 * Sets the baseline position of a box.
 *
 * This affects only horizontal boxes with at least one baseline
 * aligned child. If there is more vertical space available than
 * requested, and the baseline is not allocated by the parent then
 * @position is used to allocate the baseline with respect to the
441
442
443
444
 * extra space available.
 */
void
gtk_box_set_baseline_position (GtkBox             *box,
Timm Bäder's avatar
Timm Bäder committed
445
                               GtkBaselinePosition position)
446
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
447
  GtkBoxLayout *box_layout;
448
449
450

  g_return_if_fail (GTK_IS_BOX (box));

Emmanuele Bassi's avatar
Emmanuele Bassi committed
451
452
453
  box_layout = GTK_BOX_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (box)));
  if (position == gtk_box_layout_get_baseline_position (box_layout))
    return;
454

Emmanuele Bassi's avatar
Emmanuele Bassi committed
455
456
  gtk_box_layout_set_baseline_position (box_layout, position);
  g_object_notify_by_pspec (G_OBJECT (box), props[PROP_BASELINE_POSITION]);
457
458
459
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
460
461
 * gtk_box_get_baseline_position: (attributes org.gtk.Method.get_property=baseline-position)
 * @box: a `GtkBox`
462
463
464
 *
 * Gets the value set by gtk_box_set_baseline_position().
 *
465
 * Returns: the baseline position
Matthias Clasen's avatar
Matthias Clasen committed
466
 */
467
GtkBaselinePosition
468
gtk_box_get_baseline_position (GtkBox *box)
469
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
470
  GtkLayoutManager *box_layout;
Timm Bäder's avatar
Timm Bäder committed
471

472
473
  g_return_val_if_fail (GTK_IS_BOX (box), GTK_BASELINE_POSITION_CENTER);

Emmanuele Bassi's avatar
Emmanuele Bassi committed
474
475
476
  box_layout = gtk_widget_get_layout_manager (GTK_WIDGET (box));

  return gtk_box_layout_get_baseline_position (GTK_BOX_LAYOUT (box_layout));
477
478
}

479
480
/**
 * gtk_box_insert_child_after:
Matthias Clasen's avatar
Matthias Clasen committed
481
482
 * @box: a `GtkBox`
 * @child: the `GtkWidget` to insert
Andrei Lișiță's avatar
Andrei Lișiță committed
483
 * @sibling: (nullable): the sibling after which to insert @child
484
485
 *
 * Inserts @child in the position after @sibling in the list
Matthias Clasen's avatar
Matthias Clasen committed
486
487
488
 * of @box children.
 *
 * If @sibling is %NULL, insert @child at the first position.
489
490
491
492
493
494
 */
void
gtk_box_insert_child_after (GtkBox    *box,
                            GtkWidget *child,
                            GtkWidget *sibling)
{
495
  GtkWidget *widget;
496
497
498
499

  g_return_if_fail (GTK_IS_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (gtk_widget_get_parent (child) == NULL);
500
501
502

  widget = GTK_WIDGET (box);

503
504
505
506
507
508
509
510
511
512
513
514
515
516
  if (sibling)
    {
      g_return_if_fail (GTK_IS_WIDGET (sibling));
      g_return_if_fail (gtk_widget_get_parent (sibling) == widget);
    }

  if (child == sibling)
    return;

  gtk_widget_insert_after (child, widget, sibling);
}

/**
 * gtk_box_reorder_child_after:
Matthias Clasen's avatar
Matthias Clasen committed
517
518
 * @box: a `GtkBox`
 * @child: the `GtkWidget` to move, must be a child of @box
Matthias Clasen's avatar
Matthias Clasen committed
519
 * @sibling: (nullable): the sibling to move @child after
520
521
 *
 * Moves @child to the position after @sibling in the list
Matthias Clasen's avatar
Matthias Clasen committed
522
523
524
 * of @box children.
 *
 * If @sibling is %NULL, move @child to the first position.
525
526
527
528
529
530
 */
void
gtk_box_reorder_child_after (GtkBox    *box,
                             GtkWidget *child,
                             GtkWidget *sibling)
{
531
  GtkWidget *widget;
532
533
534

  g_return_if_fail (GTK_IS_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (child));
535
536
537
538
  g_return_if_fail (gtk_widget_get_parent (child) == (GtkWidget *)box);

 widget = GTK_WIDGET (box);

539
540
541
542
543
544
545
546
547
548
549
  if (sibling)
    {
      g_return_if_fail (GTK_IS_WIDGET (sibling));
      g_return_if_fail (gtk_widget_get_parent (sibling) == widget);
    }

  if (child == sibling)
    return;

  gtk_widget_insert_after (child, widget, sibling);
}
550
551
552

/**
 * gtk_box_append:
Matthias Clasen's avatar
Matthias Clasen committed
553
554
 * @box: a `GtkBox`
 * @child: the `GtkWidget` to append
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
 *
 * Adds @child as the last child to @box.
 */
void
gtk_box_append (GtkBox    *box,
                GtkWidget *child)
{
  g_return_if_fail (GTK_IS_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (gtk_widget_get_parent (child) == NULL);

  gtk_widget_insert_before (child, GTK_WIDGET (box), NULL);
}

/**
 * gtk_box_prepend:
Matthias Clasen's avatar
Matthias Clasen committed
571
572
 * @box: a `GtkBox`
 * @child: the `GtkWidget` to prepend
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
 *
 * Adds @child as the first child to @box.
 */
void
gtk_box_prepend (GtkBox    *box,
                 GtkWidget *child)
{
  g_return_if_fail (GTK_IS_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (gtk_widget_get_parent (child) == NULL);

  gtk_widget_insert_after (child, GTK_WIDGET (box), NULL);
}

/**
 * gtk_box_remove:
Matthias Clasen's avatar
Matthias Clasen committed
589
 * @box: a `GtkBox`
590
591
 * @child: the child to remove
 *
Matthias Clasen's avatar
Matthias Clasen committed
592
593
594
595
596
 * Removes a child widget from @box.
 *
 * The child must have been added before with
 * [method@Gtk.Box.append], [method@Gtk.Box.prepend], or
 * [method@Gtk.Box.insert_child_after].
597
598
599
600
601
602
603
604
605
606
607
 */
void
gtk_box_remove (GtkBox    *box,
                GtkWidget *child)
{
  g_return_if_fail (GTK_IS_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (gtk_widget_get_parent (child) == (GtkWidget *)box);

  gtk_widget_unparent (child);
}