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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
23 static void gtk_hbutton_box_class_init (GtkHButtonBoxClass *klass);
24 static void gtk_hbutton_box_init (GtkHButtonBox *box);
25 static void gtk_hbutton_box_size_request (GtkWidget *widget,
26 GtkRequisition *requisition);
27 static void gtk_hbutton_box_size_allocate (GtkWidget *widget,
28 GtkAllocation *allocation);
30 static gint default_spacing = 30;
31 static gint default_layout_style = GTK_BUTTONBOX_EDGE;
34 gtk_hbutton_box_get_type (void)
36 static guint hbutton_box_type = 0;
38 if (!hbutton_box_type)
40 GtkTypeInfo hbutton_box_info =
43 sizeof (GtkHButtonBox),
44 sizeof (GtkHButtonBoxClass),
45 (GtkClassInitFunc) gtk_hbutton_box_class_init,
46 (GtkObjectInitFunc) gtk_hbutton_box_init,
47 /* reserved_1 */ NULL,
48 /* reserved_2 */ NULL,
49 (GtkClassInitFunc) NULL,
52 hbutton_box_type = gtk_type_unique (gtk_button_box_get_type (), &hbutton_box_info);
55 return hbutton_box_type;
59 gtk_hbutton_box_class_init (GtkHButtonBoxClass *class)
61 GtkWidgetClass *widget_class;
63 widget_class = (GtkWidgetClass*) class;
65 widget_class->size_request = gtk_hbutton_box_size_request;
66 widget_class->size_allocate = gtk_hbutton_box_size_allocate;
70 gtk_hbutton_box_init (GtkHButtonBox *hbutton_box)
72 /* button_box_init has done everything allready */
76 gtk_hbutton_box_new (void)
78 GtkHButtonBox *hbutton_box;
80 hbutton_box = gtk_type_new (gtk_hbutton_box_get_type ());
82 return GTK_WIDGET (hbutton_box);
86 /* set default value for spacing */
88 void gtk_hbutton_box_set_spacing_default (gint spacing)
90 default_spacing = spacing;
94 /* set default value for layout style */
96 void gtk_hbutton_box_set_layout_default (GtkButtonBoxStyle layout)
98 g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
99 layout <= GTK_BUTTONBOX_END);
101 default_layout_style = layout;
104 /* get default value for spacing */
106 gint gtk_hbutton_box_get_spacing_default (void)
108 return default_spacing;
112 /* get default value for layout style */
114 GtkButtonBoxStyle gtk_hbutton_box_get_layout_default (void)
116 return default_layout_style;
122 gtk_hbutton_box_size_request (GtkWidget *widget,
123 GtkRequisition *requisition)
131 GtkButtonBoxStyle layout;
133 g_return_if_fail (widget != NULL);
134 g_return_if_fail (GTK_IS_HBUTTON_BOX (widget));
135 g_return_if_fail (requisition != NULL);
137 box = GTK_BOX (widget);
138 bbox = GTK_BUTTON_BOX (widget);
140 spacing = bbox->spacing != GTK_BUTTONBOX_DEFAULT
141 ? bbox->spacing : default_spacing;
142 layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
143 ? bbox->layout_style : default_layout_style;
145 gtk_button_box_child_requisition (widget,
150 if (nvis_children == 0)
152 requisition->width = 0;
153 requisition->height = 0;
159 case GTK_BUTTONBOX_SPREAD:
161 nvis_children*child_width + ((nvis_children+1)*spacing);
163 case GTK_BUTTONBOX_EDGE:
164 case GTK_BUTTONBOX_START:
165 case GTK_BUTTONBOX_END:
166 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
169 g_assert_not_reached();
173 requisition->height = child_height;
176 requisition->width += GTK_CONTAINER (box)->border_width * 2;
177 requisition->height += GTK_CONTAINER (box)->border_width * 2;
183 gtk_hbutton_box_size_allocate (GtkWidget *widget,
184 GtkAllocation *allocation)
190 GtkAllocation child_allocation;
198 gint childspacing = 0;
199 GtkButtonBoxStyle layout;
202 g_return_if_fail (widget != NULL);
203 g_return_if_fail (GTK_IS_HBUTTON_BOX (widget));
204 g_return_if_fail (allocation != NULL);
206 box = GTK_BUTTON_BOX (widget);
207 hbox = GTK_HBUTTON_BOX (widget);
208 spacing = box->spacing != GTK_BUTTONBOX_DEFAULT
209 ? box->spacing : default_spacing;
210 layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
211 ? box->layout_style : default_layout_style;
212 gtk_button_box_child_requisition (widget,
216 widget->allocation = *allocation;
217 width = allocation->width - GTK_CONTAINER (box)->border_width*2;
220 case GTK_BUTTONBOX_SPREAD:
221 childspacing = (width - (nvis_children*child_width)) / (nvis_children+1);
222 x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
224 case GTK_BUTTONBOX_EDGE:
225 if (nvis_children >= 2)
228 (width - (nvis_children*child_width)) / (nvis_children-1);
229 x = allocation->x + GTK_CONTAINER (box)->border_width;
233 /* one or zero children, just center */
234 childspacing = width;
235 x = allocation->x + (allocation->width - child_width) / 2;
238 case GTK_BUTTONBOX_START:
239 childspacing = spacing;
240 x = allocation->x + GTK_CONTAINER (box)->border_width;
242 case GTK_BUTTONBOX_END:
243 childspacing = spacing;
244 x = allocation->x + allocation->width - child_width * nvis_children
245 - spacing *(nvis_children-1)
246 - GTK_CONTAINER (box)->border_width;
249 g_assert_not_reached();
254 y = allocation->y + (allocation->height - child_height) / 2;
255 childspace = child_width + childspacing;
257 children = GTK_BOX (box)->children;
261 child = children->data;
262 children = children->next;
264 if (GTK_WIDGET_VISIBLE (child->widget))
266 child_allocation.width = child_width;
267 child_allocation.height = child_height;
268 child_allocation.x = x;
269 child_allocation.y = y;
270 gtk_widget_size_allocate (child->widget, &child_allocation);