Merge 'master' into 'gtk3'.
[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 void
41 psppire_hbutton_box_get_preferred_height (GtkWidget *widget,
42                                 gint      *minimal_height,
43                                 gint      *natural_height)
44 {
45   GtkRequisition requisition;
46
47   gtk_hbutton_box_size_request (widget, &requisition);
48
49   *minimal_height = *natural_height = requisition.height;
50 }
51
52
53 static void
54 psppire_hbutton_box_get_preferred_width (GtkWidget *widget,
55                                 gint      *minimal_width,
56                                 gint      *natural_width)
57 {
58   GtkRequisition requisition;
59
60   gtk_hbutton_box_size_request (widget, &requisition);
61
62   *minimal_width = *natural_width = requisition.width;
63 }
64
65
66
67
68 GType
69 psppire_hbutton_box_get_type (void)
70 {
71   static GType hbuttonbox_type = 0;
72
73   if (!hbuttonbox_type)
74     {
75       static const GTypeInfo hbuttonbox_info =
76       {
77         sizeof (PsppireHButtonBoxClass),
78         NULL, /* base_init */
79         NULL, /* base_finalize */
80         (GClassInitFunc) psppire_hbuttonbox_class_init,
81         NULL, /* class_finalize */
82         NULL, /* class_data */
83         sizeof (PsppireHButtonBox),
84         0,
85         (GInstanceInitFunc) psppire_hbuttonbox_init,
86       };
87
88       hbuttonbox_type = g_type_register_static (PSPPIRE_BUTTONBOX_TYPE,
89                                             "PsppireHButtonBox", &hbuttonbox_info, 0);
90     }
91
92   return hbuttonbox_type;
93 }
94
95 static void
96 psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *class)
97 {
98   GtkWidgetClass *widget_class;
99
100   widget_class = (GtkWidgetClass*) class;
101
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;
105 }
106
107
108 static void
109 psppire_hbuttonbox_init (PsppireHButtonBox *hbuttonbox)
110 {
111 }
112
113
114 GtkWidget*
115 psppire_hbuttonbox_new (void)
116 {
117   PsppireHButtonBox *hbuttonbox ;
118
119   hbuttonbox = g_object_new (psppire_hbutton_box_get_type (), NULL);
120
121   return GTK_WIDGET (hbuttonbox) ;
122 }
123
124
125
126 /* The following two functions are lifted verbatim from
127    the Gtk2.10.6 library */
128
129 static void
130 gtk_hbutton_box_size_request (GtkWidget      *widget,
131                               GtkRequisition *requisition)
132 {
133   GtkBox *box;
134   GtkButtonBox *bbox;
135   gint nvis_children;
136   gint child_width;
137   gint child_height;
138   gint spacing;
139   GtkButtonBoxStyle layout;
140
141   box = GTK_BOX (widget);
142   bbox = GTK_BUTTON_BOX (widget);
143
144   spacing = gtk_box_get_spacing (box);
145   layout = gtk_button_box_get_layout (bbox) ;
146
147   _psppire_button_box_child_requisition (widget,
148                                      &nvis_children,
149                                      NULL,
150                                      &child_width,
151                                      &child_height);
152
153   if (nvis_children == 0)
154   {
155     requisition->width = 0;
156     requisition->height = 0;
157   }
158   else
159   {
160     switch (layout)
161     {
162     case GTK_BUTTONBOX_SPREAD:
163       requisition->width =
164               nvis_children*child_width + ((nvis_children+1)*spacing);
165       break;
166     default:
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);
171       break;
172     }
173
174     requisition->height = child_height;
175   }
176
177   requisition->width += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
178   requisition->height += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
179 }
180
181
182
183 static void
184 gtk_hbutton_box_size_allocate (GtkWidget     *widget,
185                                GtkAllocation *allocation)
186 {
187   GtkBox *base_box;
188   GtkButtonBox *box;
189   GList *children;
190   GtkAllocation child_allocation;
191   gint nvis_children;
192   gint n_secondaries;
193   gint child_width;
194   gint child_height;
195   gint x = 0;
196   gint secondary_x = 0;
197   gint y = 0;
198   gint width;
199   gint childspace;
200   gint childspacing = 0;
201   GtkButtonBoxStyle layout;
202   gint spacing;
203
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,
209                                      &nvis_children,
210                                      &n_secondaries,
211                                      &child_width,
212                                      &child_height);
213   gtk_widget_set_allocation (widget, allocation);
214   width = allocation->width - gtk_container_get_border_width (GTK_CONTAINER (box))*2;
215   switch (layout)
216   {
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));
221     break;
222   default:
223   case GTK_BUTTONBOX_EDGE:
224     if (nvis_children >= 2)
225       {
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));
229       }
230     else
231       {
232         /* one or zero children, just center */
233         childspacing = width;
234         x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
235       }
236     break;
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));
244     break;
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));
252     break;
253   }
254
255
256   y = allocation->y + (allocation->height - child_height) / 2;
257   childspace = child_width + childspacing;
258
259   children = gtk_container_get_children (GTK_CONTAINER (box));
260
261   while (children)
262     {
263       GtkWidget *child = children->data;
264       children = children->next;
265
266       if (gtk_widget_get_visible (child))
267         {
268           gboolean is_secondary = FALSE;
269           gtk_container_child_get (GTK_CONTAINER (box), child, "secondary", &is_secondary, NULL);
270
271
272           child_allocation.width = child_width;
273           child_allocation.height = child_height;
274           child_allocation.y = y;
275
276           if (is_secondary)
277             {
278               child_allocation.x = secondary_x;
279               secondary_x += childspace;
280             }
281           else
282             {
283               child_allocation.x = x;
284               x += childspace;
285             }
286
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);
289
290           gtk_widget_size_allocate (child, &child_allocation);
291         }
292     }
293 }