Merge pull request #901 from wesnoth/shrinking-styled-widgets

styled_widget: respect minimum size even for non-text widgets
This commit is contained in:
Jyrki Vesterinen 2016-12-20 19:22:35 +02:00 committed by GitHub
commit 1155cb8f63
4 changed files with 53 additions and 0 deletions

View file

@ -28,6 +28,24 @@ namespace gui2
REGISTER_WIDGET(spacer) REGISTER_WIDGET(spacer)
void spacer::request_reduce_width(const unsigned maximum_width)
{
if(best_size_ != point()) {
// This spacer is of fixed size, do nothing.
} else {
styled_widget::request_reduce_width(maximum_width);
}
}
void spacer::request_reduce_height(const unsigned maximum_height)
{
if(best_size_ != point()) {
// This spacer is of fixed size, do nothing.
} else {
styled_widget::request_reduce_height(maximum_height);
}
}
point spacer::calculate_best_size() const point spacer::calculate_best_size() const
{ {
return best_size_ != point() ? best_size_ return best_size_ != point() ? best_size_

View file

@ -44,6 +44,12 @@ public:
/***** ***** ***** ***** layout functions ***** ***** ***** *****/ /***** ***** ***** ***** layout functions ***** ***** ***** *****/
/** See @ref widget::request_reduce_width. */
virtual void request_reduce_width(const unsigned maximum_width) override;
/** See @ref widget::request_reduce_height. */
virtual void request_reduce_height(const unsigned maximum_height) override;
private: private:
/** See @ref widget::calculate_best_size. */ /** See @ref widget::calculate_best_size. */
virtual point calculate_best_size() const override; virtual point calculate_best_size() const override;

View file

@ -31,6 +31,7 @@
#include "utils/functional.hpp" #include "utils/functional.hpp"
#include <algorithm>
#include <iomanip> #include <iomanip>
#define LOG_SCOPE_HEADER \ #define LOG_SCOPE_HEADER \
@ -218,12 +219,37 @@ void styled_widget::request_reduce_width(const unsigned maximum_width)
<< "' maximum_width " << maximum_width << " result " << size << "' maximum_width " << maximum_width << " result " << size
<< ".\n"; << ".\n";
} else if(label_.empty()) {
point size = get_best_size();
point min_size = get_config_minimum_size();
size.x = std::min(size.x, std::max<int>(maximum_width, min_size.x));
set_layout_size(size);
DBG_GUI_L << LOG_HEADER << " styled_widget " << id()
<< " maximum_width " << maximum_width << " result " << size
<< ".\n";
} else { } else {
DBG_GUI_L << LOG_HEADER << " label '" << debug_truncate(label_) DBG_GUI_L << LOG_HEADER << " label '" << debug_truncate(label_)
<< "' failed; either no label or wrapping not allowed.\n"; << "' failed; either no label or wrapping not allowed.\n";
} }
} }
void styled_widget::request_reduce_height(const unsigned maximum_height)
{
if(!label_.empty()) {
// Do nothing
} else {
point size = get_best_size();
point min_size = get_config_minimum_size();
size.y = std::min(size.y, std::max<int>(maximum_height, min_size.y));
set_layout_size(size);
DBG_GUI_L << LOG_HEADER << " styled_widget " << id()
<< " maximum_height " << maximum_height << " result " << size
<< ".\n";
}
}
point styled_widget::calculate_best_size() const point styled_widget::calculate_best_size() const
{ {
assert(config_); assert(config_);

View file

@ -176,6 +176,9 @@ public:
/** See @ref widget::request_reduce_width. */ /** See @ref widget::request_reduce_width. */
virtual void request_reduce_width(const unsigned maximum_width) override; virtual void request_reduce_width(const unsigned maximum_width) override;
/** See @ref widget::request_reduce_height. */
virtual void request_reduce_height(const unsigned maximum_height) override;
protected: protected:
/** See @ref widget::calculate_best_size. */ /** See @ref widget::calculate_best_size. */
virtual point calculate_best_size() const override; virtual point calculate_best_size() const override;