diff --git a/include/osgUI/LineEdit b/include/osgUI/LineEdit index 616803ad2..3f98f334c 100644 --- a/include/osgUI/LineEdit +++ b/include/osgUI/LineEdit @@ -36,6 +36,15 @@ public: virtual void leaveImplementation(); + virtual bool validate(const std::string& text); + virtual bool validateImplementation(const std::string& text); + + virtual void textChanged(const std::string& text); + virtual void textChangedImplementation(const std::string& text); + + virtual void returnPressed() { if (!runCallbacks("returnPressed")) returnPressedImplementation(); } + virtual void returnPressedImplementation(); + protected: virtual ~LineEdit() {} diff --git a/src/osgUI/LineEdit.cpp b/src/osgUI/LineEdit.cpp index 96000c749..5b857451b 100644 --- a/src/osgUI/LineEdit.cpp +++ b/src/osgUI/LineEdit.cpp @@ -17,6 +17,7 @@ #include #include #include +#include using namespace osgUI; @@ -45,16 +46,19 @@ bool LineEdit::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event { if (!_text.empty()) { - _text.erase(_text.size()-1, 1); - if (_textDrawable) _textDrawable->setText(_text); + setText(_text.substr(0, _text.size()-1)); return true; } } else if (ea->getKey()>=32 && ea->getKey()<=0xff00) { - _text.push_back(ea->getKey()); - if (_textDrawable) _textDrawable->setText(_text); + setText(_text + std::string::value_type(ea->getKey())); + return true; + } + else if (ea->getKey()==osgGA::GUIEventAdapter::KEY_Return ) + { + returnPressed(); return true; } @@ -70,7 +74,13 @@ bool LineEdit::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event void LineEdit::setText(const std::string& text) { + if (!validate(text)) return; + if (_text==text) return; + _text = text; + + textChanged(_text); + if (_textDrawable) _textDrawable->setText(_text); } @@ -87,6 +97,61 @@ void LineEdit::leaveImplementation() if (_backgroundSwitch.valid()) _backgroundSwitch->setSingleChildOn(0); } +bool LineEdit::validate(const std::string& text) +{ + osg::CallbackObject* co = getCallbackObject(this, "validate"); + if (co) + { + osg::Parameters inputParameters, outputParameters; + inputParameters.push_back(new osg::StringValueObject("text",text)); + if (co->run(this, inputParameters, outputParameters)) + { + if (outputParameters.size()>=1) + { + osg::Object* object = outputParameters[0].get(); + osg::BoolValueObject* bvo = dynamic_cast(object); + if (bvo) + { + OSG_NOTICE<<"Have bool return value from validate "<getValue()<getValue(); + } + OSG_NOTICE<<"Called validate CallbackObject but didn't get bool return value."<className()<run(this, inputParameters, outputParameters)) + { + return; + } + } + textChangedImplementation(text); +} + +void LineEdit::textChangedImplementation(const std::string& text) +{ + OSG_NOTICE<<"textChangedImplementation("<