4 * An OpenGL based 'interactive canvas' library.
6 * Copyright (C) 2009 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Emmanuele Bassi <ebassi@linux.intel.com>
26 * SECTION:clutter-bin-layout
27 * @short_description: A simple layout manager
29 * #ClutterBinLayout is a layout manager which implements the following
33 * <listitem><simpara>the preferred size is the maximum preferred size
34 * between all the children of the container using the
35 * layout;</simpara></listitem>
36 * <listitem><simpara>each child is allocated in "layers", on on top
37 * of the other;</simpara></listitem>
38 * <listitem><simpara>for each layer there are horizontal and vertical
39 * alignment policies.</simpara></listitem>
42 * <figure id="bin-layout">
43 * <title>Bin layout</title>
44 * <para>The image shows a #ClutterBinLayout with three layers:
45 * a background #ClutterCairoTexture, set to fill on both the X
46 * and Y axis; a #ClutterTexture, set to center on both the X and
47 * Y axis; and a #ClutterRectangle, set to %CLUTTER_BIN_ALIGNMENT_END
48 * on both the X and Y axis.</para>
49 * <graphic fileref="bin-layout.png" format="PNG"/>
52 * <example id="example-clutter-bin-layout">
53 * <title>How to pack actors inside a BinLayout</title>
55 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../tests/interactive/test-bin-layout.c">
56 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
61 * #ClutterBinLayout is available since Clutter 1.2
70 #define CLUTTER_DISABLE_DEPRECATION_WARNINGS
71 #include "deprecated/clutter-container.h"
72 #include "deprecated/clutter-bin-layout.h"
74 #include "clutter-actor-private.h"
75 #include "clutter-animatable.h"
76 #include "clutter-child-meta.h"
77 #include "clutter-debug.h"
78 #include "clutter-enum-types.h"
79 #include "clutter-layout-meta.h"
80 #include "clutter-private.h"
82 #define CLUTTER_TYPE_BIN_LAYER (clutter_bin_layer_get_type ())
83 #define CLUTTER_BIN_LAYER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_BIN_LAYER, ClutterBinLayer))
84 #define CLUTTER_IS_BIN_LAYER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_BIN_LAYER))
86 #define CLUTTER_BIN_LAYOUT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_BIN_LAYOUT, ClutterBinLayoutPrivate))
88 typedef struct _ClutterBinLayer ClutterBinLayer;
89 typedef struct _ClutterLayoutMetaClass ClutterBinLayerClass;
91 struct _ClutterBinLayoutPrivate
93 ClutterBinAlignment x_align;
94 ClutterBinAlignment y_align;
96 ClutterContainer *container;
99 struct _ClutterBinLayer
101 ClutterLayoutMeta parent_instance;
103 ClutterBinAlignment x_align;
104 ClutterBinAlignment y_align;
127 static GParamSpec *layer_props[PROP_LAYER_LAST] = { NULL, };
128 static GParamSpec *bin_props[PROP_LAST] = { NULL, };
130 GType clutter_bin_layer_get_type (void);
132 G_DEFINE_TYPE (ClutterBinLayer,
134 CLUTTER_TYPE_LAYOUT_META);
136 G_DEFINE_TYPE (ClutterBinLayout,
138 CLUTTER_TYPE_LAYOUT_MANAGER);
145 set_layer_x_align (ClutterBinLayer *self,
146 ClutterBinAlignment alignment)
148 ClutterLayoutManager *manager;
149 ClutterLayoutMeta *meta;
151 if (self->x_align == alignment)
154 self->x_align = alignment;
156 meta = CLUTTER_LAYOUT_META (self);
157 manager = clutter_layout_meta_get_manager (meta);
158 clutter_layout_manager_layout_changed (manager);
160 g_object_notify_by_pspec (G_OBJECT (self), layer_props[PROP_LAYER_X_ALIGN]);
164 set_layer_y_align (ClutterBinLayer *self,
165 ClutterBinAlignment alignment)
167 ClutterLayoutManager *manager;
168 ClutterLayoutMeta *meta;
170 if (self->y_align == alignment)
173 self->y_align = alignment;
175 meta = CLUTTER_LAYOUT_META (self);
176 manager = clutter_layout_meta_get_manager (meta);
177 clutter_layout_manager_layout_changed (manager);
179 g_object_notify_by_pspec (G_OBJECT (self), layer_props[PROP_LAYER_Y_ALIGN]);
183 clutter_bin_layer_set_property (GObject *gobject,
188 ClutterBinLayer *layer = CLUTTER_BIN_LAYER (gobject);
192 case PROP_LAYER_X_ALIGN:
193 set_layer_x_align (layer, g_value_get_enum (value));
196 case PROP_LAYER_Y_ALIGN:
197 set_layer_y_align (layer, g_value_get_enum (value));
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
207 clutter_bin_layer_get_property (GObject *gobject,
212 ClutterBinLayer *layer = CLUTTER_BIN_LAYER (gobject);
216 case PROP_LAYER_X_ALIGN:
217 g_value_set_enum (value, layer->x_align);
220 case PROP_LAYER_Y_ALIGN:
221 g_value_set_enum (value, layer->y_align);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
231 clutter_bin_layer_class_init (ClutterBinLayerClass *klass)
233 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
235 gobject_class->set_property = clutter_bin_layer_set_property;
236 gobject_class->get_property = clutter_bin_layer_get_property;
238 layer_props[PROP_LAYER_X_ALIGN] =
239 g_param_spec_enum ("x-align",
240 P_("Horizontal Alignment"),
241 P_("Horizontal alignment for the actor "
242 "inside the layout manager"),
243 CLUTTER_TYPE_BIN_ALIGNMENT,
244 CLUTTER_BIN_ALIGNMENT_CENTER,
245 CLUTTER_PARAM_READWRITE);
247 layer_props[PROP_LAYER_Y_ALIGN] =
248 g_param_spec_enum ("y-align",
249 P_("Vertical Alignment"),
250 P_("Vertical alignment for the actor "
251 "inside the layout manager"),
252 CLUTTER_TYPE_BIN_ALIGNMENT,
253 CLUTTER_BIN_ALIGNMENT_CENTER,
254 CLUTTER_PARAM_READWRITE);
256 g_object_class_install_properties (gobject_class,
262 clutter_bin_layer_init (ClutterBinLayer *layer)
264 layer->x_align = CLUTTER_BIN_ALIGNMENT_CENTER;
265 layer->y_align = CLUTTER_BIN_ALIGNMENT_CENTER;
273 set_x_align (ClutterBinLayout *self,
274 ClutterBinAlignment alignment)
276 ClutterBinLayoutPrivate *priv = self->priv;
278 if (priv->x_align != alignment)
280 ClutterLayoutManager *manager;
282 priv->x_align = alignment;
284 manager = CLUTTER_LAYOUT_MANAGER (self);
285 clutter_layout_manager_layout_changed (manager);
287 g_object_notify_by_pspec (G_OBJECT (self), bin_props[PROP_X_ALIGN]);
292 set_y_align (ClutterBinLayout *self,
293 ClutterBinAlignment alignment)
295 ClutterBinLayoutPrivate *priv = self->priv;
297 if (priv->y_align != alignment)
299 ClutterLayoutManager *manager;
301 priv->y_align = alignment;
303 manager = CLUTTER_LAYOUT_MANAGER (self);
304 clutter_layout_manager_layout_changed (manager);
306 g_object_notify_by_pspec (G_OBJECT (self), bin_props[PROP_Y_ALIGN]);
311 clutter_bin_layout_get_preferred_width (ClutterLayoutManager *manager,
312 ClutterContainer *container,
317 ClutterActor *actor = CLUTTER_ACTOR (container);
318 ClutterActorIter iter;
320 gfloat min_width, nat_width;
322 min_width = nat_width = 0.0;
324 clutter_actor_iter_init (&iter, actor);
325 while (clutter_actor_iter_next (&iter, &child))
327 gfloat minimum, natural;
329 clutter_actor_get_preferred_width (child, for_height,
333 min_width = MAX (min_width, minimum);
334 nat_width = MAX (nat_width, natural);
338 *min_width_p = min_width;
341 *nat_width_p = nat_width;
345 clutter_bin_layout_get_preferred_height (ClutterLayoutManager *manager,
346 ClutterContainer *container,
348 gfloat *min_height_p,
349 gfloat *nat_height_p)
351 ClutterActor *actor = CLUTTER_ACTOR (container);
352 ClutterActorIter iter;
354 gfloat min_height, nat_height;
356 min_height = nat_height = 0.0;
358 clutter_actor_iter_init (&iter, actor);
359 while (clutter_actor_iter_next (&iter, &child))
361 gfloat minimum, natural;
363 clutter_actor_get_preferred_height (child, for_width,
367 min_height = MAX (min_height, minimum);
368 nat_height = MAX (nat_height, natural);
372 *min_height_p = min_height;
375 *nat_height_p = nat_height;
379 get_bin_alignment_factor (ClutterBinAlignment alignment)
383 case CLUTTER_BIN_ALIGNMENT_CENTER:
386 case CLUTTER_BIN_ALIGNMENT_START:
389 case CLUTTER_BIN_ALIGNMENT_END:
392 case CLUTTER_BIN_ALIGNMENT_FIXED:
393 case CLUTTER_BIN_ALIGNMENT_FILL:
401 get_actor_align_factor (ClutterActorAlign alignment)
405 case CLUTTER_ACTOR_ALIGN_CENTER:
408 case CLUTTER_ACTOR_ALIGN_START:
411 case CLUTTER_ACTOR_ALIGN_END:
414 case CLUTTER_ACTOR_ALIGN_FILL:
422 clutter_bin_layout_allocate (ClutterLayoutManager *manager,
423 ClutterContainer *container,
424 const ClutterActorBox *allocation,
425 ClutterAllocationFlags flags)
427 gfloat allocation_x, allocation_y;
428 gfloat available_w, available_h;
429 ClutterActor *actor, *child;
430 ClutterActorIter iter;
432 clutter_actor_box_get_origin (allocation, &allocation_x, &allocation_y);
433 clutter_actor_box_get_size (allocation, &available_w, &available_h);
435 actor = CLUTTER_ACTOR (container);
437 clutter_actor_iter_init (&iter, actor);
438 while (clutter_actor_iter_next (&iter, &child))
440 ClutterLayoutMeta *meta;
441 ClutterBinLayer *layer;
442 ClutterActorBox child_alloc = { 0, };
443 gdouble x_align, y_align;
444 gboolean x_fill, y_fill;
446 meta = clutter_layout_manager_get_child_meta (manager,
449 layer = CLUTTER_BIN_LAYER (meta);
451 if (layer->x_align == CLUTTER_BIN_ALIGNMENT_FIXED)
452 child_alloc.x1 = clutter_actor_get_x (child);
454 child_alloc.x1 = allocation_x;
456 if (layer->y_align == CLUTTER_BIN_ALIGNMENT_FIXED)
457 child_alloc.y1 = clutter_actor_get_y (child);
459 child_alloc.y1 = allocation_y;
461 child_alloc.x2 = available_w;
462 child_alloc.y2 = available_h;
464 if (clutter_actor_needs_expand (child, CLUTTER_ORIENTATION_HORIZONTAL))
466 ClutterActorAlign align;
468 align = _clutter_actor_get_effective_x_align (child);
469 x_fill = align == CLUTTER_ACTOR_ALIGN_FILL;
470 x_align = get_actor_align_factor (align);
474 x_fill = (layer->x_align == CLUTTER_BIN_ALIGNMENT_FILL);
475 x_align = get_bin_alignment_factor (layer->x_align);
478 if (clutter_actor_needs_expand (child, CLUTTER_ORIENTATION_VERTICAL))
480 ClutterActorAlign align;
482 align = clutter_actor_get_y_align (child);
483 y_fill = align == CLUTTER_ACTOR_ALIGN_FILL;
484 y_align = get_actor_align_factor (align);
488 y_fill = (layer->y_align == CLUTTER_BIN_ALIGNMENT_FILL);
489 y_align = get_bin_alignment_factor (layer->y_align);
492 clutter_actor_allocate_align_fill (child, &child_alloc,
500 clutter_bin_layout_get_child_meta_type (ClutterLayoutManager *manager)
502 return CLUTTER_TYPE_BIN_LAYER;
505 static ClutterLayoutMeta *
506 clutter_bin_layout_create_child_meta (ClutterLayoutManager *manager,
507 ClutterContainer *container,
510 ClutterBinLayoutPrivate *priv;
512 priv = CLUTTER_BIN_LAYOUT (manager)->priv;
514 return g_object_new (CLUTTER_TYPE_BIN_LAYER,
515 "container", container,
518 "x-align", priv->x_align,
519 "y_align", priv->y_align,
524 clutter_bin_layout_set_container (ClutterLayoutManager *manager,
525 ClutterContainer *container)
527 ClutterBinLayoutPrivate *priv;
528 ClutterLayoutManagerClass *parent_class;
530 priv = CLUTTER_BIN_LAYOUT (manager)->priv;
531 priv->container = container;
533 parent_class = CLUTTER_LAYOUT_MANAGER_CLASS (clutter_bin_layout_parent_class);
534 parent_class->set_container (manager, container);
538 clutter_bin_layout_set_property (GObject *gobject,
543 ClutterBinLayout *layout = CLUTTER_BIN_LAYOUT (gobject);
548 set_x_align (layout, g_value_get_enum (value));
552 set_y_align (layout, g_value_get_enum (value));
556 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
562 clutter_bin_layout_get_property (GObject *gobject,
567 ClutterBinLayoutPrivate *priv;
569 priv = CLUTTER_BIN_LAYOUT (gobject)->priv;
574 g_value_set_enum (value, priv->x_align);
578 g_value_set_enum (value, priv->y_align);
582 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
588 clutter_bin_layout_class_init (ClutterBinLayoutClass *klass)
590 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
591 ClutterLayoutManagerClass *layout_class =
592 CLUTTER_LAYOUT_MANAGER_CLASS (klass);
594 g_type_class_add_private (klass, sizeof (ClutterBinLayoutPrivate));
597 * ClutterBinLayout:x-align:
599 * The default horizontal alignment policy for actors managed
600 * by the #ClutterBinLayout
604 * Deprecated: 1.12: Use the #ClutterActor:x-expand and the
605 * #ClutterActor:x-align properties on #ClutterActor instead.
607 bin_props[PROP_X_ALIGN] =
608 g_param_spec_enum ("x-align",
609 P_("Horizontal Alignment"),
610 P_("Default horizontal alignment for the actors "
611 "inside the layout manager"),
612 CLUTTER_TYPE_BIN_ALIGNMENT,
613 CLUTTER_BIN_ALIGNMENT_CENTER,
614 CLUTTER_PARAM_READWRITE);
617 * ClutterBinLayout:y-align:
619 * The default vertical alignment policy for actors managed
620 * by the #ClutterBinLayout
624 * Deprecated: 1.12: Use the #ClutterActor:y-expand and the
625 * #ClutterActor:y-align properties on #ClutterActor instead.
627 bin_props[PROP_Y_ALIGN] =
628 g_param_spec_enum ("y-align",
629 P_("Vertical Alignment"),
630 P_("Default vertical alignment for the actors "
631 "inside the layout manager"),
632 CLUTTER_TYPE_BIN_ALIGNMENT,
633 CLUTTER_BIN_ALIGNMENT_CENTER,
634 CLUTTER_PARAM_READWRITE);
636 gobject_class->set_property = clutter_bin_layout_set_property;
637 gobject_class->get_property = clutter_bin_layout_get_property;
638 g_object_class_install_properties (gobject_class, PROP_LAST, bin_props);
640 layout_class->get_preferred_width = clutter_bin_layout_get_preferred_width;
641 layout_class->get_preferred_height = clutter_bin_layout_get_preferred_height;
642 layout_class->allocate = clutter_bin_layout_allocate;
643 layout_class->create_child_meta = clutter_bin_layout_create_child_meta;
644 layout_class->get_child_meta_type = clutter_bin_layout_get_child_meta_type;
645 layout_class->set_container = clutter_bin_layout_set_container;
649 clutter_bin_layout_init (ClutterBinLayout *self)
651 self->priv = CLUTTER_BIN_LAYOUT_GET_PRIVATE (self);
653 self->priv->x_align = CLUTTER_BIN_ALIGNMENT_CENTER;
654 self->priv->y_align = CLUTTER_BIN_ALIGNMENT_CENTER;
658 * clutter_bin_layout_new:
659 * @x_align: the default alignment policy to be used on the
661 * @y_align: the default alignment policy to be used on the
664 * Creates a new #ClutterBinLayout layout manager
666 * Return value: the newly created layout manager
670 ClutterLayoutManager *
671 clutter_bin_layout_new (ClutterBinAlignment x_align,
672 ClutterBinAlignment y_align)
674 return g_object_new (CLUTTER_TYPE_BIN_LAYOUT,
681 * clutter_bin_layout_set_alignment:
682 * @self: a #ClutterBinLayout
683 * @child: (allow-none): a child of @container
684 * @x_align: the horizontal alignment policy to be used for the @child
686 * @y_align: the vertical aligment policy to be used on the @child
689 * Sets the horizontal and vertical alignment policies to be applied
690 * to a @child of @self
692 * If @child is %NULL then the @x_align and @y_align values will
693 * be set as the default alignment policies
697 * Deprecated: 1.12: Use the #ClutterActor:x-align and
698 * #ClutterActor:y-align properties of #ClutterActor instead.
701 clutter_bin_layout_set_alignment (ClutterBinLayout *self,
703 ClutterBinAlignment x_align,
704 ClutterBinAlignment y_align)
706 ClutterBinLayoutPrivate *priv;
707 ClutterLayoutManager *manager;
708 ClutterLayoutMeta *meta;
710 g_return_if_fail (CLUTTER_IS_BIN_LAYOUT (self));
711 g_return_if_fail (child == NULL || CLUTTER_IS_ACTOR (child));
715 if (priv->container == NULL)
719 set_x_align (self, x_align);
720 set_y_align (self, y_align);
723 g_warning ("The layout of type '%s' must be associated to "
724 "a ClutterContainer before setting the alignment "
726 G_OBJECT_TYPE_NAME (self));
731 manager = CLUTTER_LAYOUT_MANAGER (self);
732 meta = clutter_layout_manager_get_child_meta (manager,
735 g_assert (CLUTTER_IS_BIN_LAYER (meta));
737 set_layer_x_align (CLUTTER_BIN_LAYER (meta), x_align);
738 set_layer_y_align (CLUTTER_BIN_LAYER (meta), y_align);
742 * clutter_bin_layout_get_alignment:
743 * @self: a #ClutterBinLayout
744 * @child: (allow-none): a child of @container
745 * @x_align: (out) (allow-none): return location for the horizontal
747 * @y_align: (out) (allow-none): return location for the vertical
750 * Retrieves the horizontal and vertical alignment policies for
753 * If @child is %NULL the default alignment policies will be returned
758 * Deprecated: 1.12: Use the #ClutterActor:x-align and the
759 * #ClutterActor:y-align properties of #ClutterActor instead.
762 clutter_bin_layout_get_alignment (ClutterBinLayout *self,
764 ClutterBinAlignment *x_align,
765 ClutterBinAlignment *y_align)
767 ClutterBinLayoutPrivate *priv;
768 ClutterLayoutManager *manager;
769 ClutterLayoutMeta *meta;
770 ClutterBinLayer *layer;
772 g_return_if_fail (CLUTTER_IS_BIN_LAYOUT (self));
776 if (priv->container == NULL)
781 *x_align = priv->x_align;
784 *y_align = priv->y_align;
787 g_warning ("The layout of type '%s' must be associated to "
788 "a ClutterContainer before getting the alignment "
790 G_OBJECT_TYPE_NAME (self));
795 manager = CLUTTER_LAYOUT_MANAGER (self);
796 meta = clutter_layout_manager_get_child_meta (manager,
799 g_assert (CLUTTER_IS_BIN_LAYER (meta));
801 layer = CLUTTER_BIN_LAYER (meta);
804 *x_align = layer->x_align;
807 *y_align = layer->y_align;
811 * clutter_bin_layout_add:
812 * @self: a #ClutterBinLayout
813 * @child: a #ClutterActor
814 * @x_align: horizontal alignment policy for @child
815 * @y_align: vertical alignment policy for @child
817 * Adds a #ClutterActor to the container using @self and
818 * sets the alignment policies for it
820 * This function is equivalent to clutter_container_add_actor()
821 * and clutter_layout_manager_child_set_property() but it does not
822 * require a pointer to the #ClutterContainer associated to the
827 * Deprecated: 1.12: Use clutter_actor_add_child() instead.
830 clutter_bin_layout_add (ClutterBinLayout *self,
832 ClutterBinAlignment x_align,
833 ClutterBinAlignment y_align)
835 ClutterBinLayoutPrivate *priv;
836 ClutterLayoutManager *manager;
837 ClutterLayoutMeta *meta;
839 g_return_if_fail (CLUTTER_IS_BIN_LAYOUT (self));
840 g_return_if_fail (CLUTTER_IS_ACTOR (child));
844 if (priv->container == NULL)
846 g_warning ("The layout of type '%s' must be associated to "
847 "a ClutterContainer before adding children",
848 G_OBJECT_TYPE_NAME (self));
852 clutter_container_add_actor (priv->container, child);
854 manager = CLUTTER_LAYOUT_MANAGER (self);
855 meta = clutter_layout_manager_get_child_meta (manager,
858 g_assert (CLUTTER_IS_BIN_LAYER (meta));
860 set_layer_x_align (CLUTTER_BIN_LAYER (meta), x_align);
861 set_layer_y_align (CLUTTER_BIN_LAYER (meta), y_align);