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