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_map (GtkWidget *widget);
56 static void gtk_box_unmap (GtkWidget *widget);
57 static void gtk_box_add (GtkContainer *container,
59 static void gtk_box_remove (GtkContainer *container,
61 static void gtk_box_forall (GtkContainer *container,
62 gboolean include_internals,
64 gpointer callback_data);
65 static void gtk_box_set_child_property (GtkContainer *container,
70 static void gtk_box_get_child_property (GtkContainer *container,
75 static GtkType gtk_box_child_type (GtkContainer *container);
78 static GtkContainerClass *parent_class = NULL;
82 gtk_box_get_type (void)
84 static GtkType box_type = 0;
88 static const GtkTypeInfo box_info =
93 (GtkClassInitFunc) gtk_box_class_init,
94 (GtkObjectInitFunc) gtk_box_init,
95 /* reserved_1 */ NULL,
96 /* reserved_2 */ NULL,
97 (GtkClassInitFunc) NULL,
100 box_type = gtk_type_unique (GTK_TYPE_CONTAINER, &box_info);
107 gtk_box_class_init (GtkBoxClass *class)
109 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
110 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
111 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
113 parent_class = g_type_class_peek_parent (class);
115 gobject_class->set_property = gtk_box_set_property;
116 gobject_class->get_property = gtk_box_get_property;
118 widget_class->map = gtk_box_map;
119 widget_class->unmap = gtk_box_unmap;
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 _("The amount of space between children."),
136 G_PARAM_READABLE | G_PARAM_WRITABLE));
138 g_object_class_install_property (gobject_class,
140 g_param_spec_boolean ("homogeneous",
142 _("Whether the children should all be the same size."),
144 G_PARAM_READABLE | G_PARAM_WRITABLE));
146 gtk_container_class_install_child_property (container_class,
148 g_param_spec_boolean ("expand", NULL, NULL,
151 gtk_container_class_install_child_property (container_class,
153 g_param_spec_boolean ("fill", NULL, NULL,
156 gtk_container_class_install_child_property (container_class,
158 g_param_spec_uint ("padding", NULL, NULL,
161 gtk_container_class_install_child_property (container_class,
162 CHILD_PROP_PACK_TYPE,
163 g_param_spec_enum ("pack_type", NULL, NULL,
164 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
166 gtk_container_class_install_child_property (container_class,
168 g_param_spec_int ("position", NULL, NULL,
174 gtk_box_init (GtkBox *box)
176 GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
178 box->children = NULL;
180 box->homogeneous = FALSE;
184 gtk_box_set_property (GObject *object,
191 box = GTK_BOX (object);
196 gtk_box_set_spacing (box, g_value_get_int (value));
198 case PROP_HOMOGENEOUS:
199 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 static void gtk_box_get_property (GObject *object,
214 box = GTK_BOX (object);
219 g_value_set_int (value, box->spacing);
221 case PROP_HOMOGENEOUS:
222 g_value_set_boolean (value, box->homogeneous);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gtk_box_child_type (GtkContainer *container)
233 return GTK_TYPE_WIDGET;
237 gtk_box_set_child_property (GtkContainer *container,
246 GtkPackType pack_type = 0;
248 if (property_id != CHILD_PROP_POSITION)
249 gtk_box_query_child_packing (GTK_BOX (container),
257 case CHILD_PROP_EXPAND:
258 gtk_box_set_child_packing (GTK_BOX (container),
260 g_value_get_boolean (value),
265 case CHILD_PROP_FILL:
266 gtk_box_set_child_packing (GTK_BOX (container),
269 g_value_get_boolean (value),
273 case CHILD_PROP_PADDING:
274 gtk_box_set_child_packing (GTK_BOX (container),
278 g_value_get_uint (value),
281 case CHILD_PROP_PACK_TYPE:
282 gtk_box_set_child_packing (GTK_BOX (container),
287 g_value_get_enum (value));
289 case CHILD_PROP_POSITION:
290 gtk_box_reorder_child (GTK_BOX (container),
292 g_value_get_int (value));
295 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
301 gtk_box_get_child_property (GtkContainer *container,
310 GtkPackType pack_type = 0;
313 if (property_id != CHILD_PROP_POSITION)
314 gtk_box_query_child_packing (GTK_BOX (container),
323 case CHILD_PROP_EXPAND:
324 g_value_set_boolean (value, expand);
326 case CHILD_PROP_FILL:
327 g_value_set_boolean (value, fill);
329 case CHILD_PROP_PADDING:
330 g_value_set_uint (value, padding);
332 case CHILD_PROP_PACK_TYPE:
333 g_value_set_enum (value, pack_type);
335 case CHILD_PROP_POSITION:
337 for (list = GTK_BOX (container)->children; list; list = list->next)
339 GtkBoxChild *child_entry;
341 child_entry = list->data;
342 if (child_entry->widget == child)
346 g_value_set_int (value, list ? i : -1);
349 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
355 gtk_box_pack_start (GtkBox *box,
361 GtkBoxChild *child_info;
363 g_return_if_fail (box != NULL);
364 g_return_if_fail (GTK_IS_BOX (box));
365 g_return_if_fail (child != NULL);
366 g_return_if_fail (child->parent == NULL);
368 child_info = g_new (GtkBoxChild, 1);
369 child_info->widget = child;
370 child_info->padding = padding;
371 child_info->expand = expand ? TRUE : FALSE;
372 child_info->fill = fill ? TRUE : FALSE;
373 child_info->pack = GTK_PACK_START;
375 box->children = g_list_append (box->children, child_info);
377 gtk_widget_freeze_child_notify (child);
379 gtk_widget_set_parent (child, GTK_WIDGET (box));
381 if (GTK_WIDGET_REALIZED (box))
382 gtk_widget_realize (child);
384 if (GTK_WIDGET_VISIBLE (box) && GTK_WIDGET_VISIBLE (child))
386 if (GTK_WIDGET_MAPPED (box))
387 gtk_widget_map (child);
389 gtk_widget_queue_resize (child);
391 gtk_widget_child_notify (child, "expand");
392 gtk_widget_child_notify (child, "fill");
393 gtk_widget_child_notify (child, "padding");
394 gtk_widget_child_notify (child, "pack_type");
395 gtk_widget_child_notify (child, "position");
396 gtk_widget_thaw_child_notify (child);
400 gtk_box_pack_end (GtkBox *box,
406 GtkBoxChild *child_info;
408 g_return_if_fail (box != NULL);
409 g_return_if_fail (GTK_IS_BOX (box));
410 g_return_if_fail (child != NULL);
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;
420 box->children = g_list_append (box->children, child_info);
422 gtk_widget_freeze_child_notify (child);
424 gtk_widget_set_parent (child, GTK_WIDGET (box));
426 if (GTK_WIDGET_REALIZED (box))
427 gtk_widget_realize (child);
429 if (GTK_WIDGET_VISIBLE (box) && GTK_WIDGET_VISIBLE (child))
431 if (GTK_WIDGET_MAPPED (box))
432 gtk_widget_map (child);
434 gtk_widget_queue_resize (child);
436 gtk_widget_child_notify (child, "expand");
437 gtk_widget_child_notify (child, "fill");
438 gtk_widget_child_notify (child, "padding");
439 gtk_widget_child_notify (child, "pack_type");
440 gtk_widget_child_notify (child, "position");
441 gtk_widget_thaw_child_notify (child);
445 gtk_box_pack_start_defaults (GtkBox *box,
448 g_return_if_fail (box != NULL);
449 g_return_if_fail (GTK_IS_BOX (box));
450 g_return_if_fail (child != NULL);
452 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
456 gtk_box_pack_end_defaults (GtkBox *box,
459 g_return_if_fail (box != NULL);
460 g_return_if_fail (GTK_IS_BOX (box));
461 g_return_if_fail (child != NULL);
463 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
467 gtk_box_set_homogeneous (GtkBox *box,
468 gboolean homogeneous)
470 g_return_if_fail (box != NULL);
471 g_return_if_fail (GTK_IS_BOX (box));
473 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
475 box->homogeneous = homogeneous ? TRUE : FALSE;
476 g_object_notify (G_OBJECT (box), "homogeneous");
477 gtk_widget_queue_resize (GTK_WIDGET (box));
482 * gtk_box_get_homogeneous:
485 * Returns whether the box is homogeneous (all children are the
486 * same size). See gtk_box_set_homogeneous ().
488 * Return value: %TRUE if the box is homogeneous.
491 gtk_box_get_homogeneous (GtkBox *box)
493 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
495 return box->homogeneous;
499 gtk_box_set_spacing (GtkBox *box,
502 g_return_if_fail (box != NULL);
503 g_return_if_fail (GTK_IS_BOX (box));
505 if (spacing != box->spacing)
507 box->spacing = spacing;
508 g_object_notify (G_OBJECT (box), "spacing");
509 gtk_widget_queue_resize (GTK_WIDGET (box));
514 * gtk_box_get_spacing:
517 * Gets the value set by gtk_box_set_spacing().
519 * Return value: spacing between children
522 gtk_box_get_spacing (GtkBox *box)
524 g_return_val_if_fail (GTK_IS_BOX (box), 0);
530 gtk_box_reorder_child (GtkBox *box,
536 g_return_if_fail (box != NULL);
537 g_return_if_fail (GTK_IS_BOX (box));
538 g_return_if_fail (child != NULL);
540 list = box->children;
543 GtkBoxChild *child_info;
545 child_info = list->data;
546 if (child_info->widget == child)
552 if (list && box->children->next)
557 list->next->prev = list->prev;
559 list->prev->next = list->next;
561 box->children = list->next;
563 tmp_list = box->children;
564 while (position && tmp_list->next)
567 tmp_list = tmp_list->next;
572 tmp_list->next = list;
573 list->prev = tmp_list;
579 tmp_list->prev->next = list;
581 box->children = list;
582 list->prev = tmp_list->prev;
583 tmp_list->prev = list;
584 list->next = tmp_list;
587 gtk_widget_child_notify (child, "position");
588 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
589 gtk_widget_queue_resize (child);
594 gtk_box_query_child_packing (GtkBox *box,
599 GtkPackType *pack_type)
602 GtkBoxChild *child_info = NULL;
604 g_return_if_fail (box != NULL);
605 g_return_if_fail (GTK_IS_BOX (box));
606 g_return_if_fail (child != NULL);
608 list = box->children;
611 child_info = list->data;
612 if (child_info->widget == child)
621 *expand = child_info->expand;
623 *fill = child_info->fill;
625 *padding = child_info->padding;
627 *pack_type = child_info->pack;
632 gtk_box_set_child_packing (GtkBox *box,
637 GtkPackType pack_type)
640 GtkBoxChild *child_info = NULL;
642 g_return_if_fail (box != NULL);
643 g_return_if_fail (GTK_IS_BOX (box));
644 g_return_if_fail (child != NULL);
646 list = box->children;
649 child_info = list->data;
650 if (child_info->widget == child)
656 gtk_widget_freeze_child_notify (child);
659 child_info->expand = expand != FALSE;
660 gtk_widget_child_notify (child, "expand");
661 child_info->fill = fill != FALSE;
662 gtk_widget_child_notify (child, "fill");
663 child_info->padding = padding;
664 gtk_widget_child_notify (child, "padding");
665 if (pack_type == GTK_PACK_END)
666 child_info->pack = GTK_PACK_END;
668 child_info->pack = GTK_PACK_START;
669 gtk_widget_child_notify (child, "pack_type");
671 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
672 gtk_widget_queue_resize (child);
674 gtk_widget_thaw_child_notify (child);
678 gtk_box_map (GtkWidget *widget)
684 g_return_if_fail (widget != NULL);
685 g_return_if_fail (GTK_IS_BOX (widget));
687 box = GTK_BOX (widget);
688 GTK_WIDGET_SET_FLAGS (box, GTK_MAPPED);
690 children = box->children;
693 child = children->data;
694 children = children->next;
696 if (GTK_WIDGET_VISIBLE (child->widget) &&
697 !GTK_WIDGET_MAPPED (child->widget))
698 gtk_widget_map (child->widget);
703 gtk_box_unmap (GtkWidget *widget)
709 g_return_if_fail (widget != NULL);
710 g_return_if_fail (GTK_IS_BOX (widget));
712 box = GTK_BOX (widget);
713 GTK_WIDGET_UNSET_FLAGS (box, GTK_MAPPED);
715 children = box->children;
718 child = children->data;
719 children = children->next;
721 if (GTK_WIDGET_VISIBLE (child->widget) &&
722 GTK_WIDGET_MAPPED (child->widget))
723 gtk_widget_unmap (child->widget);
728 gtk_box_add (GtkContainer *container,
731 g_return_if_fail (container != NULL);
732 g_return_if_fail (GTK_IS_BOX (container));
733 g_return_if_fail (widget != NULL);
735 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
739 gtk_box_remove (GtkContainer *container,
746 g_return_if_fail (container != NULL);
747 g_return_if_fail (GTK_IS_BOX (container));
748 g_return_if_fail (widget != NULL);
750 box = GTK_BOX (container);
752 children = box->children;
755 child = children->data;
757 if (child->widget == widget)
759 gboolean was_visible;
761 was_visible = GTK_WIDGET_VISIBLE (widget);
762 gtk_widget_unparent (widget);
764 box->children = g_list_remove_link (box->children, children);
765 g_list_free (children);
768 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
769 * since that's what is needed by toplevels.
772 gtk_widget_queue_resize (GTK_WIDGET (container));
777 children = children->next;
782 gtk_box_forall (GtkContainer *container,
783 gboolean include_internals,
784 GtkCallback callback,
785 gpointer callback_data)
791 g_return_if_fail (container != NULL);
792 g_return_if_fail (GTK_IS_BOX (container));
793 g_return_if_fail (callback != NULL);
795 box = GTK_BOX (container);
797 children = box->children;
800 child = children->data;
801 children = children->next;
803 if (child->pack == GTK_PACK_START)
804 (* callback) (child->widget, callback_data);
807 children = g_list_last (box->children);
810 child = children->data;
811 children = children->prev;
813 if (child->pack == GTK_PACK_END)
814 (* callback) (child->widget, callback_data);