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