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 wxString& aValue,
28  const wxPoint& aPos, const wxSize& aSize, long aStyle, const wxValidator& aValidator,
29  const wxString& aName )
30  : wxTextCtrl( aParent, aId, aValue, aPos, aSize, aStyle | wxTE_PROCESS_ENTER, aValidator,
31  aName ),
32  m_eval( EDA_UNITS::UNSCALED )
33 {
34  Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusGet ), NULL, this );
35  Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( TEXT_CTRL_EVAL::onTextFocusLost ), NULL, this );
36  Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( TEXT_CTRL_EVAL::onTextEnter ), NULL, this );
37 }
38 
39 
40 void TEXT_CTRL_EVAL::SetValue( const wxString& aValue )
41 {
42  wxTextCtrl::SetValue( aValue );
43  m_eval.Clear();
44 }
45 
46 
47 void TEXT_CTRL_EVAL::onTextFocusGet( wxFocusEvent& aEvent )
48 {
49  wxString oldStr = m_eval.OriginalText();
50 
51  if( oldStr.length() )
52  SetValue( oldStr );
53 
54  aEvent.Skip();
55 }
56 
57 
58 void TEXT_CTRL_EVAL::onTextFocusLost( wxFocusEvent& aEvent )
59 {
60  evaluate();
61  aEvent.Skip();
62 }
63 
64 
65 void TEXT_CTRL_EVAL::onTextEnter( wxCommandEvent& aEvent )
66 {
67  evaluate();
68 
69  // Accept the changes and close the parent dialog
70  wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK );
71  wxPostEvent( GetParent(), event );
72 }
73 
74 
76 {
77  if( m_eval.Process( GetValue() ) )
78  SetValue( m_eval.Result() );
79 }
EDA_UNITS
Definition: common.h:198
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)
#define NULL
NUMERIC_EVALUATOR m_eval
Numeric expression evaluator
wxString Result() const
void onTextEnter(wxCommandEvent &aEvent)