]> Pileus Git - ~andy/gtk/blob - gtk/gtkvbox.c
43a0ab431c95e6a553a36de082d3e71c3953b062
[~andy/gtk] / gtk / gtkvbox.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 "gtkvbox.h"
29 #include "gtkalias.h"
30
31
32 static void gtk_vbox_class_init    (GtkVBoxClass   *klass);
33 static void gtk_vbox_init          (GtkVBox        *box);
34 static void gtk_vbox_size_request  (GtkWidget      *widget,
35                                     GtkRequisition *requisition);
36 static void gtk_vbox_size_allocate (GtkWidget      *widget,
37                                     GtkAllocation  *allocation);
38
39
40 GType
41 gtk_vbox_get_type (void)
42 {
43   static GType vbox_type = 0;
44
45   if (!vbox_type)
46     {
47       static const GTypeInfo vbox_info =
48       {
49         sizeof (GtkVBoxClass),
50         NULL,           /* base_init */
51         NULL,           /* base_finalize */
52         (GClassInitFunc) gtk_vbox_class_init,
53         NULL,           /* class_finalize */
54         NULL,           /* class_data */
55         sizeof (GtkVBox),
56         0,              /* n_preallocs */
57         (GInstanceInitFunc) gtk_vbox_init,
58       };
59
60       vbox_type = g_type_register_static (GTK_TYPE_BOX, g_intern_static_string ("GtkVBox"),
61                                           &vbox_info, 0);
62     }
63
64   return vbox_type;
65 }
66
67 static void
68 gtk_vbox_class_init (GtkVBoxClass *class)
69 {
70   GtkWidgetClass *widget_class;
71
72   widget_class = (GtkWidgetClass*) class;
73
74   widget_class->size_request = gtk_vbox_size_request;
75   widget_class->size_allocate = gtk_vbox_size_allocate;
76 }
77
78 static void
79 gtk_vbox_init (GtkVBox *vbox)
80 {
81 }
82
83 GtkWidget*
84 gtk_vbox_new (gboolean homogeneous,
85               gint spacing)
86 {
87   GtkVBox *vbox;
88
89   vbox = g_object_new (GTK_TYPE_VBOX, NULL);
90
91   GTK_BOX (vbox)->spacing = spacing;
92   GTK_BOX (vbox)->homogeneous = homogeneous ? TRUE : FALSE;
93
94   return GTK_WIDGET (vbox);
95 }
96
97
98 static void
99 gtk_vbox_size_request (GtkWidget      *widget,
100                        GtkRequisition *requisition)
101 {
102   GtkBox *box;
103   GtkBoxChild *child;
104   GtkRequisition child_requisition;
105   GList *children;
106   gint nvis_children;
107   gint height;
108
109   box = GTK_BOX (widget);
110   requisition->width = 0;
111   requisition->height = 0;
112   nvis_children = 0;
113
114   children = box->children;
115   while (children)
116     {
117       child = children->data;
118       children = children->next;
119
120       if (GTK_WIDGET_VISIBLE (child->widget))
121         {
122           gtk_widget_size_request (child->widget, &child_requisition);
123
124           if (box->homogeneous)
125             {
126               height = child_requisition.height + child->padding * 2;
127               requisition->height = MAX (requisition->height, height);
128             }
129           else
130             {
131               requisition->height += child_requisition.height + child->padding * 2;
132             }
133
134           requisition->width = MAX (requisition->width, child_requisition.width);
135
136           nvis_children += 1;
137         }
138     }
139
140   if (nvis_children > 0)
141     {
142       if (box->homogeneous)
143         requisition->height *= nvis_children;
144       requisition->height += (nvis_children - 1) * box->spacing;
145     }
146
147   requisition->width += GTK_CONTAINER (box)->border_width * 2;
148   requisition->height += GTK_CONTAINER (box)->border_width * 2;
149 }
150
151 static void
152 gtk_vbox_size_allocate (GtkWidget     *widget,
153                         GtkAllocation *allocation)
154 {
155   GtkBox *box;
156   GtkBoxChild *child;
157   GList *children;
158   GtkAllocation child_allocation;
159   gint nvis_children;
160   gint nexpand_children;
161   gint child_height;
162   gint height;
163   gint extra;
164   gint y;
165
166   box = GTK_BOX (widget);
167   widget->allocation = *allocation;
168
169   nvis_children = 0;
170   nexpand_children = 0;
171   children = box->children;
172
173   while (children)
174     {
175       child = children->data;
176       children = children->next;
177
178       if (GTK_WIDGET_VISIBLE (child->widget))
179         {
180           nvis_children += 1;
181           if (child->expand)
182             nexpand_children += 1;
183         }
184     }
185
186   if (nvis_children > 0)
187     {
188       if (box->homogeneous)
189         {
190           height = (allocation->height -
191                    GTK_CONTAINER (box)->border_width * 2 -
192                    (nvis_children - 1) * box->spacing);
193           extra = height / nvis_children;
194         }
195       else if (nexpand_children > 0)
196         {
197           height = (gint) allocation->height - (gint) widget->requisition.height;
198           extra = height / nexpand_children;
199         }
200       else
201         {
202           height = 0;
203           extra = 0;
204         }
205
206       y = allocation->y + GTK_CONTAINER (box)->border_width;
207       child_allocation.x = allocation->x + GTK_CONTAINER (box)->border_width;
208       child_allocation.width = MAX (1, (gint) allocation->width - (gint) GTK_CONTAINER (box)->border_width * 2);
209
210       children = box->children;
211       while (children)
212         {
213           child = children->data;
214           children = children->next;
215
216           if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
217             {
218               if (box->homogeneous)
219                 {
220                   if (nvis_children == 1)
221                     child_height = height;
222                   else
223                     child_height = extra;
224
225                   nvis_children -= 1;
226                   height -= extra;
227                 }
228               else
229                 {
230                   GtkRequisition child_requisition;
231
232                   gtk_widget_get_child_requisition (child->widget, &child_requisition);
233                   child_height = child_requisition.height + child->padding * 2;
234
235                   if (child->expand)
236                     {
237                       if (nexpand_children == 1)
238                         child_height += height;
239                       else
240                         child_height += extra;
241
242                       nexpand_children -= 1;
243                       height -= extra;
244                     }
245                 }
246
247               if (child->fill)
248                 {
249                   child_allocation.height = MAX (1, child_height - (gint)child->padding * 2);
250                   child_allocation.y = y + child->padding;
251                 }
252               else
253                 {
254                   GtkRequisition child_requisition;
255
256                   gtk_widget_get_child_requisition (child->widget, &child_requisition);
257                   child_allocation.height = child_requisition.height;
258                   child_allocation.y = y + (child_height - child_allocation.height) / 2;
259                 }
260
261               gtk_widget_size_allocate (child->widget, &child_allocation);
262
263               y += child_height + box->spacing;
264             }
265         }
266
267       y = allocation->y + allocation->height - GTK_CONTAINER (box)->border_width;
268
269       children = box->children;
270       while (children)
271         {
272           child = children->data;
273           children = children->next;
274
275           if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
276             {
277               GtkRequisition child_requisition;
278               gtk_widget_get_child_requisition (child->widget, &child_requisition);
279
280               if (box->homogeneous)
281                 {
282                   if (nvis_children == 1)
283                     child_height = height;
284                   else
285                     child_height = extra;
286
287                   nvis_children -= 1;
288                   height -= extra;
289                 }
290               else
291                 {
292                   child_height = child_requisition.height + child->padding * 2;
293
294                   if (child->expand)
295                     {
296                       if (nexpand_children == 1)
297                         child_height += height;
298                       else
299                         child_height += extra;
300
301                       nexpand_children -= 1;
302                       height -= extra;
303                     }
304                 }
305
306               if (child->fill)
307                 {
308                   child_allocation.height = MAX (1, child_height - (gint)child->padding * 2);
309                   child_allocation.y = y + child->padding - child_height;
310                 }
311               else
312                 {
313                   child_allocation.height = child_requisition.height;
314                   child_allocation.y = y + (child_height - child_allocation.height) / 2 - child_height;
315                 }
316
317               gtk_widget_size_allocate (child->widget, &child_allocation);
318
319               y -= (child_height + box->spacing);
320             }
321         }
322     }
323 }
324
325 #define __GTK_VBOX_C__
326 #include "gtkaliasdef.c"