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 GType button_box_type = 0;
74 static const GTypeInfo button_box_info =
76 sizeof (GtkButtonBoxClass),
78 NULL, /* base_finalize */
79 (GClassInitFunc) gtk_button_box_class_init,
80 NULL, /* class_finalize */
81 NULL, /* class_data */
82 sizeof (GtkButtonBox),
84 (GInstanceInitFunc) gtk_button_box_init,
85 NULL, /* value_table */
89 g_type_register_static (GTK_TYPE_BOX, "GtkButtonBox",
90 &button_box_info, G_TYPE_FLAG_ABSTRACT);
93 return button_box_type;
97 gtk_button_box_class_init (GtkButtonBoxClass *class)
99 GtkWidgetClass *widget_class;
100 GObjectClass *gobject_class;
101 GtkContainerClass *container_class;
103 gobject_class = G_OBJECT_CLASS (class);
104 widget_class = (GtkWidgetClass*) class;
105 container_class = (GtkContainerClass*) class;
107 gobject_class->set_property = gtk_button_box_set_property;
108 gobject_class->get_property = gtk_button_box_get_property;
110 container_class->set_child_property = gtk_button_box_set_child_property;
111 container_class->get_child_property = gtk_button_box_get_child_property;
113 /* FIXME we need to override the "spacing" property on GtkBox once
114 * libgobject allows that.
117 gtk_widget_class_install_style_property (widget_class,
118 g_param_spec_int ("child_min_width",
119 _("Minimum child width"),
120 _("Minimum width of buttons inside the box"),
123 DEFAULT_CHILD_MIN_WIDTH,
126 gtk_widget_class_install_style_property (widget_class,
127 g_param_spec_int ("child_min_height",
128 _("Minimum child height"),
129 _("Minimum height of buttons inside the box"),
132 DEFAULT_CHILD_MIN_HEIGHT,
135 gtk_widget_class_install_style_property (widget_class,
136 g_param_spec_int ("child_internal_pad_x",
137 _("Child internal width padding"),
138 _("Amount to increase child's size on either side"),
141 DEFAULT_CHILD_IPAD_X,
144 gtk_widget_class_install_style_property (widget_class,
145 g_param_spec_int ("child_internal_pad_y",
146 _("Child internal height padding"),
147 _("Amount to increase child's size on the top and bottom"),
150 DEFAULT_CHILD_IPAD_Y,
152 g_object_class_install_property (gobject_class,
154 g_param_spec_enum ("layout_style",
156 _("How to layout the buttons in the box. Possible values are default, spread, edge, start and end"),
157 GTK_TYPE_BUTTON_BOX_STYLE,
158 GTK_BUTTONBOX_DEFAULT_STYLE,
161 gtk_container_class_install_child_property (container_class,
162 CHILD_PROP_SECONDARY,
163 g_param_spec_boolean ("secondary",
165 _("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
171 gtk_button_box_init (GtkButtonBox *button_box)
173 GTK_BOX (button_box)->spacing = 0;
174 button_box->child_min_width = GTK_BUTTONBOX_DEFAULT;
175 button_box->child_min_height = GTK_BUTTONBOX_DEFAULT;
176 button_box->child_ipad_x = GTK_BUTTONBOX_DEFAULT;
177 button_box->child_ipad_y = GTK_BUTTONBOX_DEFAULT;
178 button_box->layout_style = GTK_BUTTONBOX_DEFAULT_STYLE;
182 gtk_button_box_set_property (GObject *object,
189 case PROP_LAYOUT_STYLE:
190 gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
191 g_value_get_enum (value));
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gtk_button_box_get_property (GObject *object,
207 case PROP_LAYOUT_STYLE:
208 g_value_set_enum (value, GTK_BUTTON_BOX (object)->layout_style);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 gtk_button_box_set_child_property (GtkContainer *container,
225 case CHILD_PROP_SECONDARY:
226 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
227 g_value_get_boolean (value));
230 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
236 gtk_button_box_get_child_property (GtkContainer *container,
243 GtkBoxChild *child_info = NULL;
245 list = GTK_BOX (container)->children;
248 child_info = list->data;
249 if (child_info->widget == child)
255 g_assert (list != NULL);
259 case CHILD_PROP_SECONDARY:
260 g_value_set_boolean (value, child_info->is_secondary);
263 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
268 /* set per widget values for spacing, child size and child internal padding */
271 gtk_button_box_set_child_size (GtkButtonBox *widget,
272 gint width, gint height)
274 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
276 widget->child_min_width = width;
277 widget->child_min_height = height;
281 gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
282 gint ipad_x, gint ipad_y)
284 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
286 widget->child_ipad_x = ipad_x;
287 widget->child_ipad_y = ipad_y;
291 gtk_button_box_set_layout (GtkButtonBox *widget,
292 GtkButtonBoxStyle layout_style)
294 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
295 g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
296 layout_style <= GTK_BUTTONBOX_END);
298 if (widget->layout_style != layout_style)
300 widget->layout_style = layout_style;
301 g_object_notify (G_OBJECT (widget), "layout_style");
302 gtk_widget_queue_resize (GTK_WIDGET (widget));
307 /* get per widget values for spacing, child size and child internal padding */
310 gtk_button_box_get_child_size (GtkButtonBox *widget,
311 gint *width, gint *height)
313 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
314 g_return_if_fail (width != NULL);
315 g_return_if_fail (height != NULL);
317 *width = widget->child_min_width;
318 *height = widget->child_min_height;
322 gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
323 gint* ipad_x, gint *ipad_y)
325 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
326 g_return_if_fail (ipad_x != NULL);
327 g_return_if_fail (ipad_y != NULL);
329 *ipad_x = widget->child_ipad_x;
330 *ipad_y = widget->child_ipad_y;
334 gtk_button_box_get_layout (GtkButtonBox *widget)
336 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), GTK_BUTTONBOX_SPREAD);
338 return widget->layout_style;
342 * gtk_button_box_set_child_secondary
343 * @widget: a #GtkButtonBox
344 * @child: a child of @widget
345 * @is_secondary: if %TRUE, the @child appears in a secondary group of the
348 * Sets whether @child should appear in a secondary group of children.
349 * A typical use of a secondary child is the help button in a dialog.
351 * This group appears after the other children if the style
352 * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
353 * %GTK_BUTTONBOX_EDGE, and before the the other children if the style
354 * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
355 * of before/after depends on direction of the widget (see
356 * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
357 * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
358 * the other end of the button box from the main children. For the
359 * other styles, they appear immediately next to the main children.
362 gtk_button_box_set_child_secondary (GtkButtonBox *widget,
364 gboolean is_secondary)
368 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
369 g_return_if_fail (GTK_IS_WIDGET (child));
370 g_return_if_fail (child->parent == GTK_WIDGET (widget));
372 list = GTK_BOX (widget)->children;
375 GtkBoxChild *child_info = list->data;
376 if (child_info->widget == child)
378 child_info->is_secondary = is_secondary;
385 gtk_widget_child_notify (child, "secondary");
387 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (child))
388 gtk_widget_queue_resize (child);
391 /* Ask children how much space they require and round up
392 to match minimum size and internal padding.
393 Returns the size each single child should have. */
395 _gtk_button_box_child_requisition (GtkWidget *widget,
397 int *nvis_secondaries,
408 GtkRequisition child_requisition;
416 gint child_min_width;
417 gint child_min_height;
421 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
423 bbox = GTK_BUTTON_BOX (widget);
425 gtk_widget_style_get (widget,
430 "child_internal_pad_x",
432 "child_internal_pad_y",
433 &ipad_y_default, NULL);
435 child_min_width = bbox->child_min_width != GTK_BUTTONBOX_DEFAULT
436 ? bbox->child_min_width : width_default;
437 child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
438 ? bbox->child_min_height : height_default;
439 ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
440 ? bbox->child_ipad_x : ipad_x_default;
441 ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
442 ? bbox->child_ipad_y : ipad_y_default;
446 children = GTK_BOX(bbox)->children;
447 needed_width = child_min_width;
448 needed_height = child_min_height;
454 child = children->data;
455 children = children->next;
457 if (GTK_WIDGET_VISIBLE (child->widget))
460 gtk_widget_size_request (child->widget, &child_requisition);
462 if (child_requisition.width + ipad_w > needed_width)
463 needed_width = child_requisition.width + ipad_w;
464 if (child_requisition.height + ipad_h > needed_height)
465 needed_height = child_requisition.height + ipad_h;
466 if (child->is_secondary)
472 *nvis_children = nchildren;
473 if (nvis_secondaries)
474 *nvis_secondaries = nsecondaries;
476 *width = needed_width;
478 *height = needed_height;