From aba6c5acc7f1cb15f92666d1681a95b5a805efaf Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Tue, 2 Mar 2010 15:08:01 +0000 Subject: [PATCH] flow-layout: Provide a preferred size The FlowLayout fails to provide a preferred size in case no sizing is specified on one axis. It should, instead, have the preferred size of the sum of its children, depending on the orientation property. http://bugzilla.openedhand.com/show_bug.cgi?id=2013 --- clutter/clutter-flow-layout.c | 92 +++++++++++++++++++++++++++--------- tests/interactive/test-flow-layout.c | 20 ++++---- 2 files changed, 82 insertions(+), 30 deletions(-) diff --git a/clutter/clutter-flow-layout.c b/clutter/clutter-flow-layout.c index 0888256..937b1ac 100644 --- a/clutter/clutter-flow-layout.c +++ b/clutter/clutter-flow-layout.c @@ -217,21 +217,18 @@ clutter_flow_layout_get_preferred_width (ClutterLayoutManager *manager, item_y = 0; /* clear the line width arrays */ - if (priv->orientation == CLUTTER_FLOW_VERTICAL && for_height > 0) - { - if (priv->line_min != NULL) - g_array_free (priv->line_min, TRUE); + if (priv->line_min != NULL) + g_array_free (priv->line_min, TRUE); - if (priv->line_natural != NULL) - g_array_free (priv->line_natural, TRUE); + if (priv->line_natural != NULL) + g_array_free (priv->line_natural, TRUE); - priv->line_min = g_array_sized_new (FALSE, FALSE, + priv->line_min = g_array_sized_new (FALSE, FALSE, + sizeof (gfloat), + 16); + priv->line_natural = g_array_sized_new (FALSE, FALSE, sizeof (gfloat), 16); - priv->line_natural = g_array_sized_new (FALSE, FALSE, - sizeof (gfloat), - 16); - } if (children) line_count = 1; @@ -292,6 +289,8 @@ clutter_flow_layout_get_preferred_width (ClutterLayoutManager *manager, max_min_width = MAX (max_min_width, child_min); max_natural_width = MAX (max_natural_width, child_natural); + total_min_width += max_min_width; + total_natural_width += max_natural_width; line_count += 1; } } @@ -321,6 +320,24 @@ clutter_flow_layout_get_preferred_width (ClutterLayoutManager *manager, } priv->line_count = line_count; + + if (priv->line_count > 0) + { + gfloat total_spacing; + + total_spacing = priv->col_spacing * (priv->line_count - 1); + + total_min_width += total_spacing; + total_natural_width += total_spacing; + } + } + else + { + g_array_append_val (priv->line_min, line_min_width); + g_array_append_val (priv->line_natural, line_natural_width); + + priv->line_count = line_count; + if (priv->line_count > 0) { gfloat total_spacing; @@ -332,6 +349,13 @@ clutter_flow_layout_get_preferred_width (ClutterLayoutManager *manager, } } + CLUTTER_NOTE (LAYOUT, + "Flow[w]: %d lines (%d per line): w [ %.2f, %.2f ] for h %.2f", + n_rows, priv->line_count, + total_min_width, + total_natural_width, + for_height); + if (min_width_p) *min_width_p = total_min_width; @@ -368,21 +392,18 @@ clutter_flow_layout_get_preferred_height (ClutterLayoutManager *manager, item_x = 0; /* clear the line height arrays */ - if (priv->orientation == CLUTTER_FLOW_HORIZONTAL && for_width > 0) - { - if (priv->line_min != NULL) - g_array_free (priv->line_min, TRUE); + if (priv->line_min != NULL) + g_array_free (priv->line_min, TRUE); - if (priv->line_natural != NULL) - g_array_free (priv->line_natural, TRUE); + if (priv->line_natural != NULL) + g_array_free (priv->line_natural, TRUE); - priv->line_min = g_array_sized_new (FALSE, FALSE, + priv->line_min = g_array_sized_new (FALSE, FALSE, + sizeof (gfloat), + 16); + priv->line_natural = g_array_sized_new (FALSE, FALSE, sizeof (gfloat), 16); - priv->line_natural = g_array_sized_new (FALSE, FALSE, - sizeof (gfloat), - 16); - } if (children) line_count = 1; @@ -443,6 +464,9 @@ clutter_flow_layout_get_preferred_height (ClutterLayoutManager *manager, max_min_height = MAX (max_min_height, child_min); max_natural_height = MAX (max_natural_height, child_natural); + total_min_height += max_min_height; + total_natural_height += max_natural_height; + line_count += 1; } } @@ -482,6 +506,30 @@ clutter_flow_layout_get_preferred_height (ClutterLayoutManager *manager, total_natural_height += total_spacing; } } + else + { + g_array_append_val (priv->line_min, line_min_height); + g_array_append_val (priv->line_natural, line_natural_height); + + priv->line_count = line_count; + + if (priv->line_count > 0) + { + gfloat total_spacing; + + total_spacing = priv->col_spacing * priv->line_count; + + total_min_height += total_spacing; + total_natural_height += total_spacing; + } + } + + CLUTTER_NOTE (LAYOUT, + "Flow[h]: %d lines (%d per line): w [ %.2f, %.2f ] for h %.2f", + n_columns, priv->line_count, + total_min_height, + total_natural_height, + for_width); if (min_height_p) *min_height_p = total_min_height; diff --git a/tests/interactive/test-flow-layout.c b/tests/interactive/test-flow-layout.c index c25e86e..e691706 100644 --- a/tests/interactive/test-flow-layout.c +++ b/tests/interactive/test-flow-layout.c @@ -8,6 +8,7 @@ static gboolean is_homogeneous = FALSE; static gboolean vertical = FALSE; static gboolean random_size = FALSE; +static gboolean fit_to_stage = FALSE; static gint n_rects = N_RECTS; static gint x_spacing = 0; @@ -56,6 +57,13 @@ static GOptionEntry entries[] = { &y_spacing, "Vertical spacing between elements", "PX" }, + { + "fit-to-stage", 0, + 0, + G_OPTION_ARG_NONE, + &fit_to_stage, + "Fit to the stage size", NULL + }, { NULL } }; @@ -118,11 +126,6 @@ test_flow_layout_main (int argc, char *argv[]) clutter_container_add_actor (CLUTTER_CONTAINER (stage), box); clutter_actor_set_position (box, 0, 0); - if (vertical) - clutter_actor_set_height (box, 480); - else - clutter_actor_set_width (box, 640); - clutter_actor_set_name (box, "box"); for (i = 0; i < n_rects; i++) @@ -158,9 +161,10 @@ test_flow_layout_main (int argc, char *argv[]) g_free (name); } - g_signal_connect (stage, - "allocation-changed", G_CALLBACK (on_stage_resize), - box); + if (fit_to_stage) + g_signal_connect (stage, + "allocation-changed", G_CALLBACK (on_stage_resize), + box); clutter_actor_show_all (stage); -- 2.7.4