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