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 GType gtk_box_child_type (GtkContainer *container);
76 static GtkContainerClass *parent_class = NULL;
80 gtk_box_get_type (void)
82 static GType box_type = 0;
86 static const GTypeInfo box_info =
90 NULL, /* base_finalize */
91 (GClassInitFunc) gtk_box_class_init,
92 NULL, /* class_finalize */
93 NULL, /* class_data */
96 (GInstanceInitFunc) gtk_box_init,
97 NULL, /* value_table */
100 box_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkBox",
101 &box_info, G_TYPE_FLAG_ABSTRACT);
108 gtk_box_class_init (GtkBoxClass *class)
110 GObjectClass *gobject_class = G_OBJECT_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 container_class->add = gtk_box_add;
119 container_class->remove = gtk_box_remove;
120 container_class->forall = gtk_box_forall;
121 container_class->child_type = gtk_box_child_type;
122 container_class->set_child_property = gtk_box_set_child_property;
123 container_class->get_child_property = gtk_box_get_child_property;
125 g_object_class_install_property (gobject_class,
127 g_param_spec_int ("spacing",
129 _("The amount of space between children"),
133 G_PARAM_READABLE | G_PARAM_WRITABLE));
135 g_object_class_install_property (gobject_class,
137 g_param_spec_boolean ("homogeneous",
139 _("Whether the children should all be the same size"),
141 G_PARAM_READABLE | G_PARAM_WRITABLE));
143 gtk_container_class_install_child_property (container_class,
145 g_param_spec_boolean ("expand",
147 _("Whether the child should receive extra space when the parent grows"),
150 gtk_container_class_install_child_property (container_class,
152 g_param_spec_boolean ("fill",
154 _("Whether extra space given to the child should be allocated to the child or used as padding"),
157 gtk_container_class_install_child_property (container_class,
159 g_param_spec_uint ("padding",
161 _("Extra space to put between the child and its neighbors, in pixels"),
164 gtk_container_class_install_child_property (container_class,
165 CHILD_PROP_PACK_TYPE,
166 g_param_spec_enum ("pack_type",
168 _("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
169 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
171 gtk_container_class_install_child_property (container_class,
173 g_param_spec_int ("position",
175 _("The index of the child in the parent"),
181 gtk_box_init (GtkBox *box)
183 GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
184 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
186 box->children = NULL;
188 box->homogeneous = FALSE;
192 gtk_box_set_property (GObject *object,
199 box = GTK_BOX (object);
204 gtk_box_set_spacing (box, g_value_get_int (value));
206 case PROP_HOMOGENEOUS:
207 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 static void gtk_box_get_property (GObject *object,
222 box = GTK_BOX (object);
227 g_value_set_int (value, box->spacing);
229 case PROP_HOMOGENEOUS:
230 g_value_set_boolean (value, box->homogeneous);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 gtk_box_child_type (GtkContainer *container)
241 return GTK_TYPE_WIDGET;
245 gtk_box_set_child_property (GtkContainer *container,
254 GtkPackType pack_type = 0;
256 if (property_id != CHILD_PROP_POSITION)
257 gtk_box_query_child_packing (GTK_BOX (container),
265 case CHILD_PROP_EXPAND:
266 gtk_box_set_child_packing (GTK_BOX (container),
268 g_value_get_boolean (value),
273 case CHILD_PROP_FILL:
274 gtk_box_set_child_packing (GTK_BOX (container),
277 g_value_get_boolean (value),
281 case CHILD_PROP_PADDING:
282 gtk_box_set_child_packing (GTK_BOX (container),
286 g_value_get_uint (value),
289 case CHILD_PROP_PACK_TYPE:
290 gtk_box_set_child_packing (GTK_BOX (container),
295 g_value_get_enum (value));
297 case CHILD_PROP_POSITION:
298 gtk_box_reorder_child (GTK_BOX (container),
300 g_value_get_int (value));
303 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
309 gtk_box_get_child_property (GtkContainer *container,
318 GtkPackType pack_type = 0;
321 if (property_id != CHILD_PROP_POSITION)
322 gtk_box_query_child_packing (GTK_BOX (container),
331 case CHILD_PROP_EXPAND:
332 g_value_set_boolean (value, expand);
334 case CHILD_PROP_FILL:
335 g_value_set_boolean (value, fill);
337 case CHILD_PROP_PADDING:
338 g_value_set_uint (value, padding);
340 case CHILD_PROP_PACK_TYPE:
341 g_value_set_enum (value, pack_type);
343 case CHILD_PROP_POSITION:
345 for (list = GTK_BOX (container)->children; list; list = list->next)
347 GtkBoxChild *child_entry;
349 child_entry = list->data;
350 if (child_entry->widget == child)
354 g_value_set_int (value, list ? i : -1);
357 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
363 gtk_box_pack_start (GtkBox *box,
369 GtkBoxChild *child_info;
371 g_return_if_fail (GTK_IS_BOX (box));
372 g_return_if_fail (GTK_IS_WIDGET (child));
373 g_return_if_fail (child->parent == NULL);
375 child_info = g_new (GtkBoxChild, 1);
376 child_info->widget = child;
377 child_info->padding = padding;
378 child_info->expand = expand ? TRUE : FALSE;
379 child_info->fill = fill ? TRUE : FALSE;
380 child_info->pack = GTK_PACK_START;
381 child_info->is_secondary = FALSE;
383 box->children = g_list_append (box->children, child_info);
385 gtk_widget_freeze_child_notify (child);
387 gtk_widget_set_parent (child, GTK_WIDGET (box));
389 gtk_widget_child_notify (child, "expand");
390 gtk_widget_child_notify (child, "fill");
391 gtk_widget_child_notify (child, "padding");
392 gtk_widget_child_notify (child, "pack_type");
393 gtk_widget_child_notify (child, "position");
394 gtk_widget_thaw_child_notify (child);
398 gtk_box_pack_end (GtkBox *box,
404 GtkBoxChild *child_info;
406 g_return_if_fail (GTK_IS_BOX (box));
407 g_return_if_fail (GTK_IS_WIDGET (child));
408 g_return_if_fail (child->parent == NULL);
410 child_info = g_new (GtkBoxChild, 1);
411 child_info->widget = child;
412 child_info->padding = padding;
413 child_info->expand = expand ? TRUE : FALSE;
414 child_info->fill = fill ? TRUE : FALSE;
415 child_info->pack = GTK_PACK_END;
416 child_info->is_secondary = FALSE;
418 box->children = g_list_append (box->children, child_info);
420 gtk_widget_freeze_child_notify (child);
422 gtk_widget_set_parent (child, GTK_WIDGET (box));
424 gtk_widget_child_notify (child, "expand");
425 gtk_widget_child_notify (child, "fill");
426 gtk_widget_child_notify (child, "padding");
427 gtk_widget_child_notify (child, "pack_type");
428 gtk_widget_child_notify (child, "position");
429 gtk_widget_thaw_child_notify (child);
433 gtk_box_pack_start_defaults (GtkBox *box,
436 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
440 gtk_box_pack_end_defaults (GtkBox *box,
443 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
447 gtk_box_set_homogeneous (GtkBox *box,
448 gboolean homogeneous)
450 g_return_if_fail (GTK_IS_BOX (box));
452 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
454 box->homogeneous = homogeneous ? TRUE : FALSE;
455 g_object_notify (G_OBJECT (box), "homogeneous");
456 gtk_widget_queue_resize (GTK_WIDGET (box));
461 * gtk_box_get_homogeneous:
464 * Returns whether the box is homogeneous (all children are the
465 * same size). See gtk_box_set_homogeneous ().
467 * Return value: %TRUE if the box is homogeneous.
470 gtk_box_get_homogeneous (GtkBox *box)
472 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
474 return box->homogeneous;
478 gtk_box_set_spacing (GtkBox *box,
481 g_return_if_fail (GTK_IS_BOX (box));
483 if (spacing != box->spacing)
485 box->spacing = spacing;
486 g_object_notify (G_OBJECT (box), "spacing");
487 gtk_widget_queue_resize (GTK_WIDGET (box));
492 * gtk_box_get_spacing:
495 * Gets the value set by gtk_box_set_spacing().
497 * Return value: spacing between children
500 gtk_box_get_spacing (GtkBox *box)
502 g_return_val_if_fail (GTK_IS_BOX (box), 0);
508 gtk_box_reorder_child (GtkBox *box,
514 GtkBoxChild *child_info = NULL;
517 g_return_if_fail (GTK_IS_BOX (box));
518 g_return_if_fail (GTK_IS_WIDGET (child));
520 old_link = box->children;
524 child_info = old_link->data;
525 if (child_info->widget == child)
528 old_link = old_link->next;
532 g_return_if_fail (old_link != NULL);
534 if (position == old_position)
537 box->children = g_list_delete_link (box->children, old_link);
542 new_link = g_list_nth (box->children, position);
544 box->children = g_list_insert_before (box->children, new_link, child_info);
546 gtk_widget_child_notify (child, "position");
547 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
548 gtk_widget_queue_resize (child);
552 gtk_box_query_child_packing (GtkBox *box,
557 GtkPackType *pack_type)
560 GtkBoxChild *child_info = NULL;
562 g_return_if_fail (GTK_IS_BOX (box));
563 g_return_if_fail (GTK_IS_WIDGET (child));
565 list = box->children;
568 child_info = list->data;
569 if (child_info->widget == child)
578 *expand = child_info->expand;
580 *fill = child_info->fill;
582 *padding = child_info->padding;
584 *pack_type = child_info->pack;
589 gtk_box_set_child_packing (GtkBox *box,
594 GtkPackType pack_type)
597 GtkBoxChild *child_info = NULL;
599 g_return_if_fail (GTK_IS_BOX (box));
600 g_return_if_fail (GTK_IS_WIDGET (child));
602 list = box->children;
605 child_info = list->data;
606 if (child_info->widget == child)
612 gtk_widget_freeze_child_notify (child);
615 child_info->expand = expand != FALSE;
616 gtk_widget_child_notify (child, "expand");
617 child_info->fill = fill != FALSE;
618 gtk_widget_child_notify (child, "fill");
619 child_info->padding = padding;
620 gtk_widget_child_notify (child, "padding");
621 if (pack_type == GTK_PACK_END)
622 child_info->pack = GTK_PACK_END;
624 child_info->pack = GTK_PACK_START;
625 gtk_widget_child_notify (child, "pack_type");
627 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
628 gtk_widget_queue_resize (child);
630 gtk_widget_thaw_child_notify (child);
634 gtk_box_add (GtkContainer *container,
637 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
641 gtk_box_remove (GtkContainer *container,
648 box = GTK_BOX (container);
650 children = box->children;
653 child = children->data;
655 if (child->widget == widget)
657 gboolean was_visible;
659 was_visible = GTK_WIDGET_VISIBLE (widget);
660 gtk_widget_unparent (widget);
662 box->children = g_list_remove_link (box->children, children);
663 g_list_free (children);
666 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
667 * since that's what is needed by toplevels.
670 gtk_widget_queue_resize (GTK_WIDGET (container));
675 children = children->next;
680 gtk_box_forall (GtkContainer *container,
681 gboolean include_internals,
682 GtkCallback callback,
683 gpointer callback_data)
689 g_return_if_fail (callback != NULL);
691 box = GTK_BOX (container);
693 children = box->children;
696 child = children->data;
697 children = children->next;
699 if (child->pack == GTK_PACK_START)
700 (* callback) (child->widget, callback_data);
703 children = g_list_last (box->children);
706 child = children->data;
707 children = children->prev;
709 if (child->pack == GTK_PACK_END)
710 (* callback) (child->widget, callback_data);