1 /* GTK - The GTK+ 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_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;
41 G_DEFINE_TYPE (GtkHButtonBox, gtk_hbutton_box, GTK_TYPE_BUTTON_BOX)
44 gtk_hbutton_box_class_init (GtkHButtonBoxClass *class)
46 GtkWidgetClass *widget_class;
48 widget_class = (GtkWidgetClass*) class;
50 widget_class->size_request = gtk_hbutton_box_size_request;
51 widget_class->size_allocate = gtk_hbutton_box_size_allocate;
55 gtk_hbutton_box_init (GtkHButtonBox *hbutton_box)
57 /* button_box_init has done everything already */
61 gtk_hbutton_box_new (void)
63 GtkHButtonBox *hbutton_box;
65 hbutton_box = g_object_new (GTK_TYPE_HBUTTON_BOX, NULL);
67 return GTK_WIDGET (hbutton_box);
71 /* set default value for spacing */
74 gtk_hbutton_box_set_spacing_default (gint spacing)
76 default_spacing = spacing;
80 /* set default value for layout style */
83 gtk_hbutton_box_set_layout_default (GtkButtonBoxStyle layout)
85 g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
86 layout <= GTK_BUTTONBOX_CENTER);
88 default_layout_style = layout;
91 /* get default value for spacing */
94 gtk_hbutton_box_get_spacing_default (void)
96 return default_spacing;
100 /* get default value for layout style */
103 gtk_hbutton_box_get_layout_default (void)
105 return default_layout_style;
111 gtk_hbutton_box_size_request (GtkWidget *widget,
112 GtkRequisition *requisition)
120 GtkButtonBoxStyle layout;
122 box = GTK_BOX (widget);
123 bbox = GTK_BUTTON_BOX (widget);
125 spacing = box->spacing;
126 layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
127 ? bbox->layout_style : default_layout_style;
129 _gtk_button_box_child_requisition (widget,
135 if (nvis_children == 0)
137 requisition->width = 0;
138 requisition->height = 0;
144 case GTK_BUTTONBOX_SPREAD:
146 nvis_children*child_width + ((nvis_children+1)*spacing);
148 case GTK_BUTTONBOX_EDGE:
149 case GTK_BUTTONBOX_START:
150 case GTK_BUTTONBOX_END:
151 case GTK_BUTTONBOX_CENTER:
152 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
155 g_assert_not_reached();
159 requisition->height = child_height;
162 requisition->width += GTK_CONTAINER (box)->border_width * 2;
163 requisition->height += GTK_CONTAINER (box)->border_width * 2;
169 gtk_hbutton_box_size_allocate (GtkWidget *widget,
170 GtkAllocation *allocation)
176 GtkAllocation child_allocation;
182 gint secondary_x = 0;
186 gint childspacing = 0;
187 GtkButtonBoxStyle layout;
190 base_box = GTK_BOX (widget);
191 box = GTK_BUTTON_BOX (widget);
192 spacing = base_box->spacing;
193 layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
194 ? box->layout_style : default_layout_style;
195 _gtk_button_box_child_requisition (widget,
200 widget->allocation = *allocation;
201 width = allocation->width - GTK_CONTAINER (box)->border_width*2;
204 case GTK_BUTTONBOX_SPREAD:
205 childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
206 x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
207 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
209 case GTK_BUTTONBOX_EDGE:
210 if (nvis_children >= 2)
212 childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
213 x = allocation->x + GTK_CONTAINER (box)->border_width;
214 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
218 /* one or zero children, just center */
219 childspacing = width;
220 x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
223 case GTK_BUTTONBOX_START:
224 childspacing = spacing;
225 x = allocation->x + GTK_CONTAINER (box)->border_width;
226 secondary_x = allocation->x + allocation->width
227 - child_width * n_secondaries
228 - spacing * (n_secondaries - 1)
229 - GTK_CONTAINER (box)->border_width;
231 case GTK_BUTTONBOX_END:
232 childspacing = spacing;
233 x = allocation->x + allocation->width
234 - child_width * (nvis_children - n_secondaries)
235 - spacing * (nvis_children - n_secondaries - 1)
236 - GTK_CONTAINER (box)->border_width;
237 secondary_x = allocation->x + GTK_CONTAINER (box)->border_width;
239 case GTK_BUTTONBOX_CENTER:
240 childspacing = spacing;
243 - (child_width * (nvis_children - n_secondaries)
244 + spacing * (nvis_children - n_secondaries - 1)))/2
245 + (n_secondaries * child_width + n_secondaries * spacing)/2;
246 secondary_x = allocation->x + 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.y = y;
270 if (child->is_secondary)
272 child_allocation.x = secondary_x;
273 secondary_x += childspace;
277 child_allocation.x = x;
281 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
282 child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
284 gtk_widget_size_allocate (child->widget, &child_allocation);
289 #define __GTK_HBUTTON_BOX_C__
290 #include "gtkaliasdef.c"