From cb5a80c169e10adf6d6b9392bba7c9946edf087d Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 5 Jun 2005 21:29:13 +0000 Subject: Implemented wrapping for textbox (still could use a small fix) and made death notice dialog release modal focus immediately. --- src/gui/textbox.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'src/gui/textbox.cpp') diff --git a/src/gui/textbox.cpp b/src/gui/textbox.cpp index a620f29f..06ee3598 100644 --- a/src/gui/textbox.cpp +++ b/src/gui/textbox.cpp @@ -22,6 +22,7 @@ */ #include "textbox.h" +#include TextBox::TextBox(): gcn::TextBox() @@ -36,3 +37,75 @@ TextBox::TextBox(const std::string& text): setOpaque(false); setBorderSize(0); } + +void TextBox::setText(const std::string &text) +{ + // Make sure parent scroll area sets width of this widget + if (getParent()) + { + getParent()->logic(); + } + + std::stringstream wrappedStream; + std::string::size_type newlinePos, lastNewlinePos = 0; + + do + { + // Determine next piece of string to wrap + newlinePos = text.find("\n", lastNewlinePos); + + if (newlinePos == std::string::npos) + { + newlinePos = text.size(); + } + + std::string line = + text.substr(lastNewlinePos, newlinePos - lastNewlinePos); + std::string::size_type spacePos, lastSpacePos = 0; + int xpos = 0; + + do + { + spacePos = line.find(" ", lastSpacePos); + + if (spacePos == std::string::npos) + { + spacePos = line.size(); + } + + std::string word = + line.substr(lastSpacePos, spacePos - lastSpacePos); + + int width = getFont()->getWidth(word); + + if (xpos != 0 && xpos + width < getWidth()) + { + xpos += width + getFont()->getWidth(" "); + wrappedStream << " " << word; + } + else if (lastSpacePos == 0) + { + xpos += width; + wrappedStream << word; + } + else + { + 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()); + + gcn::TextBox::setText(wrappedStream.str()); +} -- cgit v1.2.3-70-g09d2