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