]> Pileus Git - ~andy/gtk/blob - gtk/gtkvbbox.c
Skip insensitive widgets. (#84061)
[~andy/gtk] / gtk / gtkvbbox.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 "gtkvbbox.h"
28
29
30 static void gtk_vbutton_box_class_init    (GtkVButtonBoxClass   *klass);
31 static void gtk_vbutton_box_init          (GtkVButtonBox        *box);
32 static void gtk_vbutton_box_size_request  (GtkWidget      *widget,
33                                            GtkRequisition *requisition);
34 static void gtk_vbutton_box_size_allocate (GtkWidget      *widget,
35                                            GtkAllocation  *allocation);
36
37 static gint default_spacing = 10;
38 static GtkButtonBoxStyle default_layout_style = GTK_BUTTONBOX_EDGE;
39
40 GType
41 gtk_vbutton_box_get_type (void)
42 {
43   static GType vbutton_box_type = 0;
44
45   if (!vbutton_box_type)
46     {
47       static const GTypeInfo vbutton_box_info =
48       {
49         sizeof (GtkVButtonBoxClass),
50         NULL,           /* base_init */
51         NULL,           /* base_finalize */
52         (GClassInitFunc) gtk_vbutton_box_class_init,
53         NULL,           /* class_finalize */
54         NULL,           /* class_data */
55         sizeof (GtkVButtonBox),
56         0,              /* n_preallocs */
57         (GInstanceInitFunc) gtk_vbutton_box_init,
58       };
59
60       vbutton_box_type =
61         g_type_register_static (GTK_TYPE_BUTTON_BOX, "GtkVButtonBox",
62                                 &vbutton_box_info, 0);
63     }
64
65   return vbutton_box_type;
66 }
67
68 static void
69 gtk_vbutton_box_class_init (GtkVButtonBoxClass *class)
70 {
71   GtkWidgetClass *widget_class;
72
73   widget_class = (GtkWidgetClass*) class;
74
75   widget_class->size_request = gtk_vbutton_box_size_request;
76   widget_class->size_allocate = gtk_vbutton_box_size_allocate;
77 }
78
79 static void
80 gtk_vbutton_box_init (GtkVButtonBox *vbutton_box)
81 {
82   /* button_box_init has done everything allready */
83 }
84
85 GtkWidget*
86 gtk_vbutton_box_new (void)
87 {
88   GtkVButtonBox *vbutton_box;
89
90   vbutton_box = g_object_new (GTK_TYPE_VBUTTON_BOX, NULL);
91
92   return GTK_WIDGET (vbutton_box);
93 }
94
95
96
97 /* set default value for spacing */
98
99 void
100 gtk_vbutton_box_set_spacing_default (gint spacing)
101 {
102   default_spacing = spacing;
103 }
104
105
106 /* set default value for layout style */
107
108 void
109 gtk_vbutton_box_set_layout_default (GtkButtonBoxStyle layout)
110 {
111   g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
112                     layout <= GTK_BUTTONBOX_END);
113
114   default_layout_style = layout;
115 }
116
117 /* get default value for spacing */
118
119 gint
120 gtk_vbutton_box_get_spacing_default (void)
121 {
122   return default_spacing;
123 }
124
125
126
127 /* get default value for layout style */
128
129 GtkButtonBoxStyle
130 gtk_vbutton_box_get_layout_default (void)
131 {
132   return default_layout_style;
133 }
134
135
136
137
138 static void
139 gtk_vbutton_box_size_request (GtkWidget      *widget,
140                               GtkRequisition *requisition)
141 {
142   GtkBox *box;
143   GtkButtonBox *bbox;
144   gint nvis_children;
145   gint child_width;
146   gint child_height;
147   gint spacing;
148   GtkButtonBoxStyle layout;
149   
150   box = GTK_BOX (widget);
151   bbox = GTK_BUTTON_BOX (widget);
152
153   spacing = box->spacing;
154   layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
155           ? bbox->layout_style : default_layout_style;
156   
157   _gtk_button_box_child_requisition (widget,
158                                      &nvis_children,
159                                      NULL,
160                                      &child_width,
161                                      &child_height);
162
163   if (nvis_children == 0)
164     {
165       requisition->width = 0; 
166       requisition->height = 0;
167     }
168   else
169     {
170       switch (layout)
171       {
172       case GTK_BUTTONBOX_SPREAD:
173         requisition->height =
174                 nvis_children*child_height + ((nvis_children+1)*spacing);
175         break;
176       case GTK_BUTTONBOX_EDGE:
177       case GTK_BUTTONBOX_START:
178       case GTK_BUTTONBOX_END:
179         requisition->height =
180                 nvis_children*child_height + ((nvis_children-1)*spacing);
181         break;
182       default:
183             g_assert_not_reached();
184             break;
185       }
186           
187       requisition->width = child_width;
188     }
189           
190   requisition->width += GTK_CONTAINER (box)->border_width * 2;
191   requisition->height += GTK_CONTAINER (box)->border_width * 2;
192 }
193
194
195
196 static void
197 gtk_vbutton_box_size_allocate (GtkWidget     *widget,
198                                GtkAllocation *allocation)
199 {
200   GtkBox *base_box;
201   GtkButtonBox *box;
202   GtkVButtonBox *hbox;
203   GtkBoxChild *child;
204   GList *children;
205   GtkAllocation child_allocation;
206   gint nvis_children;
207   gint n_secondaries;
208   gint child_width;
209   gint child_height;
210   gint x = 0;
211   gint y = 0;
212   gint secondary_y = 0;
213   gint height;
214   gint childspace;
215   gint childspacing = 0;
216   GtkButtonBoxStyle layout;
217   gint spacing;
218   
219   base_box = GTK_BOX (widget);
220   box = GTK_BUTTON_BOX (widget);
221   hbox = GTK_VBUTTON_BOX (widget);
222   spacing = base_box->spacing;
223   layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
224           ? box->layout_style : default_layout_style;
225   _gtk_button_box_child_requisition (widget,
226                                      &nvis_children,
227                                      &n_secondaries,
228                                      &child_width,
229                                      &child_height);
230   widget->allocation = *allocation;
231   height = allocation->height - GTK_CONTAINER (box)->border_width*2;
232   switch (layout)
233   {
234   case GTK_BUTTONBOX_SPREAD:
235     childspacing = (height - (nvis_children * child_height)) / (nvis_children + 1);
236     y = allocation->y + GTK_CONTAINER (box)->border_width + childspacing;
237     secondary_y = y + ((nvis_children - n_secondaries) * (child_height + childspacing));
238     break;
239   case GTK_BUTTONBOX_EDGE:
240     if (nvis_children >= 2)
241       {
242         childspacing = (height - (nvis_children*child_height)) / (nvis_children-1);
243         y = allocation->y + GTK_CONTAINER (box)->border_width;
244         secondary_y = y + ((nvis_children - n_secondaries) * (child_height + childspacing));
245       }
246     else
247       {
248         /* one or zero children, just center */
249         childspacing = height;
250         y = secondary_y = allocation->y + (allocation->height - child_height) / 2;
251       }
252     break;
253   case GTK_BUTTONBOX_START:
254     childspacing = spacing;
255     y = allocation->y + GTK_CONTAINER (box)->border_width;
256     secondary_y = allocation->y + allocation->height
257       - child_height * n_secondaries
258       - spacing * (n_secondaries - 1)
259       - GTK_CONTAINER (box)->border_width;
260     break;
261   case GTK_BUTTONBOX_END:
262     childspacing = spacing;
263     y = allocation->y + allocation->height 
264       - child_height * (nvis_children - n_secondaries)
265       - spacing * (nvis_children - n_secondaries - 1)
266       - GTK_CONTAINER (box)->border_width;
267     secondary_y = allocation->y + GTK_CONTAINER (box)->border_width;
268     break;
269   default:
270     g_assert_not_reached();
271     break;
272   }
273                   
274   
275   x = allocation->x + (allocation->width - child_width) / 2;
276   childspace = child_height + childspacing;
277
278   children = GTK_BOX (box)->children;
279           
280   while (children)
281     {
282       child = children->data;
283       children = children->next;
284
285       if (GTK_WIDGET_VISIBLE (child->widget))
286         {
287           child_allocation.width = child_width;
288           child_allocation.height = child_height;
289           child_allocation.x = x;
290           
291           if (child->is_secondary)
292             {
293               child_allocation.y = secondary_y;
294               secondary_y += childspace;
295             }
296           else
297             {
298               child_allocation.y = y;
299               y += childspace;
300             }
301           
302           gtk_widget_size_allocate (child->widget, &child_allocation);
303         }
304     }
305 }
306   
307