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