]> Pileus Git - ~andy/gtk/blob - gtk/gtkhbbox.c
Various cleanups. (#315360, Kjartan Maraas)
[~andy/gtk] / gtk / gtkhbbox.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28 #include "gtkhbbox.h"
29 #include "gtkintl.h"
30 #include "gtkalias.h"
31
32
33 static void gtk_hbutton_box_class_init    (GtkHButtonBoxClass   *klass);
34 static void gtk_hbutton_box_init          (GtkHButtonBox        *box);
35 static void gtk_hbutton_box_size_request  (GtkWidget      *widget,
36                                            GtkRequisition *requisition);
37 static void gtk_hbutton_box_size_allocate (GtkWidget      *widget,
38                                            GtkAllocation  *allocation);
39
40 static gint default_spacing = 30;
41 static gint default_layout_style = GTK_BUTTONBOX_EDGE;
42
43 GType
44 gtk_hbutton_box_get_type (void)
45 {
46   static GType hbutton_box_type = 0;
47
48   if (!hbutton_box_type)
49     {
50       static const GTypeInfo hbutton_box_info =
51       {
52         sizeof (GtkHButtonBoxClass),
53         NULL,           /* base_init */
54         NULL,           /* base_finalize */
55         (GClassInitFunc) gtk_hbutton_box_class_init,
56         NULL,           /* class_finalize */
57         NULL,           /* class_data */
58         sizeof (GtkHButtonBox),
59         0,              /* n_preallocs */
60         (GInstanceInitFunc) gtk_hbutton_box_init,
61       };
62
63       hbutton_box_type =
64         g_type_register_static (GTK_TYPE_BUTTON_BOX, I_("GtkHButtonBox"),
65                                 &hbutton_box_info, 0);
66     }
67
68   return hbutton_box_type;
69 }
70
71 static void
72 gtk_hbutton_box_class_init (GtkHButtonBoxClass *class)
73 {
74   GtkWidgetClass *widget_class;
75
76   widget_class = (GtkWidgetClass*) class;
77
78   widget_class->size_request = gtk_hbutton_box_size_request;
79   widget_class->size_allocate = gtk_hbutton_box_size_allocate;
80 }
81
82 static void
83 gtk_hbutton_box_init (GtkHButtonBox *hbutton_box)
84 {
85         /* button_box_init has done everything allready */
86 }
87
88 GtkWidget*
89 gtk_hbutton_box_new (void)
90 {
91   GtkHButtonBox *hbutton_box;
92
93   hbutton_box = g_object_new (GTK_TYPE_HBUTTON_BOX, NULL);
94
95   return GTK_WIDGET (hbutton_box);
96 }
97
98
99 /* set default value for spacing */
100
101 void 
102 gtk_hbutton_box_set_spacing_default (gint spacing)
103 {
104   default_spacing = spacing;
105 }
106
107
108 /* set default value for layout style */
109
110 void 
111 gtk_hbutton_box_set_layout_default (GtkButtonBoxStyle layout)
112 {
113   g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
114                     layout <= GTK_BUTTONBOX_END);
115
116   default_layout_style = layout;
117 }
118
119 /* get default value for spacing */
120
121 gint 
122 gtk_hbutton_box_get_spacing_default (void)
123 {
124   return default_spacing;
125 }
126
127
128 /* get default value for layout style */
129
130 GtkButtonBoxStyle
131 gtk_hbutton_box_get_layout_default (void)
132 {
133   return default_layout_style;
134 }
135
136
137   
138 static void
139 gtk_hbutton_box_size_request (GtkWidget      *widget,
140                               GtkRequisition *requisition)
141 {
142   GtkBox *box;
143   GtkButtonBox *bbox;
144   gint nvis_children;
145   gint child_width;
146   gint child_height;
147   gint spacing;
148   GtkButtonBoxStyle layout;
149   
150   box = GTK_BOX (widget);
151   bbox = GTK_BUTTON_BOX (widget);
152
153   spacing = box->spacing;
154   layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
155           ? bbox->layout_style : default_layout_style;
156   
157   _gtk_button_box_child_requisition (widget,
158                                      &nvis_children,
159                                      NULL,
160                                      &child_width,
161                                      &child_height);
162
163   if (nvis_children == 0)
164   {
165     requisition->width = 0; 
166     requisition->height = 0;
167   }
168   else
169   {
170     switch (layout)
171     {
172     case GTK_BUTTONBOX_SPREAD:
173       requisition->width =
174               nvis_children*child_width + ((nvis_children+1)*spacing);
175       break;
176     case GTK_BUTTONBOX_EDGE:
177     case GTK_BUTTONBOX_START:
178     case GTK_BUTTONBOX_END:
179       requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
180       break;
181     default:
182       g_assert_not_reached();
183       break;
184     }
185           
186     requisition->height = child_height;
187   }
188           
189   requisition->width += GTK_CONTAINER (box)->border_width * 2;
190   requisition->height += GTK_CONTAINER (box)->border_width * 2;
191 }
192
193
194
195 static void
196 gtk_hbutton_box_size_allocate (GtkWidget     *widget,
197                                GtkAllocation *allocation)
198 {
199   GtkBox *base_box;
200   GtkButtonBox *box;
201   GtkBoxChild *child;
202   GList *children;
203   GtkAllocation child_allocation;
204   gint nvis_children;
205   gint n_secondaries;
206   gint child_width;
207   gint child_height;
208   gint x = 0;
209   gint secondary_x = 0;
210   gint y = 0;
211   gint width;
212   gint childspace;
213   gint childspacing = 0;
214   GtkButtonBoxStyle layout;
215   gint spacing;
216   
217   base_box = GTK_BOX (widget);
218   box = GTK_BUTTON_BOX (widget);
219   spacing = base_box->spacing;
220   layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
221           ? box->layout_style : default_layout_style;
222   _gtk_button_box_child_requisition (widget,
223                                      &nvis_children,
224                                      &n_secondaries,
225                                      &child_width,
226                                      &child_height);
227   widget->allocation = *allocation;
228   width = allocation->width - GTK_CONTAINER (box)->border_width*2;
229   switch (layout)
230   {
231   case GTK_BUTTONBOX_SPREAD:
232     childspacing = (width - (nvis_children * child_width)) / (nvis_children + 1);
233     x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
234     secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
235     break;
236   case GTK_BUTTONBOX_EDGE:
237     if (nvis_children >= 2)
238       {
239         childspacing = (width - (nvis_children * child_width)) / (nvis_children - 1);
240         x = allocation->x + GTK_CONTAINER (box)->border_width;
241         secondary_x = x + ((nvis_children - n_secondaries) * (child_width + childspacing));
242       }
243     else
244       {
245         /* one or zero children, just center */
246         childspacing = width;
247         x = secondary_x = allocation->x + (allocation->width - child_width) / 2;
248       }
249     break;
250   case GTK_BUTTONBOX_START:
251     childspacing = spacing;
252     x = allocation->x + GTK_CONTAINER (box)->border_width;
253     secondary_x = allocation->x + allocation->width 
254       - child_width * n_secondaries
255       - spacing * (n_secondaries - 1)
256       - GTK_CONTAINER (box)->border_width;
257     break;
258   case GTK_BUTTONBOX_END:
259     childspacing = spacing;
260     x = allocation->x + allocation->width 
261       - child_width * (nvis_children - n_secondaries)
262       - spacing * (nvis_children - n_secondaries - 1)
263       - GTK_CONTAINER (box)->border_width;
264     secondary_x = allocation->x + GTK_CONTAINER (box)->border_width;
265     break;
266   default:
267     g_assert_not_reached();
268     break;
269   }
270
271                   
272   y = allocation->y + (allocation->height - child_height) / 2;
273   childspace = child_width + childspacing;
274
275   children = GTK_BOX (box)->children;
276           
277   while (children)
278     {
279       child = children->data;
280       children = children->next;
281
282       if (GTK_WIDGET_VISIBLE (child->widget))
283         {
284           child_allocation.width = child_width;
285           child_allocation.height = child_height;
286           child_allocation.y = y;
287           
288           if (child->is_secondary)
289             {
290               child_allocation.x = secondary_x;
291               secondary_x += childspace;
292             }
293           else
294             {
295               child_allocation.x = x;
296               x += childspace;
297             }
298
299           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
300             child_allocation.x = (allocation->x + allocation->width) - (child_allocation.x + child_width - allocation->x);
301           
302           gtk_widget_size_allocate (child->widget, &child_allocation);
303         }
304     }
305 }
306   
307 #define __GTK_HBUTTON_BOX_C__
308 #include "gtkaliasdef.c"