]> Pileus Git - ~andy/gtk/blob - gtk/gtkhbbox.c
call the base class init fucntions from all parent types upon class
[~andy/gtk] / gtk / gtkhbbox.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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gtkhbbox.h"
21
22
23 static void gtk_hbutton_box_class_init    (GtkHButtonBoxClass   *klass);
24 static void gtk_hbutton_box_init          (GtkHButtonBox        *box);
25 static void gtk_hbutton_box_size_request  (GtkWidget      *widget,
26                                            GtkRequisition *requisition);
27 static void gtk_hbutton_box_size_allocate (GtkWidget      *widget,
28                                            GtkAllocation  *allocation);
29
30 static gint default_spacing = 30;
31 static gint default_layout_style = GTK_BUTTONBOX_EDGE;
32
33 guint
34 gtk_hbutton_box_get_type (void)
35 {
36   static guint hbutton_box_type = 0;
37
38   if (!hbutton_box_type)
39     {
40       GtkTypeInfo hbutton_box_info =
41       {
42         "GtkHButtonBox",
43         sizeof (GtkHButtonBox),
44         sizeof (GtkHButtonBoxClass),
45         (GtkClassInitFunc) gtk_hbutton_box_class_init,
46         (GtkObjectInitFunc) gtk_hbutton_box_init,
47         /* reversed_1 */ NULL,
48         /* reversed_2 */ NULL,
49         (GtkClassInitFunc) NULL,
50       };
51
52       hbutton_box_type = gtk_type_unique (gtk_button_box_get_type (), &hbutton_box_info);
53     }
54
55   return hbutton_box_type;
56 }
57
58 static void
59 gtk_hbutton_box_class_init (GtkHButtonBoxClass *class)
60 {
61   GtkWidgetClass *widget_class;
62
63   widget_class = (GtkWidgetClass*) class;
64
65   widget_class->size_request = gtk_hbutton_box_size_request;
66   widget_class->size_allocate = gtk_hbutton_box_size_allocate;
67 }
68
69 static void
70 gtk_hbutton_box_init (GtkHButtonBox *hbutton_box)
71 {
72         /* button_box_init has done everything allready */
73 }
74
75 GtkWidget*
76 gtk_hbutton_box_new (void)
77 {
78   GtkHButtonBox *hbutton_box;
79
80   hbutton_box = gtk_type_new (gtk_hbutton_box_get_type ());
81
82   return GTK_WIDGET (hbutton_box);
83 }
84
85
86 /* set default value for spacing */
87
88 void gtk_hbutton_box_set_spacing_default (gint spacing)
89 {
90   default_spacing = spacing;
91 }
92
93
94 /* set default value for layout style */
95
96 void gtk_hbutton_box_set_layout_default (GtkButtonBoxStyle layout)
97 {
98   g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
99                     layout <= GTK_BUTTONBOX_END);
100
101   default_layout_style = layout;
102 }
103
104 /* get default value for spacing */
105
106 gint gtk_hbutton_box_get_spacing_default (void)
107 {
108   return default_spacing;
109 }
110
111
112 /* get default value for layout style */
113
114 GtkButtonBoxStyle gtk_hbutton_box_get_layout_default (void)
115 {
116   return default_layout_style;
117 }
118
119
120   
121 static void
122 gtk_hbutton_box_size_request (GtkWidget      *widget,
123                               GtkRequisition *requisition)
124 {
125   GtkBox *box;
126   GtkButtonBox *bbox;
127   gint nvis_children;
128   gint child_width;
129   gint child_height;
130   gint spacing;
131   GtkButtonBoxStyle layout;
132   
133   g_return_if_fail (widget != NULL);
134   g_return_if_fail (GTK_IS_HBUTTON_BOX (widget));
135   g_return_if_fail (requisition != NULL);
136
137   box = GTK_BOX (widget);
138   bbox = GTK_BUTTON_BOX (widget);
139
140   spacing = bbox->spacing != GTK_BUTTONBOX_DEFAULT
141           ? bbox->spacing : default_spacing;
142   layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
143           ? bbox->layout_style : default_layout_style;
144   
145   gtk_button_box_child_requisition (widget,
146                                     &nvis_children,
147                                     &child_width,
148                                     &child_height);
149
150   if (nvis_children == 0)
151   {
152     requisition->width = 0; 
153     requisition->height = 0;
154   }
155   else
156   {
157     switch (layout)
158     {
159     case GTK_BUTTONBOX_SPREAD:
160       requisition->width =
161               nvis_children*child_width + ((nvis_children+1)*spacing);
162       break;
163     case GTK_BUTTONBOX_EDGE:
164     case GTK_BUTTONBOX_START:
165     case GTK_BUTTONBOX_END:
166       requisition->width = nvis_children*child_width + ((nvis_children-1)*spacing);
167       break;
168     default:
169       g_assert_not_reached();
170       break;
171     }
172           
173     requisition->height = child_height;
174   }
175           
176   requisition->width += GTK_CONTAINER (box)->border_width * 2;
177   requisition->height += GTK_CONTAINER (box)->border_width * 2;
178 }
179
180
181
182 static void
183 gtk_hbutton_box_size_allocate (GtkWidget     *widget,
184                                GtkAllocation *allocation)
185 {
186   GtkButtonBox *box;
187   GtkHButtonBox *hbox;
188   GtkBoxChild *child;
189   GList *children;
190   GtkAllocation child_allocation;
191   gint nvis_children;
192   gint child_width;
193   gint child_height;
194   gint x = 0;
195   gint y = 0;
196   gint width;
197   gint childspace;
198   gint childspacing = 0;
199   GtkButtonBoxStyle layout;
200   gint spacing;
201   
202   g_return_if_fail (widget != NULL);
203   g_return_if_fail (GTK_IS_HBUTTON_BOX (widget));
204   g_return_if_fail (allocation != NULL);
205
206   box = GTK_BUTTON_BOX (widget);
207   hbox = GTK_HBUTTON_BOX (widget);
208   spacing = box->spacing != GTK_BUTTONBOX_DEFAULT
209           ? box->spacing : default_spacing;
210   layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
211           ? box->layout_style : default_layout_style;
212   gtk_button_box_child_requisition (widget,
213                                     &nvis_children,
214                                     &child_width,
215                                     &child_height);
216   widget->allocation = *allocation;
217   width = allocation->width - GTK_CONTAINER (box)->border_width*2;
218   switch (layout)
219   {
220   case GTK_BUTTONBOX_SPREAD:
221     childspacing = (width - (nvis_children*child_width)) / (nvis_children+1);
222     x = allocation->x + GTK_CONTAINER (box)->border_width + childspacing;
223     break;
224   case GTK_BUTTONBOX_EDGE:
225     if (nvis_children >= 2)
226     {
227       childspacing =
228               (width - (nvis_children*child_width)) / (nvis_children-1);
229       x = allocation->x + GTK_CONTAINER (box)->border_width;
230     }
231     else
232       {
233                       /* one or zero children, just center */
234         childspacing = width;
235         x = allocation->x + (allocation->width - child_width) / 2;
236       }
237     break;
238   case GTK_BUTTONBOX_START:
239     childspacing = spacing;
240     x = allocation->x + GTK_CONTAINER (box)->border_width;
241     break;
242   case GTK_BUTTONBOX_END:
243     childspacing = spacing;
244     x = allocation->x + allocation->width - child_width * nvis_children
245             - spacing *(nvis_children-1)
246             - GTK_CONTAINER (box)->border_width;
247     break;
248   default:
249     g_assert_not_reached();
250     break;
251   }
252                   
253   
254   y = allocation->y + (allocation->height - child_height) / 2;
255   childspace = child_width + childspacing;
256
257   children = GTK_BOX (box)->children;
258           
259   while (children)
260     {
261       child = children->data;
262       children = children->next;
263
264       if (GTK_WIDGET_VISIBLE (child->widget))
265         {
266           child_allocation.width = child_width;
267           child_allocation.height = child_height;
268           child_allocation.x = x;
269           child_allocation.y = y;
270           gtk_widget_size_allocate (child->widget, &child_allocation);
271           x += childspace;
272         }
273     }
274 }
275