diff options
-rw-r--r-- | src/FbTk/Container.cc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/FbTk/Container.cc b/src/FbTk/Container.cc index f7d5c35..0ddc421 100644 --- a/src/FbTk/Container.cc +++ b/src/FbTk/Container.cc | |||
@@ -386,6 +386,35 @@ void Container::repositionItems() { | |||
386 | totalDemands += buttonDemands.back(); | 386 | totalDemands += buttonDemands.back(); |
387 | } | 387 | } |
388 | if (totalDemands) { | 388 | if (totalDemands) { |
389 | int overhead = totalDemands - total_width; | ||
390 | if (overhead > int(buttonDemands.size())) { | ||
391 | // try to be fair. If we're short on space and some items | ||
392 | // take > 150% of the average, we preferably shrink them, so | ||
393 | // "a" and "a very long item with useless information" won't | ||
394 | // become "a very long item with" and "" | ||
395 | overhead += buttonDemands.size(); // compensate forrounding errors | ||
396 | const int mean = totalDemands / buttonDemands.size(); | ||
397 | const int thresh = 3 * mean / 2; | ||
398 | int greed = 0; | ||
399 | for (int i = 0; i < buttonDemands.size(); ++i) { | ||
400 | if (buttonDemands.at(i) > thresh) | ||
401 | greed += buttonDemands.at(i); | ||
402 | } | ||
403 | if (greed) { | ||
404 | for (int i = 0; i < buttonDemands.size(); ++i) { | ||
405 | if (buttonDemands.at(i) > thresh) { | ||
406 | int d = buttonDemands.at(i)*overhead/greed; | ||
407 | if (buttonDemands.at(i) - d > mean) { | ||
408 | buttonDemands.at(i) -= d; | ||
409 | } else { // do not shrink below mean or a huge item number would super-punish larger ones | ||
410 | d = buttonDemands.at(i) - mean; | ||
411 | buttonDemands.at(i) = mean; | ||
412 | } | ||
413 | totalDemands -= d; | ||
414 | } | ||
415 | } | ||
416 | } | ||
417 | } | ||
389 | rounding_error = total_width; | 418 | rounding_error = total_width; |
390 | for (int i = 0; i < buttonDemands.size(); ++i) { | 419 | for (int i = 0; i < buttonDemands.size(); ++i) { |
391 | rounding_error -= buttonDemands.at(i)*total_width/totalDemands; | 420 | rounding_error -= buttonDemands.at(i)*total_width/totalDemands; |