]> Pileus Git - ~andy/gtk/blob - gtk/gtkvbbox.c
Various cleanups. (#315360, Kjartan Maraas)
[~andy/gtk] / gtk / gtkvbbox.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 "gtkvbbox.h"
29 #include "gtkintl.h"
30 #include "gtkalias.h"
31
32
33 static void gtk_vbutton_box_class_init    (GtkVButtonBoxClass   *klass);
34 static void gtk_vbutton_box_init          (GtkVButtonBox        *box);
35 static void gtk_vbutton_box_size_request  (GtkWidget      *widget,
36                                            GtkRequisition *requisition);
37 static void gtk_vbutton_box_size_allocate (GtkWidget      *widget,
38                                            GtkAllocation  *allocation);
39
40 static gint default_spacing = 10;
41 static GtkButtonBoxStyle default_layout_style = GTK_BUTTONBOX_EDGE;
42
43 GType
44 gtk_vbutton_box_get_type (void)
45 {
46   static GType vbutton_box_type = 0;
47
48   if (!vbutton_box_type)
49     {
50       static const GTypeInfo vbutton_box_info =
51       {
52         sizeof (GtkVButtonBoxClass),
53         NULL,           /* base_init */
54         NULL,           /* base_finalize */
55         (GClassInitFunc) gtk_vbutton_box_class_init,
56         NULL,           /* class_finalize */
57         NULL,           /* class_data */
58         sizeof (GtkVButtonBox),
59         0,              /* n_preallocs */
60         (GInstanceInitFunc) gtk_vbutton_box_init,
61       };
62
63       vbutton_box_type =
64         g_type_register_static (GTK_TYPE_BUTTON_BOX, I_("GtkVButtonBox"),
65                                 &vbutton_box_info, 0);
66     }
67
68   return vbutton_box_type;
69 }
70
71 static void
72 gtk_vbutton_box_class_init (GtkVButtonBoxClass *class)
73 {
74   GtkWidgetClass *widget_class;
75
76   widget_class = (GtkWidgetClass*) class;
77
78   widget_class->size_request = gtk_vbutton_box_size_request;
79   widget_class->size_allocate = gtk_vbutton_box_size_allocate;
80 }
81
82 static void
83 gtk_vbutton_box_init (GtkVButtonBox *vbutton_box)
84 {
85   /* button_box_init has done everything allready */
86 }
87
88 GtkWidget*
89 gtk_vbutton_box_new (void)
90 {
91   GtkVButtonBox *vbutton_box;
92
93   vbutton_box = g_object_new (GTK_TYPE_VBUTTON_BOX, NULL);
94
95   return GTK_WIDGET (vbutton_box);
96 }
97
98
99
100 /* set default value for spacing */
101
102 void
103 gtk_vbutton_box_set_spacing_default (gint spacing)
104 {
105   default_spacing = spacing;
106 }
107
108
109 /* set default value for layout style */
110
111 void
112 gtk_vbutton_box_set_layout_default (GtkButtonBoxStyle layout)
113 {
114   g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
115                     layout <= GTK_BUTTONBOX_END);
116
117   default_layout_style = layout;
118 }
119
120 /* get default value for spacing */
121
122 gint
123 gtk_vbutton_box_get_spacing_default (void)
124 {
125   return default_spacing;
126 }
127
128
129
130 /* get default value for layout style */
131
132 GtkButtonBoxStyle
133 gtk_vbutton_box_get_layout_default (void)
134 {
135   return default_layout_style;
136 }
137
138
139
140
141 static void
142 gtk_vbutton_box_size_request (GtkWidget      *widget,
143                               GtkRequisition *requisition)
144 {
145   GtkBox *box;
146   GtkButtonBox *bbox;
147   gint nvis_children;
148   gint child_width;
149   gint child_height;
150   gint spacing;
151   GtkButtonBoxStyle layout;
152   
153   box = GTK_BOX (widget);
154   bbox = GTK_BUTTON_BOX (widget);
155
156   spacing = box->spacing;
157   layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
158           ? bbox->layout_style : default_layout_style;
159   
160   _gtk_button_box_child_requisition (widget,
161                                      &nvis_children,
162                                      NULL,
163                                      &child_width,
164                                      &child_height);
165
166   if (nvis_children == 0)
167     {
168       requisition->width = 0; 
169       requisition->height = 0;
170     }
171   else
172     {
173       switch (layout)
174       {
175       case GTK_BUTTONBOX_SPREAD:
176         requisition->height =
177                 nvis_children*child_height + ((nvis_children+1)*spacing);
178         break;
179       case GTK_BUTTONBOX_EDGE:
180       case GTK_BUTTONBOX_START:
181       case GTK_BUTTONBOX_END:
182         requisition->height =
183                 nvis_children*child_height + ((nvis_children-1)*spacing);
184         break;
185       default:
186             g_assert_not_reached();
187             break;
188       }
189           
190       requisition->width = child_width;
191     }
192           
193   requisition->width += GTK_CONTAINER (box)->border_width * 2;
194   requisition->height += GTK_CONTAINER (box)->border_width * 2;
195 }
196
197
198
199 static void
200 gtk_vbutton_box_size_allocate (GtkWidget     *widget,
201                                GtkAllocation *allocation)
202 {
203   GtkBox *base_box;
204   GtkButtonBox *box;
205   GtkBoxChild *child;
206   GList *children;
207   GtkAllocation child_allocation;
208   gint nvis_children;
209   gint n_secondaries;
210   gint child_width;
211   gint child_height;
212   gint x = 0;
213   gint y = 0;
214   gint secondary_y = 0;
215   gint height;
216   gint childspace;
217   gint childspacing = 0;
218   GtkButtonBoxStyle layout;
219   gint spacing;
220   
221   base_box = GTK_BOX (widget);
222   box = GTK_BUTTON_BOX (widget);
223   spacing = base_box->spacing;
224   layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
225           ? box->layout_style : default_layout_style;
226   _gtk_button_box_child_requisition (widget,
227                                      &nvis_children,
228                                      &n_secondaries,
229                                      &child_width,
230                                      &child_height);
231   widget->allocation = *allocation;
232   height = allocation->height - GTK_CONTAINER (box)->border_width*2;
233   switch (layout)
234   {
235   case GTK_BUTTONBOX_SPREAD:
236     childspacing = (height - (nvis_children * child_height)) / (nvis_children + 1);
237     y = allocation->y + GTK_CONTAINER (box)->border_width + childspacing;
238     secondary_y = y + ((nvis_children - n_secondaries) * (child_height + childspacing));
239     break;
240   case GTK_BUTTONBOX_EDGE:
241     if (nvis_children >= 2)
242       {
243         childspacing = (height - (nvis_children*child_height)) / (nvis_children-1);
244         y = allocation->y + GTK_CONTAINER (box)->border_width;
245         secondary_y = y + ((nvis_children - n_secondaries) * (child_height + childspacing));
246       }
247     else
248       {
249         /* one or zero children, just center */
250         childspacing = height;
251         y = secondary_y = allocation->y + (allocation->height - child_height) / 2;
252       }
253     break;
254   case GTK_BUTTONBOX_START:
255     childspacing = spacing;
256     y = allocation->y + GTK_CONTAINER (box)->border_width;
257     secondary_y = allocation->y + allocation->height
258       - child_height * n_secondaries
259       - spacing * (n_secondaries - 1)
260       - GTK_CONTAINER (box)->border_width;
261     break;
262   case GTK_BUTTONBOX_END:
263     childspacing = spacing;
264     y = allocation->y + allocation->height 
265       - child_height * (nvis_children - n_secondaries)
266       - spacing * (nvis_children - n_secondaries - 1)
267       - GTK_CONTAINER (box)->border_width;
268     secondary_y = allocation->y + GTK_CONTAINER (box)->border_width;
269     break;
270   default:
271     g_assert_not_reached();
272     break;
273   }
274                   
275   
276   x = allocation->x + (allocation->width - child_width) / 2;
277   childspace = child_height + childspacing;
278
279   children = GTK_BOX (box)->children;
280           
281   while (children)
282     {
283       child = children->data;
284       children = children->next;
285
286       if (GTK_WIDGET_VISIBLE (child->widget))
287         {
288           child_allocation.width = child_width;
289           child_allocation.height = child_height;
290           child_allocation.x = x;
291           
292           if (child->is_secondary)
293             {
294               child_allocation.y = secondary_y;
295               secondary_y += childspace;
296             }
297           else
298             {
299               child_allocation.y = y;
300               y += childspace;
301             }
302           
303           gtk_widget_size_allocate (child->widget, &child_allocation);
304         }
305     }
306 }
307   
308 #define __GTK_VBBOX_C__  
309 #include "gtkaliasdef.c"