KiCad PCB EDA Suite
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 (C) 2021 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 
26 #include <widgets/text_ctrl_eval.h>
27 
28 
29 TEXT_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,
33  aName ),
34  m_eval( EDA_UNITS::UNSCALED )
35 {
36  Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusGet ), nullptr,
37  this );
38  Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusLost ), nullptr,
39  this );
40  Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( TEXT_CTRL_EVAL::onTextEnter ), nullptr,
41  this );
42 }
43 
44 
45 void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
46 {
47  wxTextCtrl::SetValue( aValue );
48  m_eval.Clear();
49 }
50 
51 
52 void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
53 {
54  wxString oldStr = m_eval.OriginalText();
55 
56  if( oldStr.length() )
57  SetValue( oldStr );
58 
59  aEvent.Skip();
60 }
61 
62 
63 void TEXT_CTRL_EVAL::onTextFocusLost( wxFocusEvent& aEvent )
64 {
65  evaluate();
66  aEvent.Skip();
67 }
68 
69 
70 void 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 }
wxString OriginalText() const
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 SetValue(const wxString &aValue) override
Set a new value in evaluator buffer, and display it in the wxTextCtrl.
void onTextFocusGet(wxFocusEvent &aEvent)
void onTextFocusLost(wxFocusEvent &aEvent)
bool Process(const wxString &aString)
NUMERIC_EVALUATOR m_eval
< Numeric expression evaluator
EDA_UNITS
Definition: eda_units.h:38
wxString Result() const
void onTextEnter(wxCommandEvent &aEvent)