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_class_init (GtkBoxClass *klass);
49 static void gtk_box_init (GtkBox *box);
50 static void gtk_box_set_property (GObject *object,
54 static void gtk_box_get_property (GObject *object,
58 static void gtk_box_add (GtkContainer *container,
60 static void gtk_box_remove (GtkContainer *container,
62 static void gtk_box_forall (GtkContainer *container,
63 gboolean include_internals,
65 gpointer callback_data);
66 static void gtk_box_set_child_property (GtkContainer *container,
71 static void gtk_box_get_child_property (GtkContainer *container,
76 static GType gtk_box_child_type (GtkContainer *container);
79 static GtkContainerClass *parent_class = NULL;
83 gtk_box_get_type (void)
85 static GType box_type = 0;
89 static const GTypeInfo box_info =
93 NULL, /* base_finalize */
94 (GClassInitFunc) gtk_box_class_init,
95 NULL, /* class_finalize */
96 NULL, /* class_data */
99 (GInstanceInitFunc) gtk_box_init,
100 NULL, /* value_table */
103 box_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkBox",
104 &box_info, G_TYPE_FLAG_ABSTRACT);
111 gtk_box_class_init (GtkBoxClass *class)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
114 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
116 parent_class = g_type_class_peek_parent (class);
118 gobject_class->set_property = gtk_box_set_property;
119 gobject_class->get_property = gtk_box_get_property;
121 container_class->add = gtk_box_add;
122 container_class->remove = gtk_box_remove;
123 container_class->forall = gtk_box_forall;
124 container_class->child_type = gtk_box_child_type;
125 container_class->set_child_property = gtk_box_set_child_property;
126 container_class->get_child_property = gtk_box_get_child_property;
128 g_object_class_install_property (gobject_class,
130 g_param_spec_int ("spacing",
132 P_("The amount of space between children"),
136 GTK_PARAM_READWRITE));
138 g_object_class_install_property (gobject_class,
140 g_param_spec_boolean ("homogeneous",
142 P_("Whether the children should all be the same size"),
144 GTK_PARAM_READWRITE));
146 gtk_container_class_install_child_property (container_class,
148 g_param_spec_boolean ("expand",
150 P_("Whether the child should receive extra space when the parent grows"),
152 GTK_PARAM_READWRITE));
153 gtk_container_class_install_child_property (container_class,
155 g_param_spec_boolean ("fill",
157 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
159 GTK_PARAM_READWRITE));
160 gtk_container_class_install_child_property (container_class,
162 g_param_spec_uint ("padding",
164 P_("Extra space to put between the child and its neighbors, in pixels"),
166 GTK_PARAM_READWRITE));
167 gtk_container_class_install_child_property (container_class,
168 CHILD_PROP_PACK_TYPE,
169 g_param_spec_enum ("pack-type",
171 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
172 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
173 GTK_PARAM_READWRITE));
174 gtk_container_class_install_child_property (container_class,
176 g_param_spec_int ("position",
178 P_("The index of the child in the parent"),
180 GTK_PARAM_READWRITE));
184 gtk_box_init (GtkBox *box)
186 GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
187 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
189 box->children = NULL;
191 box->homogeneous = FALSE;
195 gtk_box_set_property (GObject *object,
202 box = GTK_BOX (object);
207 gtk_box_set_spacing (box, g_value_get_int (value));
209 case PROP_HOMOGENEOUS:
210 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 static void gtk_box_get_property (GObject *object,
225 box = GTK_BOX (object);
230 g_value_set_int (value, box->spacing);
232 case PROP_HOMOGENEOUS:
233 g_value_set_boolean (value, box->homogeneous);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gtk_box_child_type (GtkContainer *container)
244 return GTK_TYPE_WIDGET;
248 gtk_box_set_child_property (GtkContainer *container,
257 GtkPackType pack_type = 0;
259 if (property_id != CHILD_PROP_POSITION)
260 gtk_box_query_child_packing (GTK_BOX (container),
268 case CHILD_PROP_EXPAND:
269 gtk_box_set_child_packing (GTK_BOX (container),
271 g_value_get_boolean (value),
276 case CHILD_PROP_FILL:
277 gtk_box_set_child_packing (GTK_BOX (container),
280 g_value_get_boolean (value),
284 case CHILD_PROP_PADDING:
285 gtk_box_set_child_packing (GTK_BOX (container),
289 g_value_get_uint (value),
292 case CHILD_PROP_PACK_TYPE:
293 gtk_box_set_child_packing (GTK_BOX (container),
298 g_value_get_enum (value));
300 case CHILD_PROP_POSITION:
301 gtk_box_reorder_child (GTK_BOX (container),
303 g_value_get_int (value));
306 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
312 gtk_box_get_child_property (GtkContainer *container,
321 GtkPackType pack_type = 0;
324 if (property_id != CHILD_PROP_POSITION)
325 gtk_box_query_child_packing (GTK_BOX (container),
334 case CHILD_PROP_EXPAND:
335 g_value_set_boolean (value, expand);
337 case CHILD_PROP_FILL:
338 g_value_set_boolean (value, fill);
340 case CHILD_PROP_PADDING:
341 g_value_set_uint (value, padding);
343 case CHILD_PROP_PACK_TYPE:
344 g_value_set_enum (value, pack_type);
346 case CHILD_PROP_POSITION:
348 for (list = GTK_BOX (container)->children; list; list = list->next)
350 GtkBoxChild *child_entry;
352 child_entry = list->data;
353 if (child_entry->widget == child)
357 g_value_set_int (value, list ? i : -1);
360 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
366 gtk_box_pack_start (GtkBox *box,
372 GtkBoxChild *child_info;
374 g_return_if_fail (GTK_IS_BOX (box));
375 g_return_if_fail (GTK_IS_WIDGET (child));
376 g_return_if_fail (child->parent == NULL);
378 child_info = g_new (GtkBoxChild, 1);
379 child_info->widget = child;
380 child_info->padding = padding;
381 child_info->expand = expand ? TRUE : FALSE;
382 child_info->fill = fill ? TRUE : FALSE;
383 child_info->pack = GTK_PACK_START;
384 child_info->is_secondary = FALSE;
386 box->children = g_list_append (box->children, child_info);
388 gtk_widget_freeze_child_notify (child);
390 gtk_widget_set_parent (child, GTK_WIDGET (box));
392 gtk_widget_child_notify (child, "expand");
393 gtk_widget_child_notify (child, "fill");
394 gtk_widget_child_notify (child, "padding");
395 gtk_widget_child_notify (child, "pack_type");
396 gtk_widget_child_notify (child, "position");
397 gtk_widget_thaw_child_notify (child);
401 gtk_box_pack_end (GtkBox *box,
407 GtkBoxChild *child_info;
409 g_return_if_fail (GTK_IS_BOX (box));
410 g_return_if_fail (GTK_IS_WIDGET (child));
411 g_return_if_fail (child->parent == NULL);
413 child_info = g_new (GtkBoxChild, 1);
414 child_info->widget = child;
415 child_info->padding = padding;
416 child_info->expand = expand ? TRUE : FALSE;
417 child_info->fill = fill ? TRUE : FALSE;
418 child_info->pack = GTK_PACK_END;
419 child_info->is_secondary = FALSE;
421 box->children = g_list_append (box->children, child_info);
423 gtk_widget_freeze_child_notify (child);
425 gtk_widget_set_parent (child, GTK_WIDGET (box));
427 gtk_widget_child_notify (child, "expand");
428 gtk_widget_child_notify (child, "fill");
429 gtk_widget_child_notify (child, "padding");
430 gtk_widget_child_notify (child, "pack_type");
431 gtk_widget_child_notify (child, "position");
432 gtk_widget_thaw_child_notify (child);
436 gtk_box_pack_start_defaults (GtkBox *box,
439 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
443 gtk_box_pack_end_defaults (GtkBox *box,
446 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
450 gtk_box_set_homogeneous (GtkBox *box,
451 gboolean homogeneous)
453 g_return_if_fail (GTK_IS_BOX (box));
455 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
457 box->homogeneous = homogeneous ? TRUE : FALSE;
458 g_object_notify (G_OBJECT (box), "homogeneous");
459 gtk_widget_queue_resize (GTK_WIDGET (box));
464 * gtk_box_get_homogeneous:
467 * Returns whether the box is homogeneous (all children are the
468 * same size). See gtk_box_set_homogeneous ().
470 * Return value: %TRUE if the box is homogeneous.
473 gtk_box_get_homogeneous (GtkBox *box)
475 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
477 return box->homogeneous;
481 gtk_box_set_spacing (GtkBox *box,
484 g_return_if_fail (GTK_IS_BOX (box));
486 if (spacing != box->spacing)
488 box->spacing = spacing;
489 g_object_notify (G_OBJECT (box), "spacing");
490 gtk_widget_queue_resize (GTK_WIDGET (box));
495 * gtk_box_get_spacing:
498 * Gets the value set by gtk_box_set_spacing().
500 * Return value: spacing between children
503 gtk_box_get_spacing (GtkBox *box)
505 g_return_val_if_fail (GTK_IS_BOX (box), 0);
511 gtk_box_reorder_child (GtkBox *box,
517 GtkBoxChild *child_info = NULL;
520 g_return_if_fail (GTK_IS_BOX (box));
521 g_return_if_fail (GTK_IS_WIDGET (child));
523 old_link = box->children;
527 child_info = old_link->data;
528 if (child_info->widget == child)
531 old_link = old_link->next;
535 g_return_if_fail (old_link != NULL);
537 if (position == old_position)
540 box->children = g_list_delete_link (box->children, old_link);
545 new_link = g_list_nth (box->children, position);
547 box->children = g_list_insert_before (box->children, new_link, child_info);
549 gtk_widget_child_notify (child, "position");
550 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
551 gtk_widget_queue_resize (child);
555 gtk_box_query_child_packing (GtkBox *box,
560 GtkPackType *pack_type)
563 GtkBoxChild *child_info = NULL;
565 g_return_if_fail (GTK_IS_BOX (box));
566 g_return_if_fail (GTK_IS_WIDGET (child));
568 list = box->children;
571 child_info = list->data;
572 if (child_info->widget == child)
581 *expand = child_info->expand;
583 *fill = child_info->fill;
585 *padding = child_info->padding;
587 *pack_type = child_info->pack;
592 gtk_box_set_child_packing (GtkBox *box,
597 GtkPackType pack_type)
600 GtkBoxChild *child_info = NULL;
602 g_return_if_fail (GTK_IS_BOX (box));
603 g_return_if_fail (GTK_IS_WIDGET (child));
605 list = box->children;
608 child_info = list->data;
609 if (child_info->widget == child)
615 gtk_widget_freeze_child_notify (child);
618 child_info->expand = expand != FALSE;
619 gtk_widget_child_notify (child, "expand");
620 child_info->fill = fill != FALSE;
621 gtk_widget_child_notify (child, "fill");
622 child_info->padding = padding;
623 gtk_widget_child_notify (child, "padding");
624 if (pack_type == GTK_PACK_END)
625 child_info->pack = GTK_PACK_END;
627 child_info->pack = GTK_PACK_START;
628 gtk_widget_child_notify (child, "pack_type");
630 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
631 gtk_widget_queue_resize (child);
633 gtk_widget_thaw_child_notify (child);
637 gtk_box_add (GtkContainer *container,
640 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
644 gtk_box_remove (GtkContainer *container,
651 box = GTK_BOX (container);
653 children = box->children;
656 child = children->data;
658 if (child->widget == widget)
660 gboolean was_visible;
662 was_visible = GTK_WIDGET_VISIBLE (widget);
663 gtk_widget_unparent (widget);
665 box->children = g_list_remove_link (box->children, children);
666 g_list_free (children);
669 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
670 * since that's what is needed by toplevels.
673 gtk_widget_queue_resize (GTK_WIDGET (container));
678 children = children->next;
683 gtk_box_forall (GtkContainer *container,
684 gboolean include_internals,
685 GtkCallback callback,
686 gpointer callback_data)
692 g_return_if_fail (callback != NULL);
694 box = GTK_BOX (container);
696 children = box->children;
699 child = children->data;
700 children = children->next;
702 if (child->pack == GTK_PACK_START)
703 (* callback) (child->widget, callback_data);
706 children = g_list_last (box->children);
709 child = children->data;
710 children = children->prev;
712 if (child->pack == GTK_PACK_END)
713 (* callback) (child->widget, callback_data);
717 #define __GTK_BOX_C__
718 #include "gtkaliasdef.c"