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);
41 psppire_hbutton_box_get_preferred_height (GtkWidget *widget,
45 GtkRequisition requisition;
47 gtk_hbutton_box_size_request (widget, &requisition);
49 *minimal_height = *natural_height = requisition.height;
54 psppire_hbutton_box_get_preferred_width (GtkWidget *widget,
58 GtkRequisition requisition;
60 gtk_hbutton_box_size_request (widget, &requisition);
62 *minimal_width = *natural_width = requisition.width;
69 psppire_hbutton_box_get_type (void)
71 static GType hbuttonbox_type = 0;
75 static const GTypeInfo hbuttonbox_info =
77 sizeof (PsppireHButtonBoxClass),
79 NULL, /* base_finalize */
80 (GClassInitFunc) psppire_hbuttonbox_class_init,
81 NULL, /* class_finalize */
82 NULL, /* class_data */
83 sizeof (PsppireHButtonBox),
85 (GInstanceInitFunc) psppire_hbuttonbox_init,
88 hbuttonbox_type = g_type_register_static (PSPPIRE_BUTTONBOX_TYPE,
89 "PsppireHButtonBox", &hbuttonbox_info, 0);
92 return hbuttonbox_type;
96 psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *class)
98 GtkWidgetClass *widget_class;
100 widget_class = (GtkWidgetClass*) class;
102 widget_class->get_preferred_width = psppire_hbutton_box_get_preferred_width;
103 widget_class->get_preferred_height = psppire_hbutton_box_get_preferred_height;
104 widget_class->size_allocate = gtk_hbutton_box_size_allocate;
109 psppire_hbuttonbox_init (PsppireHButtonBox *hbuttonbox)
115 psppire_hbuttonbox_new (void)
117 PsppireHButtonBox *hbuttonbox ;
119 hbuttonbox = g_object_new (psppire_hbutton_box_get_type (), NULL);
121 return GTK_WIDGET (hbuttonbox) ;
126 /* The following two functions are lifted verbatim from
127 the Gtk2.10.6 library */
130 gtk_hbutton_box_size_request (GtkWidget *widget,
131 GtkRequisition *requisition)
139 GtkButtonBoxStyle layout;
141 box = GTK_BOX (widget);
142 bbox = GTK_BUTTON_BOX (widget);
144 spacing = gtk_box_get_spacing (box);
145 layout = gtk_button_box_get_layout (bbox) ;
147 _psppire_button_box_child_requisition (widget,
153 if (nvis_children == 0)
155 requisition->width = 0;
156 requisition->height = 0;
162 case GTK_BUTTONBOX_SPREAD:
164 nvis_children*child_width + ((nvis_children+1)*spacing);
167 case GTK_BUTTONBOX_EDGE:
168 case GTK_BUTTONBOX_START:
169 case GTK_BUTTONBOX_END:
170 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
174 requisition->height = child_height;
177 requisition->width += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
178 requisition->height += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
184 gtk_hbutton_box_size_allocate (GtkWidget *widget,
185 GtkAllocation *allocation)
190 GtkAllocation child_allocation;
196 gint secondary_x = 0;
200 gint childspacing = 0;
201 GtkButtonBoxStyle layout;
204 base_box = GTK_BOX (widget);
205 box = GTK_BUTTON_BOX (widget);
206 spacing = gtk_box_get_spacing (base_box);
207 layout = gtk_button_box_get_layout (box) ;
208 _psppire_button_box_child_requisition (widget,
213 gtk_widget_set_allocation (widget, allocation);
214 width = allocation->width - gtk_container_get_border_width (GTK_CONTAINER (box))*2;
217 case GTK_BUTTONBOX_SPREAD:
218 childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
219 x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box)) + childspacing;
220 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
223 case GTK_BUTTONBOX_EDGE:
224 if (nvis_children >= 2)
226 childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
227 x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box));
228 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
232 /* one or zero children, just center */
233 childspacing = width;
234 x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
237 case GTK_BUTTONBOX_START:
238 childspacing = spacing;
239 x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box));
240 secondary_x = allocation->x + allocation->width
241 - child_width * n_secondaries
242 - spacing * (n_secondaries - 1)
243 - gtk_container_get_border_width (GTK_CONTAINER (box));
245 case GTK_BUTTONBOX_END:
246 childspacing = spacing;
247 x = allocation->x + allocation->width
248 - child_width * (nvis_children - n_secondaries)
249 - spacing * (nvis_children - n_secondaries - 1)
250 - gtk_container_get_border_width (GTK_CONTAINER (box));
251 secondary_x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box));
256 y = allocation->y + (allocation->height - child_height) / 2;
257 childspace = child_width + childspacing;
259 children = gtk_container_get_children (GTK_CONTAINER (box));
263 GtkWidget *child = children->data;
264 children = children->next;
266 if (gtk_widget_get_visible (child))
268 gboolean is_secondary = FALSE;
269 gtk_container_child_get (GTK_CONTAINER (box), child, "secondary", &is_secondary, NULL);
272 child_allocation.width = child_width;
273 child_allocation.height = child_height;
274 child_allocation.y = y;
278 child_allocation.x = secondary_x;
279 secondary_x += childspace;
283 child_allocation.x = x;
287 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
288 child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
290 gtk_widget_size_allocate (child, &child_allocation);