KiCad PCB EDA Suite
incremental_text_ctrl.h
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) 2013 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 2
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-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 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 #ifndef INCREMENTAL_TEXT_CTRL__H_
26 #define INCREMENTAL_TEXT_CTRL__H_
27 
28 #include <wx/wx.h>
29 #include <wx/spinbutt.h>
30 
31 #include <functional>
32 
39 {
40 public:
41 
51  using STEP_FUNCTION = std::function<double(bool aUp, double aCurrVal)>;
52 
54 
56 
63  void SetValue( double aValue );
64 
68  double GetValue();
69 
81  void SetStep( double aMin, double aMax, STEP_FUNCTION aNewFunc );
82 
92  void SetStep( double aMin, double aMax, double aStep )
93  {
94  SetStep( aMin, aMax,
95  [aStep] ( bool aUp, double aCurrent ) { return aUp ? aStep : -aStep; } );
96  }
97 
101  void SetPrecision( int aPrecision );
102 
103 protected:
104 
108  void incrementCtrlBy( double aInc);
109 
113  void incrementCtrl( bool aUp );
114 
119  void updateTextValue();
120 
121  /*
122  * Implementation-specific interfaces
123  */
124 
128  virtual void setTextCtrl( const wxString& aVal ) = 0;
129 
133  virtual wxString getCtrlText() const = 0;
134 
135 private:
136 
137  double m_minVal;
138  double m_maxVal;
139 
142 
145 
148 };
149 
150 
158 {
159 public:
160 
167  SPIN_INCREMENTAL_TEXT_CTRL( wxSpinButton& aSpinBtn,
168  wxTextCtrl& aTextCtrl );
169 
171 
172 protected:
173 
175  void setTextCtrl( const wxString& val ) override;
176 
178  wxString getCtrlText() const override;
179 
180 private:
181 
182  void onFocusLoss( wxFocusEvent& aEvent );
183 
184  wxSpinButton& m_spinBtn;
185  wxTextCtrl& m_textCtrl;
186 };
187 
188 #endif /* INCREMENTAL_TEXT_CTRL__H_ */
std::function< double(bool aUp, double aCurrVal)> STEP_FUNCTION
A callable object type that can be used to provide a step value.
double m_maxVal
Current value of the control.
void SetValue(double aValue)
Set the value of the text control, but obey the limits currently set.
double m_currentValue
Precision to display.
virtual void setTextCtrl(const wxString &aVal)=0
Set the text control string value after an increment.
Class that governs a textual control holding a number that can be incremented/decremented according t...
void SetPrecision(int aPrecision)
Set the number of decimal places to display.
void SetStep(double aMin, double aMax, STEP_FUNCTION aNewFunc)
Function SetStep()
void SetStep(double aMin, double aMax, double aStep)
Function SetStep()
void updateTextValue()
Update the text control value with the current value, clamping to limits as needed.
void incrementCtrl(bool aUp)
Single increment up or down by one step.
int m_precision
The function used to determine the step.
Class SPIN_INCREMENTING_TEXT_CTRL.
double GetValue()
Get the current value of the control.
virtual wxString getCtrlText() const =0
void incrementCtrlBy(double aInc)
Increment the control by the given amount.