00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/widgets/textbox.h"
00023
00024 #include "gui/palette.h"
00025
00026 #include <guichan/font.hpp>
00027
00028 #include <sstream>
00029
00030 TextBox::TextBox() :
00031 gcn::TextBox(), mTextColor(&guiPalette->getColor(Palette::TEXT))
00032 {
00033 setOpaque(false);
00034 setFrameSize(0);
00035 mMinWidth = getWidth();
00036 }
00037
00038 void TextBox::setTextWrapped(const std::string &text, int minDimension)
00039 {
00040
00041 if (getParent())
00042 getParent()->logic();
00043
00044
00045 mMinWidth = minDimension;
00046
00047 std::stringstream wrappedStream;
00048 std::string::size_type spacePos, newlinePos, lastNewlinePos = 0;
00049 int minWidth = 0;
00050 int xpos;
00051
00052 spacePos = text.rfind(" ", text.size());
00053
00054 if (spacePos != std::string::npos)
00055 {
00056 const std::string word = text.substr(spacePos + 1);
00057 const int length = getFont()->getWidth(word);
00058
00059 if (length > mMinWidth)
00060 mMinWidth = length;
00061 }
00062
00063 do
00064 {
00065
00066 newlinePos = text.find("\n", lastNewlinePos);
00067
00068 if (newlinePos == std::string::npos)
00069 newlinePos = text.size();
00070
00071 std::string line =
00072 text.substr(lastNewlinePos, newlinePos - lastNewlinePos);
00073 std::string::size_type lastSpacePos = 0;
00074 xpos = 0;
00075
00076 do
00077 {
00078 spacePos = line.find(" ", lastSpacePos);
00079
00080 if (spacePos == std::string::npos)
00081 spacePos = line.size();
00082
00083 std::string word =
00084 line.substr(lastSpacePos, spacePos - lastSpacePos);
00085
00086 int width = getFont()->getWidth(word);
00087
00088 if (xpos == 0 && width > mMinWidth)
00089 {
00090 mMinWidth = width;
00091 xpos = width;
00092 wrappedStream << word;
00093 }
00094 else if (xpos != 0 && xpos + getFont()->getWidth(" ") + width <=
00095 mMinWidth)
00096 {
00097 xpos += getFont()->getWidth(" ") + width;
00098 wrappedStream << " " << word;
00099 }
00100 else if (lastSpacePos == 0)
00101 {
00102 xpos += width;
00103 wrappedStream << word;
00104 }
00105 else
00106 {
00107 if (xpos > minWidth)
00108 minWidth = xpos;
00109
00110
00111 if (minWidth > mMinWidth)
00112 {
00113 mMinWidth = minWidth;
00114 wrappedStream.clear();
00115 wrappedStream.str("");
00116 spacePos = 0;
00117 lastNewlinePos = 0;
00118 newlinePos = text.find("\n", lastNewlinePos);
00119 if (newlinePos == std::string::npos)
00120 newlinePos = text.size();
00121 line = text.substr(lastNewlinePos, newlinePos -
00122 lastNewlinePos);
00123 width = 0;
00124 break;
00125 }
00126 else
00127 {
00128 wrappedStream << "\n" << word;
00129 }
00130 xpos = width;
00131 }
00132 lastSpacePos = spacePos + 1;
00133 }
00134 while (spacePos != line.size());
00135
00136 if (text.find("\n", lastNewlinePos) != std::string::npos)
00137 wrappedStream << "\n";
00138
00139 lastNewlinePos = newlinePos + 1;
00140 }
00141 while (newlinePos != text.size());
00142
00143 if (xpos > minWidth)
00144 minWidth = xpos;
00145
00146 mMinWidth = minWidth;
00147
00148 gcn::TextBox::setText(wrappedStream.str());
00149 }