00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include "guichan/keyinput.hpp"
00060 #include "guichan/mouseinput.hpp"
00061 #include "guichan/widgets/textfield.hpp"
00062
00063 namespace gcn
00064 {
00065 TextField::TextField()
00066 {
00067 mCaretPosition = 0;
00068 mXScroll = 0;
00069
00070 setFocusable(true);
00071
00072 addMouseListener(this);
00073 addKeyListener(this);
00074 adjustHeight();
00075 setBorderSize(1);
00076 }
00077
00078 TextField::TextField(const std::string& text)
00079 {
00080 mCaretPosition = 0;
00081 mXScroll = 0;
00082
00083 mText = text;
00084 adjustSize();
00085 setBorderSize(1);
00086
00087 setFocusable(true);
00088
00089 addMouseListener(this);
00090 addKeyListener(this);
00091 }
00092
00093 void TextField::setText(const std::string& text)
00094 {
00095 if(text.size() < mCaretPosition )
00096 {
00097 mCaretPosition = text.size();
00098 }
00099
00100 mText = text;
00101 }
00102
00103 void TextField::draw(Graphics* graphics)
00104 {
00105 Color faceColor = getBackgroundColor();
00106 graphics->setColor(faceColor);
00107 graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
00108
00109 if (hasFocus())
00110 {
00111 drawCaret(graphics, getFont()->getWidth(mText.substr(0, mCaretPosition)) - mXScroll);
00112 }
00113
00114 graphics->setColor(getForegroundColor());
00115 graphics->setFont(getFont());
00116 graphics->drawText(mText, 1 - mXScroll, 1);
00117 }
00118
00119 void TextField::drawBorder(Graphics* graphics)
00120 {
00121 Color faceColor = getBaseColor();
00122 Color highlightColor, shadowColor;
00123 int alpha = getBaseColor().a;
00124 int width = getWidth() + getBorderSize() * 2 - 1;
00125 int height = getHeight() + getBorderSize() * 2 - 1;
00126 highlightColor = faceColor + 0x303030;
00127 highlightColor.a = alpha;
00128 shadowColor = faceColor - 0x303030;
00129 shadowColor.a = alpha;
00130
00131 unsigned int i;
00132 for (i = 0; i < getBorderSize(); ++i)
00133 {
00134 graphics->setColor(shadowColor);
00135 graphics->drawLine(i,i, width - i, i);
00136 graphics->drawLine(i,i + 1, i, height - i - 1);
00137 graphics->setColor(highlightColor);
00138 graphics->drawLine(width - i,i + 1, width - i, height - i);
00139 graphics->drawLine(i,height - i, width - i - 1, height - i);
00140 }
00141 }
00142
00143 void TextField::drawCaret(Graphics* graphics, int x)
00144 {
00145 graphics->setColor(getForegroundColor());
00146 graphics->drawLine(x, getHeight() - 2, x, 1);
00147 }
00148
00149 void TextField::mousePress(int x, int y, int button)
00150 {
00151 if (hasMouse() && button == MouseInput::LEFT)
00152 {
00153 mCaretPosition = getFont()->getStringIndexAt(mText, x + mXScroll);
00154 fixScroll();
00155 }
00156 }
00157
00158 void TextField::keyPress(const Key& key)
00159 {
00160 if (key.getValue() == Key::LEFT && mCaretPosition > 0)
00161 {
00162 --mCaretPosition;
00163 }
00164
00165 else if (key.getValue() == Key::RIGHT && mCaretPosition < mText.size())
00166 {
00167 ++mCaretPosition;
00168 }
00169
00170 else if (key.getValue() == Key::DELETE && mCaretPosition < mText.size())
00171 {
00172 mText.erase(mCaretPosition, 1);
00173 }
00174
00175 else if (key.getValue() == Key::BACKSPACE && mCaretPosition > 0)
00176 {
00177 mText.erase(mCaretPosition - 1, 1);
00178 --mCaretPosition;
00179 }
00180
00181 else if (key.getValue() == Key::ENTER)
00182 {
00183 generateAction();
00184 }
00185
00186 else if (key.getValue() == Key::HOME)
00187 {
00188 mCaretPosition = 0;
00189 }
00190
00191 else if (key.getValue() == Key::END)
00192 {
00193 mCaretPosition = mText.size();
00194 }
00195
00196 else if (key.isCharacter())
00197 {
00198 mText.insert(mCaretPosition, std::string(1,(char)key.getValue()));
00199 ++mCaretPosition;
00200 }
00201
00202 fixScroll();
00203 }
00204
00205 void TextField::adjustSize()
00206 {
00207 setWidth(getFont()->getWidth(mText) + 4);
00208 adjustHeight();
00209
00210 fixScroll();
00211 }
00212
00213 void TextField::adjustHeight()
00214 {
00215 setHeight(getFont()->getHeight() + 2);
00216 }
00217
00218 void TextField::fixScroll()
00219 {
00220 if (hasFocus())
00221 {
00222 int caretX = getFont()->getWidth(mText.substr(0, mCaretPosition));
00223
00224 if (caretX - mXScroll > getWidth() - 4)
00225 {
00226 mXScroll = caretX - getWidth() + 4;
00227 }
00228 else if (caretX - mXScroll < getFont()->getWidth(" "))
00229 {
00230 mXScroll = caretX - getFont()->getWidth(" ");
00231
00232 if (mXScroll < 0)
00233 {
00234 mXScroll = 0;
00235 }
00236 }
00237 }
00238 }
00239
00240 void TextField::setCaretPosition(unsigned int position)
00241 {
00242 if (position > mText.size())
00243 {
00244 mCaretPosition = mText.size();
00245 }
00246 else
00247 {
00248 mCaretPosition = position;
00249 }
00250
00251 fixScroll();
00252 }
00253
00254 unsigned int TextField::getCaretPosition() const
00255 {
00256 return mCaretPosition;
00257 }
00258
00259 const std::string& TextField::getText() const
00260 {
00261 return mText;
00262 }
00263
00264 void TextField::fontChanged()
00265 {
00266 fixScroll();
00267 }
00268 }