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