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 "gtktypebuiltins.h"
53 #include "gtkprivate.h"
54 #include "gtksizerequest.h"
59 struct _GtkButtonBoxPrivate
61 GtkButtonBoxStyle layout_style;
74 #define GTK_BOX_SECONDARY_CHILD "gtk-box-secondary-child"
76 static void gtk_button_box_set_property (GObject *object,
80 static void gtk_button_box_get_property (GObject *object,
84 static void gtk_button_box_get_preferred_width (GtkWidget *widget,
87 static void gtk_button_box_get_preferred_height (GtkWidget *widget,
90 static void gtk_button_box_get_preferred_width_for_height (GtkWidget *widget,
94 static void gtk_button_box_get_preferred_height_for_width (GtkWidget *widget,
99 static void gtk_button_box_size_allocate (GtkWidget *widget,
100 GtkAllocation *allocation);
101 static void gtk_button_box_remove (GtkContainer *container,
103 static void gtk_button_box_set_child_property (GtkContainer *container,
108 static void gtk_button_box_get_child_property (GtkContainer *container,
114 #define DEFAULT_CHILD_MIN_WIDTH 85
115 #define DEFAULT_CHILD_MIN_HEIGHT 27
116 #define DEFAULT_CHILD_IPAD_X 4
117 #define DEFAULT_CHILD_IPAD_Y 0
118 #define DEFAULT_LAYOUT_STYLE GTK_BUTTONBOX_EDGE
120 G_DEFINE_TYPE (GtkButtonBox, gtk_button_box, GTK_TYPE_BOX)
123 gtk_button_box_class_init (GtkButtonBoxClass *class)
125 GtkWidgetClass *widget_class;
126 GObjectClass *gobject_class;
127 GtkContainerClass *container_class;
129 gobject_class = G_OBJECT_CLASS (class);
130 widget_class = (GtkWidgetClass*) class;
131 container_class = (GtkContainerClass*) class;
133 gobject_class->set_property = gtk_button_box_set_property;
134 gobject_class->get_property = gtk_button_box_get_property;
136 widget_class->get_preferred_width = gtk_button_box_get_preferred_width;
137 widget_class->get_preferred_height = gtk_button_box_get_preferred_height;
138 widget_class->get_preferred_width_for_height = gtk_button_box_get_preferred_width_for_height;
139 widget_class->get_preferred_height_for_width = gtk_button_box_get_preferred_height_for_width;
140 widget_class->size_allocate = gtk_button_box_size_allocate;
142 container_class->remove = gtk_button_box_remove;
143 container_class->set_child_property = gtk_button_box_set_child_property;
144 container_class->get_child_property = gtk_button_box_get_child_property;
145 gtk_container_class_handle_border_width (container_class);
147 /* FIXME we need to override the "spacing" property on GtkBox once
148 * libgobject allows that.
150 gtk_widget_class_install_style_property (widget_class,
151 g_param_spec_int ("child-min-width",
152 P_("Minimum child width"),
153 P_("Minimum width of buttons inside the box"),
156 DEFAULT_CHILD_MIN_WIDTH,
157 GTK_PARAM_READABLE));
159 gtk_widget_class_install_style_property (widget_class,
160 g_param_spec_int ("child-min-height",
161 P_("Minimum child height"),
162 P_("Minimum height of buttons inside the box"),
165 DEFAULT_CHILD_MIN_HEIGHT,
166 GTK_PARAM_READABLE));
168 gtk_widget_class_install_style_property (widget_class,
169 g_param_spec_int ("child-internal-pad-x",
170 P_("Child internal width padding"),
171 P_("Amount to increase child's size on either side"),
174 DEFAULT_CHILD_IPAD_X,
175 GTK_PARAM_READABLE));
177 gtk_widget_class_install_style_property (widget_class,
178 g_param_spec_int ("child-internal-pad-y",
179 P_("Child internal height padding"),
180 P_("Amount to increase child's size on the top and bottom"),
183 DEFAULT_CHILD_IPAD_Y,
184 GTK_PARAM_READABLE));
185 g_object_class_install_property (gobject_class,
187 g_param_spec_enum ("layout-style",
189 P_("How to lay out the buttons in the box. Possible values are: spread, edge, start and end"),
190 GTK_TYPE_BUTTON_BOX_STYLE,
191 DEFAULT_LAYOUT_STYLE,
192 GTK_PARAM_READWRITE));
194 gtk_container_class_install_child_property (container_class,
195 CHILD_PROP_SECONDARY,
196 g_param_spec_boolean ("secondary",
198 P_("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
200 GTK_PARAM_READWRITE));
202 g_type_class_add_private (class, sizeof (GtkButtonBoxPrivate));
206 gtk_button_box_init (GtkButtonBox *button_box)
208 GtkButtonBoxPrivate *priv;
210 button_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (button_box,
212 GtkButtonBoxPrivate);
213 priv = button_box->priv;
215 gtk_box_set_spacing (GTK_BOX (button_box), 0);
216 priv->layout_style = DEFAULT_LAYOUT_STYLE;
220 gtk_button_box_set_property (GObject *object,
227 case PROP_LAYOUT_STYLE:
228 gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
229 g_value_get_enum (value));
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gtk_button_box_get_property (GObject *object,
243 GtkButtonBoxPrivate *priv = GTK_BUTTON_BOX (object)->priv;
247 case PROP_LAYOUT_STYLE:
248 g_value_set_enum (value, priv->layout_style);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 gtk_button_box_set_child_property (GtkContainer *container,
265 case CHILD_PROP_SECONDARY:
266 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
267 g_value_get_boolean (value));
270 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
276 gtk_button_box_get_child_property (GtkContainer *container,
284 case CHILD_PROP_SECONDARY:
285 g_value_set_boolean (value,
286 gtk_button_box_get_child_secondary (GTK_BUTTON_BOX (container),
290 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
296 gtk_button_box_remove (GtkContainer *container,
299 /* clear is_secondary flag in case the widget
300 * is added to another container
302 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container),
306 GTK_CONTAINER_CLASS (gtk_button_box_parent_class)->remove (container, widget);
310 * gtk_button_box_set_layout:
311 * @widget: a #GtkButtonBox
312 * @layout_style: the new layout style
314 * Changes the way buttons are arranged in their container.
317 gtk_button_box_set_layout (GtkButtonBox *widget,
318 GtkButtonBoxStyle layout_style)
320 GtkButtonBoxPrivate *priv;
322 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
326 if (priv->layout_style != layout_style)
328 priv->layout_style = layout_style;
329 g_object_notify (G_OBJECT (widget), "layout-style");
330 gtk_widget_queue_resize (GTK_WIDGET (widget));
335 * gtk_button_box_get_layout:
336 * @widget: a #GtkButtonBox
338 * Retrieves the method being used to arrange the buttons in a button box.
340 * Returns: the method used to lay out buttons in @widget.
343 gtk_button_box_get_layout (GtkButtonBox *widget)
345 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), DEFAULT_LAYOUT_STYLE);
347 return widget->priv->layout_style;
351 * gtk_button_box_get_child_secondary:
352 * @widget: a #GtkButtonBox
353 * @child: a child of @widget
355 * Returns whether @child should appear in a secondary group of children.
357 * Return value: whether @child should appear in a secondary group of children.
362 gtk_button_box_get_child_secondary (GtkButtonBox *widget,
365 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), FALSE);
366 g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
368 return (g_object_get_data (G_OBJECT (child), GTK_BOX_SECONDARY_CHILD) != NULL);
372 * gtk_button_box_set_child_secondary
373 * @widget: a #GtkButtonBox
374 * @child: a child of @widget
375 * @is_secondary: if %TRUE, the @child appears in a secondary group of the
378 * Sets whether @child should appear in a secondary group of children.
379 * A typical use of a secondary child is the help button in a dialog.
381 * This group appears after the other children if the style
382 * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
383 * %GTK_BUTTONBOX_EDGE, and before the other children if the style
384 * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
385 * of before/after depends on direction of the widget (see
386 * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
387 * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
388 * the other end of the button box from the main children. For the
389 * other styles, they appear immediately next to the main children.
392 gtk_button_box_set_child_secondary (GtkButtonBox *widget,
394 gboolean is_secondary)
396 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
397 g_return_if_fail (GTK_IS_WIDGET (child));
398 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (widget));
400 g_object_set_data (G_OBJECT (child),
401 GTK_BOX_SECONDARY_CHILD,
402 is_secondary ? GINT_TO_POINTER (1) : NULL);
403 gtk_widget_child_notify (child, "secondary");
405 if (gtk_widget_get_visible (GTK_WIDGET (widget)) &&
406 gtk_widget_get_visible (child))
407 gtk_widget_queue_resize (child);
410 /* Ask children how much space they require and round up
411 * to match minimum size and internal padding.
412 * Returns the size each single child should have.
415 gtk_button_box_child_requisition (GtkWidget *widget,
417 gint *nvis_secondaries,
422 GList *children, *list;
428 GtkRequisition child_requisition;
431 gint child_min_width;
432 gint child_min_height;
435 gboolean homogeneous;
438 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
440 bbox = GTK_BUTTON_BOX (widget);
442 homogeneous = gtk_box_get_homogeneous (GTK_BOX (widget));
444 gtk_widget_style_get (widget,
445 "child-min-width", &child_min_width,
446 "child-min-height", &child_min_height,
447 "child-internal-pad-x", &ipad_x,
448 "child-internal-pad-y", &ipad_y,
453 list = children = _gtk_box_get_children (GTK_BOX (bbox));
454 needed_width = child_min_width;
455 needed_height = child_min_height;
464 child = children->data;
465 children = children->next;
467 if (gtk_widget_get_visible (child))
470 gtk_widget_get_preferred_size (child,
471 &child_requisition, NULL);
472 avg_w += child_requisition.width + ipad_w;
473 avg_h += child_requisition.height + ipad_h;
476 avg_w /= MAX (nchildren, 1);
477 avg_h /= MAX (nchildren, 1);
479 *widths = g_new (gint, nchildren);
480 *heights = g_new (gint, nchildren);
487 gboolean is_secondary;
489 child = children->data;
490 children = children->next;
492 if (gtk_widget_get_visible (child))
494 is_secondary = gtk_button_box_get_child_secondary (bbox, child);
498 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
500 if (homogeneous || (child_requisition.width + ipad_w < avg_w * 1.5))
503 if (child_requisition.width + ipad_w > needed_width)
504 needed_width = child_requisition.width + ipad_w;
508 (*widths)[i] = child_requisition.width + ipad_w;
511 if (homogeneous || (child_requisition.height + ipad_h < avg_h * 1.5))
514 if (child_requisition.height + ipad_h > needed_height)
515 needed_height = child_requisition.height + ipad_h;
519 (*heights)[i] = child_requisition.height + ipad_h;
528 for (i = 0; i < nchildren; i++)
530 if ((*widths)[i] == -1)
531 (*widths)[i] = needed_width;
532 if ((*heights)[i] == -1)
533 (*heights)[i] = needed_height;
537 *nvis_children = nchildren;
539 if (nvis_secondaries)
540 *nvis_secondaries = nsecondaries;
544 gtk_button_box_size_request (GtkWidget *widget,
545 GtkRequisition *requisition)
547 GtkButtonBoxPrivate *priv;
553 GtkOrientation orientation;
558 bbox = GTK_BUTTON_BOX (widget);
561 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
562 spacing = gtk_box_get_spacing (GTK_BOX (widget));
564 gtk_button_box_child_requisition (widget,
571 for (i = 0; i < nvis_children; i++)
573 if (orientation == GTK_ORIENTATION_HORIZONTAL)
575 total_size += widths[i];
576 max_size = MAX (max_size, heights[i]);
580 total_size += heights[i];
581 max_size = MAX (max_size, widths[i]);
587 if (nvis_children == 0)
589 requisition->width = 0;
590 requisition->height = 0;
594 switch (priv->layout_style)
596 case GTK_BUTTONBOX_SPREAD:
597 if (orientation == GTK_ORIENTATION_HORIZONTAL)
598 requisition->width = total_size + ((nvis_children + 1)*spacing);
600 requisition->height = total_size + ((nvis_children + 1)*spacing);
603 case GTK_BUTTONBOX_EDGE:
604 case GTK_BUTTONBOX_START:
605 case GTK_BUTTONBOX_END:
606 case GTK_BUTTONBOX_CENTER:
607 if (orientation == GTK_ORIENTATION_HORIZONTAL)
608 requisition->width = total_size + ((nvis_children - 1)*spacing);
610 requisition->height = total_size + ((nvis_children - 1)*spacing);
614 g_assert_not_reached ();
618 if (orientation == GTK_ORIENTATION_HORIZONTAL)
619 requisition->height = max_size;
621 requisition->width = max_size;
626 gtk_button_box_get_preferred_width (GtkWidget *widget,
630 GtkRequisition requisition;
632 gtk_button_box_size_request (widget, &requisition);
634 *minimum = *natural = requisition.width;
638 gtk_button_box_get_preferred_height (GtkWidget *widget,
642 GtkRequisition requisition;
644 gtk_button_box_size_request (widget, &requisition);
646 *minimum = *natural = requisition.height;
650 gtk_button_box_get_preferred_width_for_height (GtkWidget *widget,
655 gtk_button_box_get_preferred_width (widget, minimum, natural);
659 gtk_button_box_get_preferred_height_for_width (GtkWidget *widget,
664 gtk_button_box_get_preferred_height (widget, minimum, natural);
668 gtk_button_box_size_allocate (GtkWidget *widget,
669 GtkAllocation *allocation)
671 GtkButtonBoxPrivate *priv;
673 GList *children, *list;
674 GtkAllocation child_allocation;
680 gint secondary_x = 0;
681 gint secondary_y = 0;
684 gint childspacing = 0;
686 GtkOrientation orientation;
696 bbox = GTK_BUTTON_BOX (widget);
699 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
700 spacing = gtk_box_get_spacing (GTK_BOX (widget));
702 gtk_widget_style_get (widget,
703 "child-internal-pad-x", &ipad_x,
704 "child-internal-pad-y", &ipad_y,
706 gtk_button_box_child_requisition (widget,
711 n_primaries = nvis_children - n_secondaries;
714 if (orientation == GTK_ORIENTATION_HORIZONTAL)
720 list = children = _gtk_box_get_children (GTK_BOX (widget));
725 child = children->data;
726 children = children->next;
728 if (gtk_widget_get_visible (child))
730 if (gtk_button_box_get_child_secondary (bbox, child))
731 secondary_size += sizes[i];
733 primary_size += sizes[i];
737 total_size = primary_size + secondary_size;
739 gtk_widget_set_allocation (widget, allocation);
741 if (orientation == GTK_ORIENTATION_HORIZONTAL)
742 width = allocation->width;
744 height = allocation->height;
746 switch (priv->layout_style)
748 case GTK_BUTTONBOX_SPREAD:
750 if (orientation == GTK_ORIENTATION_HORIZONTAL)
752 childspacing = (width - total_size) / (nvis_children + 1);
753 x = allocation->x + childspacing;
754 secondary_x = x + primary_size + n_primaries * childspacing;
758 childspacing = (height - total_size) / (nvis_children + 1);
759 y = allocation->y + childspacing;
760 secondary_y = y + primary_size + n_primaries * childspacing;
765 case GTK_BUTTONBOX_EDGE:
767 if (orientation == GTK_ORIENTATION_HORIZONTAL)
769 if (nvis_children >= 2)
771 childspacing = (width - total_size) / (nvis_children - 1);
773 secondary_x = x + primary_size + n_primaries * childspacing;
775 else if (nvis_children == 1)
777 /* one child, just center */
778 childspacing = width;
779 x = secondary_x = allocation->x
780 + (allocation->width - widths[0]) / 2;
784 /* zero children, meh */
785 childspacing = width;
786 x = secondary_x = allocation->x + allocation->width / 2;
791 if (nvis_children >= 2)
793 childspacing = (height - total_size) / (nvis_children - 1);
795 secondary_y = y + primary_size + n_primaries * childspacing;
797 else if (nvis_children == 1)
799 /* one child, just center */
800 childspacing = height;
801 y = secondary_y = allocation->y
802 + (allocation->height - heights[0]) / 2;
806 /* zero children, meh */
807 childspacing = height;
808 y = secondary_y = allocation->y + allocation->height / 2;
814 case GTK_BUTTONBOX_START:
816 if (orientation == GTK_ORIENTATION_HORIZONTAL)
818 childspacing = spacing;
820 secondary_x = allocation->x + allocation->width
821 - secondary_size - spacing * (n_secondaries - 1);
825 childspacing = spacing;
827 secondary_y = allocation->y + allocation->height
828 - secondary_size - spacing * (n_secondaries - 1);
833 case GTK_BUTTONBOX_END:
835 if (orientation == GTK_ORIENTATION_HORIZONTAL)
837 childspacing = spacing;
838 x = allocation->x + allocation->width
839 - primary_size - spacing * (n_primaries - 1);
840 secondary_x = allocation->x;
844 childspacing = spacing;
845 y = allocation->y + allocation->height
846 - primary_size - spacing * (n_primaries - 1);
847 secondary_y = allocation->y;
852 case GTK_BUTTONBOX_CENTER:
854 if (orientation == GTK_ORIENTATION_HORIZONTAL)
856 childspacing = spacing;
859 - (primary_size + spacing * (n_primaries - 1))) / 2
860 + (secondary_size + n_secondaries * spacing) / 2;
861 secondary_x = allocation->x;
865 childspacing = spacing;
868 - (primary_size + spacing * (n_primaries - 1))) / 2
869 + (secondary_size + n_secondaries * spacing) / 2;
870 secondary_y = allocation->y;
876 g_assert_not_reached ();
886 child = children->data;
887 children = children->next;
889 if (gtk_widget_get_visible (child))
891 child_allocation.width = widths[i];
892 child_allocation.height = heights[i];
894 if (orientation == GTK_ORIENTATION_HORIZONTAL)
896 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
898 if (gtk_button_box_get_child_secondary (bbox, child))
900 child_allocation.x = secondary_x;
901 secondary_x += child_allocation.width + childspacing;
905 child_allocation.x = x;
906 x += child_allocation.width + childspacing;
909 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
910 child_allocation.x = (allocation->x + allocation->width)
911 - (child_allocation.x + child_allocation.width - allocation->x);
915 child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2;
917 if (gtk_button_box_get_child_secondary (bbox, child))
919 child_allocation.y = secondary_y;
920 secondary_y += child_allocation.height + childspacing;
924 child_allocation.y = y;
925 y += child_allocation.height + childspacing;
929 gtk_widget_size_allocate (child, &child_allocation);
940 * gtk_button_box_new:
941 * @orientation: the box' orientation.
943 * Creates a new #GtkButtonBox.
945 * Return value: a new #GtkButtonBox.
950 gtk_button_box_new (GtkOrientation orientation)
952 return g_object_new (GTK_TYPE_BUTTON_BOX,
953 "orientation", orientation,