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