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{
31 Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusGet ), nullptr, this );
32 Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusLost ), nullptr, this );
33 Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( TEXT_CTRL_EVAL::onTextEnter ), nullptr, this );
34}
35
36
37void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
38{
39 wxTextCtrl::SetValue( aValue );
40 m_eval.Clear();
41}
42
43
44void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
45{
46 wxString oldStr = m_eval.OriginalText();
47
48 if( oldStr.length() )
49 SetValue( oldStr );
50
51 aEvent.Skip();
52}
53
54
55void TEXT_CTRL_EVAL::onTextFocusLost( wxFocusEvent& aEvent )
56{
57 if( m_customEval.has_value() )
58 m_customEval.value()( this );
59 else
60 evaluate();
61
62 aEvent.Skip();
63}
64
65
66void TEXT_CTRL_EVAL::onTextEnter( wxCommandEvent& aEvent )
67{
68 evaluate();
69
70 // Accept the changes and close the parent dialog
71 wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK );
72 wxPostEvent( GetParent(), event );
73}
74
75
77{
78 if( m_eval.Process( GetValue() ) )
79 SetValue( m_eval.Result() );
80}
void onTextFocusLost(wxFocusEvent &aEvent)
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