diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-06-05 21:29:13 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-06-05 21:29:13 +0000 |
commit | cb5a80c169e10adf6d6b9392bba7c9946edf087d (patch) | |
tree | 321f9be819b01406dbbd6cf31cb7ddbb2244b3c1 /src/gui/textbox.cpp | |
parent | 7c7c50bf115299cd3a160b0eb9abfa8d7fdb2fd5 (diff) | |
download | mana-cb5a80c169e10adf6d6b9392bba7c9946edf087d.tar.gz mana-cb5a80c169e10adf6d6b9392bba7c9946edf087d.tar.bz2 mana-cb5a80c169e10adf6d6b9392bba7c9946edf087d.tar.xz mana-cb5a80c169e10adf6d6b9392bba7c9946edf087d.zip |
Implemented wrapping for textbox (still could use a small fix) and made death
notice dialog release modal focus immediately.
Diffstat (limited to 'src/gui/textbox.cpp')
-rw-r--r-- | src/gui/textbox.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
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 <sstream> 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()); +} |