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