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);
42 psppire_hbutton_box_get_type (void)
44 static GType hbuttonbox_type = 0;
48 static const GTypeInfo hbuttonbox_info =
50 sizeof (PsppireHButtonBoxClass),
52 NULL, /* base_finalize */
53 (GClassInitFunc) psppire_hbuttonbox_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
56 sizeof (PsppireHButtonBox),
58 (GInstanceInitFunc) psppire_hbuttonbox_init,
61 hbuttonbox_type = g_type_register_static (PSPPIRE_BUTTONBOX_TYPE,
62 "PsppireHButtonBox", &hbuttonbox_info, 0);
65 return hbuttonbox_type;
69 psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *class)
71 GtkWidgetClass *widget_class;
73 widget_class = (GtkWidgetClass*) class;
75 widget_class->size_request = gtk_hbutton_box_size_request;
76 widget_class->size_allocate = gtk_hbutton_box_size_allocate;
81 psppire_hbuttonbox_init (PsppireHButtonBox *hbuttonbox)
87 psppire_hbuttonbox_new (void)
89 PsppireHButtonBox *hbuttonbox ;
91 hbuttonbox = g_object_new (psppire_hbutton_box_get_type (), NULL);
93 return GTK_WIDGET (hbuttonbox) ;
98 /* The following two functions are lifted verbatim from
99 the Gtk2.10.6 library */
102 gtk_hbutton_box_size_request (GtkWidget *widget,
103 GtkRequisition *requisition)
111 GtkButtonBoxStyle layout;
113 box = GTK_BOX (widget);
114 bbox = GTK_BUTTON_BOX (widget);
116 spacing = gtk_box_get_spacing (box);
117 layout = gtk_button_box_get_layout (bbox) ;
119 _psppire_button_box_child_requisition (widget,
125 if (nvis_children == 0)
127 requisition->width = 0;
128 requisition->height = 0;
134 case GTK_BUTTONBOX_SPREAD:
136 nvis_children*child_width + ((nvis_children+1)*spacing);
139 case GTK_BUTTONBOX_EDGE:
140 case GTK_BUTTONBOX_START:
141 case GTK_BUTTONBOX_END:
142 requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
146 requisition->height = child_height;
149 requisition->width += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
150 requisition->height += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
156 gtk_hbutton_box_size_allocate (GtkWidget *widget,
157 GtkAllocation *allocation)
162 GtkAllocation child_allocation;
168 gint secondary_x = 0;
172 gint childspacing = 0;
173 GtkButtonBoxStyle layout;
176 base_box = GTK_BOX (widget);
177 box = GTK_BUTTON_BOX (widget);
178 spacing = gtk_box_get_spacing (base_box);
179 layout = gtk_button_box_get_layout (box) ;
180 _psppire_button_box_child_requisition (widget,
185 gtk_widget_set_allocation (widget, allocation);
186 width = allocation->width - gtk_container_get_border_width (GTK_CONTAINER (box))*2;
189 case GTK_BUTTONBOX_SPREAD:
190 childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
191 x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box)) + childspacing;
192 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
195 case GTK_BUTTONBOX_EDGE:
196 if (nvis_children >= 2)
198 childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
199 x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box));
200 secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
204 /* one or zero children, just center */
205 childspacing = width;
206 x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
209 case GTK_BUTTONBOX_START:
210 childspacing = spacing;
211 x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box));
212 secondary_x = allocation->x + allocation->width
213 - child_width * n_secondaries
214 - spacing * (n_secondaries - 1)
215 - gtk_container_get_border_width (GTK_CONTAINER (box));
217 case GTK_BUTTONBOX_END:
218 childspacing = spacing;
219 x = allocation->x + allocation->width
220 - child_width * (nvis_children - n_secondaries)
221 - spacing * (nvis_children - n_secondaries - 1)
222 - gtk_container_get_border_width (GTK_CONTAINER (box));
223 secondary_x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (box));
228 y = allocation->y + (allocation->height - child_height) / 2;
229 childspace = child_width + childspacing;
231 children = gtk_container_get_children (GTK_CONTAINER (box));
235 GtkWidget *child = children->data;
236 children = children->next;
238 if (gtk_widget_get_visible (child))
240 gboolean is_secondary = FALSE;
241 gtk_container_child_get (GTK_CONTAINER (box), child, "secondary", &is_secondary, NULL);
244 child_allocation.width = child_width;
245 child_allocation.height = child_height;
246 child_allocation.y = y;
250 child_allocation.x = secondary_x;
251 secondary_x += childspace;
255 child_allocation.x = x;
259 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
260 child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
262 gtk_widget_size_allocate (child, &child_allocation);