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 GtkType hbox_type = 0;
45 static const GtkTypeInfo hbox_info =
49 sizeof (GtkHBoxClass),
50 (GtkClassInitFunc) gtk_hbox_class_init,
51 (GtkObjectInitFunc) gtk_hbox_init,
52 /* reserved_1 */ NULL,
53 /* reserved_2 */ NULL,
54 (GtkClassInitFunc) NULL,
57 hbox_type = gtk_type_unique (GTK_TYPE_BOX, &hbox_info);
64 gtk_hbox_class_init (GtkHBoxClass *class)
66 GtkWidgetClass *widget_class;
68 widget_class = (GtkWidgetClass*) class;
70 widget_class->size_request = gtk_hbox_size_request;
71 widget_class->size_allocate = gtk_hbox_size_allocate;
75 gtk_hbox_init (GtkHBox *hbox)
80 gtk_hbox_new (gboolean homogeneous,
85 hbox = gtk_type_new (gtk_hbox_get_type ());
87 GTK_BOX (hbox)->spacing = spacing;
88 GTK_BOX (hbox)->homogeneous = homogeneous ? TRUE : FALSE;
90 return GTK_WIDGET (hbox);
95 gtk_hbox_size_request (GtkWidget *widget,
96 GtkRequisition *requisition)
104 box = GTK_BOX (widget);
105 requisition->width = 0;
106 requisition->height = 0;
109 children = box->children;
112 child = children->data;
113 children = children->next;
115 if (GTK_WIDGET_VISIBLE (child->widget))
117 GtkRequisition child_requisition;
119 gtk_widget_size_request (child->widget, &child_requisition);
121 if (box->homogeneous)
123 width = child_requisition.width + child->padding * 2;
124 requisition->width = MAX (requisition->width, width);
128 requisition->width += child_requisition.width + child->padding * 2;
131 requisition->height = MAX (requisition->height, child_requisition.height);
137 if (nvis_children > 0)
139 if (box->homogeneous)
140 requisition->width *= nvis_children;
141 requisition->width += (nvis_children - 1) * box->spacing;
144 requisition->width += GTK_CONTAINER (box)->border_width * 2;
145 requisition->height += GTK_CONTAINER (box)->border_width * 2;
149 gtk_hbox_size_allocate (GtkWidget *widget,
150 GtkAllocation *allocation)
155 GtkAllocation child_allocation;
157 gint nexpand_children;
162 GtkTextDirection direction;
164 box = GTK_BOX (widget);
165 widget->allocation = *allocation;
167 direction = gtk_widget_get_direction (widget);
170 nexpand_children = 0;
171 children = box->children;
175 child = children->data;
176 children = children->next;
178 if (GTK_WIDGET_VISIBLE (child->widget))
182 nexpand_children += 1;
186 if (nvis_children > 0)
188 if (box->homogeneous)
190 width = (allocation->width -
191 GTK_CONTAINER (box)->border_width * 2 -
192 (nvis_children - 1) * box->spacing);
193 extra = width / nvis_children;
195 else if (nexpand_children > 0)
197 width = (gint) allocation->width - (gint) widget->requisition.width;
198 extra = width / nexpand_children;
206 x = allocation->x + GTK_CONTAINER (box)->border_width;
207 child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
208 child_allocation.height = MAX (1, (gint) allocation->height - (gint) GTK_CONTAINER (box)->border_width * 2);
210 children = box->children;
213 child = children->data;
214 children = children->next;
216 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
218 if (box->homogeneous)
220 if (nvis_children == 1)
230 GtkRequisition child_requisition;
232 gtk_widget_get_child_requisition (child->widget, &child_requisition);
234 child_width = child_requisition.width + child->padding * 2;
238 if (nexpand_children == 1)
239 child_width += width;
241 child_width += extra;
243 nexpand_children -= 1;
250 child_allocation.width = MAX (1, (gint) child_width - (gint) child->padding * 2);
251 child_allocation.x = x + child->padding;
255 GtkRequisition child_requisition;
257 gtk_widget_get_child_requisition (child->widget, &child_requisition);
258 child_allocation.width = child_requisition.width;
259 child_allocation.x = x + (child_width - child_allocation.width) / 2;
262 if (direction == GTK_TEXT_DIR_RTL)
263 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
265 gtk_widget_size_allocate (child->widget, &child_allocation);
267 x += child_width + box->spacing;
271 x = allocation->x + allocation->width - 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)
296 child_width = child_requisition.width + child->padding * 2;
300 if (nexpand_children == 1)
301 child_width += width;
303 child_width += extra;
305 nexpand_children -= 1;
312 child_allocation.width = MAX (1, (gint)child_width - (gint)child->padding * 2);
313 child_allocation.x = x + child->padding - child_width;
317 child_allocation.width = child_requisition.width;
318 child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
321 if (direction == GTK_TEXT_DIR_RTL)
322 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
324 gtk_widget_size_allocate (child->widget, &child_allocation);
326 x -= (child_width + box->spacing);