From c47d42d9459445d9e9869c49ebe66a0671a3a0a7 Mon Sep 17 00:00:00 2001 From: Kadah_Coba Date: Wed, 29 Jun 2011 23:40:20 -0700 Subject: STORM-1315 Ability to do simple math in numeric edit fields --- indra/llui/lllineeditor.cpp | 33 ++++++++++++++++++++++++++++++++- indra/llui/lllineeditor.h | 4 ++++ indra/llui/llspinctrl.cpp | 31 +++++++++++++++++++------------ 3 files changed, 55 insertions(+), 13 deletions(-) (limited to 'indra/llui') diff --git a/indra/llui/lllineeditor.cpp b/indra/llui/lllineeditor.cpp index 0196080d90..64969856da 100644 --- a/indra/llui/lllineeditor.cpp +++ b/indra/llui/lllineeditor.cpp @@ -37,6 +37,7 @@ #include "llgl.h" #include "lltimer.h" +#include "llcalc.h" //#include "llclipboard.h" #include "llcontrol.h" #include "llbutton.h" @@ -132,6 +133,7 @@ LLLineEditor::LLLineEditor(const LLLineEditor::Params& p) mIgnoreTab( p.ignore_tab ), mDrawAsterixes( p.is_password ), mSelectAllonFocusReceived( p.select_on_focus ), + mSelectAllonCommit( TRUE ), mPassDelete(FALSE), mReadOnly(FALSE), mBgImage( p.background_image ), @@ -228,7 +230,10 @@ void LLLineEditor::onCommit() setControlValue(getValue()); LLUICtrl::onCommit(); - selectAll(); + + // Selection on commit needs to be turned off when evaluating maths + // expressions, to allow indication of the error position + if (mSelectAllonCommit) selectAll(); } // Returns TRUE if user changed value at all @@ -1995,6 +2000,32 @@ BOOL LLLineEditor::postvalidateFloat(const std::string &str) return success; } +BOOL LLLineEditor::evaluateFloat() +{ + bool success; + F32 result = 0.f; + std::string expr = getText(); + LLStringUtil::toUpper(expr); + + success = LLCalc::getInstance()->evalString(expr, result); + + if (!success) + { + // Move the cursor to near the error on failure + setCursor(LLCalc::getInstance()->getLastErrorPos()); + // *TODO: Translated error message indicating the type of error? Select error text? + } + else + { + // Replace the expression with the result + std::string result_str = llformat("%f",result); + setText(result_str); + selectAll(); + } + + return success; +} + void LLLineEditor::onMouseCaptureLost() { endSelection(); diff --git a/indra/llui/lllineeditor.h b/indra/llui/lllineeditor.h index fe191e5971..987abc038c 100644 --- a/indra/llui/lllineeditor.h +++ b/indra/llui/lllineeditor.h @@ -220,6 +220,7 @@ public: void deleteSelection(); void setSelectAllonFocusReceived(BOOL b); + void setSelectAllonCommit(BOOL b) { mSelectAllonCommit = b; } typedef boost::function callback_t; void setKeystrokeCallback(callback_t callback, void* user_data); @@ -234,6 +235,8 @@ public: void setPrevalidate( LLTextValidate::validate_func_t func ); static BOOL postvalidateFloat(const std::string &str); + BOOL evaluateFloat(); + // line history support: void setEnableLineHistory( BOOL enabled ) { mHaveHistory = enabled; } // switches line history on or off void updateHistory(); // stores current line in history @@ -330,6 +333,7 @@ protected: BOOL mDrawAsterixes; BOOL mSelectAllonFocusReceived; + BOOL mSelectAllonCommit; BOOL mPassDelete; BOOL mReadOnly; diff --git a/indra/llui/llspinctrl.cpp b/indra/llui/llspinctrl.cpp index 15a7438ec9..b76d604953 100644 --- a/indra/llui/llspinctrl.cpp +++ b/indra/llui/llspinctrl.cpp @@ -44,7 +44,7 @@ #include "llresmgr.h" #include "lluictrlfactory.h" -const U32 MAX_STRING_LENGTH = 32; +const U32 MAX_STRING_LENGTH = 255; static LLDefaultChildRegistry::Register r2("spinner"); @@ -124,14 +124,15 @@ LLSpinCtrl::LLSpinCtrl(const LLSpinCtrl::Params& p) params.max_length.bytes(MAX_STRING_LENGTH); params.commit_callback.function((boost::bind(&LLSpinCtrl::onEditorCommit, this, _2))); - if( mPrecision>0 )//should accept float numbers - { - params.prevalidate_callback(&LLTextValidate::validateFloat); - } - else //should accept int numbers - { - params.prevalidate_callback(&LLTextValidate::validateInt); - } + //allow entering of any chars for LLCalc, proper input will be evaluated on commit + //if( mPrecision>0 )//should accept float numbers + //{ + // params.prevalidate_callback(&LLTextValidate::validateFloat); + //} + //else //should accept int numbers + //{ + // params.prevalidate_callback(&LLTextValidate::validateInt); + //} params.follows.flags(FOLLOWS_LEFT | FOLLOWS_BOTTOM); mEditor = LLUICtrlFactory::create (params); @@ -140,6 +141,7 @@ LLSpinCtrl::LLSpinCtrl(const LLSpinCtrl::Params& p) // than when it doesn't. Instead, if you always have to double click to select all the text, // it's easier to understand //mEditor->setSelectAllonFocusReceived(TRUE); + mEditor->setSelectAllonCommit(FALSE); addChild(mEditor); updateEditor(); @@ -304,9 +306,10 @@ void LLSpinCtrl::onEditorCommit( const LLSD& data ) { BOOL success = FALSE; - std::string text = mEditor->getText(); - if( LLLineEditor::postvalidateFloat( text ) ) + if( mEditor->evaluateFloat() ) { + std::string text = mEditor->getText(); + LLLocale locale(LLLocale::USER_LOCALE); F32 val = (F32) atof(text.c_str()); @@ -327,7 +330,11 @@ void LLSpinCtrl::onEditorCommit( const LLSD& data ) } updateEditor(); - if( !success ) + if( success ) + { + updateEditor(); + } + else { reportInvalidData(); } -- cgit v1.2.3 From 57c30517817a6458edab6bfdfad7d7112467ca48 Mon Sep 17 00:00:00 2001 From: Kadah_Coba Date: Mon, 4 Jul 2011 14:20:52 -0700 Subject: Changes for STORM-1315 LLCalc Removed dynamic allocation on mVariables and mConstants CS cleanup --- indra/llui/llspinctrl.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'indra/llui') diff --git a/indra/llui/llspinctrl.cpp b/indra/llui/llspinctrl.cpp index b76d604953..934879cdfd 100644 --- a/indra/llui/llspinctrl.cpp +++ b/indra/llui/llspinctrl.cpp @@ -124,15 +124,7 @@ LLSpinCtrl::LLSpinCtrl(const LLSpinCtrl::Params& p) params.max_length.bytes(MAX_STRING_LENGTH); params.commit_callback.function((boost::bind(&LLSpinCtrl::onEditorCommit, this, _2))); - //allow entering of any chars for LLCalc, proper input will be evaluated on commit - //if( mPrecision>0 )//should accept float numbers - //{ - // params.prevalidate_callback(&LLTextValidate::validateFloat); - //} - //else //should accept int numbers - //{ - // params.prevalidate_callback(&LLTextValidate::validateInt); - //} + //*NOTE: allow entering of any chars for LLCalc, proper input will be evaluated on commit params.follows.flags(FOLLOWS_LEFT | FOLLOWS_BOTTOM); mEditor = LLUICtrlFactory::create (params); -- cgit v1.2.3