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_hbutton_box_class_init (GtkHButtonBoxClass *klass);
33 static void gtk_hbutton_box_init (GtkHButtonBox *box);
34 static void gtk_hbutton_box_size_request (GtkWidget *widget,
35 GtkRequisition *requisition);
36 static void gtk_hbutton_box_size_allocate (GtkWidget *widget,
37 GtkAllocation *allocation);
39 static gint default_spacing = 30;
40 static gint default_layout_style = GTK_BUTTONBOX_EDGE;
43 gtk_hbutton_box_get_type (void)
45 static GType hbutton_box_type = 0;
47 if (!hbutton_box_type)
49 static const GTypeInfo hbutton_box_info =
51 sizeof (GtkHButtonBoxClass),
53 NULL, /* base_finalize */
54 (GClassInitFunc) gtk_hbutton_box_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
57 sizeof (GtkHButtonBox),
59 (GInstanceInitFunc) gtk_hbutton_box_init,
63 g_type_register_static (GTK_TYPE_BUTTON_BOX, "GtkHButtonBox",
64 &hbutton_box_info, 0);
67 return hbutton_box_type;
71 gtk_hbutton_box_class_init (GtkHButtonBoxClass *class)
73 GtkWidgetClass *widget_class;
75 widget_class = (GtkWidgetClass*) class;
77 widget_class->size_request = gtk_hbutton_box_size_request;
78 widget_class->size_allocate = gtk_hbutton_box_size_allocate;
82 gtk_hbutton_box_init (GtkHButtonBox *hbutton_box)
84 /* button_box_init has done everything allready */
88 gtk_hbutton_box_new (void)
90 GtkHButtonBox *hbutton_box;
92 hbutton_box = g_object_new (GTK_TYPE_HBUTTON_BOX, NULL);
94 return GTK_WIDGET (hbutton_box);
98 /* set default value for spacing */
101 gtk_hbutton_box_set_spacing_default (gint spacing)
103 default_spacing = spacing;
107 /* set default value for layout style */
110 gtk_hbutton_box_set_layout_default (GtkButtonBoxStyle layout)
112 g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
113 layout <= GTK_BUTTONBOX_END);
115 default_layout_style = layout;
118 /* get default value for spacing */
121 gtk_hbutton_box_get_spacing_default (void)
123 return default_spacing;
127 /* get default value for layout style */
130 gtk_hbutton_box_get_layout_default (void)
132 return default_layout_style;
138 gtk_hbutton_box_size_request (GtkWidget *widget,
139 GtkRequisition *requisition)
147 GtkButtonBoxStyle layout;
149 box = GTK_BOX (widget);
150 bbox = GTK_BUTTON_BOX (widget);
152 spacing = box->spacing;
153 layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
154 ? bbox->layout_style : default_layout_style;
156 _gtk_button_box_child_requisition (widget,
162 if (nvis_children == 0)
164 requisition->width = 0;
165 requisition->height = 0;
171 case GTK_BUTTONBOX_SPREAD:
173 nvis_children*child_width + ((nvis_children+1)*spacing);
175 case GTK_BUTTONBOX_EDGE:
176 case GTK_BUTTONBOX_START:
177 case GTK_BUTTONBOX_END:
178 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
181 g_assert_not_reached();
185 requisition->height = child_height;
188 requisition->width += GTK_CONTAINER (box)->border_width * 2;
189 requisition->height += GTK_CONTAINER (box)->border_width * 2;
195 gtk_hbutton_box_size_allocate (GtkWidget *widget,
196 GtkAllocation *allocation)
203 GtkAllocation child_allocation;
209 gint secondary_x = 0;
213 gint childspacing = 0;
214 GtkButtonBoxStyle layout;
217 base_box = GTK_BOX (widget);
218 box = GTK_BUTTON_BOX (widget);
219 hbox = GTK_HBUTTON_BOX (widget);
220 spacing = base_box->spacing;
221 layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
222 ? box->layout_style : default_layout_style;
223 _gtk_button_box_child_requisition (widget,
228 widget->allocation = *allocation;
229 width = allocation->width - GTK_CONTAINER (box)->border_width*2;
232 case GTK_BUTTONBOX_SPREAD:
233 childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
234 x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
235 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
237 case GTK_BUTTONBOX_EDGE:
238 if (nvis_children >= 2)
240 childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
241 x = allocation->x + GTK_CONTAINER (box)->border_width;
242 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
246 /* one or zero children, just center */
247 childspacing = width;
248 x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
251 case GTK_BUTTONBOX_START:
252 childspacing = spacing;
253 x = allocation->x + GTK_CONTAINER (box)->border_width;
254 secondary_x = allocation->x + allocation->width
255 - child_width * n_secondaries
256 - spacing * (n_secondaries - 1)
257 - GTK_CONTAINER (box)->border_width;
259 case GTK_BUTTONBOX_END:
260 childspacing = spacing;
261 x = allocation->x + allocation->width
262 - child_width * (nvis_children - n_secondaries)
263 - spacing * (nvis_children - n_secondaries - 1)
264 - GTK_CONTAINER (box)->border_width;
265 secondary_x = allocation->x + GTK_CONTAINER (box)->border_width;
268 g_assert_not_reached();
273 y = allocation->y + (allocation->height - child_height) / 2;
274 childspace = child_width + childspacing;
276 children = GTK_BOX (box)->children;
280 child = children->data;
281 children = children->next;
283 if (GTK_WIDGET_VISIBLE (child->widget))
285 child_allocation.width = child_width;
286 child_allocation.height = child_height;
287 child_allocation.y = y;
289 if (child->is_secondary)
291 child_allocation.x = secondary_x;
292 secondary_x += childspace;
296 child_allocation.x = x;
300 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
301 child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
303 gtk_widget_size_allocate (child->widget, &child_allocation);