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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27
28
29TEXT_CTRL_EVAL::TEXT_CTRL_EVAL( wxWindow* aParent, wxWindowID aId, const wxString& aValue,
30 const wxPoint& aPos, const wxSize& aSize, long aStyle,
31 const wxValidator& aValidator, const wxString& aName ) :
32 wxTextCtrl( aParent, aId, aValue, aPos, aSize, aStyle | wxTE_PROCESS_ENTER, aValidator, aName ),
34{
35 Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusGet ), nullptr, this );
36 Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusLost ), nullptr, this );
37 Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( TEXT_CTRL_EVAL::onTextEnter ), nullptr, this );
38}
39
40
41void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
42{
43 wxTextCtrl::SetValue( aValue );
44 m_eval.Clear();
45}
46
47
48void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
49{
50 wxString oldStr = m_eval.OriginalText();
51
52 if( oldStr.length() )
53 SetValue( oldStr );
54
55 aEvent.Skip();
56}
57
58
59void TEXT_CTRL_EVAL::onTextFocusLost( wxFocusEvent& aEvent )
60{
61 if( m_customEval.has_value() )
62 m_customEval.value()( this );
63 else
64 evaluate();
65
66 aEvent.Skip();
67}
68
69
70void TEXT_CTRL_EVAL::onTextEnter( wxCommandEvent& aEvent )
71{
72 evaluate();
73
74 // Accept the changes and close the parent dialog
75 wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK );
76 wxPostEvent( GetParent(), event );
77}
78
79
81{
82 if( m_eval.Process( GetValue() ) )
83 SetValue( m_eval.Result() );
84}
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:48