diff options
Diffstat (limited to 'src/gui/textbox.cpp')
-rw-r--r-- | src/gui/textbox.cpp | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/src/gui/textbox.cpp b/src/gui/textbox.cpp index 75f0b5a1..2a86d549 100644 --- a/src/gui/textbox.cpp +++ b/src/gui/textbox.cpp @@ -19,21 +19,21 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "textbox.h" - #include <sstream> -#include <guichan/basiccontainer.hpp> #include <guichan/font.hpp> +#include "textbox.h" + TextBox::TextBox(): gcn::TextBox() { setOpaque(false); setFrameSize(0); + mMinWidth = getWidth(); } -void TextBox::setTextWrapped(const std::string &text) +void TextBox::setTextWrapped(const std::string &text, int minDimension) { // Make sure parent scroll area sets width of this widget if (getParent()) @@ -41,8 +41,13 @@ void TextBox::setTextWrapped(const std::string &text) getParent()->logic(); } + // Take the supplied minimum dimension as a starting point and try to beat it + mMinWidth = minDimension; + std::stringstream wrappedStream; std::string::size_type newlinePos, lastNewlinePos = 0; + int minWidth = 0; + int xpos; do { @@ -57,7 +62,18 @@ void TextBox::setTextWrapped(const std::string &text) std::string line = text.substr(lastNewlinePos, newlinePos - lastNewlinePos); std::string::size_type spacePos, lastSpacePos = 0; - int xpos = 0; + xpos = 0; + + spacePos = text.rfind(" ", text.size()); + + if (spacePos != std::string::npos) + { + const std::string word = text.substr(spacePos + 1); + const int length = getFont()->getWidth(word); + + if (length > mMinWidth) + mMinWidth = length; + } do { @@ -73,7 +89,7 @@ void TextBox::setTextWrapped(const std::string &text) int width = getFont()->getWidth(word); - if (xpos != 0 && xpos + width < getWidth()) + if (xpos != 0 && xpos + width + getFont()->getWidth(" ") <= mMinWidth) { xpos += width + getFont()->getWidth(" "); wrappedStream << " " << word; @@ -85,22 +101,46 @@ void TextBox::setTextWrapped(const std::string &text) } else { + if (xpos > minWidth) + minWidth = xpos; + + // The window wasn't big enough. Resize it and try again. + if (minWidth > mMinWidth) + { + mMinWidth = minWidth; + wrappedStream.clear(); + wrappedStream.str(""); + spacePos = 0; + lastNewlinePos = 0; + newlinePos = text.find("\n", lastNewlinePos); + if (newlinePos == std::string::npos) + newlinePos = text.size(); + line = text.substr(lastNewlinePos, newlinePos - + lastNewlinePos); + width = 0; + break; + } + else + { + wrappedStream << "\n" << word; + } xpos = width; - wrappedStream << "\n" << word; } - lastSpacePos = spacePos + 1; } while (spacePos != line.size()); if (text.find("\n", lastNewlinePos) != std::string::npos) - { wrappedStream << "\n"; - } lastNewlinePos = newlinePos + 1; } while (newlinePos != text.size()); + if (xpos > minWidth) + minWidth = xpos; + + mMinWidth = minWidth; + gcn::TextBox::setText(wrappedStream.str()); } |