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/.
31 static void gtk_hbox_class_init (GtkHBoxClass *klass);
32 static void gtk_hbox_init (GtkHBox *box);
33 static void gtk_hbox_size_request (GtkWidget *widget,
34 GtkRequisition *requisition);
35 static void gtk_hbox_size_allocate (GtkWidget *widget,
36 GtkAllocation *allocation);
40 gtk_hbox_get_type (void)
42 static GType hbox_type = 0;
46 static const GTypeInfo hbox_info =
48 sizeof (GtkHBoxClass),
50 NULL, /* base_finalize */
51 (GClassInitFunc) gtk_hbox_class_init,
52 NULL, /* class_finalize */
53 NULL, /* class_data */
56 (GInstanceInitFunc) gtk_hbox_init,
59 hbox_type = g_type_register_static (GTK_TYPE_BOX, "GtkHBox",
67 gtk_hbox_class_init (GtkHBoxClass *class)
69 GtkWidgetClass *widget_class;
71 widget_class = (GtkWidgetClass*) class;
73 widget_class->size_request = gtk_hbox_size_request;
74 widget_class->size_allocate = gtk_hbox_size_allocate;
78 gtk_hbox_init (GtkHBox *hbox)
83 gtk_hbox_new (gboolean homogeneous,
88 hbox = g_object_new (GTK_TYPE_HBOX, NULL);
90 GTK_BOX (hbox)->spacing = spacing;
91 GTK_BOX (hbox)->homogeneous = homogeneous ? TRUE : FALSE;
93 return GTK_WIDGET (hbox);
98 gtk_hbox_size_request (GtkWidget *widget,
99 GtkRequisition *requisition)
107 box = GTK_BOX (widget);
108 requisition->width = 0;
109 requisition->height = 0;
112 children = box->children;
115 child = children->data;
116 children = children->next;
118 if (GTK_WIDGET_VISIBLE (child->widget))
120 GtkRequisition child_requisition;
122 gtk_widget_size_request (child->widget, &child_requisition);
124 if (box->homogeneous)
126 width = child_requisition.width + child->padding * 2;
127 requisition->width = MAX (requisition->width, width);
131 requisition->width += child_requisition.width + child->padding * 2;
134 requisition->height = MAX (requisition->height, child_requisition.height);
140 if (nvis_children > 0)
142 if (box->homogeneous)
143 requisition->width *= nvis_children;
144 requisition->width += (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_hbox_size_allocate (GtkWidget *widget,
153 GtkAllocation *allocation)
158 GtkAllocation child_allocation;
160 gint nexpand_children;
165 GtkTextDirection direction;
167 box = GTK_BOX (widget);
168 widget->allocation = *allocation;
170 direction = gtk_widget_get_direction (widget);
173 nexpand_children = 0;
174 children = box->children;
178 child = children->data;
179 children = children->next;
181 if (GTK_WIDGET_VISIBLE (child->widget))
185 nexpand_children += 1;
189 if (nvis_children > 0)
191 if (box->homogeneous)
193 width = (allocation->width -
194 GTK_CONTAINER (box)->border_width * 2 -
195 (nvis_children - 1) * box->spacing);
196 extra = width / nvis_children;
198 else if (nexpand_children > 0)
200 width = (gint) allocation->width - (gint) widget->requisition.width;
201 extra = width / nexpand_children;
209 x = allocation->x + GTK_CONTAINER (box)->border_width;
210 child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
211 child_allocation.height = MAX (1, (gint) allocation->height - (gint) GTK_CONTAINER (box)->border_width * 2);
213 children = box->children;
216 child = children->data;
217 children = children->next;
219 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
221 if (box->homogeneous)
223 if (nvis_children == 1)
233 GtkRequisition child_requisition;
235 gtk_widget_get_child_requisition (child->widget, &child_requisition);
237 child_width = child_requisition.width + child->padding * 2;
241 if (nexpand_children == 1)
242 child_width += width;
244 child_width += extra;
246 nexpand_children -= 1;
253 child_allocation.width = MAX (1, (gint) child_width - (gint) child->padding * 2);
254 child_allocation.x = x + child->padding;
258 GtkRequisition child_requisition;
260 gtk_widget_get_child_requisition (child->widget, &child_requisition);
261 child_allocation.width = child_requisition.width;
262 child_allocation.x = x + (child_width - child_allocation.width) / 2;
265 if (direction == GTK_TEXT_DIR_RTL)
266 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
268 gtk_widget_size_allocate (child->widget, &child_allocation);
270 x += child_width + box->spacing;
274 x = allocation->x + allocation->width - GTK_CONTAINER (box)->border_width;
276 children = box->children;
279 child = children->data;
280 children = children->next;
282 if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
284 GtkRequisition child_requisition;
285 gtk_widget_get_child_requisition (child->widget, &child_requisition);
287 if (box->homogeneous)
289 if (nvis_children == 1)
299 child_width = child_requisition.width + child->padding * 2;
303 if (nexpand_children == 1)
304 child_width += width;
306 child_width += extra;
308 nexpand_children -= 1;
315 child_allocation.width = MAX (1, (gint)child_width - (gint)child->padding * 2);
316 child_allocation.x = x + child->padding - child_width;
320 child_allocation.width = child_requisition.width;
321 child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
324 if (direction == GTK_TEXT_DIR_RTL)
325 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
327 gtk_widget_size_allocate (child->widget, &child_allocation);
329 x -= (child_width + box->spacing);