Fix SetValue() method in TEXT_CTRL_EVAL

Normal SetValue() call would temporarily change the displayed
value, but as soon as the text widget receives focus again, the original
expression (not evaluated) is restored.

To avoid this, the original expression is cleared in the associated
NumericEvaluator object.
This commit is contained in:
jean-pierre charras 2018-02-27 11:41:11 +01:00
parent 5f2b8e0409
commit 5f578c884c
2 changed files with 18 additions and 3 deletions

View File

@ -38,12 +38,19 @@ TEXT_CTRL_EVAL::TEXT_CTRL_EVAL( wxWindow* aParent, wxWindowID aId, const
} }
void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
{
wxTextCtrl::SetValue( aValue );
m_eval.clear( this );
}
void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent ) void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
{ {
auto oldStr = m_eval.textInput( this ); auto oldStr = m_eval.textInput( this );
if( oldStr ) if( oldStr )
SetValue( wxString::FromUTF8( oldStr ) ); wxTextCtrl::SetValue( wxString::FromUTF8( oldStr ) );
aEvent.Skip(); aEvent.Skip();
} }
@ -69,8 +76,8 @@ void TEXT_CTRL_EVAL::onTextEnter( wxCommandEvent& aEvent )
void TEXT_CTRL_EVAL::evaluate() void TEXT_CTRL_EVAL::evaluate()
{ {
if( GetValue().IsEmpty() ) if( GetValue().IsEmpty() )
SetValue( "0" ); wxTextCtrl::SetValue( "0" );
if( m_eval.process( GetValue().mb_str(), this ) ) if( m_eval.process( GetValue().mb_str(), this ) )
SetValue( wxString::FromUTF8( m_eval.result() ) ); wxTextCtrl::SetValue( wxString::FromUTF8( m_eval.result() ) );
} }

View File

@ -47,6 +47,14 @@ public:
{ {
} }
/**
* Set a new value in evaluator buffer, and display it
* in the wxTextCtrl.
* @param aValue is the new value to store and display
* if aValue is empty, the value "0" is stored and displayed
*/
void SetValue( const wxString& aValue ) override;
protected: protected:
///> Numeric expression evaluator ///> Numeric expression evaluator
NumericEvaluator m_eval; NumericEvaluator m_eval;