KiCad PCB EDA Suite
Loading...
Searching...
No Matches
text_ctrl_eval.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017-2018 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
23
24
25TEXT_CTRL_EVAL::TEXT_CTRL_EVAL( wxWindow* aParent, wxWindowID aId, const wxString& aValue,
26 const wxPoint& aPos, const wxSize& aSize, long aStyle,
27 const wxValidator& aValidator, const wxString& aName ) :
28 wxTextCtrl( aParent, aId, aValue, aPos, aSize, aStyle | wxTE_PROCESS_ENTER, aValidator, aName ),
30 m_deferToBinder( false )
31{
32 Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusGet ), nullptr, this );
33 Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusLost ), nullptr, this );
34 Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( TEXT_CTRL_EVAL::onTextEnter ), nullptr, this );
35}
36
37
38void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
39{
40 wxTextCtrl::SetValue( aValue );
41 m_eval.Clear();
42}
43
44
46{
47 wxString formula = m_eval.OriginalText();
48
49 if( m_eval.IsValid() && !formula.IsEmpty() && GetValue() == m_eval.Result() )
50 return formula;
51
52 return GetValue();
53}
54
55
56bool TEXT_CTRL_EVAL::RestoreFormula( const wxString& aFormula )
57{
58 if( m_deferToBinder || aFormula.IsEmpty() )
59 return false;
60
61 if( !m_eval.Process( aFormula ) )
62 {
63 // A failed expression must not be handed back to the control on the next focus
64 m_eval.Clear();
65 return false;
66 }
67
68 ChangeValue( m_eval.Result() );
69 return true;
70}
71
72
73void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
74{
75 wxString oldStr = m_eval.OriginalText();
76
77 if( !m_deferToBinder && oldStr.length() && oldStr != GetValue() )
78 SetValue( oldStr );
79
80 aEvent.Skip();
81}
82
83
84void TEXT_CTRL_EVAL::onTextFocusLost( wxFocusEvent& aEvent )
85{
86 if( m_deferToBinder )
87 {
88 aEvent.Skip();
89 return;
90 }
91
92 if( m_customEval.has_value() )
93 m_customEval.value()( this );
94 else
95 evaluate();
96
97 aEvent.Skip();
98}
99
100
101void TEXT_CTRL_EVAL::onTextEnter( wxCommandEvent& aEvent )
102{
103 evaluate();
104
105 // Accept the changes and close the parent dialog
106 wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK );
107 wxPostEvent( GetParent(), event );
108}
109
110
112{
113 if( m_deferToBinder )
114 return;
115
116 // Bypass the override, which would discard the expression that was just evaluated
117 if( m_eval.Process( GetValue() ) )
118 wxTextCtrl::SetValue( m_eval.Result() );
119 else
120 m_eval.Clear();
121}
bool RestoreFormula(const wxString &aFormula)
Show the result of an expression returned by GetFormula() and retain the expression so that it reappe...
void onTextFocusLost(wxFocusEvent &aEvent)
wxString GetFormula() const
Return the expression behind the displayed value, or the displayed text if there is none.
NUMERIC_EVALUATOR m_eval
Numeric expression evaluator.
void onTextEnter(wxCommandEvent &aEvent)
std::optional< std::function< void(TEXT_CTRL_EVAL *aCtrl)> > m_customEval
void SetValue(const wxString &aValue) override
Set a new value in evaluator buffer and display it in the wxTextCtrl.
TEXT_CTRL_EVAL(wxWindow *aParent, wxWindowID aId, const wxString &aValue=wxEmptyString, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=0, const wxValidator &aValidator=wxDefaultValidator, const wxString &aName=wxTextCtrlNameStr)
void onTextFocusGet(wxFocusEvent &aEvent)
EDA_UNITS
Definition eda_units.h:44