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/.
29 * @Short_description: Base class for GtkHButtonBox and GtkVButtonBox
30 * @Title: GtkButtonBox
31 * @See_also: #GtkVButtonBox, #GtkHButtonBox
33 * The primary purpose of this class is to keep track of the various
34 * properties of #GtkHButtonBox and #GtkVButtonBox widgets.
36 * gtk_button_box_get_layout() and gtk_button_box_set_layout() retrieve and
37 * alter the method used to spread the buttons in a button box across the
38 * container, respectively.
40 * The main purpose of GtkButtonBox is to make sure the children have all the
41 * same size. GtkButtonBox gives all children the same size, but it does allow
42 * 'outliers' to keep their own larger size. To force all children to be
43 * strictly the same size without exceptions, you can set the
44 * #GtkButtonBox::homogeneous property to %TRUE.
51 #include "gtkorientable.h"
52 #include "gtkprivate.h"
53 #include "gtksizerequest.h"
58 struct _GtkButtonBoxPrivate
60 GtkButtonBoxStyle layout_style;
73 #define GTK_BOX_SECONDARY_CHILD "gtk-box-secondary-child"
75 static void gtk_button_box_set_property (GObject *object,
79 static void gtk_button_box_get_property (GObject *object,
83 static void gtk_button_box_size_request (GtkWidget *widget,
84 GtkRequisition *requisition);
85 static void gtk_button_box_size_allocate (GtkWidget *widget,
86 GtkAllocation *allocation);
87 static void gtk_button_box_remove (GtkContainer *container,
89 static void gtk_button_box_set_child_property (GtkContainer *container,
94 static void gtk_button_box_get_child_property (GtkContainer *container,
100 #define DEFAULT_CHILD_MIN_WIDTH 85
101 #define DEFAULT_CHILD_MIN_HEIGHT 27
102 #define DEFAULT_CHILD_IPAD_X 4
103 #define DEFAULT_CHILD_IPAD_Y 0
104 #define DEFAULT_LAYOUT_STYLE GTK_BUTTONBOX_EDGE
106 G_DEFINE_TYPE (GtkButtonBox, gtk_button_box, GTK_TYPE_BOX)
109 gtk_button_box_class_init (GtkButtonBoxClass *class)
111 GtkWidgetClass *widget_class;
112 GObjectClass *gobject_class;
113 GtkContainerClass *container_class;
115 gobject_class = G_OBJECT_CLASS (class);
116 widget_class = (GtkWidgetClass*) class;
117 container_class = (GtkContainerClass*) class;
119 gobject_class->set_property = gtk_button_box_set_property;
120 gobject_class->get_property = gtk_button_box_get_property;
122 widget_class->size_request = gtk_button_box_size_request;
123 widget_class->size_allocate = gtk_button_box_size_allocate;
125 container_class->remove = gtk_button_box_remove;
126 container_class->set_child_property = gtk_button_box_set_child_property;
127 container_class->get_child_property = gtk_button_box_get_child_property;
129 /* FIXME we need to override the "spacing" property on GtkBox once
130 * libgobject allows that.
132 gtk_widget_class_install_style_property (widget_class,
133 g_param_spec_int ("child-min-width",
134 P_("Minimum child width"),
135 P_("Minimum width of buttons inside the box"),
138 DEFAULT_CHILD_MIN_WIDTH,
139 GTK_PARAM_READABLE));
141 gtk_widget_class_install_style_property (widget_class,
142 g_param_spec_int ("child-min-height",
143 P_("Minimum child height"),
144 P_("Minimum height of buttons inside the box"),
147 DEFAULT_CHILD_MIN_HEIGHT,
148 GTK_PARAM_READABLE));
150 gtk_widget_class_install_style_property (widget_class,
151 g_param_spec_int ("child-internal-pad-x",
152 P_("Child internal width padding"),
153 P_("Amount to increase child's size on either side"),
156 DEFAULT_CHILD_IPAD_X,
157 GTK_PARAM_READABLE));
159 gtk_widget_class_install_style_property (widget_class,
160 g_param_spec_int ("child-internal-pad-y",
161 P_("Child internal height padding"),
162 P_("Amount to increase child's size on the top and bottom"),
165 DEFAULT_CHILD_IPAD_Y,
166 GTK_PARAM_READABLE));
167 g_object_class_install_property (gobject_class,
169 g_param_spec_enum ("layout-style",
171 P_("How to lay out the buttons in the box. Possible values are: spread, edge, start and end"),
172 GTK_TYPE_BUTTON_BOX_STYLE,
173 DEFAULT_LAYOUT_STYLE,
174 GTK_PARAM_READWRITE));
176 gtk_container_class_install_child_property (container_class,
177 CHILD_PROP_SECONDARY,
178 g_param_spec_boolean ("secondary",
180 P_("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
182 GTK_PARAM_READWRITE));
184 g_type_class_add_private (class, sizeof (GtkButtonBoxPrivate));
188 gtk_button_box_init (GtkButtonBox *button_box)
190 GtkButtonBoxPrivate *priv;
192 button_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (button_box,
194 GtkButtonBoxPrivate);
195 priv = button_box->priv;
197 gtk_box_set_spacing (GTK_BOX (button_box), 0);
198 priv->layout_style = DEFAULT_LAYOUT_STYLE;
202 gtk_button_box_set_property (GObject *object,
209 case PROP_LAYOUT_STYLE:
210 gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
211 g_value_get_enum (value));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gtk_button_box_get_property (GObject *object,
225 GtkButtonBoxPrivate *priv = GTK_BUTTON_BOX (object)->priv;
229 case PROP_LAYOUT_STYLE:
230 g_value_set_enum (value, priv->layout_style);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 gtk_button_box_set_child_property (GtkContainer *container,
247 case CHILD_PROP_SECONDARY:
248 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
249 g_value_get_boolean (value));
252 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
258 gtk_button_box_get_child_property (GtkContainer *container,
266 case CHILD_PROP_SECONDARY:
267 g_value_set_boolean (value,
268 gtk_button_box_get_child_secondary (GTK_BUTTON_BOX (container),
272 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
278 gtk_button_box_remove (GtkContainer *container,
281 /* clear is_secondary flag in case the widget
282 * is added to another container
284 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container),
288 GTK_CONTAINER_CLASS (gtk_button_box_parent_class)->remove (container, widget);
292 * gtk_button_box_set_layout:
293 * @widget: a #GtkButtonBox
294 * @layout_style: the new layout style
296 * Changes the way buttons are arranged in their container.
299 gtk_button_box_set_layout (GtkButtonBox *widget,
300 GtkButtonBoxStyle layout_style)
302 GtkButtonBoxPrivate *priv;
304 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
308 if (priv->layout_style != layout_style)
310 priv->layout_style = layout_style;
311 g_object_notify (G_OBJECT (widget), "layout-style");
312 gtk_widget_queue_resize (GTK_WIDGET (widget));
317 * gtk_button_box_get_layout:
318 * @widget: a #GtkButtonBox
320 * Retrieves the method being used to arrange the buttons in a button box.
322 * Returns: the method used to lay out buttons in @widget.
325 gtk_button_box_get_layout (GtkButtonBox *widget)
327 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), DEFAULT_LAYOUT_STYLE);
329 return widget->priv->layout_style;
333 * gtk_button_box_get_child_secondary:
334 * @widget: a #GtkButtonBox
335 * @child: a child of @widget
337 * Returns whether @child should appear in a secondary group of children.
339 * Return value: whether @child should appear in a secondary group of children.
344 gtk_button_box_get_child_secondary (GtkButtonBox *widget,
347 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), FALSE);
348 g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
350 return (g_object_get_data (G_OBJECT (child), GTK_BOX_SECONDARY_CHILD) != NULL);
354 * gtk_button_box_set_child_secondary
355 * @widget: a #GtkButtonBox
356 * @child: a child of @widget
357 * @is_secondary: if %TRUE, the @child appears in a secondary group of the
360 * Sets whether @child should appear in a secondary group of children.
361 * A typical use of a secondary child is the help button in a dialog.
363 * This group appears after the other children if the style
364 * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
365 * %GTK_BUTTONBOX_EDGE, and before the other children if the style
366 * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
367 * of before/after depends on direction of the widget (see
368 * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
369 * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
370 * the other end of the button box from the main children. For the
371 * other styles, they appear immediately next to the main children.
374 gtk_button_box_set_child_secondary (GtkButtonBox *widget,
376 gboolean is_secondary)
378 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
379 g_return_if_fail (GTK_IS_WIDGET (child));
380 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (widget));
382 g_object_set_data (G_OBJECT (child),
383 GTK_BOX_SECONDARY_CHILD,
384 is_secondary ? GINT_TO_POINTER (1) : NULL);
385 gtk_widget_child_notify (child, "secondary");
387 if (gtk_widget_get_visible (GTK_WIDGET (widget)) &&
388 gtk_widget_get_visible (child))
389 gtk_widget_queue_resize (child);
392 /* Ask children how much space they require and round up
393 * to match minimum size and internal padding.
394 * Returns the size each single child should have.
397 gtk_button_box_child_requisition (GtkWidget *widget,
399 gint *nvis_secondaries,
403 GtkButtonBoxPrivate *priv;
405 GList *children, *list;
411 GtkRequisition child_requisition;
414 gint child_min_width;
415 gint child_min_height;
418 gboolean homogeneous;
421 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
423 bbox = GTK_BUTTON_BOX (widget);
426 homogeneous = gtk_box_get_homogeneous (GTK_BOX (widget));
428 gtk_widget_style_get (widget,
429 "child-min-width", &child_min_width,
430 "child-min-height", &child_min_height,
431 "child-internal-pad-x", &ipad_x,
432 "child-internal-pad-y", &ipad_y,
437 list = children = _gtk_box_get_children (GTK_BOX (bbox));
438 needed_width = child_min_width;
439 needed_height = child_min_height;
448 child = children->data;
449 children = children->next;
451 if (gtk_widget_get_visible (child))
454 gtk_size_request_get_size (GTK_SIZE_REQUEST (child),
455 &child_requisition, NULL);
456 avg_w += child_requisition.width + ipad_w;
457 avg_h += child_requisition.height + ipad_h;
460 avg_w /= MAX (nchildren, 1);
461 avg_h /= MAX (nchildren, 1);
463 *widths = g_new (gint, nchildren);
464 *heights = g_new (gint, nchildren);
471 gboolean is_secondary;
473 child = children->data;
474 children = children->next;
476 if (gtk_widget_get_visible (child))
478 is_secondary = gtk_button_box_get_child_secondary (bbox, child);
482 gtk_size_request_get_size (GTK_SIZE_REQUEST (child),
483 &child_requisition, NULL);
485 if (homogeneous || (child_requisition.width + ipad_w < avg_w * 1.5))
488 if (child_requisition.width + ipad_w > needed_width)
489 needed_width = child_requisition.width + ipad_w;
493 (*widths)[i] = child_requisition.width + ipad_w;
496 if (homogeneous || (child_requisition.height + ipad_h < avg_h * 1.5))
499 if (child_requisition.height + ipad_h > needed_height)
500 needed_height = child_requisition.height + ipad_h;
504 (*heights)[i] = child_requisition.height + ipad_h;
513 for (i = 0; i < nchildren; i++)
515 if ((*widths)[i] == -1)
516 (*widths)[i] = needed_width;
517 if ((*heights)[i] == -1)
518 (*heights)[i] = needed_height;
522 *nvis_children = nchildren;
524 if (nvis_secondaries)
525 *nvis_secondaries = nsecondaries;
529 gtk_button_box_size_request (GtkWidget *widget,
530 GtkRequisition *requisition)
532 GtkButtonBoxPrivate *priv;
539 GtkOrientation orientation;
544 bbox = GTK_BUTTON_BOX (widget);
547 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
548 spacing = gtk_box_get_spacing (GTK_BOX (widget));
550 gtk_button_box_child_requisition (widget,
557 for (i = 0; i < nvis_children; i++)
559 if (orientation == GTK_ORIENTATION_HORIZONTAL)
561 total_size += widths[i];
562 max_size = MAX (max_size, heights[i]);
566 total_size += heights[i];
567 max_size = MAX (max_size, widths[i]);
573 if (nvis_children == 0)
575 requisition->width = 0;
576 requisition->height = 0;
580 switch (priv->layout_style)
582 case GTK_BUTTONBOX_SPREAD:
583 if (orientation == GTK_ORIENTATION_HORIZONTAL)
584 requisition->width = total_size + ((nvis_children + 1)*spacing);
586 requisition->height = total_size + ((nvis_children + 1)*spacing);
589 case GTK_BUTTONBOX_EDGE:
590 case GTK_BUTTONBOX_START:
591 case GTK_BUTTONBOX_END:
592 case GTK_BUTTONBOX_CENTER:
593 if (orientation == GTK_ORIENTATION_HORIZONTAL)
594 requisition->width = total_size + ((nvis_children - 1)*spacing);
596 requisition->height = total_size + ((nvis_children - 1)*spacing);
600 g_assert_not_reached ();
604 if (orientation == GTK_ORIENTATION_HORIZONTAL)
605 requisition->height = max_size;
607 requisition->width = max_size;
610 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
611 requisition->width += border_width * 2;
612 requisition->height += border_width * 2;
616 gtk_button_box_size_allocate (GtkWidget *widget,
617 GtkAllocation *allocation)
619 GtkButtonBoxPrivate *priv;
621 GList *children, *list;
622 GtkAllocation child_allocation;
628 gint secondary_x = 0;
629 gint secondary_y = 0;
632 gint childspacing = 0;
635 GtkOrientation orientation;
645 bbox = GTK_BUTTON_BOX (widget);
648 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
649 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
650 spacing = gtk_box_get_spacing (GTK_BOX (widget));
652 gtk_widget_style_get (widget,
653 "child-internal-pad-x", &ipad_x,
654 "child-internal-pad-y", &ipad_y,
656 gtk_button_box_child_requisition (widget,
661 n_primaries = nvis_children - n_secondaries;
664 if (orientation == GTK_ORIENTATION_HORIZONTAL)
670 list = children = _gtk_box_get_children (GTK_BOX (widget));
675 child = children->data;
676 children = children->next;
678 if (gtk_widget_get_visible (child))
680 if (gtk_button_box_get_child_secondary (bbox, child))
681 secondary_size += sizes[i];
683 primary_size += sizes[i];
687 total_size = primary_size + secondary_size;
689 gtk_widget_set_allocation (widget, allocation);
691 if (orientation == GTK_ORIENTATION_HORIZONTAL)
692 width = allocation->width - border_width*2;
694 height = allocation->height - border_width*2;
696 switch (priv->layout_style)
698 case GTK_BUTTONBOX_SPREAD:
700 if (orientation == GTK_ORIENTATION_HORIZONTAL)
702 childspacing = (width - total_size) / (nvis_children + 1);
703 x = allocation->x + border_width + childspacing;
704 secondary_x = x + primary_size + n_primaries * childspacing;
708 childspacing = (height - total_size) / (nvis_children + 1);
709 y = allocation->y + border_width + childspacing;
710 secondary_y = y + primary_size + n_primaries * childspacing;
715 case GTK_BUTTONBOX_EDGE:
717 if (orientation == GTK_ORIENTATION_HORIZONTAL)
719 if (nvis_children >= 2)
721 childspacing = (width - total_size) / (nvis_children - 1);
722 x = allocation->x + border_width;
723 secondary_x = x + primary_size + n_primaries * childspacing;
727 /* one or zero children, just center */
728 childspacing = width;
729 x = secondary_x = allocation->x
730 + (allocation->width - widths[0]) / 2;
735 if (nvis_children >= 2)
737 childspacing = (height - total_size) / (nvis_children - 1);
738 y = allocation->y + border_width;
739 secondary_y = y + primary_size + n_primaries * childspacing;
743 /* one or zero children, just center */
744 childspacing = height;
745 y = secondary_y = allocation->y
746 + (allocation->height - heights[0]) / 2;
752 case GTK_BUTTONBOX_START:
754 if (orientation == GTK_ORIENTATION_HORIZONTAL)
756 childspacing = spacing;
757 x = allocation->x + border_width;
758 secondary_x = allocation->x + allocation->width
759 - secondary_size - spacing * (n_secondaries - 1) - border_width;
763 childspacing = spacing;
764 y = allocation->y + border_width;
765 secondary_y = allocation->y + allocation->height
766 - secondary_size - spacing * (n_secondaries - 1) - border_width;
771 case GTK_BUTTONBOX_END:
773 if (orientation == GTK_ORIENTATION_HORIZONTAL)
775 childspacing = spacing;
776 x = allocation->x + allocation->width
777 - primary_size - spacing * (n_primaries - 1) - border_width;
778 secondary_x = allocation->x + border_width;
782 childspacing = spacing;
783 y = allocation->y + allocation->height
784 - primary_size - spacing * (n_primaries - 1) - border_width;
785 secondary_y = allocation->y + border_width;
790 case GTK_BUTTONBOX_CENTER:
792 if (orientation == GTK_ORIENTATION_HORIZONTAL)
794 childspacing = spacing;
797 - (primary_size + spacing * (n_primaries - 1))) / 2
798 + (secondary_size + n_secondaries * spacing) / 2;
799 secondary_x = allocation->x + border_width;
803 childspacing = spacing;
806 - (primary_size + spacing * (n_primaries - 1))) / 2
807 + (secondary_size + n_secondaries * spacing) / 2;
808 secondary_y = allocation->y + border_width;
814 g_assert_not_reached ();
824 child = children->data;
825 children = children->next;
827 if (gtk_widget_get_visible (child))
829 child_allocation.width = widths[i];
830 child_allocation.height = heights[i];
832 if (orientation == GTK_ORIENTATION_HORIZONTAL)
834 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
836 if (gtk_button_box_get_child_secondary (bbox, child))
838 child_allocation.x = secondary_x;
839 secondary_x += child_allocation.width + childspacing;
843 child_allocation.x = x;
844 x += child_allocation.width + childspacing;
847 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
848 child_allocation.x = (allocation->x + allocation->width)
849 - (child_allocation.x + child_allocation.width - allocation->x);
853 child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2;
855 if (gtk_button_box_get_child_secondary (bbox, child))
857 child_allocation.y = secondary_y;
858 secondary_y += child_allocation.height + childspacing;
862 child_allocation.y = y;
863 y += child_allocation.height + childspacing;
867 gtk_widget_size_allocate (child, &child_allocation);
878 * gtk_button_box_new:
879 * @orientation: the box' orientation.
881 * Creates a new #GtkButtonBox.
883 * Return value: a new #GtkButtonBox.
888 gtk_button_box_new (GtkOrientation orientation)
890 return g_object_new (GTK_TYPE_BUTTON_BOX,
891 "orientation", orientation,