1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkprivate.h"
48 static void gtk_box_set_property (GObject *object,
52 static void gtk_box_get_property (GObject *object,
56 static void gtk_box_add (GtkContainer *container,
58 static void gtk_box_remove (GtkContainer *container,
60 static void gtk_box_forall (GtkContainer *container,
61 gboolean include_internals,
63 gpointer callback_data);
64 static void gtk_box_set_child_property (GtkContainer *container,
69 static void gtk_box_get_child_property (GtkContainer *container,
74 static GType gtk_box_child_type (GtkContainer *container);
77 G_DEFINE_ABSTRACT_TYPE (GtkBox, gtk_box, GTK_TYPE_CONTAINER)
80 gtk_box_class_init (GtkBoxClass *class)
82 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
83 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
85 gobject_class->set_property = gtk_box_set_property;
86 gobject_class->get_property = gtk_box_get_property;
88 container_class->add = gtk_box_add;
89 container_class->remove = gtk_box_remove;
90 container_class->forall = gtk_box_forall;
91 container_class->child_type = gtk_box_child_type;
92 container_class->set_child_property = gtk_box_set_child_property;
93 container_class->get_child_property = gtk_box_get_child_property;
95 g_object_class_install_property (gobject_class,
97 g_param_spec_int ("spacing",
99 P_("The amount of space between children"),
103 GTK_PARAM_READWRITE));
105 g_object_class_install_property (gobject_class,
107 g_param_spec_boolean ("homogeneous",
109 P_("Whether the children should all be the same size"),
111 GTK_PARAM_READWRITE));
113 gtk_container_class_install_child_property (container_class,
115 g_param_spec_boolean ("expand",
117 P_("Whether the child should receive extra space when the parent grows"),
119 GTK_PARAM_READWRITE));
120 gtk_container_class_install_child_property (container_class,
122 g_param_spec_boolean ("fill",
124 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
126 GTK_PARAM_READWRITE));
127 gtk_container_class_install_child_property (container_class,
129 g_param_spec_uint ("padding",
131 P_("Extra space to put between the child and its neighbors, in pixels"),
133 GTK_PARAM_READWRITE));
134 gtk_container_class_install_child_property (container_class,
135 CHILD_PROP_PACK_TYPE,
136 g_param_spec_enum ("pack-type",
138 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
139 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
140 GTK_PARAM_READWRITE));
141 gtk_container_class_install_child_property (container_class,
143 g_param_spec_int ("position",
145 P_("The index of the child in the parent"),
147 GTK_PARAM_READWRITE));
151 gtk_box_init (GtkBox *box)
153 GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
154 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
156 box->children = NULL;
158 box->homogeneous = FALSE;
162 gtk_box_set_property (GObject *object,
169 box = GTK_BOX (object);
174 gtk_box_set_spacing (box, g_value_get_int (value));
176 case PROP_HOMOGENEOUS:
177 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 static void gtk_box_get_property (GObject *object,
192 box = GTK_BOX (object);
197 g_value_set_int (value, box->spacing);
199 case PROP_HOMOGENEOUS:
200 g_value_set_boolean (value, box->homogeneous);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 gtk_box_child_type (GtkContainer *container)
211 return GTK_TYPE_WIDGET;
215 gtk_box_set_child_property (GtkContainer *container,
224 GtkPackType pack_type = 0;
226 if (property_id != CHILD_PROP_POSITION)
227 gtk_box_query_child_packing (GTK_BOX (container),
235 case CHILD_PROP_EXPAND:
236 gtk_box_set_child_packing (GTK_BOX (container),
238 g_value_get_boolean (value),
243 case CHILD_PROP_FILL:
244 gtk_box_set_child_packing (GTK_BOX (container),
247 g_value_get_boolean (value),
251 case CHILD_PROP_PADDING:
252 gtk_box_set_child_packing (GTK_BOX (container),
256 g_value_get_uint (value),
259 case CHILD_PROP_PACK_TYPE:
260 gtk_box_set_child_packing (GTK_BOX (container),
265 g_value_get_enum (value));
267 case CHILD_PROP_POSITION:
268 gtk_box_reorder_child (GTK_BOX (container),
270 g_value_get_int (value));
273 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
279 gtk_box_get_child_property (GtkContainer *container,
288 GtkPackType pack_type = 0;
291 if (property_id != CHILD_PROP_POSITION)
292 gtk_box_query_child_packing (GTK_BOX (container),
301 case CHILD_PROP_EXPAND:
302 g_value_set_boolean (value, expand);
304 case CHILD_PROP_FILL:
305 g_value_set_boolean (value, fill);
307 case CHILD_PROP_PADDING:
308 g_value_set_uint (value, padding);
310 case CHILD_PROP_PACK_TYPE:
311 g_value_set_enum (value, pack_type);
313 case CHILD_PROP_POSITION:
315 for (list = GTK_BOX (container)->children; list; list = list->next)
317 GtkBoxChild *child_entry;
319 child_entry = list->data;
320 if (child_entry->widget == child)
324 g_value_set_int (value, list ? i : -1);
327 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
333 gtk_box_pack (GtkBox *box,
338 GtkPackType pack_type)
340 GtkBoxChild *child_info;
342 g_return_if_fail (GTK_IS_BOX (box));
343 g_return_if_fail (GTK_IS_WIDGET (child));
344 g_return_if_fail (child->parent == NULL);
346 child_info = g_new (GtkBoxChild, 1);
347 child_info->widget = child;
348 child_info->padding = padding;
349 child_info->expand = expand ? TRUE : FALSE;
350 child_info->fill = fill ? TRUE : FALSE;
351 child_info->pack = pack_type;
352 child_info->is_secondary = FALSE;
354 box->children = g_list_append (box->children, child_info);
356 gtk_widget_freeze_child_notify (child);
358 gtk_widget_set_parent (child, GTK_WIDGET (box));
360 gtk_widget_child_notify (child, "expand");
361 gtk_widget_child_notify (child, "fill");
362 gtk_widget_child_notify (child, "padding");
363 gtk_widget_child_notify (child, "pack-type");
364 gtk_widget_child_notify (child, "position");
365 gtk_widget_thaw_child_notify (child);
370 * gtk_box_pack_start:
372 * @child: the #GtkWidget to be added to @box
373 * @expand: %TRUE if the new child is to be given extra space allocated to
374 * @box. The extra space will be divided evenly between all children of
375 * @box that use this option
376 * @fill: %TRUE if space given to @child by the @expand option is
377 * actually allocated to @child, rather than just padding it. This
378 * parameter has no effect if @expand is set to %FALSE. A child is
379 * always allocated the full height of a #GtkHBox and the full width
380 * of a #GtkVBox. This option affects the other dimension
381 * @padding: extra space in pixels to put between this child and its
382 * neighbors, over and above the global amount specified by
383 * #GtkBox:spacing property. If @child is a widget at one of the
384 * reference ends of @box, then @padding pixels are also put between
385 * @child and the reference edge of @box
387 * Adds @child to @box, packed with reference to the start of @box.
388 * The @child is packed after any other child packed with reference
389 * to the start of @box.
392 gtk_box_pack_start (GtkBox *box,
398 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
404 * @child: the #GtkWidget to be added to @box
405 * @expand: %TRUE if the new child is to be given extra space allocated
406 * to @box. The extra space will be divided evenly between all children
407 * of @box that use this option
408 * @fill: %TRUE if space given to @child by the @expand option is
409 * actually allocated to @child, rather than just padding it. This
410 * parameter has no effect if @expand is set to %FALSE. A child is
411 * always allocated the full height of a #GtkHBox and the full width
412 * of a #GtkVBox. This option affects the other dimension
413 * @padding: extra space in pixels to put between this child and its
414 * neighbors, over and above the global amount specified by
415 * #GtkBox:spacing property. If @child is a widget at one of the
416 * reference ends of @box, then @padding pixels are also put between
417 * @child and the reference edge of @box
419 * Adds @child to @box, packed with reference to the end of @box.
420 * The @child is packed after (away from end of) any other child
421 * packed with reference to the end of @box.
424 gtk_box_pack_end (GtkBox *box,
430 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
434 * gtk_box_pack_start_defaults:
436 * @widget: the #GtkWidget to be added to @box
438 * Adds @widget to @box, packed with reference to the start of @box.
439 * The child is packed after any other child packed with reference
440 * to the start of @box.
442 * Parameters for how to pack the child @widget, #GtkBox:expand,
443 * #GtkBox:fill and #GtkBox:padding, are given their default
444 * values, %TRUE, %TRUE, and 0, respectively.
447 gtk_box_pack_start_defaults (GtkBox *box,
450 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
454 * gtk_box_pack_end_defaults:
456 * @widget: the #GtkWidget to be added to @box
458 * Adds @widget to @box, packed with reference to the end of @box.
459 * The child is packed after any other child packed with reference
460 * to the start of @box.
462 * Parameters for how to pack the child @widget, #GtkBox:expand,
463 * #GtkBox:fill and #GtkBox:padding, are given their default
464 * values, %TRUE, %TRUE, and 0, respectively.
467 gtk_box_pack_end_defaults (GtkBox *box,
470 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
474 * gtk_box_set_homogeneous:
476 * @homogeneous: a boolean value, %TRUE to create equal allotments,
477 * %FALSE for variable allotments
479 * Sets the #GtkBox:homogeneous property of @box, controlling
480 * whether or not all children of @box are given equal space
484 gtk_box_set_homogeneous (GtkBox *box,
485 gboolean homogeneous)
487 g_return_if_fail (GTK_IS_BOX (box));
489 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
491 box->homogeneous = homogeneous ? TRUE : FALSE;
492 g_object_notify (G_OBJECT (box), "homogeneous");
493 gtk_widget_queue_resize (GTK_WIDGET (box));
498 * gtk_box_get_homogeneous:
501 * Returns whether the box is homogeneous (all children are the
502 * same size). See gtk_box_set_homogeneous().
504 * Return value: %TRUE if the box is homogeneous.
507 gtk_box_get_homogeneous (GtkBox *box)
509 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
511 return box->homogeneous;
515 * gtk_box_set_spacing:
517 * @spacing: the number of pixels to put between children
519 * Sets the #GtkBox:spacing property of @box, which is the
520 * number of pixels to place between children of @box.
523 gtk_box_set_spacing (GtkBox *box,
526 g_return_if_fail (GTK_IS_BOX (box));
528 if (spacing != box->spacing)
530 box->spacing = spacing;
531 g_object_notify (G_OBJECT (box), "spacing");
532 gtk_widget_queue_resize (GTK_WIDGET (box));
537 * gtk_box_get_spacing:
540 * Gets the value set by gtk_box_set_spacing().
542 * Return value: spacing between children
545 gtk_box_get_spacing (GtkBox *box)
547 g_return_val_if_fail (GTK_IS_BOX (box), 0);
553 * gtk_box_reorder_child:
555 * @child: the #GtkWidget to move
556 * @position: the new position for @child in the list of children
557 * of @box, starting from 0. If negative, indicates the end of
560 * Moves @child to a new @position in the list of @box children.
561 * The list is the <structfield>children</structfield> field of
562 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
563 * as well as widgets packed #GTK_PACK_END, in the order that these
564 * widgets were added to @box.
566 * A widget's position in the @box children list determines where
567 * the widget is packed into @box. A child widget at some position
568 * in the list will be packed just after all other widgets of the
569 * same packing type that appear earlier in the list.
572 gtk_box_reorder_child (GtkBox *box,
578 GtkBoxChild *child_info = NULL;
581 g_return_if_fail (GTK_IS_BOX (box));
582 g_return_if_fail (GTK_IS_WIDGET (child));
584 old_link = box->children;
588 child_info = old_link->data;
589 if (child_info->widget == child)
592 old_link = old_link->next;
596 g_return_if_fail (old_link != NULL);
598 if (position == old_position)
601 box->children = g_list_delete_link (box->children, old_link);
606 new_link = g_list_nth (box->children, position);
608 box->children = g_list_insert_before (box->children, new_link, child_info);
610 gtk_widget_child_notify (child, "position");
611 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
612 gtk_widget_queue_resize (child);
616 * gtk_box_query_child_packing:
618 * @child: the #GtkWidget of the child to query
619 * @expand: pointer to return location for #GtkBox:expand child property
620 * @fill: pointer to return location for #GtkBox:fill child property
621 * @padding: pointer to return location for #GtkBox:padding child property
622 * @pack_type: pointer to return location for #GtkBox:pack-type child property
624 * Obtains information about how @child is packed into @box.
627 gtk_box_query_child_packing (GtkBox *box,
632 GtkPackType *pack_type)
635 GtkBoxChild *child_info = NULL;
637 g_return_if_fail (GTK_IS_BOX (box));
638 g_return_if_fail (GTK_IS_WIDGET (child));
640 list = box->children;
643 child_info = list->data;
644 if (child_info->widget == child)
653 *expand = child_info->expand;
655 *fill = child_info->fill;
657 *padding = child_info->padding;
659 *pack_type = child_info->pack;
664 * gtk_box_set_child_packing:
666 * @child: the #GtkWidget of the child to set
667 * @expand: the new value of the #GtkBox:expand child property
668 * @fill: the new value of the #GtkBox:fill child property
669 * @padding: the new value of the #GtkBox:padding child property
670 * @pack_type: the new value of the #GtkBox:pack-type child property
672 * Sets the way @child is packed into @box.
675 gtk_box_set_child_packing (GtkBox *box,
680 GtkPackType pack_type)
683 GtkBoxChild *child_info = NULL;
685 g_return_if_fail (GTK_IS_BOX (box));
686 g_return_if_fail (GTK_IS_WIDGET (child));
688 list = box->children;
691 child_info = list->data;
692 if (child_info->widget == child)
698 gtk_widget_freeze_child_notify (child);
701 child_info->expand = expand != FALSE;
702 gtk_widget_child_notify (child, "expand");
703 child_info->fill = fill != FALSE;
704 gtk_widget_child_notify (child, "fill");
705 child_info->padding = padding;
706 gtk_widget_child_notify (child, "padding");
707 if (pack_type == GTK_PACK_END)
708 child_info->pack = GTK_PACK_END;
710 child_info->pack = GTK_PACK_START;
711 gtk_widget_child_notify (child, "pack-type");
713 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
714 gtk_widget_queue_resize (child);
716 gtk_widget_thaw_child_notify (child);
720 gtk_box_add (GtkContainer *container,
723 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
727 gtk_box_remove (GtkContainer *container,
730 GtkBox *box = GTK_BOX (container);
734 children = box->children;
737 child = children->data;
739 if (child->widget == widget)
741 gboolean was_visible;
743 was_visible = GTK_WIDGET_VISIBLE (widget);
744 gtk_widget_unparent (widget);
746 box->children = g_list_remove_link (box->children, children);
747 g_list_free (children);
750 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
751 * since that's what is needed by toplevels.
754 gtk_widget_queue_resize (GTK_WIDGET (container));
759 children = children->next;
764 gtk_box_forall (GtkContainer *container,
765 gboolean include_internals,
766 GtkCallback callback,
767 gpointer callback_data)
769 GtkBox *box = GTK_BOX (container);
773 children = box->children;
776 child = children->data;
777 children = children->next;
779 if (child->pack == GTK_PACK_START)
780 (* callback) (child->widget, callback_data);
783 children = g_list_last (box->children);
786 child = children->data;
787 children = children->prev;
789 if (child->pack == GTK_PACK_END)
790 (* callback) (child->widget, callback_data);
794 #define __GTK_BOX_C__
795 #include "gtkaliasdef.c"