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