From 7a99b87f6f5e42072fa268876795289ab75a271e Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Wed, 28 Jan 2009 19:14:41 -0700 Subject: Finally put in the last missing wrapping case that has been missing from the textbox class. Should now always choose the most ideal text wrap possible. Signed-off-by: Ira Rice --- src/gui/textbox.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/gui') diff --git a/src/gui/textbox.cpp b/src/gui/textbox.cpp index fb5a39e6..98096112 100644 --- a/src/gui/textbox.cpp +++ b/src/gui/textbox.cpp @@ -92,9 +92,8 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension) else { if (xpos > minWidth) - { minWidth = xpos; - } + // The window wasn't big enough. Resize it and try again. if (minWidth > mMinWidth) { @@ -120,17 +119,37 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension) while (spacePos != line.size()); if (text.find("\n", lastNewlinePos) != std::string::npos) - { wrappedStream << "\n"; - } + lastNewlinePos = newlinePos + 1; + + // Is the last line we're trying to text wrap longer than the minimum + // width we're trying to beat in the first place? If so, then rewrap + // using that length. + if (newlinePos == text.size()) + { + spacePos = text.rfind(" ", text.size()); + const std::string word = line.substr(spacePos + 1); + const int length = getFont()->getWidth(word); + + if ((length > xpos || length > minWidth) && mMinWidth != length) + { + mMinWidth = length; + wrappedStream.clear(); + wrappedStream.str(""); + spacePos = 0; + lastNewlinePos = 0; + newlinePos = text.find("\n", lastNewlinePos); + line = text.substr(lastNewlinePos, newlinePos - + lastNewlinePos); + } + } } while (newlinePos != text.size()); if (xpos > minWidth) - { minWidth = xpos; - } + mMinWidth = minWidth; gcn::TextBox::setText(wrappedStream.str()); -- cgit v1.2.3-70-g09d2