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