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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
30 static void gtk_vbox_class_init (GtkVBoxClass *klass);
31 static void gtk_vbox_init (GtkVBox *box);
32 static void gtk_vbox_size_request (GtkWidget *widget,
33 GtkRequisition *requisition);
34 static void gtk_vbox_size_allocate (GtkWidget *widget,
35 GtkAllocation *allocation);
39 gtk_vbox_get_type (void)
41 static GtkType vbox_type = 0;
45 static const GtkTypeInfo vbox_info =
49 sizeof (GtkVBoxClass),
50 (GtkClassInitFunc) gtk_vbox_class_init,
51 (GtkObjectInitFunc) gtk_vbox_init,
52 /* reserved_1 */ NULL,
53 /* reserved_2 */ NULL,
54 (GtkClassInitFunc) NULL,
57 vbox_type = gtk_type_unique (GTK_TYPE_BOX, &vbox_info);
64 gtk_vbox_class_init (GtkVBoxClass *class)
66 GtkWidgetClass *widget_class;
68 widget_class = (GtkWidgetClass*) class;
70 widget_class->size_request = gtk_vbox_size_request;
71 widget_class->size_allocate = gtk_vbox_size_allocate;
75 gtk_vbox_init (GtkVBox *vbox)
80 gtk_vbox_new (gboolean homogeneous,
85 vbox = gtk_type_new (gtk_vbox_get_type ());
87 GTK_BOX (vbox)->spacing = spacing;
88 GTK_BOX (vbox)->homogeneous = homogeneous ? TRUE : FALSE;
90 return GTK_WIDGET (vbox);
95 gtk_vbox_size_request (GtkWidget *widget,
96 GtkRequisition *requisition)
100 GtkRequisition child_requisition;
105 g_return_if_fail (widget != NULL);
106 g_return_if_fail (GTK_IS_VBOX (widget));
107 g_return_if_fail (requisition != NULL);
109 box = GTK_BOX (widget);
110 requisition->width = 0;
111 requisition->height = 0;
114 children = box->children;
117 child = children->data;
118 children = children->next;
120 if (GTK_WIDGET_VISIBLE (child->widget))
122 gtk_widget_size_request (child->widget, &child_requisition);
124 if (box->homogeneous)
126 height = child_requisition.height + child->padding * 2;
127 requisition->height = MAX (requisition->height, height);
131 requisition->height += child_requisition.height + child->padding * 2;
134 requisition->width = MAX (requisition->width, child_requisition.width);
140 if (nvis_children > 0)
142 if (box->homogeneous)
143 requisition->height *= nvis_children;
144 requisition->height += (nvis_children - 1) * box->spacing;
147 requisition->width += GTK_CONTAINER (box)->border_width * 2;
148 requisition->height += GTK_CONTAINER (box)->border_width * 2;
152 gtk_vbox_size_allocate (GtkWidget *widget,
153 GtkAllocation *allocation)
158 GtkAllocation child_allocation;
160 gint nexpand_children;
166 g_return_if_fail (widget != NULL);
167 g_return_if_fail (GTK_IS_VBOX (widget));
168 g_return_if_fail (allocation != NULL);
170 box = GTK_BOX (widget);
171 widget->allocation = *allocation;
174 nexpand_children = 0;
175 children = box->children;
179 child = children->data;
180 children = children->next;
182 if (GTK_WIDGET_VISIBLE (child->widget))
186 nexpand_children += 1;
190 if (nvis_children > 0)
192 if (box->homogeneous)
194 height = (allocation->height -
195 GTK_CONTAINER (box)->border_width * 2 -
196 (nvis_children - 1) * box->spacing);
197 extra = height / nvis_children;
199 else if (nexpand_children > 0)
201 height = (gint) allocation->height - (gint) widget->requisition.height;
202 extra = height / nexpand_children;
210 y = allocation->y + GTK_CONTAINER (box)->border_width;
211 child_allocation.x = allocation->x + GTK_CONTAINER (box)->border_width;
212 child_allocation.width = MAX (1, (gint) allocation->width - (gint) GTK_CONTAINER (box)->border_width * 2);
214 children = box->children;
217 child = children->data;
218 children = children->next;
220 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
222 if (box->homogeneous)
224 if (nvis_children == 1)
225 child_height = height;
227 child_height = extra;
234 GtkRequisition child_requisition;
236 gtk_widget_get_child_requisition (child->widget, &child_requisition);
237 child_height = child_requisition.height + child->padding * 2;
241 if (nexpand_children == 1)
242 child_height += height;
244 child_height += extra;
246 nexpand_children -= 1;
253 child_allocation.height = MAX (1, child_height - (gint)child->padding * 2);
254 child_allocation.y = y + child->padding;
258 GtkRequisition child_requisition;
260 gtk_widget_get_child_requisition (child->widget, &child_requisition);
261 child_allocation.height = child_requisition.height;
262 child_allocation.y = y + (child_height - child_allocation.height) / 2;
265 gtk_widget_size_allocate (child->widget, &child_allocation);
267 y += child_height + box->spacing;
271 y = allocation->y + allocation->height - GTK_CONTAINER (box)->border_width;
273 children = box->children;
276 child = children->data;
277 children = children->next;
279 if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
281 GtkRequisition child_requisition;
282 gtk_widget_get_child_requisition (child->widget, &child_requisition);
284 if (box->homogeneous)
286 if (nvis_children == 1)
287 child_height = height;
289 child_height = extra;
296 child_height = child_requisition.height + child->padding * 2;
300 if (nexpand_children == 1)
301 child_height += height;
303 child_height += extra;
305 nexpand_children -= 1;
312 child_allocation.height = MAX (1, child_height - (gint)child->padding * 2);
313 child_allocation.y = y + child->padding - child_height;
317 child_allocation.height = child_requisition.height;
318 child_allocation.y = y + (child_height - child_allocation.height) / 2 - child_height;
321 gtk_widget_size_allocate (child->widget, &child_allocation);
323 y -= (child_height + box->spacing);