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