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/.
31 static void gtk_hbutton_box_class_init (GtkHButtonBoxClass *klass);
32 static void gtk_hbutton_box_init (GtkHButtonBox *box);
33 static void gtk_hbutton_box_size_request (GtkWidget *widget,
34 GtkRequisition *requisition);
35 static void gtk_hbutton_box_size_allocate (GtkWidget *widget,
36 GtkAllocation *allocation);
38 static gint default_spacing = 30;
39 static gint default_layout_style = GTK_BUTTONBOX_EDGE;
42 gtk_hbutton_box_get_type (void)
44 static GType hbutton_box_type = 0;
46 if (!hbutton_box_type)
48 static const GTypeInfo hbutton_box_info =
50 sizeof (GtkHButtonBoxClass),
52 NULL, /* base_finalize */
53 (GClassInitFunc) gtk_hbutton_box_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
56 sizeof (GtkHButtonBox),
58 (GInstanceInitFunc) gtk_hbutton_box_init,
62 g_type_register_static (GTK_TYPE_BUTTON_BOX, "GtkHButtonBox",
63 &hbutton_box_info, 0);
66 return hbutton_box_type;
70 gtk_hbutton_box_class_init (GtkHButtonBoxClass *class)
72 GtkWidgetClass *widget_class;
74 widget_class = (GtkWidgetClass*) class;
76 widget_class->size_request = gtk_hbutton_box_size_request;
77 widget_class->size_allocate = gtk_hbutton_box_size_allocate;
81 gtk_hbutton_box_init (GtkHButtonBox *hbutton_box)
83 /* button_box_init has done everything allready */
87 gtk_hbutton_box_new (void)
89 GtkHButtonBox *hbutton_box;
91 hbutton_box = g_object_new (GTK_TYPE_HBUTTON_BOX, NULL);
93 return GTK_WIDGET (hbutton_box);
97 /* set default value for spacing */
100 gtk_hbutton_box_set_spacing_default (gint spacing)
102 default_spacing = spacing;
106 /* set default value for layout style */
109 gtk_hbutton_box_set_layout_default (GtkButtonBoxStyle layout)
111 g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
112 layout <= GTK_BUTTONBOX_END);
114 default_layout_style = layout;
117 /* get default value for spacing */
120 gtk_hbutton_box_get_spacing_default (void)
122 return default_spacing;
126 /* get default value for layout style */
129 gtk_hbutton_box_get_layout_default (void)
131 return default_layout_style;
137 gtk_hbutton_box_size_request (GtkWidget *widget,
138 GtkRequisition *requisition)
146 GtkButtonBoxStyle layout;
148 box = GTK_BOX (widget);
149 bbox = GTK_BUTTON_BOX (widget);
151 spacing = box->spacing;
152 layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
153 ? bbox->layout_style : default_layout_style;
155 _gtk_button_box_child_requisition (widget,
161 if (nvis_children == 0)
163 requisition->width = 0;
164 requisition->height = 0;
170 case GTK_BUTTONBOX_SPREAD:
172 nvis_children*child_width + ((nvis_children+1)*spacing);
174 case GTK_BUTTONBOX_EDGE:
175 case GTK_BUTTONBOX_START:
176 case GTK_BUTTONBOX_END:
177 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
180 g_assert_not_reached();
184 requisition->height = child_height;
187 requisition->width += GTK_CONTAINER (box)->border_width * 2;
188 requisition->height += GTK_CONTAINER (box)->border_width * 2;
194 gtk_hbutton_box_size_allocate (GtkWidget *widget,
195 GtkAllocation *allocation)
202 GtkAllocation child_allocation;
208 gint secondary_x = 0;
212 gint childspacing = 0;
213 GtkButtonBoxStyle layout;
216 base_box = GTK_BOX (widget);
217 box = GTK_BUTTON_BOX (widget);
218 hbox = GTK_HBUTTON_BOX (widget);
219 spacing = base_box->spacing;
220 layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
221 ? box->layout_style : default_layout_style;
222 _gtk_button_box_child_requisition (widget,
227 widget->allocation = *allocation;
228 width = allocation->width - GTK_CONTAINER (box)->border_width*2;
231 case GTK_BUTTONBOX_SPREAD:
232 childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
233 x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
234 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
236 case GTK_BUTTONBOX_EDGE:
237 if (nvis_children >= 2)
239 childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
240 x = allocation->x + GTK_CONTAINER (box)->border_width;
241 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
245 /* one or zero children, just center */
246 childspacing = width;
247 x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
250 case GTK_BUTTONBOX_START:
251 childspacing = spacing;
252 x = allocation->x + GTK_CONTAINER (box)->border_width;
253 secondary_x = allocation->x + allocation->width
254 - child_width * n_secondaries
255 - spacing * (n_secondaries - 1)
256 - GTK_CONTAINER (box)->border_width;
258 case GTK_BUTTONBOX_END:
259 childspacing = spacing;
260 x = allocation->x + allocation->width
261 - child_width * (nvis_children - n_secondaries)
262 - spacing * (nvis_children - n_secondaries - 1)
263 - GTK_CONTAINER (box)->border_width;
264 secondary_x = allocation->x + GTK_CONTAINER (box)->border_width;
267 g_assert_not_reached();
272 y = allocation->y + (allocation->height - child_height) / 2;
273 childspace = child_width + childspacing;
275 children = GTK_BOX (box)->children;
279 child = children->data;
280 children = children->next;
282 if (GTK_WIDGET_VISIBLE (child->widget))
284 child_allocation.width = child_width;
285 child_allocation.height = child_height;
286 child_allocation.y = y;
288 if (child->is_secondary)
290 child_allocation.x = secondary_x;
291 secondary_x += childspace;
295 child_allocation.x = x;
299 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
300 child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
302 gtk_widget_size_allocate (child->widget, &child_allocation);