]> Pileus Git - ~andy/gtk/blob - gtk/gtkbbox.c
Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>
[~andy/gtk] / gtk / gtkbbox.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 "gtkbbox.h"
29 #include "gtkprivate.h"
30 #include "gtkintl.h"
31 #include "gtkalias.h"
32
33 enum {
34   PROP_0,
35   PROP_LAYOUT_STYLE,
36   PROP_LAST
37 };
38
39 enum {
40   CHILD_PROP_0,
41   CHILD_PROP_SECONDARY
42 };
43
44 static void gtk_button_box_set_property       (GObject           *object,
45                                                guint              prop_id,
46                                                const GValue      *value,
47                                                GParamSpec        *pspec);
48 static void gtk_button_box_get_property       (GObject           *object,
49                                                guint              prop_id,
50                                                GValue            *value,
51                                                GParamSpec        *pspec);
52 static void gtk_button_box_set_child_property (GtkContainer      *container,
53                                                GtkWidget         *child,
54                                                guint              property_id,
55                                                const GValue      *value,
56                                                GParamSpec        *pspec);
57 static void gtk_button_box_get_child_property (GtkContainer      *container,
58                                                GtkWidget         *child,
59                                                guint              property_id,
60                                                GValue            *value,
61                                                GParamSpec        *pspec);
62
63 #define DEFAULT_CHILD_MIN_WIDTH 85
64 #define DEFAULT_CHILD_MIN_HEIGHT 27
65 #define DEFAULT_CHILD_IPAD_X 4
66 #define DEFAULT_CHILD_IPAD_Y 0
67
68 G_DEFINE_ABSTRACT_TYPE (GtkButtonBox, gtk_button_box, GTK_TYPE_BOX)
69
70 static void
71 gtk_button_box_class_init (GtkButtonBoxClass *class)
72 {
73   GtkWidgetClass *widget_class;
74   GObjectClass *gobject_class;
75   GtkContainerClass *container_class;
76
77   gobject_class = G_OBJECT_CLASS (class);
78   widget_class = (GtkWidgetClass*) class;
79   container_class = (GtkContainerClass*) class;
80
81   gobject_class->set_property = gtk_button_box_set_property;
82   gobject_class->get_property = gtk_button_box_get_property;
83
84   container_class->set_child_property = gtk_button_box_set_child_property;
85   container_class->get_child_property = gtk_button_box_get_child_property;
86   
87   /* FIXME we need to override the "spacing" property on GtkBox once
88    * libgobject allows that.
89    */
90   gtk_widget_class_install_style_property (widget_class,
91                                            g_param_spec_int ("child-min-width",
92                                                              P_("Minimum child width"),
93                                                              P_("Minimum width of buttons inside the box"),
94                                                              0,
95                                                              G_MAXINT,
96                                                              DEFAULT_CHILD_MIN_WIDTH,
97                                                              GTK_PARAM_READABLE));
98
99   gtk_widget_class_install_style_property (widget_class,
100                                            g_param_spec_int ("child-min-height",
101                                                              P_("Minimum child height"),
102                                                              P_("Minimum height of buttons inside the box"),
103                                                              0,
104                                                              G_MAXINT,
105                                                              DEFAULT_CHILD_MIN_HEIGHT,
106                                                              GTK_PARAM_READABLE));
107
108   gtk_widget_class_install_style_property (widget_class,
109                                            g_param_spec_int ("child-internal-pad-x",
110                                                              P_("Child internal width padding"),
111                                                              P_("Amount to increase child's size on either side"),
112                                                              0,
113                                                              G_MAXINT,
114                                                              DEFAULT_CHILD_IPAD_X,
115                                                              GTK_PARAM_READABLE));
116
117   gtk_widget_class_install_style_property (widget_class,
118                                            g_param_spec_int ("child-internal-pad-y",
119                                                              P_("Child internal height padding"),
120                                                              P_("Amount to increase child's size on the top and bottom"),
121                                                              0,
122                                                              G_MAXINT,
123                                                              DEFAULT_CHILD_IPAD_Y,
124                                                              GTK_PARAM_READABLE));
125   g_object_class_install_property (gobject_class,
126                                    PROP_LAYOUT_STYLE,
127                                    g_param_spec_enum ("layout-style",
128                                                       P_("Layout style"),
129                                                       P_("How to layout the buttons in the box. Possible values are default, spread, edge, start and end"),
130                                                       GTK_TYPE_BUTTON_BOX_STYLE,
131                                                       GTK_BUTTONBOX_DEFAULT_STYLE,
132                                                       GTK_PARAM_READWRITE));
133
134   gtk_container_class_install_child_property (container_class,
135                                               CHILD_PROP_SECONDARY,
136                                               g_param_spec_boolean ("secondary", 
137                                                                     P_("Secondary"),
138                                                                     P_("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
139                                                                     FALSE,
140                                                                     GTK_PARAM_READWRITE));
141 }
142
143 static void
144 gtk_button_box_init (GtkButtonBox *button_box)
145 {
146   GTK_BOX (button_box)->spacing = 0;
147   button_box->child_min_width = GTK_BUTTONBOX_DEFAULT;
148   button_box->child_min_height = GTK_BUTTONBOX_DEFAULT;
149   button_box->child_ipad_x = GTK_BUTTONBOX_DEFAULT;
150   button_box->child_ipad_y = GTK_BUTTONBOX_DEFAULT;
151   button_box->layout_style = GTK_BUTTONBOX_DEFAULT_STYLE;
152 }
153
154 static void
155 gtk_button_box_set_property (GObject         *object,
156                              guint            prop_id,
157                              const GValue    *value,
158                              GParamSpec      *pspec)
159 {
160   switch (prop_id) 
161     {
162     case PROP_LAYOUT_STYLE:
163       gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
164                                  g_value_get_enum (value));
165       break;
166     default:
167       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168       break;
169     }
170 }
171
172 static void
173 gtk_button_box_get_property (GObject         *object,
174                              guint            prop_id,
175                              GValue          *value,
176                              GParamSpec      *pspec)
177 {
178   switch (prop_id)
179     {
180     case PROP_LAYOUT_STYLE:
181       g_value_set_enum (value, GTK_BUTTON_BOX (object)->layout_style);
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186     }
187 }
188
189 static void
190 gtk_button_box_set_child_property (GtkContainer    *container,
191                                    GtkWidget       *child,
192                                    guint            property_id,
193                                    const GValue    *value,
194                                    GParamSpec      *pspec)
195 {
196   switch (property_id)
197     {
198     case CHILD_PROP_SECONDARY:
199       gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
200                                           g_value_get_boolean (value));
201       break;
202     default:
203       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
204       break;
205     }
206 }
207
208 static void
209 gtk_button_box_get_child_property (GtkContainer *container,
210                                    GtkWidget    *child,
211                                    guint         property_id,
212                                    GValue       *value,
213                                    GParamSpec   *pspec)
214 {
215   switch (property_id)
216     {
217     case CHILD_PROP_SECONDARY:
218       g_value_set_boolean (value, 
219                            gtk_button_box_get_child_secondary (GTK_BUTTON_BOX (container), 
220                                                                child));
221       break;
222     default:
223       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
224       break;
225     }
226 }
227
228 /* set per widget values for spacing, child size and child internal padding */
229
230 void 
231 gtk_button_box_set_child_size (GtkButtonBox *widget, 
232                                gint width, gint height)
233 {
234   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
235
236   widget->child_min_width = width;
237   widget->child_min_height = height;
238 }
239
240 void 
241 gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
242                                    gint ipad_x, gint ipad_y)
243 {
244   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
245
246   widget->child_ipad_x = ipad_x;
247   widget->child_ipad_y = ipad_y;
248 }
249
250 void
251 gtk_button_box_set_layout (GtkButtonBox      *widget, 
252                            GtkButtonBoxStyle  layout_style)
253 {
254   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
255   g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
256                     layout_style <= GTK_BUTTONBOX_CENTER);
257
258   if (widget->layout_style != layout_style)
259     {
260       widget->layout_style = layout_style;
261       g_object_notify (G_OBJECT (widget), "layout-style");
262       gtk_widget_queue_resize (GTK_WIDGET (widget));
263     }
264 }
265
266
267 /* get per widget values for spacing, child size and child internal padding */
268
269 void 
270 gtk_button_box_get_child_size (GtkButtonBox *widget,
271                                gint *width, gint *height)
272 {
273   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
274   g_return_if_fail (width != NULL);
275   g_return_if_fail (height != NULL);
276
277   *width  = widget->child_min_width;
278   *height = widget->child_min_height;
279 }
280
281 void
282 gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
283                                    gint* ipad_x, gint *ipad_y)
284 {
285   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
286   g_return_if_fail (ipad_x != NULL);
287   g_return_if_fail (ipad_y != NULL);
288
289   *ipad_x = widget->child_ipad_x;
290   *ipad_y = widget->child_ipad_y;
291 }
292
293 GtkButtonBoxStyle 
294 gtk_button_box_get_layout (GtkButtonBox *widget)
295 {
296   g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), GTK_BUTTONBOX_SPREAD);
297   
298   return widget->layout_style;
299 }
300
301 /**
302  * gtk_button_box_get_child_secondary:
303  * @widget: a #GtkButtonBox
304  * @child: a child of @widget 
305  * 
306  * Returns whether @child should appear in a secondary group of children.
307  *
308  * Return value: whether @child should appear in a secondary group of children.
309  *
310  * Since: 2.4
311  **/
312 gboolean 
313 gtk_button_box_get_child_secondary (GtkButtonBox *widget,
314                                     GtkWidget    *child)
315 {
316   GList *list;
317   GtkBoxChild *child_info;
318
319   g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), FALSE);
320   g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
321
322   child_info = NULL;
323   list = GTK_BOX (widget)->children;
324   while (list)
325     {
326       child_info = list->data;
327       if (child_info->widget == child)
328         break;
329
330       list = list->next;
331     }
332
333   g_return_val_if_fail (list != NULL, FALSE);
334
335   return child_info->is_secondary;
336 }
337
338 /**
339  * gtk_button_box_set_child_secondary
340  * @widget: a #GtkButtonBox
341  * @child: a child of @widget
342  * @is_secondary: if %TRUE, the @child appears in a secondary group of the
343  *                button box.
344  *
345  * Sets whether @child should appear in a secondary group of children.
346  * A typical use of a secondary child is the help button in a dialog.
347  *
348  * This group appears after the other children if the style
349  * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
350  * %GTK_BUTTONBOX_EDGE, and before the other children if the style
351  * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
352  * of before/after depends on direction of the widget (see
353  * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
354  * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
355  * the other end of the button box from the main children. For the
356  * other styles, they appear immediately next to the main children.
357  **/
358 void 
359 gtk_button_box_set_child_secondary (GtkButtonBox *widget, 
360                                     GtkWidget    *child,
361                                     gboolean      is_secondary)
362 {
363   GList *list;
364   
365   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
366   g_return_if_fail (GTK_IS_WIDGET (child));
367   g_return_if_fail (child->parent == GTK_WIDGET (widget));
368
369   list = GTK_BOX (widget)->children;
370   while (list)
371     {
372       GtkBoxChild *child_info = list->data;
373       if (child_info->widget == child)
374         {
375           child_info->is_secondary = is_secondary;
376           break;
377         }
378
379       list = list->next;
380     }
381
382   gtk_widget_child_notify (child, "secondary");
383
384   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (child))
385     gtk_widget_queue_resize (child);
386 }
387
388 /* Ask children how much space they require and round up 
389    to match minimum size and internal padding.
390    Returns the size each single child should have. */
391 void
392 _gtk_button_box_child_requisition (GtkWidget *widget,
393                                    int       *nvis_children,
394                                    int       *nvis_secondaries,
395                                    int       *width,
396                                    int       *height)
397 {
398   GtkButtonBox *bbox;
399   GtkBoxChild *child;
400   GList *children;
401   gint nchildren;
402   gint nsecondaries;
403   gint needed_width;
404   gint needed_height;
405   GtkRequisition child_requisition;
406   gint ipad_w;
407   gint ipad_h;
408   gint width_default;
409   gint height_default;
410   gint ipad_x_default;
411   gint ipad_y_default;
412   
413   gint child_min_width;
414   gint child_min_height;
415   gint ipad_x;
416   gint ipad_y;
417   
418   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
419
420   bbox = GTK_BUTTON_BOX (widget);
421
422   gtk_widget_style_get (widget,
423                         "child-min-width", &width_default,
424                         "child-min-height", &height_default,
425                         "child-internal-pad-x", &ipad_x_default,
426                         "child-internal-pad-y", &ipad_y_default, 
427                         NULL);
428   
429   child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
430           ? bbox->child_min_width : width_default;
431   child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
432           ? bbox->child_min_height : height_default;
433   ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
434           ? bbox->child_ipad_x : ipad_x_default;
435   ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
436           ? bbox->child_ipad_y : ipad_y_default;
437
438   nchildren = 0;
439   nsecondaries = 0;
440   children = GTK_BOX(bbox)->children;
441   needed_width = child_min_width;
442   needed_height = child_min_height;  
443   ipad_w = ipad_x * 2;
444   ipad_h = ipad_y * 2;
445   
446   while (children)
447     {
448       child = children->data;
449       children = children->next;
450
451       if (GTK_WIDGET_VISIBLE (child->widget))
452         {
453           nchildren += 1;
454           gtk_widget_size_request (child->widget, &child_requisition);
455           
456           if (child_requisition.width + ipad_w > needed_width)
457             needed_width = child_requisition.width + ipad_w;
458           if (child_requisition.height + ipad_h > needed_height)
459             needed_height = child_requisition.height + ipad_h;
460           if (child->is_secondary)
461             nsecondaries++;
462         }
463     }
464
465   if (nvis_children)
466     *nvis_children = nchildren;
467   if (nvis_secondaries)
468     *nvis_secondaries = nsecondaries;
469   if (width)
470     *width = needed_width;
471   if (height)
472     *height = needed_height;
473 }
474
475 #define __GTK_BUTTON_BOX_C__
476 #include "gtkaliasdef.c"