Add support for reading SPSS/PC+ system files.
[pspp] / src / ui / gui / psppire-hbuttonbox.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include <glib.h>
21 #include <gtk/gtk.h>
22 #include "psppire-hbuttonbox.h"
23 #include "psppire-dialog.h"
24
25 #include <gettext.h>
26
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
29
30
31 static void psppire_hbuttonbox_class_init          (PsppireHButtonBoxClass *);
32 static void psppire_hbuttonbox_init                (PsppireHButtonBox      *);
33
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);
38
39
40 static const GtkButtonBoxStyle default_layout_style = GTK_BUTTONBOX_EDGE;
41
42 GType
43 psppire_hbutton_box_get_type (void)
44 {
45   static GType hbuttonbox_type = 0;
46
47   if (!hbuttonbox_type)
48     {
49       static const GTypeInfo hbuttonbox_info =
50       {
51         sizeof (PsppireHButtonBoxClass),
52         NULL, /* base_init */
53         NULL, /* base_finalize */
54         (GClassInitFunc) psppire_hbuttonbox_class_init,
55         NULL, /* class_finalize */
56         NULL, /* class_data */
57         sizeof (PsppireHButtonBox),
58         0,
59         (GInstanceInitFunc) psppire_hbuttonbox_init,
60       };
61
62       hbuttonbox_type = g_type_register_static (PSPPIRE_BUTTONBOX_TYPE,
63                                             "PsppireHButtonBox", &hbuttonbox_info, 0);
64     }
65
66   return hbuttonbox_type;
67 }
68
69 static void
70 psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *class)
71 {
72   GtkWidgetClass *widget_class;
73
74   widget_class = (GtkWidgetClass*) class;
75
76   widget_class->size_request = gtk_hbutton_box_size_request;
77   widget_class->size_allocate = gtk_hbutton_box_size_allocate;
78 }
79
80
81 static void
82 psppire_hbuttonbox_init (PsppireHButtonBox *hbuttonbox)
83 {
84 }
85
86
87 GtkWidget*
88 psppire_hbuttonbox_new (void)
89 {
90   PsppireHButtonBox *hbuttonbox ;
91
92   hbuttonbox = g_object_new (psppire_hbutton_box_get_type (), NULL);
93
94   return GTK_WIDGET (hbuttonbox) ;
95 }
96
97
98
99 /* The following two functions are lifted verbatim from
100    the Gtk2.10.6 library */
101
102 static void
103 gtk_hbutton_box_size_request (GtkWidget      *widget,
104                               GtkRequisition *requisition)
105 {
106   GtkBox *box;
107   GtkButtonBox *bbox;
108   gint nvis_children;
109   gint child_width;
110   gint child_height;
111   gint spacing;
112   GtkButtonBoxStyle layout;
113
114   box = GTK_BOX (widget);
115   bbox = GTK_BUTTON_BOX (widget);
116
117   spacing = box->spacing;
118   layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
119           ? bbox->layout_style : default_layout_style;
120
121   _psppire_button_box_child_requisition (widget,
122                                      &nvis_children,
123                                      NULL,
124                                      &child_width,
125                                      &child_height);
126
127   if (nvis_children == 0)
128   {
129     requisition->width = 0;
130     requisition->height = 0;
131   }
132   else
133   {
134     switch (layout)
135     {
136     case GTK_BUTTONBOX_SPREAD:
137       requisition->width =
138               nvis_children*child_width + ((nvis_children+1)*spacing);
139       break;
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);
144       break;
145     default:
146       g_assert_not_reached();
147       break;
148     }
149
150     requisition->height = child_height;
151   }
152
153   requisition->width += GTK_CONTAINER (box)->border_width * 2;
154   requisition->height += GTK_CONTAINER (box)->border_width * 2;
155 }
156
157
158
159 static void
160 gtk_hbutton_box_size_allocate (GtkWidget     *widget,
161                                GtkAllocation *allocation)
162 {
163   GtkBox *base_box;
164   GtkButtonBox *box;
165   GList *children;
166   GtkAllocation child_allocation;
167   gint nvis_children;
168   gint n_secondaries;
169   gint child_width;
170   gint child_height;
171   gint x = 0;
172   gint secondary_x = 0;
173   gint y = 0;
174   gint width;
175   gint childspace;
176   gint childspacing = 0;
177   GtkButtonBoxStyle layout;
178   gint spacing;
179
180   base_box = GTK_BOX (widget);
181   box = GTK_BUTTON_BOX (widget);
182   spacing = base_box->spacing;
183   layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
184           ? box->layout_style : default_layout_style;
185   _psppire_button_box_child_requisition (widget,
186                                      &nvis_children,
187                                      &n_secondaries,
188                                      &child_width,
189                                      &child_height);
190   widget->allocation = *allocation;
191   width = allocation->width - GTK_CONTAINER (box)->border_width*2;
192   switch (layout)
193   {
194   case GTK_BUTTONBOX_SPREAD:
195     childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
196     x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
197     secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
198     break;
199   case GTK_BUTTONBOX_EDGE:
200     if (nvis_children >= 2)
201       {
202         childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
203         x = allocation->x + GTK_CONTAINER (box)->border_width;
204         secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
205       }
206     else
207       {
208         /* one or zero children, just center */
209         childspacing = width;
210         x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
211       }
212     break;
213   case GTK_BUTTONBOX_START:
214     childspacing = spacing;
215     x = allocation->x + GTK_CONTAINER (box)->border_width;
216     secondary_x = allocation->x + allocation->width
217       - child_width * n_secondaries
218       - spacing * (n_secondaries - 1)
219       - GTK_CONTAINER (box)->border_width;
220     break;
221   case GTK_BUTTONBOX_END:
222     childspacing = spacing;
223     x = allocation->x + allocation->width
224       - child_width * (nvis_children - n_secondaries)
225       - spacing * (nvis_children - n_secondaries - 1)
226       - GTK_CONTAINER (box)->border_width;
227     secondary_x = allocation->x + GTK_CONTAINER (box)->border_width;
228     break;
229   default:
230     g_assert_not_reached();
231     break;
232   }
233
234
235   y = allocation->y + (allocation->height - child_height) / 2;
236   childspace = child_width + childspacing;
237
238   children = gtk_container_get_children (GTK_CONTAINER (box));
239
240   while (children)
241     {
242       GtkWidget *child = children->data;
243       children = children->next;
244
245       if (gtk_widget_get_visible (child))
246         {
247           gboolean is_secondary = FALSE;
248           gtk_container_child_get (GTK_CONTAINER (box), child, "secondary", &is_secondary, NULL);
249
250
251           child_allocation.width = child_width;
252           child_allocation.height = child_height;
253           child_allocation.y = y;
254
255           if (is_secondary)
256             {
257               child_allocation.x = secondary_x;
258               secondary_x += childspace;
259             }
260           else
261             {
262               child_allocation.x = x;
263               x += childspace;
264             }
265
266           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
267             child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
268
269           gtk_widget_size_allocate (child, &child_allocation);
270         }
271     }
272 }