From 45227fba547fe8d264dce91a48143b6d1d413530 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fernando=20Tarl=C3=A1=20Cardoso=20Lemos?= Date: Tue, 25 Jan 2011 21:50:23 -0200 Subject: [PATCH] A new logic to calculate the percentages. It's slower, but this way we make sure that the resulting percentages *ALWAYS* sum up to 1.0 (or as close to that as we get with double math). --- src/con.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/con.c b/src/con.c index 82574b34..6439b1ed 100644 --- a/src/con.c +++ b/src/con.c @@ -385,17 +385,38 @@ int con_num_children(Con *con) { void con_fix_percent(Con *con, int action) { Con *child; int children = con_num_children(con); - /* TODO: better document why this math works */ - double fix; - if (action == WINDOW_ADD) - fix = (1.0 - (1.0 / (children+1))); - else - fix = 1.0 / (1.0 - (1.0 / (children+1))); + // calculate how much we have distributed and how many containers + // with a percentage set we have + double total = 0.0; + int children_with_percent = 0; TAILQ_FOREACH(child, &(con->nodes_head), nodes) { - if (child->percent <= 0.0) - continue; - child->percent *= fix; + if (child->percent > 0.0) { + total += child->percent; + ++children_with_percent; + } + } + + // if there were children without a percentage set, set to a value that + // will make those children proportional to all others + if (children_with_percent != children) { + TAILQ_FOREACH(child, &(con->nodes_head), nodes) { + if (child->percent <= 0.0) { + if (children_with_percent == 0) + total += (child->percent = 1.0); + else total += (child->percent = total / children_with_percent); + } + } + } + + // if we got a zero, just distribute the space equally, otherwise + // distribute according to the proportions we got + if (total == 0.0) { + TAILQ_FOREACH(child, &(con->nodes_head), nodes) + child->percent = 1.0 / children; + } else if (total != 1.0) { + TAILQ_FOREACH(child, &(con->nodes_head), nodes) + child->percent /= total; } } -- 2.39.5