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