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/.
30 static void gtk_hbox_class_init (GtkHBoxClass *klass);
31 static void gtk_hbox_init (GtkHBox *box);
32 static void gtk_hbox_size_request (GtkWidget *widget,
33 GtkRequisition *requisition);
34 static void gtk_hbox_size_allocate (GtkWidget *widget,
35 GtkAllocation *allocation);
39 gtk_hbox_get_type (void)
41 static GType hbox_type = 0;
45 static const GTypeInfo hbox_info =
47 sizeof (GtkHBoxClass),
49 NULL, /* base_finalize */
50 (GClassInitFunc) gtk_hbox_class_init,
51 NULL, /* class_finalize */
52 NULL, /* class_data */
55 (GInstanceInitFunc) gtk_hbox_init,
58 hbox_type = g_type_register_static (GTK_TYPE_BOX, "GtkHBox",
66 gtk_hbox_class_init (GtkHBoxClass *class)
68 GtkWidgetClass *widget_class;
70 widget_class = (GtkWidgetClass*) class;
72 widget_class->size_request = gtk_hbox_size_request;
73 widget_class->size_allocate = gtk_hbox_size_allocate;
77 gtk_hbox_init (GtkHBox *hbox)
82 gtk_hbox_new (gboolean homogeneous,
87 hbox = g_object_new (GTK_TYPE_HBOX, NULL);
89 GTK_BOX (hbox)->spacing = spacing;
90 GTK_BOX (hbox)->homogeneous = homogeneous ? TRUE : FALSE;
92 return GTK_WIDGET (hbox);
97 gtk_hbox_size_request (GtkWidget *widget,
98 GtkRequisition *requisition)
106 box = GTK_BOX (widget);
107 requisition->width = 0;
108 requisition->height = 0;
111 children = box->children;
114 child = children->data;
115 children = children->next;
117 if (GTK_WIDGET_VISIBLE (child->widget))
119 GtkRequisition child_requisition;
121 gtk_widget_size_request (child->widget, &child_requisition);
123 if (box->homogeneous)
125 width = child_requisition.width + child->padding * 2;
126 requisition->width = MAX (requisition->width, width);
130 requisition->width += child_requisition.width + child->padding * 2;
133 requisition->height = MAX (requisition->height, child_requisition.height);
139 if (nvis_children > 0)
141 if (box->homogeneous)
142 requisition->width *= nvis_children;
143 requisition->width += (nvis_children - 1) * box->spacing;
146 requisition->width += GTK_CONTAINER (box)->border_width * 2;
147 requisition->height += GTK_CONTAINER (box)->border_width * 2;
151 gtk_hbox_size_allocate (GtkWidget *widget,
152 GtkAllocation *allocation)
157 GtkAllocation child_allocation;
159 gint nexpand_children;
164 GtkTextDirection direction;
166 box = GTK_BOX (widget);
167 widget->allocation = *allocation;
169 direction = gtk_widget_get_direction (widget);
172 nexpand_children = 0;
173 children = box->children;
177 child = children->data;
178 children = children->next;
180 if (GTK_WIDGET_VISIBLE (child->widget))
184 nexpand_children += 1;
188 if (nvis_children > 0)
190 if (box->homogeneous)
192 width = (allocation->width -
193 GTK_CONTAINER (box)->border_width * 2 -
194 (nvis_children - 1) * box->spacing);
195 extra = width / nvis_children;
197 else if (nexpand_children > 0)
199 width = (gint) allocation->width - (gint) widget->requisition.width;
200 extra = width / nexpand_children;
208 x = allocation->x + GTK_CONTAINER (box)->border_width;
209 child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
210 child_allocation.height = MAX (1, (gint) allocation->height - (gint) GTK_CONTAINER (box)->border_width * 2);
212 children = box->children;
215 child = children->data;
216 children = children->next;
218 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
220 if (box->homogeneous)
222 if (nvis_children == 1)
232 GtkRequisition child_requisition;
234 gtk_widget_get_child_requisition (child->widget, &child_requisition);
236 child_width = child_requisition.width + child->padding * 2;
240 if (nexpand_children == 1)
241 child_width += width;
243 child_width += extra;
245 nexpand_children -= 1;
252 child_allocation.width = MAX (1, (gint) child_width - (gint) child->padding * 2);
253 child_allocation.x = x + child->padding;
257 GtkRequisition child_requisition;
259 gtk_widget_get_child_requisition (child->widget, &child_requisition);
260 child_allocation.width = child_requisition.width;
261 child_allocation.x = x + (child_width - child_allocation.width) / 2;
264 if (direction == GTK_TEXT_DIR_RTL)
265 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
267 gtk_widget_size_allocate (child->widget, &child_allocation);
269 x += child_width + box->spacing;
273 x = allocation->x + allocation->width - GTK_CONTAINER (box)->border_width;
275 children = box->children;
278 child = children->data;
279 children = children->next;
281 if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
283 GtkRequisition child_requisition;
284 gtk_widget_get_child_requisition (child->widget, &child_requisition);
286 if (box->homogeneous)
288 if (nvis_children == 1)
298 child_width = child_requisition.width + child->padding * 2;
302 if (nexpand_children == 1)
303 child_width += width;
305 child_width += extra;
307 nexpand_children -= 1;
314 child_allocation.width = MAX (1, (gint)child_width - (gint)child->padding * 2);
315 child_allocation.x = x + child->padding - child_width;
319 child_allocation.width = child_requisition.width;
320 child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
323 if (direction == GTK_TEXT_DIR_RTL)
324 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
326 gtk_widget_size_allocate (child->widget, &child_allocation);
328 x -= (child_width + box->spacing);