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/.
33 static void gtk_hbox_class_init (GtkHBoxClass *klass);
34 static void gtk_hbox_init (GtkHBox *box);
35 static void gtk_hbox_size_request (GtkWidget *widget,
36 GtkRequisition *requisition);
37 static void gtk_hbox_size_allocate (GtkWidget *widget,
38 GtkAllocation *allocation);
42 gtk_hbox_get_type (void)
44 static GType hbox_type = 0;
48 static const GTypeInfo hbox_info =
50 sizeof (GtkHBoxClass),
52 NULL, /* base_finalize */
53 (GClassInitFunc) gtk_hbox_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
58 (GInstanceInitFunc) gtk_hbox_init,
61 hbox_type = g_type_register_static (GTK_TYPE_BOX, I_("GtkHBox"),
69 gtk_hbox_class_init (GtkHBoxClass *class)
71 GtkWidgetClass *widget_class;
73 widget_class = (GtkWidgetClass*) class;
75 widget_class->size_request = gtk_hbox_size_request;
76 widget_class->size_allocate = gtk_hbox_size_allocate;
80 gtk_hbox_init (GtkHBox *hbox)
85 gtk_hbox_new (gboolean homogeneous,
90 hbox = g_object_new (GTK_TYPE_HBOX, NULL);
92 GTK_BOX (hbox)->spacing = spacing;
93 GTK_BOX (hbox)->homogeneous = homogeneous ? TRUE : FALSE;
95 return GTK_WIDGET (hbox);
100 gtk_hbox_size_request (GtkWidget *widget,
101 GtkRequisition *requisition)
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 GtkRequisition child_requisition;
124 gtk_widget_size_request (child->widget, &child_requisition);
126 if (box->homogeneous)
128 width = child_requisition.width + child->padding * 2;
129 requisition->width = MAX (requisition->width, width);
133 requisition->width += child_requisition.width + child->padding * 2;
136 requisition->height = MAX (requisition->height, child_requisition.height);
142 if (nvis_children > 0)
144 if (box->homogeneous)
145 requisition->width *= nvis_children;
146 requisition->width += (nvis_children - 1) * box->spacing;
149 requisition->width += GTK_CONTAINER (box)->border_width * 2;
150 requisition->height += GTK_CONTAINER (box)->border_width * 2;
154 gtk_hbox_size_allocate (GtkWidget *widget,
155 GtkAllocation *allocation)
160 GtkAllocation child_allocation;
162 gint nexpand_children;
167 GtkTextDirection direction;
169 box = GTK_BOX (widget);
170 widget->allocation = *allocation;
172 direction = gtk_widget_get_direction (widget);
175 nexpand_children = 0;
176 children = box->children;
180 child = children->data;
181 children = children->next;
183 if (GTK_WIDGET_VISIBLE (child->widget))
187 nexpand_children += 1;
191 if (nvis_children > 0)
193 if (box->homogeneous)
195 width = (allocation->width -
196 GTK_CONTAINER (box)->border_width * 2 -
197 (nvis_children - 1) * box->spacing);
198 extra = width / nvis_children;
200 else if (nexpand_children > 0)
202 width = (gint) allocation->width - (gint) widget->requisition.width;
203 extra = width / nexpand_children;
211 x = allocation->x + GTK_CONTAINER (box)->border_width;
212 child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
213 child_allocation.height = MAX (1, (gint) allocation->height - (gint) GTK_CONTAINER (box)->border_width * 2);
215 children = box->children;
218 child = children->data;
219 children = children->next;
221 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
223 if (box->homogeneous)
225 if (nvis_children == 1)
235 GtkRequisition child_requisition;
237 gtk_widget_get_child_requisition (child->widget, &child_requisition);
239 child_width = child_requisition.width + child->padding * 2;
243 if (nexpand_children == 1)
244 child_width += width;
246 child_width += extra;
248 nexpand_children -= 1;
255 child_allocation.width = MAX (1, (gint) child_width - (gint) child->padding * 2);
256 child_allocation.x = x + child->padding;
260 GtkRequisition child_requisition;
262 gtk_widget_get_child_requisition (child->widget, &child_requisition);
263 child_allocation.width = child_requisition.width;
264 child_allocation.x = x + (child_width - child_allocation.width) / 2;
267 if (direction == GTK_TEXT_DIR_RTL)
268 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
270 gtk_widget_size_allocate (child->widget, &child_allocation);
272 x += child_width + box->spacing;
276 x = allocation->x + allocation->width - GTK_CONTAINER (box)->border_width;
278 children = box->children;
281 child = children->data;
282 children = children->next;
284 if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
286 GtkRequisition child_requisition;
287 gtk_widget_get_child_requisition (child->widget, &child_requisition);
289 if (box->homogeneous)
291 if (nvis_children == 1)
301 child_width = child_requisition.width + child->padding * 2;
305 if (nexpand_children == 1)
306 child_width += width;
308 child_width += extra;
310 nexpand_children -= 1;
317 child_allocation.width = MAX (1, (gint)child_width - (gint)child->padding * 2);
318 child_allocation.x = x + child->padding - child_width;
322 child_allocation.width = child_requisition.width;
323 child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
326 if (direction == GTK_TEXT_DIR_RTL)
327 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
329 gtk_widget_size_allocate (child->widget, &child_allocation);
331 x -= (child_width + box->spacing);
337 #define __GTK_HBOX_C__
338 #include "gtkaliasdef.c"