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);
171 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
173 box->children = NULL;
175 box->homogeneous = FALSE;
179 gtk_box_set_property (GObject *object,
186 box = GTK_BOX (object);
191 gtk_box_set_spacing (box, g_value_get_int (value));
193 case PROP_HOMOGENEOUS:
194 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 static void gtk_box_get_property (GObject *object,
209 box = GTK_BOX (object);
214 g_value_set_int (value, box->spacing);
216 case PROP_HOMOGENEOUS:
217 g_value_set_boolean (value, box->homogeneous);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gtk_box_child_type (GtkContainer *container)
228 return GTK_TYPE_WIDGET;
232 gtk_box_set_child_property (GtkContainer *container,
241 GtkPackType pack_type = 0;
243 if (property_id != CHILD_PROP_POSITION)
244 gtk_box_query_child_packing (GTK_BOX (container),
252 case CHILD_PROP_EXPAND:
253 gtk_box_set_child_packing (GTK_BOX (container),
255 g_value_get_boolean (value),
260 case CHILD_PROP_FILL:
261 gtk_box_set_child_packing (GTK_BOX (container),
264 g_value_get_boolean (value),
268 case CHILD_PROP_PADDING:
269 gtk_box_set_child_packing (GTK_BOX (container),
273 g_value_get_uint (value),
276 case CHILD_PROP_PACK_TYPE:
277 gtk_box_set_child_packing (GTK_BOX (container),
282 g_value_get_enum (value));
284 case CHILD_PROP_POSITION:
285 gtk_box_reorder_child (GTK_BOX (container),
287 g_value_get_int (value));
290 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
296 gtk_box_get_child_property (GtkContainer *container,
305 GtkPackType pack_type = 0;
308 if (property_id != CHILD_PROP_POSITION)
309 gtk_box_query_child_packing (GTK_BOX (container),
318 case CHILD_PROP_EXPAND:
319 g_value_set_boolean (value, expand);
321 case CHILD_PROP_FILL:
322 g_value_set_boolean (value, fill);
324 case CHILD_PROP_PADDING:
325 g_value_set_uint (value, padding);
327 case CHILD_PROP_PACK_TYPE:
328 g_value_set_enum (value, pack_type);
330 case CHILD_PROP_POSITION:
332 for (list = GTK_BOX (container)->children; list; list = list->next)
334 GtkBoxChild *child_entry;
336 child_entry = list->data;
337 if (child_entry->widget == child)
341 g_value_set_int (value, list ? i : -1);
344 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
350 gtk_box_pack_start (GtkBox *box,
356 GtkBoxChild *child_info;
358 g_return_if_fail (GTK_IS_BOX (box));
359 g_return_if_fail (GTK_IS_WIDGET (child));
360 g_return_if_fail (child->parent == NULL);
362 child_info = g_new (GtkBoxChild, 1);
363 child_info->widget = child;
364 child_info->padding = padding;
365 child_info->expand = expand ? TRUE : FALSE;
366 child_info->fill = fill ? TRUE : FALSE;
367 child_info->pack = GTK_PACK_START;
368 child_info->is_secondary = FALSE;
370 box->children = g_list_append (box->children, child_info);
372 gtk_widget_freeze_child_notify (child);
374 gtk_widget_set_parent (child, GTK_WIDGET (box));
376 gtk_widget_child_notify (child, "expand");
377 gtk_widget_child_notify (child, "fill");
378 gtk_widget_child_notify (child, "padding");
379 gtk_widget_child_notify (child, "pack_type");
380 gtk_widget_child_notify (child, "position");
381 gtk_widget_thaw_child_notify (child);
385 gtk_box_pack_end (GtkBox *box,
391 GtkBoxChild *child_info;
393 g_return_if_fail (GTK_IS_BOX (box));
394 g_return_if_fail (GTK_IS_WIDGET (child));
395 g_return_if_fail (child->parent == NULL);
397 child_info = g_new (GtkBoxChild, 1);
398 child_info->widget = child;
399 child_info->padding = padding;
400 child_info->expand = expand ? TRUE : FALSE;
401 child_info->fill = fill ? TRUE : FALSE;
402 child_info->pack = GTK_PACK_END;
403 child_info->is_secondary = FALSE;
405 box->children = g_list_append (box->children, child_info);
407 gtk_widget_freeze_child_notify (child);
409 gtk_widget_set_parent (child, GTK_WIDGET (box));
411 gtk_widget_child_notify (child, "expand");
412 gtk_widget_child_notify (child, "fill");
413 gtk_widget_child_notify (child, "padding");
414 gtk_widget_child_notify (child, "pack_type");
415 gtk_widget_child_notify (child, "position");
416 gtk_widget_thaw_child_notify (child);
420 gtk_box_pack_start_defaults (GtkBox *box,
423 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
427 gtk_box_pack_end_defaults (GtkBox *box,
430 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
434 gtk_box_set_homogeneous (GtkBox *box,
435 gboolean homogeneous)
437 g_return_if_fail (GTK_IS_BOX (box));
439 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
441 box->homogeneous = homogeneous ? TRUE : FALSE;
442 g_object_notify (G_OBJECT (box), "homogeneous");
443 gtk_widget_queue_resize (GTK_WIDGET (box));
448 * gtk_box_get_homogeneous:
451 * Returns whether the box is homogeneous (all children are the
452 * same size). See gtk_box_set_homogeneous ().
454 * Return value: %TRUE if the box is homogeneous.
457 gtk_box_get_homogeneous (GtkBox *box)
459 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
461 return box->homogeneous;
465 gtk_box_set_spacing (GtkBox *box,
468 g_return_if_fail (GTK_IS_BOX (box));
470 if (spacing != box->spacing)
472 box->spacing = spacing;
473 g_object_notify (G_OBJECT (box), "spacing");
474 gtk_widget_queue_resize (GTK_WIDGET (box));
479 * gtk_box_get_spacing:
482 * Gets the value set by gtk_box_set_spacing().
484 * Return value: spacing between children
487 gtk_box_get_spacing (GtkBox *box)
489 g_return_val_if_fail (GTK_IS_BOX (box), 0);
495 gtk_box_reorder_child (GtkBox *box,
501 g_return_if_fail (GTK_IS_BOX (box));
502 g_return_if_fail (GTK_IS_WIDGET (child));
504 list = box->children;
507 GtkBoxChild *child_info;
509 child_info = list->data;
510 if (child_info->widget == child)
516 if (list && box->children->next)
521 list->next->prev = list->prev;
523 list->prev->next = list->next;
525 box->children = list->next;
527 tmp_list = box->children;
528 while (position && tmp_list->next)
531 tmp_list = tmp_list->next;
536 tmp_list->next = list;
537 list->prev = tmp_list;
543 tmp_list->prev->next = list;
545 box->children = list;
546 list->prev = tmp_list->prev;
547 tmp_list->prev = list;
548 list->next = tmp_list;
551 gtk_widget_child_notify (child, "position");
552 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
553 gtk_widget_queue_resize (child);
558 gtk_box_query_child_packing (GtkBox *box,
563 GtkPackType *pack_type)
566 GtkBoxChild *child_info = NULL;
568 g_return_if_fail (GTK_IS_BOX (box));
569 g_return_if_fail (GTK_IS_WIDGET (child));
571 list = box->children;
574 child_info = list->data;
575 if (child_info->widget == child)
584 *expand = child_info->expand;
586 *fill = child_info->fill;
588 *padding = child_info->padding;
590 *pack_type = child_info->pack;
595 gtk_box_set_child_packing (GtkBox *box,
600 GtkPackType pack_type)
603 GtkBoxChild *child_info = NULL;
605 g_return_if_fail (GTK_IS_BOX (box));
606 g_return_if_fail (GTK_IS_WIDGET (child));
608 list = box->children;
611 child_info = list->data;
612 if (child_info->widget == child)
618 gtk_widget_freeze_child_notify (child);
621 child_info->expand = expand != FALSE;
622 gtk_widget_child_notify (child, "expand");
623 child_info->fill = fill != FALSE;
624 gtk_widget_child_notify (child, "fill");
625 child_info->padding = padding;
626 gtk_widget_child_notify (child, "padding");
627 if (pack_type == GTK_PACK_END)
628 child_info->pack = GTK_PACK_END;
630 child_info->pack = GTK_PACK_START;
631 gtk_widget_child_notify (child, "pack_type");
633 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
634 gtk_widget_queue_resize (child);
636 gtk_widget_thaw_child_notify (child);
640 gtk_box_add (GtkContainer *container,
643 gtk_box_pack_start_defaults (GTK_BOX (container), widget);
647 gtk_box_remove (GtkContainer *container,
654 box = GTK_BOX (container);
656 children = box->children;
659 child = children->data;
661 if (child->widget == widget)
663 gboolean was_visible;
665 was_visible = GTK_WIDGET_VISIBLE (widget);
666 gtk_widget_unparent (widget);
668 box->children = g_list_remove_link (box->children, children);
669 g_list_free (children);
672 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
673 * since that's what is needed by toplevels.
676 gtk_widget_queue_resize (GTK_WIDGET (container));
681 children = children->next;
686 gtk_box_forall (GtkContainer *container,
687 gboolean include_internals,
688 GtkCallback callback,
689 gpointer callback_data)
695 g_return_if_fail (callback != NULL);
697 box = GTK_BOX (container);
699 children = box->children;
702 child = children->data;
703 children = children->next;
705 if (child->pack == GTK_PACK_START)
706 (* callback) (child->widget, callback_data);
709 children = g_list_last (box->children);
712 child = children->data;
713 children = children->prev;
715 if (child->pack == GTK_PACK_END)
716 (* callback) (child->widget, callback_data);