diff --git a/src/widget.cpp b/src/widget.cpp index d0e25cc2a6..03eb3ff636 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -1156,6 +1156,26 @@ void NWidgetResizeBase::SetResize(uint resize_x, uint resize_y) this->resize_y = resize_y; } +/** + * Try to set optimum widget size for a multiline text widget. + * The window will need to be reinited if the size is changed. + * @param str Multiline string contents that will fill the widget. + * @param max_line Maximum number of lines. + * @return true iff the widget minimum size has changed. + */ +bool NWidgetResizeBase::UpdateMultilineWidgetSize(const std::string &str, int max_lines) +{ + int y = GetStringHeight(str, this->current_x); + if (y > max_lines * FONT_HEIGHT_NORMAL) { + /* Text at the current width is too tall, so try to guess a better width. */ + Dimension d = GetStringBoundingBox(str); + d.height *= max_lines; + d.width /= 2; + return this->UpdateSize(d.width, d.height); + } + return this->UpdateVerticalSize(y); +} + /** * Set absolute (post-scaling) minimal size of the widget. * The window will need to be reinited if the size is changed. diff --git a/src/widget_type.h b/src/widget_type.h index dcf2253428..b1f8cde029 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -262,6 +262,7 @@ public: void SetFill(uint fill_x, uint fill_y); void SetResize(uint resize_x, uint resize_y); + bool UpdateMultilineWidgetSize(const std::string &str, int max_lines); bool UpdateSize(uint min_x, uint min_y); bool UpdateVerticalSize(uint min_y);