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/.
32 static void gtk_hbox_class_init (GtkHBoxClass *klass);
33 static void gtk_hbox_init (GtkHBox *box);
34 static void gtk_hbox_size_request (GtkWidget *widget,
35 GtkRequisition *requisition);
36 static void gtk_hbox_size_allocate (GtkWidget *widget,
37 GtkAllocation *allocation);
41 gtk_hbox_get_type (void)
43 static GType hbox_type = 0;
47 static const GTypeInfo hbox_info =
49 sizeof (GtkHBoxClass),
51 NULL, /* base_finalize */
52 (GClassInitFunc) gtk_hbox_class_init,
53 NULL, /* class_finalize */
54 NULL, /* class_data */
57 (GInstanceInitFunc) gtk_hbox_init,
60 hbox_type = g_type_register_static (GTK_TYPE_BOX, g_intern_static_string ("GtkHBox"),
68 gtk_hbox_class_init (GtkHBoxClass *class)
70 GtkWidgetClass *widget_class;
72 widget_class = (GtkWidgetClass*) class;
74 widget_class->size_request = gtk_hbox_size_request;
75 widget_class->size_allocate = gtk_hbox_size_allocate;
79 gtk_hbox_init (GtkHBox *hbox)
84 gtk_hbox_new (gboolean homogeneous,
89 hbox = g_object_new (GTK_TYPE_HBOX, NULL);
91 GTK_BOX (hbox)->spacing = spacing;
92 GTK_BOX (hbox)->homogeneous = homogeneous ? TRUE : FALSE;
94 return GTK_WIDGET (hbox);
99 gtk_hbox_size_request (GtkWidget *widget,
100 GtkRequisition *requisition)
108 box = GTK_BOX (widget);
109 requisition->width = 0;
110 requisition->height = 0;
113 children = box->children;
116 child = children->data;
117 children = children->next;
119 if (GTK_WIDGET_VISIBLE (child->widget))
121 GtkRequisition child_requisition;
123 gtk_widget_size_request (child->widget, &child_requisition);
125 if (box->homogeneous)
127 width = child_requisition.width + child->padding * 2;
128 requisition->width = MAX (requisition->width, width);
132 requisition->width += child_requisition.width + child->padding * 2;
135 requisition->height = MAX (requisition->height, child_requisition.height);
141 if (nvis_children > 0)
143 if (box->homogeneous)
144 requisition->width *= nvis_children;
145 requisition->width += (nvis_children - 1) * box->spacing;
148 requisition->width += GTK_CONTAINER (box)->border_width * 2;
149 requisition->height += GTK_CONTAINER (box)->border_width * 2;
153 gtk_hbox_size_allocate (GtkWidget *widget,
154 GtkAllocation *allocation)
159 GtkAllocation child_allocation;
161 gint nexpand_children;
166 GtkTextDirection direction;
168 box = GTK_BOX (widget);
169 widget->allocation = *allocation;
171 direction = gtk_widget_get_direction (widget);
174 nexpand_children = 0;
175 children = box->children;
179 child = children->data;
180 children = children->next;
182 if (GTK_WIDGET_VISIBLE (child->widget))
186 nexpand_children += 1;
190 if (nvis_children > 0)
192 if (box->homogeneous)
194 width = (allocation->width -
195 GTK_CONTAINER (box)->border_width * 2 -
196 (nvis_children - 1) * box->spacing);
197 extra = width / nvis_children;
199 else if (nexpand_children > 0)
201 width = (gint) allocation->width - (gint) widget->requisition.width;
202 extra = width / nexpand_children;
210 x = allocation->x + GTK_CONTAINER (box)->border_width;
211 child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
212 child_allocation.height = MAX (1, (gint) allocation->height - (gint) GTK_CONTAINER (box)->border_width * 2);
214 children = box->children;
217 child = children->data;
218 children = children->next;
220 if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
222 if (box->homogeneous)
224 if (nvis_children == 1)
234 GtkRequisition child_requisition;
236 gtk_widget_get_child_requisition (child->widget, &child_requisition);
238 child_width = child_requisition.width + child->padding * 2;
242 if (nexpand_children == 1)
243 child_width += width;
245 child_width += extra;
247 nexpand_children -= 1;
254 child_allocation.width = MAX (1, (gint) child_width - (gint) child->padding * 2);
255 child_allocation.x = x + child->padding;
259 GtkRequisition child_requisition;
261 gtk_widget_get_child_requisition (child->widget, &child_requisition);
262 child_allocation.width = child_requisition.width;
263 child_allocation.x = x + (child_width - child_allocation.width) / 2;
266 if (direction == GTK_TEXT_DIR_RTL)
267 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
269 gtk_widget_size_allocate (child->widget, &child_allocation);
271 x += child_width + box->spacing;
275 x = allocation->x + allocation->width - GTK_CONTAINER (box)->border_width;
277 children = box->children;
280 child = children->data;
281 children = children->next;
283 if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
285 GtkRequisition child_requisition;
286 gtk_widget_get_child_requisition (child->widget, &child_requisition);
288 if (box->homogeneous)
290 if (nvis_children == 1)
300 child_width = child_requisition.width + child->padding * 2;
304 if (nexpand_children == 1)
305 child_width += width;
307 child_width += extra;
309 nexpand_children -= 1;
316 child_allocation.width = MAX (1, (gint)child_width - (gint)child->padding * 2);
317 child_allocation.x = x + child->padding - child_width;
321 child_allocation.width = child_requisition.width;
322 child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
325 if (direction == GTK_TEXT_DIR_RTL)
326 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
328 gtk_widget_size_allocate (child->widget, &child_allocation);
330 x -= (child_width + box->spacing);
336 #define __GTK_HBOX_C__
337 #include "gtkaliasdef.c"