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