1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
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,
47 static void gtk_button_box_get_property (GObject *object,
51 static void gtk_button_box_set_child_property (GtkContainer *container,
56 static void gtk_button_box_get_child_property (GtkContainer *container,
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
68 gtk_button_box_get_type (void)
70 static GtkType button_box_type = 0;
74 static const GtkTypeInfo button_box_info =
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,
86 button_box_type = gtk_type_unique (gtk_box_get_type (), &button_box_info);
89 return button_box_type;
93 gtk_button_box_class_init (GtkButtonBoxClass *class)
95 GtkWidgetClass *widget_class;
96 GObjectClass *gobject_class;
97 GtkContainerClass *container_class;
99 gobject_class = G_OBJECT_CLASS (class);
100 widget_class = (GtkWidgetClass*) class;
101 container_class = (GtkContainerClass*) class;
103 gobject_class->set_property = gtk_button_box_set_property;
104 gobject_class->get_property = gtk_button_box_get_property;
106 container_class->set_child_property = gtk_button_box_set_child_property;
107 container_class->get_child_property = gtk_button_box_get_child_property;
109 /* FIXME we need to override the "spacing" property on GtkBox once
110 * libgobject allows that.
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"),
119 DEFAULT_CHILD_MIN_WIDTH,
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"),
128 DEFAULT_CHILD_MIN_HEIGHT,
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"),
137 DEFAULT_CHILD_IPAD_X,
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"),
146 DEFAULT_CHILD_IPAD_Y,
148 g_object_class_install_property (gobject_class,
150 g_param_spec_enum ("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,
157 gtk_container_class_install_child_property (container_class,
158 CHILD_PROP_SECONDARY,
159 g_param_spec_boolean ("secondary",
161 _("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons."),
167 gtk_button_box_init (GtkButtonBox *button_box)
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;
178 gtk_button_box_set_property (GObject *object,
185 case PROP_LAYOUT_STYLE:
186 gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
187 g_value_get_enum (value));
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 gtk_button_box_get_property (GObject *object,
203 case PROP_LAYOUT_STYLE:
204 g_value_set_enum (value, GTK_BUTTON_BOX (object)->layout_style);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 gtk_button_box_set_child_property (GtkContainer *container,
221 case CHILD_PROP_SECONDARY:
222 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
223 g_value_get_boolean (value));
226 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
232 gtk_button_box_get_child_property (GtkContainer *container,
239 GtkBoxChild *child_info = NULL;
241 list = GTK_BOX (container)->children;
244 child_info = list->data;
245 if (child_info->widget == child)
251 g_assert (list != NULL);
255 case CHILD_PROP_SECONDARY:
256 g_value_set_boolean (value, child_info->is_secondary);
259 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
264 /* set per widget values for spacing, child size and child internal padding */
266 void gtk_button_box_set_child_size (GtkButtonBox *widget, gint width, gint height)
268 widget->child_min_width = width;
269 widget->child_min_height = height;
272 void gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
273 gint ipad_x, gint ipad_y)
275 widget->child_ipad_x = ipad_x;
276 widget->child_ipad_y = ipad_y;
279 void gtk_button_box_set_layout (GtkButtonBox *widget,
280 GtkButtonBoxStyle layout_style)
282 g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
283 layout_style <= GTK_BUTTONBOX_END);
285 if (widget->layout_style != layout_style)
287 widget->layout_style = layout_style;
288 g_object_notify (G_OBJECT (widget), "layout_style");
289 gtk_widget_queue_resize (GTK_WIDGET (widget));
294 /* get per widget values for spacing, child size and child internal padding */
296 void gtk_button_box_get_child_size (GtkButtonBox *widget,
297 gint *width, gint *height)
299 *width = widget->child_min_width;
300 *height = widget->child_min_height;
303 void gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
304 gint* ipad_x, gint *ipad_y)
306 *ipad_x = widget->child_ipad_x;
307 *ipad_y = widget->child_ipad_y;
311 gtk_button_box_get_layout (GtkButtonBox *widget)
313 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), GTK_BUTTONBOX_SPREAD);
315 return widget->layout_style;
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
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.
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.
339 gtk_button_box_set_child_secondary (GtkButtonBox *widget,
341 gboolean is_secondary)
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));
349 list = GTK_BOX (widget)->children;
352 GtkBoxChild *child_info = list->data;
353 if (child_info->widget == child)
355 child_info->is_secondary = is_secondary;
362 gtk_widget_child_notify (child, "secondary");
364 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (child))
365 gtk_widget_queue_resize (child);
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. */
372 _gtk_button_box_child_requisition (GtkWidget *widget,
374 int *nvis_secondaries,
385 GtkRequisition child_requisition;
393 gint child_min_width;
394 gint child_min_height;
398 g_return_if_fail (widget != NULL);
399 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
401 bbox = GTK_BUTTON_BOX (widget);
403 gtk_widget_style_get (widget,
408 "child_internal_pad_x",
410 "child_internal_pad_y",
411 &ipad_y_default, NULL);
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;
424 children = GTK_BOX(bbox)->children;
425 needed_width = child_min_width;
426 needed_height = child_min_height;
432 child = children->data;
433 children = children->next;
435 if (GTK_WIDGET_VISIBLE (child->widget))
438 gtk_widget_size_request (child->widget, &child_requisition);
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)
450 *nvis_children = nchildren;
451 if (nvis_secondaries)
452 *nvis_secondaries = nsecondaries;
454 *width = needed_width;
456 *height = needed_height;