PsppSheetViewColumn: Avoid name conflict with Gtk3 standard headers
[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
41 GType
42 psppire_hbutton_box_get_type (void)
43 {
44   static GType hbuttonbox_type = 0;
45
46   if (!hbuttonbox_type)
47     {
48       static const GTypeInfo hbuttonbox_info =
49       {
50         sizeof (PsppireHButtonBoxClass),
51         NULL, /* base_init */
52         NULL, /* base_finalize */
53         (GClassInitFunc) psppire_hbuttonbox_class_init,
54         NULL, /* class_finalize */
55         NULL, /* class_data */
56         sizeof (PsppireHButtonBox),
57         0,
58         (GInstanceInitFunc) psppire_hbuttonbox_init,
59       };
60
61       hbuttonbox_type = g_type_register_static (PSPPIRE_BUTTONBOX_TYPE,
62                                             "PsppireHButtonBox", &hbuttonbox_info, 0);
63     }
64
65   return hbuttonbox_type;
66 }
67
68 static void
69 psppire_hbuttonbox_class_init (PsppireHButtonBoxClass *class)
70 {
71   GtkWidgetClass *widget_class;
72
73   widget_class = (GtkWidgetClass*) class;
74
75   widget_class->size_request = gtk_hbutton_box_size_request;
76   widget_class->size_allocate = gtk_hbutton_box_size_allocate;
77 }
78
79
80 static void
81 psppire_hbuttonbox_init (PsppireHButtonBox *hbuttonbox)
82 {
83 }
84
85
86 GtkWidget*
87 psppire_hbuttonbox_new (void)
88 {
89   PsppireHButtonBox *hbuttonbox ;
90
91   hbuttonbox = g_object_new (psppire_hbutton_box_get_type (), NULL);
92
93   return GTK_WIDGET (hbuttonbox) ;
94 }
95
96
97
98 /* The following two functions are lifted verbatim from
99    the Gtk2.10.6 library */
100
101 static void
102 gtk_hbutton_box_size_request (GtkWidget      *widget,
103                               GtkRequisition *requisition)
104 {
105   GtkBox *box;
106   GtkButtonBox *bbox;
107   gint nvis_children;
108   gint child_width;
109   gint child_height;
110   gint spacing;
111   GtkButtonBoxStyle layout;
112
113   box = GTK_BOX (widget);
114   bbox = GTK_BUTTON_BOX (widget);
115
116   spacing = gtk_box_get_spacing (box);
117   layout = gtk_button_box_get_layout (bbox) ;
118
119   _psppire_button_box_child_requisition (widget,
120                                      &nvis_children,
121                                      NULL,
122                                      &child_width,
123                                      &child_height);
124
125   if (nvis_children == 0)
126   {
127     requisition->width = 0;
128     requisition->height = 0;
129   }
130   else
131   {
132     switch (layout)
133     {
134     case GTK_BUTTONBOX_SPREAD:
135       requisition->width =
136               nvis_children*child_width + ((nvis_children+1)*spacing);
137       break;
138     default:
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);
143       break;
144     }
145
146     requisition->height = child_height;
147   }
148
149   requisition->width += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
150   requisition->height += gtk_container_get_border_width (GTK_CONTAINER (box)) * 2;
151 }
152
153
154
155 static void
156 gtk_hbutton_box_size_allocate (GtkWidget     *widget,
157                                GtkAllocation *allocation)
158 {
159   GtkBox *base_box;
160   GtkButtonBox *box;
161   GList *children;
162   GtkAllocation child_allocation;
163   gint nvis_children;
164   gint n_secondaries;
165   gint child_width;
166   gint child_height;
167   gint x = 0;
168   gint secondary_x = 0;
169   gint y = 0;
170   gint width;
171   gint childspace;
172   gint childspacing = 0;
173   GtkButtonBoxStyle layout;
174   gint spacing;
175
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,
181                                      &nvis_children,
182                                      &n_secondaries,
183                                      &child_width,
184                                      &child_height);
185   gtk_widget_set_allocation (widget, allocation);
186   width = allocation->width - gtk_container_get_border_width (GTK_CONTAINER (box))*2;
187   switch (layout)
188   {
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));
193     break;
194   default:
195   case GTK_BUTTONBOX_EDGE:
196     if (nvis_children >= 2)
197       {
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));
201       }
202     else
203       {
204         /* one or zero children, just center */
205         childspacing = width;
206         x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
207       }
208     break;
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));
216     break;
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));
224     break;
225   }
226
227
228   y = allocation->y + (allocation->height - child_height) / 2;
229   childspace = child_width + childspacing;
230
231   children = gtk_container_get_children (GTK_CONTAINER (box));
232
233   while (children)
234     {
235       GtkWidget *child = children->data;
236       children = children->next;
237
238       if (gtk_widget_get_visible (child))
239         {
240           gboolean is_secondary = FALSE;
241           gtk_container_child_get (GTK_CONTAINER (box), child, "secondary", &is_secondary, NULL);
242
243
244           child_allocation.width = child_width;
245           child_allocation.height = child_height;
246           child_allocation.y = y;
247
248           if (is_secondary)
249             {
250               child_allocation.x = secondary_x;
251               secondary_x += childspace;
252             }
253           else
254             {
255               child_allocation.x = x;
256               x += childspace;
257             }
258
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);
261
262           gtk_widget_size_allocate (child, &child_allocation);
263         }
264     }
265 }