diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/widgets/textfield.cpp | 86 | ||||
-rw-r--r-- | src/gui/widgets/textfield.h | 12 | ||||
-rw-r--r-- | src/guichan/widgets/textfield.cpp | 29 |
3 files changed, 86 insertions, 41 deletions
diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index afe6555a4..4e5e7e027 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -42,6 +42,7 @@ int TextField::instances = 0; float TextField::mAlpha = 1.0; ImageRect TextField::skin; +Skin *TextField::mSkin; TextField::TextField(const Widget2 *const widget, const std::string &text, const bool loseFocusOnTab, @@ -55,6 +56,7 @@ TextField::TextField(const Widget2 *const widget, mMaximum(0), mLoseFocusOnTab(loseFocusOnTab), mLastEventPaste(false), + mPadding(1), mCaretColor(&getThemeColor(Theme::CARET)) { setFrameSize(2); @@ -64,13 +66,20 @@ TextField::TextField(const Widget2 *const widget, { if (Theme::instance()) { - Theme::instance()->loadRect(skin, "textfield.xml", + mSkin = Theme::instance()->loadSkinRect(skin, "textfield.xml", "textfield_background.xml"); } } instances++; + if (mSkin) + { + mPadding = mSkin->getPadding(); + mFrameSize = mSkin->getOption("frameSize", 2); + } + + adjustSize(); if (!eventId.empty()) setActionEventId(eventId); @@ -81,8 +90,15 @@ TextField::TextField(const Widget2 *const widget, TextField::~TextField() { instances--; - if (instances == 0 && Theme::instance()) - Theme::instance()->unloadRect(skin); + if (instances == 0) + { + Theme *const theme = Theme::instance(); + if (theme) + { + theme->unload(mSkin); + theme->unloadRect(skin); + } + } } void TextField::updateAlpha() @@ -114,19 +130,14 @@ void TextField::draw(gcn::Graphics *graphics) graphics->setColor(mForegroundColor); graphics->setFont(getFont()); - graphics->drawText(mText, 1 - mXScroll, 1); + graphics->drawText(mText, mPadding - mXScroll, mPadding); } void TextField::drawFrame(gcn::Graphics *graphics) { - //updateAlpha(); -> Not useful... - - int w, h; const int bs = 2 * getFrameSize(); - w = getWidth() + bs; - h = getHeight() + bs; - - static_cast<Graphics*>(graphics)->drawImageRect(0, 0, w, h, skin); + static_cast<Graphics*>(graphics)->drawImageRect(0, 0, + getWidth() + bs, getHeight() + bs, skin); } void TextField::setNumeric(const bool numeric) @@ -447,5 +458,56 @@ void TextField::drawCaret(gcn::Graphics* graphics, int x) const gcn::Rectangle clipArea = graphics->getCurrentClipArea(); graphics->setColor(*mCaretColor); - graphics->drawLine(x, clipArea.height - 2, x, 1); + graphics->drawLine(x + mPadding, clipArea.height - mPadding, + x + mPadding, mPadding); +} + +void TextField::adjustSize() +{ + setWidth(getFont()->getWidth(mText) + 2 * mPadding + 1); + adjustHeight(); + + fixScroll(); +} + +void TextField::adjustHeight() +{ + setHeight(getFont()->getHeight() + 2 * mPadding); +} + +void TextField::fixScroll() +{ + if (isFocused()) + { + const int caretX = getFont()->getWidth( + mText.substr(0, mCaretPosition)); + + const int pad = 2 * mPadding; + if (caretX - mXScroll >= getWidth() - pad) + { + mXScroll = caretX - getWidth() + pad; + } + else if (caretX - mXScroll <= 0) + { + mXScroll = caretX - getWidth() / 2; + + if (mXScroll < 0) + mXScroll = 0; + } + } +} + +void TextField::setCaretPosition(unsigned int position) +{ + if (position > mText.size()) + mCaretPosition = static_cast<int>(mText.size()); + else + mCaretPosition = position; + + fixScroll(); +} + +void TextField::fontChanged() +{ + fixScroll(); } diff --git a/src/gui/widgets/textfield.h b/src/gui/widgets/textfield.h index f5f6a3eb9..dc35cfe70 100644 --- a/src/gui/widgets/textfield.h +++ b/src/gui/widgets/textfield.h @@ -109,9 +109,19 @@ class TextField : public gcn::TextField, void setSendAlwaysEvents(bool b) { mSendAlwaysEvents = b; } + void adjustSize(); + + void adjustHeight(); + + void setCaretPosition(unsigned int position); + protected: void drawCaret(gcn::Graphics* graphics, int x) override; + void fixScroll(); + + void fontChanged(); + bool mSendAlwaysEvents; private: @@ -122,11 +132,13 @@ class TextField : public gcn::TextField, static int instances; static float mAlpha; static ImageRect skin; + static Skin *mSkin; bool mNumeric; int mMinimum; unsigned mMaximum; bool mLoseFocusOnTab; int mLastEventPaste; + int mPadding; const gcn::Color *mCaretColor; }; diff --git a/src/guichan/widgets/textfield.cpp b/src/guichan/widgets/textfield.cpp index 64d647631..c0575eace 100644 --- a/src/guichan/widgets/textfield.cpp +++ b/src/guichan/widgets/textfield.cpp @@ -115,46 +115,18 @@ namespace gcn void TextField::adjustSize() { - setWidth(getFont()->getWidth(mText) + 7); - adjustHeight(); - - fixScroll(); } void TextField::adjustHeight() { - setHeight(getFont()->getHeight() + 4); } void TextField::fixScroll() { - if (isFocused()) - { - const int caretX = getFont()->getWidth( - mText.substr(0, mCaretPosition)); - - if (caretX - mXScroll >= getWidth() - 4) - { - mXScroll = caretX - getWidth() + 4; - } - else if (caretX - mXScroll <= 0) - { - mXScroll = caretX - getWidth() / 2; - - if (mXScroll < 0) - mXScroll = 0; - } - } } void TextField::setCaretPosition(unsigned int position) { - if (position > mText.size()) - mCaretPosition = static_cast<int>(mText.size()); - else - mCaretPosition = position; - - fixScroll(); } unsigned int TextField::getCaretPosition() const @@ -169,6 +141,5 @@ namespace gcn void TextField::fontChanged() { - fixScroll(); } } |