1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "psppire-hbuttonbox.h"
23 #include "psppire-dialog.h"
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
31 static void psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *);
32 static void psppire_hbuttonbox_init (PsppireHButtonBox *);
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);
40 static const GtkButtonBoxStyle default_layout_style = GTK_BUTTONBOX_EDGE;
43 psppire_hbutton_box_get_type (void)
45 static GType hbuttonbox_type = 0;
49 static const GTypeInfo hbuttonbox_info =
51 sizeof (PsppireHButtonBoxClass),
53 NULL, /* base_finalize */
54 (GClassInitFunc) psppire_hbuttonbox_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
57 sizeof (PsppireHButtonBox),
59 (GInstanceInitFunc) psppire_hbuttonbox_init,
62 hbuttonbox_type = g_type_register_static (PSPPIRE_BUTTONBOX_TYPE,
63 "PsppireHButtonBox", &hbuttonbox_info, 0);
66 return hbuttonbox_type;
70 psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *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;
82 psppire_hbuttonbox_init (PsppireHButtonBox *hbuttonbox)
88 psppire_hbuttonbox_new (void)
90 PsppireHButtonBox *hbuttonbox ;
92 hbuttonbox = g_object_new (psppire_hbutton_box_get_type (), NULL);
94 return GTK_WIDGET (hbuttonbox) ;
99 /* The following two functions are lifted verbatim from
100 the Gtk2.10.6 library */
103 gtk_hbutton_box_size_request (GtkWidget *widget,
104 GtkRequisition *requisition)
112 GtkButtonBoxStyle layout;
114 box = GTK_BOX (widget);
115 bbox = GTK_BUTTON_BOX (widget);
117 spacing = box->spacing;
118 layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
119 ? bbox->layout_style : default_layout_style;
121 _psppire_button_box_child_requisition (widget,
127 if (nvis_children == 0)
129 requisition->width = 0;
130 requisition->height = 0;
136 case GTK_BUTTONBOX_SPREAD:
138 nvis_children*child_width + ((nvis_children+1)*spacing);
140 case GTK_BUTTONBOX_EDGE:
141 case GTK_BUTTONBOX_START:
142 case GTK_BUTTONBOX_END:
143 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
146 g_assert_not_reached();
150 requisition->height = child_height;
153 requisition->width += GTK_CONTAINER (box)->border_width * 2;
154 requisition->height += GTK_CONTAINER (box)->border_width * 2;
160 gtk_hbutton_box_size_allocate (GtkWidget *widget,
161 GtkAllocation *allocation)
167 GtkAllocation child_allocation;
173 gint secondary_x = 0;
177 gint childspacing = 0;
178 GtkButtonBoxStyle layout;
181 base_box = GTK_BOX (widget);
182 box = GTK_BUTTON_BOX (widget);
183 spacing = base_box->spacing;
184 layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
185 ? box->layout_style : default_layout_style;
186 _psppire_button_box_child_requisition (widget,
191 widget->allocation = *allocation;
192 width = allocation->width - GTK_CONTAINER (box)->border_width*2;
195 case GTK_BUTTONBOX_SPREAD:
196 childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
197 x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
198 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
200 case GTK_BUTTONBOX_EDGE:
201 if (nvis_children >= 2)
203 childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
204 x = allocation->x + GTK_CONTAINER (box)->border_width;
205 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
209 /* one or zero children, just center */
210 childspacing = width;
211 x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
214 case GTK_BUTTONBOX_START:
215 childspacing = spacing;
216 x = allocation->x + GTK_CONTAINER (box)->border_width;
217 secondary_x = allocation->x + allocation->width
218 - child_width * n_secondaries
219 - spacing * (n_secondaries - 1)
220 - GTK_CONTAINER (box)->border_width;
222 case GTK_BUTTONBOX_END:
223 childspacing = spacing;
224 x = allocation->x + allocation->width
225 - child_width * (nvis_children - n_secondaries)
226 - spacing * (nvis_children - n_secondaries - 1)
227 - GTK_CONTAINER (box)->border_width;
228 secondary_x = allocation->x + GTK_CONTAINER (box)->border_width;
231 g_assert_not_reached();
236 y = allocation->y + (allocation->height - child_height) / 2;
237 childspace = child_width + childspacing;
239 children = GTK_BOX (box)->children;
243 child = children->data;
244 children = children->next;
246 if (gtk_widget_get_visible (child->widget))
248 child_allocation.width = child_width;
249 child_allocation.height = child_height;
250 child_allocation.y = y;
252 if (child->is_secondary)
254 child_allocation.x = secondary_x;
255 secondary_x += childspace;
259 child_allocation.x = x;
263 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
264 child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
266 gtk_widget_size_allocate (child->widget, &child_allocation);