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/.
45 static void gtk_box_class_init (GtkBoxClass *klass);
46 static void gtk_box_init (GtkBox *box);
47 static void gtk_box_set_property (GObject *object,
51 static void gtk_box_get_property (GObject *object,
55 static void gtk_box_add (GtkContainer *container,
57 static void gtk_box_remove (GtkContainer *container,
59 static void gtk_box_forall (GtkContainer *container,
60 gboolean include_internals,
62 gpointer callback_data);
63 static void gtk_box_set_child_property (GtkContainer *container,
68 static void gtk_box_get_child_property (GtkContainer *container,
73 static GtkType gtk_box_child_type (GtkContainer *container);
76 static GtkContainerClass *parent_class = NULL;
80 gtk_box_get_type (void)
82 static GtkType box_type = 0;
86 static const GtkTypeInfo box_info =
91 (GtkClassInitFunc) gtk_box_class_init,
92 (GtkObjectInitFunc) gtk_box_init,
93 /* reserved_1 */ NULL,
94 /* reserved_2 */ NULL,
95 (GtkClassInitFunc) NULL,
98 box_type = gtk_type_unique (GTK_TYPE_CONTAINER, &box_info);
105 gtk_box_class_init (GtkBoxClass *class)
107 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
108 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
110 parent_class = g_type_class_peek_parent (class);
112 gobject_class->set_property = gtk_box_set_property;
113 gobject_class->get_property = gtk_box_get_property;
115 container_class->add = gtk_box_add;
116 container_class->remove = gtk_box_remove;
117 container_class->forall = gtk_box_forall;
118 container_class->child_type = gtk_box_child_type;
119 container_class->set_child_property = gtk_box_set_child_property;
120 container_class->get_child_property = gtk_box_get_child_property;
122 g_object_class_install_property (gobject_class,
124 g_param_spec_int ("spacing",
126 _("The amount of space between children."),
130 G_PARAM_READABLE | G_PARAM_WRITABLE));
132 g_object_class_install_property (gobject_class,
134 g_param_spec_boolean ("homogeneous",
136 _("Whether the children should all be the same size."),
138 G_PARAM_READABLE | G_PARAM_WRITABLE));
140 gtk_container_class_install_child_property (container_class,
142 g_param_spec_boolean ("expand", NULL, NULL,
145 gtk_container_class_install_child_property (container_class,
147 g_param_spec_boolean ("fill", NULL, NULL,
150 gtk_container_class_install_child_property (container_class,
152 g_param_spec_uint ("padding", NULL, NULL,
155 gtk_container_class_install_child_property (container_class,
156 CHILD_PROP_PACK_TYPE,
157 g_param_spec_enum ("pack_type", NULL, NULL,
158 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
160 gtk_container_class_install_child_property (container_class,
162 g_param_spec_int ("position", NULL, NULL,
168 gtk_box_init (GtkBox *box)
170 GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
172 box->children = NULL;
174 box->homogeneous = FALSE;
178 gtk_box_set_property (GObject *object,
185 box = GTK_BOX (object);
190 gtk_box_set_spacing (box, g_value_get_int (value));
192 case PROP_HOMOGENEOUS:
193 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 static void gtk_box_get_property (GObject *object,
208 box = GTK_BOX (object);
213 g_value_set_int (value, box->spacing);
215 case PROP_HOMOGENEOUS:
216 g_value_set_boolean (value, box->homogeneous);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 gtk_box_child_type (GtkContainer *container)
227 return GTK_TYPE_WIDGET;
231 gtk_box_set_child_property (GtkContainer *container,
240 GtkPackType pack_type = 0;
242 if (property_id != CHILD_PROP_POSITION)
243 gtk_box_query_child_packing (GTK_BOX (container),
251 case CHILD_PROP_EXPAND:
252 gtk_box_set_child_packing (GTK_BOX (container),
254 g_value_get_boolean (value),
259 case CHILD_PROP_FILL:
260 gtk_box_set_child_packing (GTK_BOX (container),
263 g_value_get_boolean (value),
267 case CHILD_PROP_PADDING:
268 gtk_box_set_child_packing (GTK_BOX (container),
272 g_value_get_uint (value),
275 case CHILD_PROP_PACK_TYPE:
276 gtk_box_set_child_packing (GTK_BOX (container),
281 g_value_get_enum (value));
283 case CHILD_PROP_POSITION:
284 gtk_box_reorder_child (GTK_BOX (container),
286 g_value_get_int (value));
289 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
295 gtk_box_get_child_property (GtkContainer *container,
304 GtkPackType pack_type = 0;
307 if (property_id != CHILD_PROP_POSITION)
308 gtk_box_query_child_packing (GTK_BOX (container),
317 case CHILD_PROP_EXPAND:
318 g_value_set_boolean (value, expand);
320 case CHILD_PROP_FILL:
321 g_value_set_boolean (value, fill);
323 case CHILD_PROP_PADDING:
324 g_value_set_uint (value, padding);
326 case CHILD_PROP_PACK_TYPE:
327 g_value_set_enum (value, pack_type);
329 case CHILD_PROP_POSITION:
331 for (list = GTK_BOX (container)->children; list; list = list->next)
333 GtkBoxChild *child_entry;
335 child_entry = list->data;
336 if (child_entry->widget == child)
340 g_value_set_int (value, list ? i : -1);
343 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
349 gtk_box_pack_start (GtkBox *box,
355 GtkBoxChild *child_info;
357 g_return_if_fail (GTK_IS_BOX (box));
358 g_return_if_fail (child != NULL);
359 g_return_if_fail (child->parent == NULL);
361 child_info = g_new (GtkBoxChild, 1);
362 child_info->widget = child;
363 child_info->padding = padding;
364 child_info->expand = expand ? TRUE : FALSE;
365 child_info->fill = fill ? TRUE : FALSE;
366 child_info->pack = GTK_PACK_START;
367 child_info->is_secondary = FALSE;
369 box->children = g_list_append (box->children, child_info);
371 gtk_widget_freeze_child_notify (child);
373 gtk_widget_set_parent (child, GTK_WIDGET (box));
375 gtk_widget_child_notify (child, "expand");
376 gtk_widget_child_notify (child, "fill");
377 gtk_widget_child_notify (child, "padding");
378 gtk_widget_child_notify (child, "pack_type");
379 gtk_widget_child_notify (child, "position");
380 gtk_widget_thaw_child_notify (child);
384 gtk_box_pack_end (GtkBox *box,
390 GtkBoxChild *child_info;
392 g_return_if_fail (GTK_IS_BOX (box));
393 g_return_if_fail (child != NULL);
394 g_return_if_fail (child->parent == NULL);
396 child_info = g_new (GtkBoxChild, 1);
397 child_info->widget = child;
398 child_info->padding = padding;
399 child_info->expand = expand ? TRUE : FALSE;
400 child_info->fill = fill ? TRUE : FALSE;
401 child_info->pack = GTK_PACK_END;
402 child_info->is_secondary = FALSE;
404 box->children = g_list_append (box->children, child_info);
406 gtk_widget_freeze_child_notify (child);
408 gtk_widget_set_parent (child, GTK_WIDGET (box));
410 gtk_widget_child_notify (child, "expand");
411 gtk_widget_child_notify (child, "fill");
412 gtk_widget_child_notify (child, "padding");
413 gtk_widget_child_notify (child, "pack_type");
414 gtk_widget_child_notify (child, "position");
415 gtk_widget_thaw_child_notify (child);
419 gtk_box_pack_start_defaults (GtkBox *box,
422 g_return_if_fail (GTK_IS_BOX (box));
423 g_return_if_fail (child != NULL);
425 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
429 gtk_box_pack_end_defaults (GtkBox *box,
432 g_return_if_fail (GTK_IS_BOX (box));
433 g_return_if_fail (child != NULL);
435 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
439 gtk_box_set_homogeneous (GtkBox *box,
440 gboolean homogeneous)
442 g_return_if_fail (GTK_IS_BOX (box));
444 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
446 box->homogeneous = homogeneous ? TRUE : FALSE;
447 g_object_notify (G_OBJECT (box), "homogeneous");
448 gtk_widget_queue_resize (GTK_WIDGET (box));
453 * gtk_box_get_homogeneous:
456 * Returns whether the box is homogeneous (all children are the
457 * same size). See gtk_box_set_homogeneous ().
459 * Return value: %TRUE if the box is homogeneous.
462 gtk_box_get_homogeneous (GtkBox *box)
464 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
466 return box->homogeneous;
470 gtk_box_set_spacing (GtkBox *box,
473 g_return_if_fail (GTK_IS_BOX (box));
475 if (spacing != box->spacing)
477 box->spacing = spacing;
478 g_object_notify (G_OBJECT (box), "spacing");
479 gtk_widget_queue_resize (GTK_WIDGET (box));
484 * gtk_box_get_spacing:
487 * Gets the value set by gtk_box_set_spacing().
489 * Return value: spacing between children
492 gtk_box_get_spacing (GtkBox *box)
494 g_return_val_if_fail (GTK_IS_BOX (box), 0);
500 gtk_box_reorder_child (GtkBox *box,
506 g_return_if_fail (GTK_IS_BOX (box));
507 g_return_if_fail (child != NULL);
509 list = box->children;
512 GtkBoxChild *child_info;
514 child_info = list->data;
515 if (child_info->widget == child)
521 if (list && box->children->next)
526 list->next->prev = list->prev;
528 list->prev->next = list->next;
530 box->children = list->next;
532 tmp_list = box->children;
533 while (position && tmp_list->next)
536 tmp_list = tmp_list->next;
541 tmp_list->next = list;
542 list->prev = tmp_list;
548 tmp_list->prev->next = list;
550 box->children = list;
551 list->prev = tmp_list->prev;
552 tmp_list->prev = list;
553 list->next = tmp_list;
556 gtk_widget_child_notify (child, "position");
557 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
558 gtk_widget_queue_resize (child);
563 gtk_box_query_child_packing (GtkBox *box,
568 GtkPackType *pack_type)
571 GtkBoxChild *child_info = NULL;
573 g_return_if_fail (GTK_IS_BOX (box));
574 g_return_if_fail (child != NULL);
576 list = box->children;
579 child_info = list->data;
580 if (child_info->widget == child)
589 *expand = child_info->expand;
591 *fill = child_info->fill;
593 *padding = child_info->padding;
595 *pack_type = child_info->pack;
600 gtk_box_set_child_packing (GtkBox *box,
605 GtkPackType pack_type)
608 GtkBoxChild *child_info = NULL;
610 g_return_if_fail (GTK_IS_BOX (box));
611 g_return_if_fail (child != NULL);
613 list = box->children;
616 child_info = list->data;
617 if (child_info->widget == child)
623 gtk_widget_freeze_child_notify (child);
626 child_info->expand = expand != FALSE;
627 gtk_widget_child_notify (child, "expand");
628 child_info->fill = fill != FALSE;
629 gtk_widget_child_notify (child, "fill");
630 child_info->padding = padding;
631 gtk_widget_child_notify (child, "padding");
632 if (pack_type == GTK_PACK_END)
633 child_info->pack = GTK_PACK_END;
635 child_info->pack = GTK_PACK_START;
636 gtk_widget_child_notify (child, "pack_type");
638 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
639 gtk_widget_queue_resize (child);
641 gtk_widget_thaw_child_notify (child);
645 gtk_box_add (GtkContainer *container,
648 g_return_if_fail (GTK_IS_BOX (container));
649 g_return_if_fail (widget != NULL);
651 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
655 gtk_box_remove (GtkContainer *container,
662 g_return_if_fail (GTK_IS_BOX (container));
663 g_return_if_fail (widget != NULL);
665 box = GTK_BOX (container);
667 children = box->children;
670 child = children->data;
672 if (child->widget == widget)
674 gboolean was_visible;
676 was_visible = GTK_WIDGET_VISIBLE (widget);
677 gtk_widget_unparent (widget);
679 box->children = g_list_remove_link (box->children, children);
680 g_list_free (children);
683 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
684 * since that's what is needed by toplevels.
687 gtk_widget_queue_resize (GTK_WIDGET (container));
692 children = children->next;
697 gtk_box_forall (GtkContainer *container,
698 gboolean include_internals,
699 GtkCallback callback,
700 gpointer callback_data)
706 g_return_if_fail (GTK_IS_BOX (container));
707 g_return_if_fail (callback != NULL);
709 box = GTK_BOX (container);
711 children = box->children;
714 child = children->data;
715 children = children->next;
717 if (child->pack == GTK_PACK_START)
718 (* callback) (child->widget, callback_data);
721 children = g_list_last (box->children);
724 child = children->data;
725 children = children->prev;
727 if (child->pack == GTK_PACK_END)
728 (* callback) (child->widget, callback_data);