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;
374 child_info->is_secondary = FALSE;
376 box->children = g_list_append (box->children, child_info);
378 gtk_widget_freeze_child_notify (child);
380 gtk_widget_set_parent (child, GTK_WIDGET (box));
382 if (GTK_WIDGET_REALIZED (box))
383 gtk_widget_realize (child);
385 if (GTK_WIDGET_VISIBLE (box) && GTK_WIDGET_VISIBLE (child))
387 if (GTK_WIDGET_MAPPED (box))
388 gtk_widget_map (child);
390 gtk_widget_queue_resize (child);
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 (box != NULL);
410 g_return_if_fail (GTK_IS_BOX (box));
411 g_return_if_fail (child != NULL);
412 g_return_if_fail (child->parent == NULL);
414 child_info = g_new (GtkBoxChild, 1);
415 child_info->widget = child;
416 child_info->padding = padding;
417 child_info->expand = expand ? TRUE : FALSE;
418 child_info->fill = fill ? TRUE : FALSE;
419 child_info->pack = GTK_PACK_END;
420 child_info->is_secondary = FALSE;
422 box->children = g_list_append (box->children, child_info);
424 gtk_widget_freeze_child_notify (child);
426 gtk_widget_set_parent (child, GTK_WIDGET (box));
428 if (GTK_WIDGET_REALIZED (box))
429 gtk_widget_realize (child);
431 if (GTK_WIDGET_VISIBLE (box) && GTK_WIDGET_VISIBLE (child))
433 if (GTK_WIDGET_MAPPED (box))
434 gtk_widget_map (child);
436 gtk_widget_queue_resize (child);
438 gtk_widget_child_notify (child, "expand");
439 gtk_widget_child_notify (child, "fill");
440 gtk_widget_child_notify (child, "padding");
441 gtk_widget_child_notify (child, "pack_type");
442 gtk_widget_child_notify (child, "position");
443 gtk_widget_thaw_child_notify (child);
447 gtk_box_pack_start_defaults (GtkBox *box,
450 g_return_if_fail (box != NULL);
451 g_return_if_fail (GTK_IS_BOX (box));
452 g_return_if_fail (child != NULL);
454 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
458 gtk_box_pack_end_defaults (GtkBox *box,
461 g_return_if_fail (box != NULL);
462 g_return_if_fail (GTK_IS_BOX (box));
463 g_return_if_fail (child != NULL);
465 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
469 gtk_box_set_homogeneous (GtkBox *box,
470 gboolean homogeneous)
472 g_return_if_fail (box != NULL);
473 g_return_if_fail (GTK_IS_BOX (box));
475 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
477 box->homogeneous = homogeneous ? TRUE : FALSE;
478 g_object_notify (G_OBJECT (box), "homogeneous");
479 gtk_widget_queue_resize (GTK_WIDGET (box));
484 * gtk_box_get_homogeneous:
487 * Returns whether the box is homogeneous (all children are the
488 * same size). See gtk_box_set_homogeneous ().
490 * Return value: %TRUE if the box is homogeneous.
493 gtk_box_get_homogeneous (GtkBox *box)
495 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
497 return box->homogeneous;
501 gtk_box_set_spacing (GtkBox *box,
504 g_return_if_fail (box != NULL);
505 g_return_if_fail (GTK_IS_BOX (box));
507 if (spacing != box->spacing)
509 box->spacing = spacing;
510 g_object_notify (G_OBJECT (box), "spacing");
511 gtk_widget_queue_resize (GTK_WIDGET (box));
516 * gtk_box_get_spacing:
519 * Gets the value set by gtk_box_set_spacing().
521 * Return value: spacing between children
524 gtk_box_get_spacing (GtkBox *box)
526 g_return_val_if_fail (GTK_IS_BOX (box), 0);
532 gtk_box_reorder_child (GtkBox *box,
538 g_return_if_fail (box != NULL);
539 g_return_if_fail (GTK_IS_BOX (box));
540 g_return_if_fail (child != NULL);
542 list = box->children;
545 GtkBoxChild *child_info;
547 child_info = list->data;
548 if (child_info->widget == child)
554 if (list && box->children->next)
559 list->next->prev = list->prev;
561 list->prev->next = list->next;
563 box->children = list->next;
565 tmp_list = box->children;
566 while (position && tmp_list->next)
569 tmp_list = tmp_list->next;
574 tmp_list->next = list;
575 list->prev = tmp_list;
581 tmp_list->prev->next = list;
583 box->children = list;
584 list->prev = tmp_list->prev;
585 tmp_list->prev = list;
586 list->next = tmp_list;
589 gtk_widget_child_notify (child, "position");
590 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
591 gtk_widget_queue_resize (child);
596 gtk_box_query_child_packing (GtkBox *box,
601 GtkPackType *pack_type)
604 GtkBoxChild *child_info = NULL;
606 g_return_if_fail (box != NULL);
607 g_return_if_fail (GTK_IS_BOX (box));
608 g_return_if_fail (child != NULL);
610 list = box->children;
613 child_info = list->data;
614 if (child_info->widget == child)
623 *expand = child_info->expand;
625 *fill = child_info->fill;
627 *padding = child_info->padding;
629 *pack_type = child_info->pack;
634 gtk_box_set_child_packing (GtkBox *box,
639 GtkPackType pack_type)
642 GtkBoxChild *child_info = NULL;
644 g_return_if_fail (box != NULL);
645 g_return_if_fail (GTK_IS_BOX (box));
646 g_return_if_fail (child != NULL);
648 list = box->children;
651 child_info = list->data;
652 if (child_info->widget == child)
658 gtk_widget_freeze_child_notify (child);
661 child_info->expand = expand != FALSE;
662 gtk_widget_child_notify (child, "expand");
663 child_info->fill = fill != FALSE;
664 gtk_widget_child_notify (child, "fill");
665 child_info->padding = padding;
666 gtk_widget_child_notify (child, "padding");
667 if (pack_type == GTK_PACK_END)
668 child_info->pack = GTK_PACK_END;
670 child_info->pack = GTK_PACK_START;
671 gtk_widget_child_notify (child, "pack_type");
673 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
674 gtk_widget_queue_resize (child);
676 gtk_widget_thaw_child_notify (child);
680 gtk_box_map (GtkWidget *widget)
686 g_return_if_fail (widget != NULL);
687 g_return_if_fail (GTK_IS_BOX (widget));
689 box = GTK_BOX (widget);
690 GTK_WIDGET_SET_FLAGS (box, GTK_MAPPED);
692 children = box->children;
695 child = children->data;
696 children = children->next;
698 if (GTK_WIDGET_VISIBLE (child->widget) &&
699 !GTK_WIDGET_MAPPED (child->widget))
700 gtk_widget_map (child->widget);
705 gtk_box_unmap (GtkWidget *widget)
711 g_return_if_fail (widget != NULL);
712 g_return_if_fail (GTK_IS_BOX (widget));
714 box = GTK_BOX (widget);
715 GTK_WIDGET_UNSET_FLAGS (box, GTK_MAPPED);
717 children = box->children;
720 child = children->data;
721 children = children->next;
723 if (GTK_WIDGET_VISIBLE (child->widget) &&
724 GTK_WIDGET_MAPPED (child->widget))
725 gtk_widget_unmap (child->widget);
730 gtk_box_add (GtkContainer *container,
733 g_return_if_fail (container != NULL);
734 g_return_if_fail (GTK_IS_BOX (container));
735 g_return_if_fail (widget != NULL);
737 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
741 gtk_box_remove (GtkContainer *container,
748 g_return_if_fail (container != NULL);
749 g_return_if_fail (GTK_IS_BOX (container));
750 g_return_if_fail (widget != NULL);
752 box = GTK_BOX (container);
754 children = box->children;
757 child = children->data;
759 if (child->widget == widget)
761 gboolean was_visible;
763 was_visible = GTK_WIDGET_VISIBLE (widget);
764 gtk_widget_unparent (widget);
766 box->children = g_list_remove_link (box->children, children);
767 g_list_free (children);
770 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
771 * since that's what is needed by toplevels.
774 gtk_widget_queue_resize (GTK_WIDGET (container));
779 children = children->next;
784 gtk_box_forall (GtkContainer *container,
785 gboolean include_internals,
786 GtkCallback callback,
787 gpointer callback_data)
793 g_return_if_fail (container != NULL);
794 g_return_if_fail (GTK_IS_BOX (container));
795 g_return_if_fail (callback != NULL);
797 box = GTK_BOX (container);
799 children = box->children;
802 child = children->data;
803 children = children->next;
805 if (child->pack == GTK_PACK_START)
806 (* callback) (child->widget, callback_data);
809 children = g_list_last (box->children);
812 child = children->data;
813 children = children->prev;
815 if (child->pack == GTK_PACK_END)
816 (* callback) (child->widget, callback_data);