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.
22 static void gtk_hbox_class_init (GtkHBoxClass *klass);
23 static void gtk_hbox_init (GtkHBox *box);
24 static void gtk_hbox_size_request (GtkWidget *widget,
25 GtkRequisition *requisition);
26 static void gtk_hbox_size_allocate (GtkWidget *widget,
27 GtkAllocation *allocation);
31 gtk_hbox_get_type (void)
33 static GtkType hbox_type = 0;
37 GtkTypeInfo hbox_info =
41 sizeof (GtkHBoxClass),
42 (GtkClassInitFunc) gtk_hbox_class_init,
43 (GtkObjectInitFunc) gtk_hbox_init,
44 /* reserved_1 */ NULL,
45 /* reserved_2 */ NULL,
46 (GtkClassInitFunc) NULL,
49 hbox_type = gtk_type_unique (GTK_TYPE_BOX, &hbox_info);
56 gtk_hbox_class_init (GtkHBoxClass *class)
58 GtkWidgetClass *widget_class;
60 widget_class = (GtkWidgetClass*) class;
62 widget_class->size_request = gtk_hbox_size_request;
63 widget_class->size_allocate = gtk_hbox_size_allocate;
67 gtk_hbox_init (GtkHBox *hbox)
72 gtk_hbox_new (gint homogeneous,
77 hbox = gtk_type_new (gtk_hbox_get_type ());
79 GTK_BOX (hbox)->spacing = spacing;
80 GTK_BOX (hbox)->homogeneous = homogeneous ? TRUE : FALSE;
82 return GTK_WIDGET (hbox);
87 gtk_hbox_size_request (GtkWidget *widget,
88 GtkRequisition *requisition)
96 g_return_if_fail (widget != NULL);
97 g_return_if_fail (GTK_IS_HBOX (widget));
98 g_return_if_fail (requisition != NULL);
100 box = GTK_BOX (widget);
101 requisition->width = 0;
102 requisition->height = 0;
105 children = box->children;
108 child = children->data;
109 children = children->next;
111 if (GTK_WIDGET_VISIBLE (child->widget))
113 gtk_widget_size_request (child->widget, &child->widget->requisition);
115 if (box->homogeneous)
117 width = child->widget->requisition.width + child->padding * 2;
118 requisition->width = MAX (requisition->width, width);
122 requisition->width += child->widget->requisition.width + child->padding * 2;
125 requisition->height = MAX (requisition->height, child->widget->requisition.height);
131 if (nvis_children > 0)
133 if (box->homogeneous)
134 requisition->width *= nvis_children;
135 requisition->width += (nvis_children - 1) * box->spacing;
138 requisition->width += GTK_CONTAINER (box)->border_width * 2;
139 requisition->height += GTK_CONTAINER (box)->border_width * 2;
143 gtk_hbox_size_allocate (GtkWidget *widget,
144 GtkAllocation *allocation)
149 GtkAllocation child_allocation;
151 gint nexpand_children;
157 g_return_if_fail (widget != NULL);
158 g_return_if_fail (GTK_IS_HBOX (widget));
159 g_return_if_fail (allocation != NULL);
161 box = GTK_BOX (widget);
162 widget->allocation = *allocation;
165 nexpand_children = 0;
166 children = box->children;
170 child = children->data;
171 children = children->next;
173 if (GTK_WIDGET_VISIBLE (child->widget))
177 nexpand_children += 1;
181 if (nvis_children > 0)
183 if (box->homogeneous)
185 width = (allocation->width -
186 GTK_CONTAINER (box)->border_width * 2 -
187 (nvis_children - 1) * box->spacing);
188 extra = width / nvis_children;
190 else if (nexpand_children > 0)
192 width = (gint)allocation->width - (gint)widget->requisition.width;
193 extra = width / nexpand_children;
201 x = allocation->x + GTK_CONTAINER (box)->border_width;
202 child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
203 child_allocation.height = MAX (1, allocation->height - GTK_CONTAINER (box)->border_width * 2);
205 children = box->children;
208 child = children->data;
209 children = children->next;
211 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
213 if (box->homogeneous)
215 if (nvis_children == 1)
225 child_width = child->widget->requisition.width + child->padding * 2;
229 if (nexpand_children == 1)
230 child_width += width;
232 child_width += extra;
234 nexpand_children -= 1;
241 child_allocation.width = MAX (1, child_width - child->padding * 2);
242 child_allocation.x = x + child->padding;
246 child_allocation.width = child->widget->requisition.width;
247 child_allocation.x = x + (child_width - child_allocation.width) / 2;
250 gtk_widget_size_allocate (child->widget, &child_allocation);
252 x += child_width + box->spacing;
256 x = allocation->x + allocation->width - GTK_CONTAINER (box)->border_width;
258 children = box->children;
261 child = children->data;
262 children = children->next;
264 if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
266 if (box->homogeneous)
268 if (nvis_children == 1)
278 child_width = child->widget->requisition.width + child->padding * 2;
282 if (nexpand_children == 1)
283 child_width += width;
285 child_width += extra;
287 nexpand_children -= 1;
294 child_allocation.width = MAX (1, child_width - child->padding * 2);
295 child_allocation.x = x + child->padding - child_width;
299 child_allocation.width = child->widget->requisition.width;
300 child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
303 gtk_widget_size_allocate (child->widget, &child_allocation);
305 x -= (child_width + box->spacing);