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