Changeset - r28164:3b06f80fb9f5
[Not reviewed]
master
0 2 0
Peter Nelson - 13 months ago 2023-11-20 22:27:16
peter1138@openttd.org
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().
2 files changed with 21 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/widget.cpp
Show inline comments
 
@@ -1157,6 +1157,26 @@ void NWidgetResizeBase::SetResize(uint r
 
}
 

	
 
/**
 
 * 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.
 
 * @param min_x Horizontal minimal size of the widget.
src/widget_type.h
Show inline comments
 
@@ -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);
 

	
0 comments (0 inline, 0 general)