Codechange: Add method to guess the width/height required for a multiline string.

This is necessary for widget layouts where a minimum width is not yet known during UpdateWidgetSize().
wip-string
Peter Nelson 7 months ago committed by Peter Nelson
parent f281525492
commit 62d4fd0572

@ -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.

@ -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);

Loading…
Cancel
Save