Skip to content

Commit

Permalink
styled_widget: respect minimum size even for non-text widgets
Browse files Browse the repository at this point in the history
For widgets which don't contain text (label_ is empty),
request_reduce_width() and request_reduce_height() can be implemented in a
way that respects the minimum size set in widget definition. However,
neither of those functions do anything for non-text widgets.

This commit simply implements the request_reduce_* functions for non-text
widgets, and overrides the implementation for spacers because ingame WML
messages break otherwise.
  • Loading branch information
jyrkive committed Dec 13, 2016
1 parent f27dcef commit e4aa7c0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/gui/widgets/spacer.cpp
Expand Up @@ -28,6 +28,24 @@ namespace gui2

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
{
return best_size_ != point() ? best_size_
Expand Down
6 changes: 6 additions & 0 deletions src/gui/widgets/spacer.hpp
Expand Up @@ -44,6 +44,12 @@ class spacer : public styled_widget

/***** ***** ***** ***** 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:
/** See @ref widget::calculate_best_size. */
virtual point calculate_best_size() const override;
Expand Down
26 changes: 26 additions & 0 deletions src/gui/widgets/styled_widget.cpp
Expand Up @@ -31,6 +31,7 @@

#include "utils/functional.hpp"

#include <algorithm>
#include <iomanip>

#define LOG_SCOPE_HEADER \
Expand Down Expand Up @@ -218,12 +219,37 @@ void styled_widget::request_reduce_width(const unsigned maximum_width)
<< "' maximum_width " << maximum_width << " result " << size
<< ".\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 {
DBG_GUI_L << LOG_HEADER << " label '" << debug_truncate(label_)
<< "' 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
{
assert(config_);
Expand Down
3 changes: 3 additions & 0 deletions src/gui/widgets/styled_widget.hpp
Expand Up @@ -176,6 +176,9 @@ class styled_widget : public widget
/** 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;

protected:
/** See @ref widget::calculate_best_size. */
virtual point calculate_best_size() const override;
Expand Down

0 comments on commit e4aa7c0

Please sign in to comment.