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