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