]> Pileus Git - ~andy/gtk/blob - gtk/gtkbbox.c
6d77e83358a0a206869e4d1818a2db0fa0342dcc
[~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 
267 gtk_button_box_set_child_size (GtkButtonBox *widget, 
268                                gint width, gint height)
269 {
270   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
271
272   widget->child_min_width = width;
273   widget->child_min_height = height;
274 }
275
276 void 
277 gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
278                                    gint ipad_x, gint ipad_y)
279 {
280   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
281
282   widget->child_ipad_x = ipad_x;
283   widget->child_ipad_y = ipad_y;
284 }
285
286 void
287 gtk_button_box_set_layout (GtkButtonBox      *widget, 
288                            GtkButtonBoxStyle  layout_style)
289 {
290   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
291   g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
292                     layout_style <= GTK_BUTTONBOX_END);
293
294   if (widget->layout_style != layout_style)
295     {
296       widget->layout_style = layout_style;
297       g_object_notify (G_OBJECT (widget), "layout_style");
298       gtk_widget_queue_resize (GTK_WIDGET (widget));
299     }
300 }
301
302
303 /* get per widget values for spacing, child size and child internal padding */
304
305 void 
306 gtk_button_box_get_child_size (GtkButtonBox *widget,
307                                gint *width, gint *height)
308 {
309   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
310   g_return_if_fail (width != NULL);
311   g_return_if_fail (height != NULL);
312
313   *width  = widget->child_min_width;
314   *height = widget->child_min_height;
315 }
316
317 void
318 gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
319                                    gint* ipad_x, gint *ipad_y)
320 {
321   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
322   g_return_if_fail (ipad_x != NULL);
323   g_return_if_fail (ipad_y != NULL);
324
325   *ipad_x = widget->child_ipad_x;
326   *ipad_y = widget->child_ipad_y;
327 }
328
329 GtkButtonBoxStyle 
330 gtk_button_box_get_layout (GtkButtonBox *widget)
331 {
332   g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), GTK_BUTTONBOX_SPREAD);
333   
334   return widget->layout_style;
335 }
336
337 /**
338  * gtk_button_box_set_child_secondary
339  * @widget: a #GtkButtonBox
340  * @child: a child of @widget
341  * @is_secondary: if %TRUE, the @child appears in a secondary group of the
342  *                button box.
343  *
344  * Sets whether @child should appear in a secondary group of children.
345  * A typical use of a secondary child is the help button in a dialog.
346  *
347  * This group appears after the other children if the style
348  * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
349  * %GTK_BUTTONBOX_EDGE, and before the the other children if the style
350  * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
351  * of before/after depends on direction of the widget (see
352  * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
353  * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
354  * the other end of the button box from the main children. For the
355  * other styles, they appear immediately next to the main children.
356  **/
357 void 
358 gtk_button_box_set_child_secondary (GtkButtonBox *widget, 
359                                     GtkWidget    *child,
360                                     gboolean      is_secondary)
361 {
362   GList *list;
363   
364   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
365   g_return_if_fail (GTK_IS_WIDGET (child));
366   g_return_if_fail (child->parent == GTK_WIDGET (widget));
367
368   list = GTK_BOX (widget)->children;
369   while (list)
370     {
371       GtkBoxChild *child_info = list->data;
372       if (child_info->widget == child)
373         {
374           child_info->is_secondary = is_secondary;
375           break;
376         }
377
378       list = list->next;
379     }
380
381   gtk_widget_child_notify (child, "secondary");
382
383   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (child))
384     gtk_widget_queue_resize (child);
385 }
386
387 /* Ask children how much space they require and round up 
388    to match minimum size and internal padding.
389    Returns the size each single child should have. */
390 void
391 _gtk_button_box_child_requisition (GtkWidget *widget,
392                                    int       *nvis_children,
393                                    int       *nvis_secondaries,
394                                    int       *width,
395                                    int       *height)
396 {
397   GtkButtonBox *bbox;
398   GtkBoxChild *child;
399   GList *children;
400   gint nchildren;
401   gint nsecondaries;
402   gint needed_width;
403   gint needed_height;
404   GtkRequisition child_requisition;
405   gint ipad_w;
406   gint ipad_h;
407   gint width_default;
408   gint height_default;
409   gint ipad_x_default;
410   gint ipad_y_default;
411   
412   gint child_min_width;
413   gint child_min_height;
414   gint ipad_x;
415   gint ipad_y;
416   
417   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
418
419   bbox = GTK_BUTTON_BOX (widget);
420
421   gtk_widget_style_get (widget,
422                         "child_min_width",
423                         &width_default,
424                         "child_min_height",
425                         &height_default,
426                         "child_internal_pad_x",
427                         &ipad_x_default,
428                         "child_internal_pad_y",
429                         &ipad_y_default, NULL);
430   
431   child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
432           ? bbox->child_min_width : width_default;
433   child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
434           ? bbox->child_min_height : height_default;
435   ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
436           ? bbox->child_ipad_x : ipad_x_default;
437   ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
438           ? bbox->child_ipad_y : ipad_y_default;
439
440   nchildren = 0;
441   nsecondaries = 0;
442   children = GTK_BOX(bbox)->children;
443   needed_width = child_min_width;
444   needed_height = child_min_height;  
445   ipad_w = ipad_x * 2;
446   ipad_h = ipad_y * 2;
447   
448   while (children)
449     {
450       child = children->data;
451       children = children->next;
452
453       if (GTK_WIDGET_VISIBLE (child->widget))
454         {
455           nchildren += 1;
456           gtk_widget_size_request (child->widget, &child_requisition);
457           
458           if (child_requisition.width + ipad_w > needed_width)
459             needed_width = child_requisition.width + ipad_w;
460           if (child_requisition.height + ipad_h > needed_height)
461             needed_height = child_requisition.height + ipad_h;
462           if (child->is_secondary)
463             nsecondaries++;
464         }
465     }
466
467   if (nvis_children)
468     *nvis_children = nchildren;
469   if (nvis_secondaries)
470     *nvis_secondaries = nsecondaries;
471   if (width)
472     *width = needed_width;
473   if (height)
474     *height = needed_height;
475 }