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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <widgets/text_ctrl_eval.h>
26 
27 TEXT_CTRL_EVAL::TEXT_CTRL_EVAL( wxWindow* aParent, wxWindowID aId, const
28  wxString& aValue, const wxPoint& aPos, const wxSize& aSize, long aStyle,
29  const wxValidator& aValidator, const wxString& aName )
30  : wxTextCtrl( aParent, aId, aValue, aPos, aSize, aStyle | wxTE_PROCESS_ENTER, aValidator, aName ),
31  m_eval( UNSCALED_UNITS )
32 {
33  Connect( wxEVT_SET_FOCUS,
34  wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusGet ), NULL, this );
35  Connect( wxEVT_KILL_FOCUS,
36  wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusLost ), NULL, this );
37  Connect( wxEVT_TEXT_ENTER,
38  wxCommandEventHandler( TEXT_CTRL_EVAL::onTextEnter ), NULL, this );
39 }
40 
41 
42 void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
43 {
44  wxTextCtrl::SetValue( aValue );
45  m_eval.Clear();
46 }
47 
48 
49 void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
50 {
51  wxString oldStr = m_eval.OriginalText();
52 
53  if( oldStr.length() )
54  SetValue( oldStr );
55 
56  aEvent.Skip();
57 }
58 
59 
60 void TEXT_CTRL_EVAL::onTextFocusLost( wxFocusEvent& aEvent )
61 {
62  evaluate();
63  aEvent.Skip();
64 }
65 
66 
67 void TEXT_CTRL_EVAL::onTextEnter( wxCommandEvent& aEvent )
68 {
69  evaluate();
70 
71  // Accept the changes and close the parent dialog
72  wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK );
73  wxPostEvent( GetParent(), event );
74 }
75 
76 
78 {
79  if( m_eval.Process( GetValue() ) )
80  SetValue( m_eval.Result() );
81 }
wxString Result() const
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
void onTextEnter(wxCommandEvent &aEvent)