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.
49 #include "gtkorientable.h"
50 #include "gtkprivate.h"
54 struct _GtkButtonBoxPrivate
56 GtkButtonBoxStyle layout_style;
69 #define GTK_BOX_SECONDARY_CHILD "gtk-box-secondary-child"
71 static void gtk_button_box_set_property (GObject *object,
75 static void gtk_button_box_get_property (GObject *object,
79 static void gtk_button_box_size_request (GtkWidget *widget,
80 GtkRequisition *requisition);
81 static void gtk_button_box_size_allocate (GtkWidget *widget,
82 GtkAllocation *allocation);
83 static void gtk_button_box_remove (GtkContainer *container,
85 static void gtk_button_box_set_child_property (GtkContainer *container,
90 static void gtk_button_box_get_child_property (GtkContainer *container,
96 #define DEFAULT_CHILD_MIN_WIDTH 85
97 #define DEFAULT_CHILD_MIN_HEIGHT 27
98 #define DEFAULT_CHILD_IPAD_X 4
99 #define DEFAULT_CHILD_IPAD_Y 0
100 #define DEFAULT_LAYOUT_STYLE GTK_BUTTONBOX_EDGE
102 G_DEFINE_TYPE (GtkButtonBox, gtk_button_box, GTK_TYPE_BOX)
105 gtk_button_box_class_init (GtkButtonBoxClass *class)
107 GtkWidgetClass *widget_class;
108 GObjectClass *gobject_class;
109 GtkContainerClass *container_class;
111 gobject_class = G_OBJECT_CLASS (class);
112 widget_class = (GtkWidgetClass*) class;
113 container_class = (GtkContainerClass*) class;
115 gobject_class->set_property = gtk_button_box_set_property;
116 gobject_class->get_property = gtk_button_box_get_property;
118 widget_class->size_request = gtk_button_box_size_request;
119 widget_class->size_allocate = gtk_button_box_size_allocate;
121 container_class->remove = gtk_button_box_remove;
122 container_class->set_child_property = gtk_button_box_set_child_property;
123 container_class->get_child_property = gtk_button_box_get_child_property;
125 /* FIXME we need to override the "spacing" property on GtkBox once
126 * libgobject allows that.
128 gtk_widget_class_install_style_property (widget_class,
129 g_param_spec_int ("child-min-width",
130 P_("Minimum child width"),
131 P_("Minimum width of buttons inside the box"),
134 DEFAULT_CHILD_MIN_WIDTH,
135 GTK_PARAM_READABLE));
137 gtk_widget_class_install_style_property (widget_class,
138 g_param_spec_int ("child-min-height",
139 P_("Minimum child height"),
140 P_("Minimum height of buttons inside the box"),
143 DEFAULT_CHILD_MIN_HEIGHT,
144 GTK_PARAM_READABLE));
146 gtk_widget_class_install_style_property (widget_class,
147 g_param_spec_int ("child-internal-pad-x",
148 P_("Child internal width padding"),
149 P_("Amount to increase child's size on either side"),
152 DEFAULT_CHILD_IPAD_X,
153 GTK_PARAM_READABLE));
155 gtk_widget_class_install_style_property (widget_class,
156 g_param_spec_int ("child-internal-pad-y",
157 P_("Child internal height padding"),
158 P_("Amount to increase child's size on the top and bottom"),
161 DEFAULT_CHILD_IPAD_Y,
162 GTK_PARAM_READABLE));
163 g_object_class_install_property (gobject_class,
165 g_param_spec_enum ("layout-style",
167 P_("How to lay out the buttons in the box. Possible values are: spread, edge, start and end"),
168 GTK_TYPE_BUTTON_BOX_STYLE,
169 DEFAULT_LAYOUT_STYLE,
170 GTK_PARAM_READWRITE));
172 gtk_container_class_install_child_property (container_class,
173 CHILD_PROP_SECONDARY,
174 g_param_spec_boolean ("secondary",
176 P_("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
178 GTK_PARAM_READWRITE));
180 g_type_class_add_private (class, sizeof (GtkButtonBoxPrivate));
184 gtk_button_box_init (GtkButtonBox *button_box)
186 GtkButtonBoxPrivate *priv;
188 button_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (button_box,
190 GtkButtonBoxPrivate);
191 priv = button_box->priv;
193 gtk_box_set_spacing (GTK_BOX (button_box), 0);
194 priv->layout_style = DEFAULT_LAYOUT_STYLE;
198 gtk_button_box_set_property (GObject *object,
205 case PROP_LAYOUT_STYLE:
206 gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
207 g_value_get_enum (value));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 gtk_button_box_get_property (GObject *object,
221 GtkButtonBoxPrivate *priv = GTK_BUTTON_BOX (object)->priv;
225 case PROP_LAYOUT_STYLE:
226 g_value_set_enum (value, priv->layout_style);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 gtk_button_box_set_child_property (GtkContainer *container,
243 case CHILD_PROP_SECONDARY:
244 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
245 g_value_get_boolean (value));
248 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
254 gtk_button_box_get_child_property (GtkContainer *container,
262 case CHILD_PROP_SECONDARY:
263 g_value_set_boolean (value,
264 gtk_button_box_get_child_secondary (GTK_BUTTON_BOX (container),
268 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
274 gtk_button_box_remove (GtkContainer *container,
277 /* clear is_secondary flag in case the widget
278 * is added to another container
280 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container),
284 GTK_CONTAINER_CLASS (gtk_button_box_parent_class)->remove (container, widget);
288 * gtk_button_box_set_layout:
289 * @widget: a #GtkButtonBox
290 * @layout_style: the new layout style
292 * Changes the way buttons are arranged in their container.
295 gtk_button_box_set_layout (GtkButtonBox *widget,
296 GtkButtonBoxStyle layout_style)
298 GtkButtonBoxPrivate *priv;
300 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
304 if (priv->layout_style != layout_style)
306 priv->layout_style = layout_style;
307 g_object_notify (G_OBJECT (widget), "layout-style");
308 gtk_widget_queue_resize (GTK_WIDGET (widget));
313 * gtk_button_box_get_layout:
314 * @widget: a #GtkButtonBox
316 * Retrieves the method being used to arrange the buttons in a button box.
318 * Returns: the method used to lay out buttons in @widget.
321 gtk_button_box_get_layout (GtkButtonBox *widget)
323 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), DEFAULT_LAYOUT_STYLE);
325 return widget->priv->layout_style;
329 * gtk_button_box_get_child_secondary:
330 * @widget: a #GtkButtonBox
331 * @child: a child of @widget
333 * Returns whether @child should appear in a secondary group of children.
335 * Return value: whether @child should appear in a secondary group of children.
340 gtk_button_box_get_child_secondary (GtkButtonBox *widget,
343 g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), FALSE);
344 g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
346 return (g_object_get_data (G_OBJECT (child), GTK_BOX_SECONDARY_CHILD) != NULL);
350 * gtk_button_box_set_child_secondary
351 * @widget: a #GtkButtonBox
352 * @child: a child of @widget
353 * @is_secondary: if %TRUE, the @child appears in a secondary group of the
356 * Sets whether @child should appear in a secondary group of children.
357 * A typical use of a secondary child is the help button in a dialog.
359 * This group appears after the other children if the style
360 * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
361 * %GTK_BUTTONBOX_EDGE, and before the other children if the style
362 * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
363 * of before/after depends on direction of the widget (see
364 * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
365 * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
366 * the other end of the button box from the main children. For the
367 * other styles, they appear immediately next to the main children.
370 gtk_button_box_set_child_secondary (GtkButtonBox *widget,
372 gboolean is_secondary)
374 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
375 g_return_if_fail (GTK_IS_WIDGET (child));
376 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (widget));
378 g_object_set_data (G_OBJECT (child),
379 GTK_BOX_SECONDARY_CHILD,
380 is_secondary ? GINT_TO_POINTER (1) : NULL);
381 gtk_widget_child_notify (child, "secondary");
383 if (gtk_widget_get_visible (GTK_WIDGET (widget)) &&
384 gtk_widget_get_visible (child))
385 gtk_widget_queue_resize (child);
388 /* Ask children how much space they require and round up
389 * to match minimum size and internal padding.
390 * Returns the size each single child should have.
393 gtk_button_box_child_requisition (GtkWidget *widget,
395 gint *nvis_secondaries,
399 GtkButtonBoxPrivate *priv;
401 GList *children, *list;
407 GtkRequisition child_requisition;
410 gint child_min_width;
411 gint child_min_height;
414 gboolean homogeneous;
417 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
419 bbox = GTK_BUTTON_BOX (widget);
422 homogeneous = gtk_box_get_homogeneous (GTK_BOX (widget));
424 gtk_widget_style_get (widget,
425 "child-min-width", &child_min_width,
426 "child-min-height", &child_min_height,
427 "child-internal-pad-x", &ipad_x,
428 "child-internal-pad-y", &ipad_y,
433 list = children = _gtk_box_get_children (GTK_BOX (bbox));
434 needed_width = child_min_width;
435 needed_height = child_min_height;
444 child = children->data;
445 children = children->next;
447 if (gtk_widget_get_visible (child))
450 gtk_widget_size_request (child, &child_requisition);
451 avg_w += child_requisition.width + ipad_w;
452 avg_h += child_requisition.height + ipad_h;
455 avg_w /= MAX (nchildren, 1);
456 avg_h /= MAX (nchildren, 1);
458 *widths = g_new (gint, nchildren);
459 *heights = g_new (gint, nchildren);
466 gboolean is_secondary;
468 child = children->data;
469 children = children->next;
471 if (gtk_widget_get_visible (child))
473 is_secondary = gtk_button_box_get_child_secondary (bbox, child);
477 gtk_widget_get_child_requisition (child, &child_requisition);
479 if (homogeneous || (child_requisition.width + ipad_w < avg_w * 1.5))
482 if (child_requisition.width + ipad_w > needed_width)
483 needed_width = child_requisition.width + ipad_w;
487 (*widths)[i] = child_requisition.width + ipad_w;
490 if (homogeneous || (child_requisition.height + ipad_h < avg_h * 1.5))
493 if (child_requisition.height + ipad_h > needed_height)
494 needed_height = child_requisition.height + ipad_h;
498 (*heights)[i] = child_requisition.height + ipad_h;
507 for (i = 0; i < nchildren; i++)
509 if ((*widths)[i] == -1)
510 (*widths)[i] = needed_width;
511 if ((*heights)[i] == -1)
512 (*heights)[i] = needed_height;
516 *nvis_children = nchildren;
518 if (nvis_secondaries)
519 *nvis_secondaries = nsecondaries;
523 gtk_button_box_size_request (GtkWidget *widget,
524 GtkRequisition *requisition)
526 GtkButtonBoxPrivate *priv;
533 GtkOrientation orientation;
538 bbox = GTK_BUTTON_BOX (widget);
541 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
542 spacing = gtk_box_get_spacing (GTK_BOX (widget));
544 gtk_button_box_child_requisition (widget,
551 for (i = 0; i < nvis_children; i++)
553 if (orientation == GTK_ORIENTATION_HORIZONTAL)
555 total_size += widths[i];
556 max_size = MAX (max_size, heights[i]);
560 total_size += heights[i];
561 max_size = MAX (max_size, widths[i]);
567 if (nvis_children == 0)
569 requisition->width = 0;
570 requisition->height = 0;
574 switch (priv->layout_style)
576 case GTK_BUTTONBOX_SPREAD:
577 if (orientation == GTK_ORIENTATION_HORIZONTAL)
578 requisition->width = total_size + ((nvis_children + 1)*spacing);
580 requisition->height = total_size + ((nvis_children + 1)*spacing);
583 case GTK_BUTTONBOX_EDGE:
584 case GTK_BUTTONBOX_START:
585 case GTK_BUTTONBOX_END:
586 case GTK_BUTTONBOX_CENTER:
587 if (orientation == GTK_ORIENTATION_HORIZONTAL)
588 requisition->width = total_size + ((nvis_children - 1)*spacing);
590 requisition->height = total_size + ((nvis_children - 1)*spacing);
594 g_assert_not_reached ();
598 if (orientation == GTK_ORIENTATION_HORIZONTAL)
599 requisition->height = max_size;
601 requisition->width = max_size;
604 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
605 requisition->width += border_width * 2;
606 requisition->height += border_width * 2;
610 gtk_button_box_size_allocate (GtkWidget *widget,
611 GtkAllocation *allocation)
613 GtkButtonBoxPrivate *priv;
615 GList *children, *list;
616 GtkAllocation child_allocation;
622 gint secondary_x = 0;
623 gint secondary_y = 0;
626 gint childspacing = 0;
629 GtkOrientation orientation;
639 bbox = GTK_BUTTON_BOX (widget);
642 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
643 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (widget));
644 spacing = gtk_box_get_spacing (GTK_BOX (widget));
646 gtk_widget_style_get (widget,
647 "child-internal-pad-x", &ipad_x,
648 "child-internal-pad-y", &ipad_y,
650 gtk_button_box_child_requisition (widget,
655 n_primaries = nvis_children - n_secondaries;
658 if (orientation == GTK_ORIENTATION_HORIZONTAL)
664 list = children = _gtk_box_get_children (GTK_BOX (widget));
669 child = children->data;
670 children = children->next;
672 if (gtk_widget_get_visible (child))
674 if (gtk_button_box_get_child_secondary (bbox, child))
675 secondary_size += sizes[i];
677 primary_size += sizes[i];
681 total_size = primary_size + secondary_size;
683 gtk_widget_set_allocation (widget, allocation);
685 if (orientation == GTK_ORIENTATION_HORIZONTAL)
686 width = allocation->width - border_width*2;
688 height = allocation->height - border_width*2;
690 switch (priv->layout_style)
692 case GTK_BUTTONBOX_SPREAD:
694 if (orientation == GTK_ORIENTATION_HORIZONTAL)
696 childspacing = (width - total_size) / (nvis_children + 1);
697 x = allocation->x + border_width + childspacing;
698 secondary_x = x + primary_size + n_primaries * childspacing;
702 childspacing = (height - total_size) / (nvis_children + 1);
703 y = allocation->y + border_width + childspacing;
704 secondary_y = y + primary_size + n_primaries * childspacing;
709 case GTK_BUTTONBOX_EDGE:
711 if (orientation == GTK_ORIENTATION_HORIZONTAL)
713 if (nvis_children >= 2)
715 childspacing = (width - total_size) / (nvis_children - 1);
716 x = allocation->x + border_width;
717 secondary_x = x + primary_size + n_primaries * childspacing;
721 /* one or zero children, just center */
722 childspacing = width;
723 x = secondary_x = allocation->x
724 + (allocation->width - widths[0]) / 2;
729 if (nvis_children >= 2)
731 childspacing = (height - total_size) / (nvis_children - 1);
732 y = allocation->y + border_width;
733 secondary_y = y + primary_size + n_primaries * childspacing;
737 /* one or zero children, just center */
738 childspacing = height;
739 y = secondary_y = allocation->y
740 + (allocation->height - heights[0]) / 2;
746 case GTK_BUTTONBOX_START:
748 if (orientation == GTK_ORIENTATION_HORIZONTAL)
750 childspacing = spacing;
751 x = allocation->x + border_width;
752 secondary_x = allocation->x + allocation->width
753 - secondary_size - spacing * (n_secondaries - 1) - border_width;
757 childspacing = spacing;
758 y = allocation->y + border_width;
759 secondary_y = allocation->y + allocation->height
760 - secondary_size - spacing * (n_secondaries - 1) - border_width;
765 case GTK_BUTTONBOX_END:
767 if (orientation == GTK_ORIENTATION_HORIZONTAL)
769 childspacing = spacing;
770 x = allocation->x + allocation->width
771 - primary_size - spacing * (n_primaries - 1) - border_width;
772 secondary_x = allocation->x + border_width;
776 childspacing = spacing;
777 y = allocation->y + allocation->height
778 - primary_size - spacing * (n_primaries - 1) - border_width;
779 secondary_y = allocation->y + border_width;
784 case GTK_BUTTONBOX_CENTER:
786 if (orientation == GTK_ORIENTATION_HORIZONTAL)
788 childspacing = spacing;
791 - (primary_size + spacing * (n_primaries - 1))) / 2
792 + (secondary_size + n_secondaries * spacing) / 2;
793 secondary_x = allocation->x + border_width;
797 childspacing = spacing;
800 - (primary_size + spacing * (n_primaries - 1))) / 2
801 + (secondary_size + n_secondaries * spacing) / 2;
802 secondary_y = allocation->y + border_width;
808 g_assert_not_reached ();
818 child = children->data;
819 children = children->next;
821 if (gtk_widget_get_visible (child))
823 child_allocation.width = widths[i];
824 child_allocation.height = heights[i];
826 if (orientation == GTK_ORIENTATION_HORIZONTAL)
828 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
830 if (gtk_button_box_get_child_secondary (bbox, child))
832 child_allocation.x = secondary_x;
833 secondary_x += child_allocation.width + childspacing;
837 child_allocation.x = x;
838 x += child_allocation.width + childspacing;
841 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
842 child_allocation.x = (allocation->x + allocation->width)
843 - (child_allocation.x + child_allocation.width - allocation->x);
847 child_allocation.x = allocation->x + (allocation->width - child_allocation.width) / 2;
849 if (gtk_button_box_get_child_secondary (bbox, child))
851 child_allocation.y = secondary_y;
852 secondary_y += child_allocation.height + childspacing;
856 child_allocation.y = y;
857 y += child_allocation.height + childspacing;
861 gtk_widget_size_allocate (child, &child_allocation);
872 * gtk_button_box_new:
873 * @orientation: the box' orientation.
875 * Creates a new #GtkButtonBox.
877 * Return value: a new #GtkButtonBox.
882 gtk_button_box_new (GtkOrientation orientation)
884 return g_object_new (GTK_TYPE_BUTTON_BOX,
885 "orientation", orientation,