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"
54 #include "gtktypeutils.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;
146 /* FIXME we need to override the "spacing" property on GtkBox once
147 * libgobject allows that.
149 gtk_widget_class_install_style_property (widget_class,
150 g_param_spec_int ("child-min-width",
151 P_("Minimum child width"),
152 P_("Minimum width of buttons inside the box"),
155 DEFAULT_CHILD_MIN_WIDTH,
156 GTK_PARAM_READABLE));
158 gtk_widget_class_install_style_property (widget_class,
159 g_param_spec_int ("child-min-height",
160 P_("Minimum child height"),
161 P_("Minimum height of buttons inside the box"),
164 DEFAULT_CHILD_MIN_HEIGHT,
165 GTK_PARAM_READABLE));
167 gtk_widget_class_install_style_property (widget_class,
168 g_param_spec_int ("child-internal-pad-x",
169 P_("Child internal width padding"),
170 P_("Amount to increase child's size on either side"),
173 DEFAULT_CHILD_IPAD_X,
174 GTK_PARAM_READABLE));
176 gtk_widget_class_install_style_property (widget_class,
177 g_param_spec_int ("child-internal-pad-y",
178 P_("Child internal height padding"),
179 P_("Amount to increase child's size on the top and bottom"),
182 DEFAULT_CHILD_IPAD_Y,
183 GTK_PARAM_READABLE));
184 g_object_class_install_property (gobject_class,
186 g_param_spec_enum ("layout-style",
188 P_("How to lay out the buttons in the box. Possible values are: spread, edge, start and end"),
189 GTK_TYPE_BUTTON_BOX_STYLE,
190 DEFAULT_LAYOUT_STYLE,
191 GTK_PARAM_READWRITE));
193 gtk_container_class_install_child_property (container_class,
194 CHILD_PROP_SECONDARY,
195 g_param_spec_boolean ("secondary",
197 P_("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
199 GTK_PARAM_READWRITE));
201 g_type_class_add_private (class, sizeof (GtkButtonBoxPrivate));
205 gtk_button_box_init (GtkButtonBox *button_box)
207 GtkButtonBoxPrivate *priv;
209 button_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (button_box,
211 GtkButtonBoxPrivate);
212 priv = button_box->priv;
214 gtk_box_set_spacing (GTK_BOX (button_box), 0);
215 priv->layout_style = DEFAULT_LAYOUT_STYLE;
219 gtk_button_box_set_property (GObject *object,
226 case PROP_LAYOUT_STYLE:
227 gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
228 g_value_get_enum (value));
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 gtk_button_box_get_property (GObject *object,
242 GtkButtonBoxPrivate *priv = GTK_BUTTON_BOX (object)->priv;
246 case PROP_LAYOUT_STYLE:
247 g_value_set_enum (value, priv->layout_style);
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gtk_button_box_set_child_property (GtkContainer *container,
264 case CHILD_PROP_SECONDARY:
265 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
266 g_value_get_boolean (value));
269 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
275 gtk_button_box_get_child_property (GtkContainer *container,
283 case CHILD_PROP_SECONDARY:
284 g_value_set_boolean (value,
285 gtk_button_box_get_child_secondary (GTK_BUTTON_BOX (container),
289 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
295 gtk_button_box_remove (GtkContainer *container,
298 /* clear is_secondary flag in case the widget
299 * is added to another container
301 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container),
305 GTK_CONTAINER_CLASS (gtk_button_box_parent_class)->remove (container, widget);
309 * gtk_button_box_set_layout:
310 * @widget: a #GtkButtonBox
311 * @layout_style: the new layout style
313 * Changes the way buttons are arranged in their container.
316 gtk_button_box_set_layout (GtkButtonBox *widget,
317 GtkButtonBoxStyle layout_style)
319 GtkButtonBoxPrivate *priv;
321 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
325 if (priv->layout_style != layout_style)
327 priv->layout_style = layout_style;
328 g_object_notify (G_OBJECT (widget), "layout-style");
329 gtk_widget_queue_resize (GTK_WIDGET (widget));
334 * gtk_button_box_get_layout:
335 * @widget: a #GtkButtonBox
337 * Retrieves the method being used to arrange the buttons in a button box.
339 * Returns: the method used to lay out buttons in @widget.
342 gtk_button_box_get_layout (GtkButtonBox *widget)
344 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), DEFAULT_LAYOUT_STYLE);
346 return widget->priv->layout_style;
350 * gtk_button_box_get_child_secondary:
351 * @widget: a #GtkButtonBox
352 * @child: a child of @widget
354 * Returns whether @child should appear in a secondary group of children.
356 * Return value: whether @child should appear in a secondary group of children.
361 gtk_button_box_get_child_secondary (GtkButtonBox *widget,
364 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), FALSE);
365 g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
367 return (g_object_get_data (G_OBJECT (child), GTK_BOX_SECONDARY_CHILD) != NULL);
371 * gtk_button_box_set_child_secondary
372 * @widget: a #GtkButtonBox
373 * @child: a child of @widget
374 * @is_secondary: if %TRUE, the @child appears in a secondary group of the
377 * Sets whether @child should appear in a secondary group of children.
378 * A typical use of a secondary child is the help button in a dialog.
380 * This group appears after the other children if the style
381 * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
382 * %GTK_BUTTONBOX_EDGE, and before the other children if the style
383 * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
384 * of before/after depends on direction of the widget (see
385 * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
386 * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
387 * the other end of the button box from the main children. For the
388 * other styles, they appear immediately next to the main children.
391 gtk_button_box_set_child_secondary (GtkButtonBox *widget,
393 gboolean is_secondary)
395 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
396 g_return_if_fail (GTK_IS_WIDGET (child));
397 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (widget));
399 g_object_set_data (G_OBJECT (child),
400 GTK_BOX_SECONDARY_CHILD,
401 is_secondary ? GINT_TO_POINTER (1) : NULL);
402 gtk_widget_child_notify (child, "secondary");
404 if (gtk_widget_get_visible (GTK_WIDGET (widget)) &&
405 gtk_widget_get_visible (child))
406 gtk_widget_queue_resize (child);
409 /* Ask children how much space they require and round up
410 * to match minimum size and internal padding.
411 * Returns the size each single child should have.
414 gtk_button_box_child_requisition (GtkWidget *widget,
416 gint *nvis_secondaries,
420 GtkButtonBoxPrivate *priv;
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);
443 homogeneous = gtk_box_get_homogeneous (GTK_BOX (widget));
445 gtk_widget_style_get (widget,
446 "child-min-width", &child_min_width,
447 "child-min-height", &child_min_height,
448 "child-internal-pad-x", &ipad_x,
449 "child-internal-pad-y", &ipad_y,
454 list = children = _gtk_box_get_children (GTK_BOX (bbox));
455 needed_width = child_min_width;
456 needed_height = child_min_height;
465 child = children->data;
466 children = children->next;
468 if (gtk_widget_get_visible (child))
471 gtk_widget_get_preferred_size (child,
472 &child_requisition, NULL);
473 avg_w += child_requisition.width + ipad_w;
474 avg_h += child_requisition.height + ipad_h;
477 avg_w /= MAX (nchildren, 1);
478 avg_h /= MAX (nchildren, 1);
480 *widths = g_new (gint, nchildren);
481 *heights = g_new (gint, nchildren);
488 gboolean is_secondary;
490 child = children->data;
491 children = children->next;
493 if (gtk_widget_get_visible (child))
495 is_secondary = gtk_button_box_get_child_secondary (bbox, child);
499 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
501 if (homogeneous || (child_requisition.width + ipad_w < avg_w * 1.5))
504 if (child_requisition.width + ipad_w > needed_width)
505 needed_width = child_requisition.width + ipad_w;
509 (*widths)[i] = child_requisition.width + ipad_w;
512 if (homogeneous || (child_requisition.height + ipad_h < avg_h * 1.5))
515 if (child_requisition.height + ipad_h > needed_height)
516 needed_height = child_requisition.height + ipad_h;
520 (*heights)[i] = child_requisition.height + ipad_h;
529 for (i = 0; i < nchildren; i++)
531 if ((*widths)[i] == -1)
532 (*widths)[i] = needed_width;
533 if ((*heights)[i] == -1)
534 (*heights)[i] = needed_height;
538 *nvis_children = nchildren;
540 if (nvis_secondaries)
541 *nvis_secondaries = nsecondaries;
545 gtk_button_box_size_request (GtkWidget *widget,
546 GtkRequisition *requisition)
548 GtkButtonBoxPrivate *priv;
555 GtkOrientation orientation;
560 bbox = GTK_BUTTON_BOX (widget);
563 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
564 spacing = gtk_box_get_spacing (GTK_BOX (widget));
566 gtk_button_box_child_requisition (widget,
573 for (i = 0; i < nvis_children; i++)
575 if (orientation == GTK_ORIENTATION_HORIZONTAL)
577 total_size += widths[i];
578 max_size = MAX (max_size, heights[i]);
582 total_size += heights[i];
583 max_size = MAX (max_size, widths[i]);
589 if (nvis_children == 0)
591 requisition->width = 0;
592 requisition->height = 0;
596 switch (priv->layout_style)
598 case GTK_BUTTONBOX_SPREAD:
599 if (orientation == GTK_ORIENTATION_HORIZONTAL)
600 requisition->width = total_size + ((nvis_children + 1)*spacing);
602 requisition->height = total_size + ((nvis_children + 1)*spacing);
605 case GTK_BUTTONBOX_EDGE:
606 case GTK_BUTTONBOX_START:
607 case GTK_BUTTONBOX_END:
608 case GTK_BUTTONBOX_CENTER:
609 if (orientation == GTK_ORIENTATION_HORIZONTAL)
610 requisition->width = total_size + ((nvis_children - 1)*spacing);
612 requisition->height = total_size + ((nvis_children - 1)*spacing);
616 g_assert_not_reached ();
620 if (orientation == GTK_ORIENTATION_HORIZONTAL)
621 requisition->height = max_size;
623 requisition->width = max_size;
626 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
627 requisition->width += border_width * 2;
628 requisition->height += border_width * 2;
632 gtk_button_box_get_preferred_width (GtkWidget *widget,
636 GtkRequisition requisition;
638 gtk_button_box_size_request (widget, &requisition);
640 *minimum = *natural = requisition.width;
644 gtk_button_box_get_preferred_height (GtkWidget *widget,
648 GtkRequisition requisition;
650 gtk_button_box_size_request (widget, &requisition);
652 *minimum = *natural = requisition.height;
656 gtk_button_box_get_preferred_width_for_height (GtkWidget *widget,
661 gtk_button_box_get_preferred_width (widget, minimum, natural);
665 gtk_button_box_get_preferred_height_for_width (GtkWidget *widget,
670 gtk_button_box_get_preferred_height (widget, minimum, natural);
674 gtk_button_box_size_allocate (GtkWidget *widget,
675 GtkAllocation *allocation)
677 GtkButtonBoxPrivate *priv;
679 GList *children, *list;
680 GtkAllocation child_allocation;
686 gint secondary_x = 0;
687 gint secondary_y = 0;
690 gint childspacing = 0;
693 GtkOrientation orientation;
703 bbox = GTK_BUTTON_BOX (widget);
706 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
707 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
708 spacing = gtk_box_get_spacing (GTK_BOX (widget));
710 gtk_widget_style_get (widget,
711 "child-internal-pad-x", &ipad_x,
712 "child-internal-pad-y", &ipad_y,
714 gtk_button_box_child_requisition (widget,
719 n_primaries = nvis_children - n_secondaries;
722 if (orientation == GTK_ORIENTATION_HORIZONTAL)
728 list = children = _gtk_box_get_children (GTK_BOX (widget));
733 child = children->data;
734 children = children->next;
736 if (gtk_widget_get_visible (child))
738 if (gtk_button_box_get_child_secondary (bbox, child))
739 secondary_size += sizes[i];
741 primary_size += sizes[i];
745 total_size = primary_size + secondary_size;
747 gtk_widget_set_allocation (widget, allocation);
749 if (orientation == GTK_ORIENTATION_HORIZONTAL)
750 width = allocation->width - border_width*2;
752 height = allocation->height - border_width*2;
754 switch (priv->layout_style)
756 case GTK_BUTTONBOX_SPREAD:
758 if (orientation == GTK_ORIENTATION_HORIZONTAL)
760 childspacing = (width - total_size) / (nvis_children + 1);
761 x = allocation->x + border_width + childspacing;
762 secondary_x = x + primary_size + n_primaries * childspacing;
766 childspacing = (height - total_size) / (nvis_children + 1);
767 y = allocation->y + border_width + childspacing;
768 secondary_y = y + primary_size + n_primaries * childspacing;
773 case GTK_BUTTONBOX_EDGE:
775 if (orientation == GTK_ORIENTATION_HORIZONTAL)
777 if (nvis_children >= 2)
779 childspacing = (width - total_size) / (nvis_children - 1);
780 x = allocation->x + border_width;
781 secondary_x = x + primary_size + n_primaries * childspacing;
785 /* one or zero children, just center */
786 childspacing = width;
787 x = secondary_x = allocation->x
788 + (allocation->width - widths[0]) / 2;
793 if (nvis_children >= 2)
795 childspacing = (height - total_size) / (nvis_children - 1);
796 y = allocation->y + border_width;
797 secondary_y = y + primary_size + n_primaries * childspacing;
801 /* one or zero children, just center */
802 childspacing = height;
803 y = secondary_y = allocation->y
804 + (allocation->height - heights[0]) / 2;
810 case GTK_BUTTONBOX_START:
812 if (orientation == GTK_ORIENTATION_HORIZONTAL)
814 childspacing = spacing;
815 x = allocation->x + border_width;
816 secondary_x = allocation->x + allocation->width
817 - secondary_size - spacing * (n_secondaries - 1) - border_width;
821 childspacing = spacing;
822 y = allocation->y + border_width;
823 secondary_y = allocation->y + allocation->height
824 - secondary_size - spacing * (n_secondaries - 1) - border_width;
829 case GTK_BUTTONBOX_END:
831 if (orientation == GTK_ORIENTATION_HORIZONTAL)
833 childspacing = spacing;
834 x = allocation->x + allocation->width
835 - primary_size - spacing * (n_primaries - 1) - border_width;
836 secondary_x = allocation->x + border_width;
840 childspacing = spacing;
841 y = allocation->y + allocation->height
842 - primary_size - spacing * (n_primaries - 1) - border_width;
843 secondary_y = allocation->y + border_width;
848 case GTK_BUTTONBOX_CENTER:
850 if (orientation == GTK_ORIENTATION_HORIZONTAL)
852 childspacing = spacing;
855 - (primary_size + spacing * (n_primaries - 1))) / 2
856 + (secondary_size + n_secondaries * spacing) / 2;
857 secondary_x = allocation->x + border_width;
861 childspacing = spacing;
864 - (primary_size + spacing * (n_primaries - 1))) / 2
865 + (secondary_size + n_secondaries * spacing) / 2;
866 secondary_y = allocation->y + border_width;
872 g_assert_not_reached ();
882 child = children->data;
883 children = children->next;
885 if (gtk_widget_get_visible (child))
887 child_allocation.width = widths[i];
888 child_allocation.height = heights[i];
890 if (orientation == GTK_ORIENTATION_HORIZONTAL)
892 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
894 if (gtk_button_box_get_child_secondary (bbox, child))
896 child_allocation.x = secondary_x;
897 secondary_x += child_allocation.width + childspacing;
901 child_allocation.x = x;
902 x += child_allocation.width + childspacing;
905 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
906 child_allocation.x = (allocation->x + allocation->width)
907 - (child_allocation.x + child_allocation.width - allocation->x);
911 child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2;
913 if (gtk_button_box_get_child_secondary (bbox, child))
915 child_allocation.y = secondary_y;
916 secondary_y += child_allocation.height + childspacing;
920 child_allocation.y = y;
921 y += child_allocation.height + childspacing;
925 gtk_widget_size_allocate (child, &child_allocation);
936 * gtk_button_box_new:
937 * @orientation: the box' orientation.
939 * Creates a new #GtkButtonBox.
941 * Return value: a new #GtkButtonBox.
946 gtk_button_box_new (GtkOrientation orientation)
948 return g_object_new (GTK_TYPE_BUTTON_BOX,
949 "orientation", orientation,