KiCad PCB EDA Suite
SPIN_INCREMENTAL_TEXT_CTRL Class Reference

Class SPIN_INCREMENTING_TEXT_CTRL. More...

#include <incremental_text_ctrl.h>

Inheritance diagram for SPIN_INCREMENTAL_TEXT_CTRL:
INCREMENTAL_TEXT_CTRL

Public Types

using STEP_FUNCTION = std::function< double(bool aUp, double aCurrVal)>
 A callable object type that can be used to provide a step value. More...
 

Public Member Functions

 SPIN_INCREMENTAL_TEXT_CTRL (wxSpinButton &aSpinBtn, wxTextCtrl &aTextCtrl)
 Constructor. More...
 
 ~SPIN_INCREMENTAL_TEXT_CTRL ()
 
void SetValue (double aValue)
 Set the value of the text control, but obey the limits currently set. More...
 
double GetValue ()
 Get the current value of the control. More...
 
void SetStep (double aMin, double aMax, STEP_FUNCTION aNewFunc)
 Function SetStep() More...
 
void SetStep (double aMin, double aMax, double aStep)
 Function SetStep() More...
 
void SetPrecision (int aPrecision)
 Set the number of decimal places to display. More...
 

Protected Member Functions

void setTextCtrl (const wxString &val) override
 > More...
 
wxString getCtrlText () const override
 > More...
 
void incrementCtrlBy (double aInc)
 Increment the control by the given amount. More...
 
void incrementCtrl (bool aUp)
 Single increment up or down by one step. More...
 
void updateTextValue ()
 Update the text control value with the current value, clamping to limits as needed. More...
 

Private Member Functions

void onFocusLoss (wxFocusEvent &aEvent)
 

Private Attributes

wxSpinButton & m_spinBtn
 
wxTextCtrl & m_textCtrl
 

Detailed Description

Class SPIN_INCREMENTING_TEXT_CTRL.

An incrementable text control, with WX spin buttons for clickable control.

Definition at line 157 of file incremental_text_ctrl.h.

Member Typedef Documentation

using INCREMENTAL_TEXT_CTRL::STEP_FUNCTION = std::function<double(bool aUp, double aCurrVal)>
inherited

A callable object type that can be used to provide a step value.

Client can provide one of these to use for implementing non-linear stepping, or stepping based on external parameters, such as unit selection.

Parameters
aUptrue if the next step is upwards
aCurrValthe current value of the control

Definition at line 51 of file incremental_text_ctrl.h.

Constructor & Destructor Documentation

SPIN_INCREMENTAL_TEXT_CTRL::SPIN_INCREMENTAL_TEXT_CTRL ( wxSpinButton &  aSpinBtn,
wxTextCtrl &  aTextCtrl 
)

Constructor.

Parameters
aSpinBtnthe spin button to control the value
aTextCtrlthe text control that will display the value

Definition at line 127 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::incrementCtrl(), m_spinBtn, m_textCtrl, and onFocusLoss().

128  :
129  m_spinBtn( aSpinBtn ),
130  m_textCtrl( aTextCtrl )
131 {
132  (void) m_spinBtn;
133 
134  // set always enabled, otherwise it's very hard to keep in sync
135  aSpinBtn.SetRange( -INT_MAX, INT_MAX );
136 
137  auto spinUpHandler = [this] ( wxSpinEvent& event )
138  {
139  incrementCtrl( true );
140  };
141 
142  // spin up/down if a single step of the field
143  auto spinDownHandler = [this] ( wxSpinEvent& event )
144  {
145  incrementCtrl( false );
146  };
147 
148  auto mouseWheelHandler = [this] ( wxMouseEvent& aEvent )
149  {
150  incrementCtrl( aEvent.GetWheelRotation() >= 0 );
151  };
152 
153  aSpinBtn.Bind( wxEVT_SPIN_UP, spinUpHandler );
154  aSpinBtn.Bind( wxEVT_SPIN_DOWN, spinDownHandler );
155 
156  m_textCtrl.Bind( wxEVT_MOUSEWHEEL, mouseWheelHandler );
157 
158  m_textCtrl.Bind( wxEVT_KILL_FOCUS, &SPIN_INCREMENTAL_TEXT_CTRL::onFocusLoss, this );
159 }
void onFocusLoss(wxFocusEvent &aEvent)
void incrementCtrl(bool aUp)
Single increment up or down by one step.
SPIN_INCREMENTAL_TEXT_CTRL::~SPIN_INCREMENTAL_TEXT_CTRL ( )

Definition at line 161 of file incremental_text_ctrl.cpp.

References m_textCtrl, and onFocusLoss().

162 {
163  // this must be unbound, as kill focus can arrive after the
164  // text control is gone
165  m_textCtrl.Unbind( wxEVT_KILL_FOCUS, &SPIN_INCREMENTAL_TEXT_CTRL::onFocusLoss, this );
166 }
void onFocusLoss(wxFocusEvent &aEvent)

Member Function Documentation

wxString SPIN_INCREMENTAL_TEXT_CTRL::getCtrlText ( ) const
overrideprotectedvirtual

>

Returns
the current string value of the text control

Implements INCREMENTAL_TEXT_CTRL.

Definition at line 182 of file incremental_text_ctrl.cpp.

References m_textCtrl.

183 {
184  return m_textCtrl.GetValue();
185 }
double INCREMENTAL_TEXT_CTRL::GetValue ( )
inherited

Get the current value of the control.

Definition at line 116 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::incrementCtrlBy(), and INCREMENTAL_TEXT_CTRL::m_currentValue.

117 {
118  // sanitise before handing the value - if the user did something
119  // like close a window with outstanding text changes, we need
120  // to clamp the value and re-interpret the text
121  incrementCtrlBy( 0.0 );
122 
123  return m_currentValue;
124 }
double m_currentValue
Precision to display.
void incrementCtrlBy(double aInc)
Increment the control by the given amount.
void INCREMENTAL_TEXT_CTRL::incrementCtrl ( bool  aUp)
protectedinherited

Single increment up or down by one step.

Definition at line 97 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::incrementCtrlBy(), INCREMENTAL_TEXT_CTRL::m_currentValue, and INCREMENTAL_TEXT_CTRL::m_stepFunc.

Referenced by SPIN_INCREMENTAL_TEXT_CTRL().

98 {
100 }
double m_currentValue
Precision to display.
void incrementCtrlBy(double aInc)
Increment the control by the given amount.
void INCREMENTAL_TEXT_CTRL::incrementCtrlBy ( double  aInc)
protectedinherited

Increment the control by the given amount.

Definition at line 84 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::getCtrlText(), INCREMENTAL_TEXT_CTRL::m_currentValue, INCREMENTAL_TEXT_CTRL::updateTextValue(), and validateFloatField().

Referenced by INCREMENTAL_TEXT_CTRL::GetValue(), INCREMENTAL_TEXT_CTRL::incrementCtrl(), and onFocusLoss().

85 {
86  const wxString txt = getCtrlText();
87  if( !validateFloatField( txt ) )
88  return;
89 
90  txt.ToDouble( &m_currentValue );
91  m_currentValue += aInc;
92 
94 }
double m_currentValue
Precision to display.
static bool validateFloatField(const wxString &aStr)
Check that a string looks like a floating point number that can be dealt with.
void updateTextValue()
Update the text control value with the current value, clamping to limits as needed.
virtual wxString getCtrlText() const =0
void SPIN_INCREMENTAL_TEXT_CTRL::onFocusLoss ( wxFocusEvent &  aEvent)
private

Definition at line 169 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::incrementCtrlBy().

Referenced by SPIN_INCREMENTAL_TEXT_CTRL(), and ~SPIN_INCREMENTAL_TEXT_CTRL().

170 {
171  // re-read the input and sanitize any user changes
172  incrementCtrlBy( 0.0 );
173 }
void incrementCtrlBy(double aInc)
Increment the control by the given amount.
void INCREMENTAL_TEXT_CTRL::SetPrecision ( int  aPrecision)
inherited

Set the number of decimal places to display.

Definition at line 103 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::m_precision.

104 {
105  m_precision = aPrecision;
106 }
int m_precision
The function used to determine the step.
void INCREMENTAL_TEXT_CTRL::SetStep ( double  aMin,
double  aMax,
STEP_FUNCTION  aNewFunc 
)
inherited

Function SetStep()

Set the stepping parameters of the control. The range is enforced by not allowing the scroll to exceed it, and on loss of focus, the control is also clamped to the range.

Parameters
aMinthe minium value allowed
aMaxthe maximum value allows
aNewFuncthe step function used to calculate the next step

Definition at line 57 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::m_maxVal, INCREMENTAL_TEXT_CTRL::m_minVal, INCREMENTAL_TEXT_CTRL::m_stepFunc, max, min, and INCREMENTAL_TEXT_CTRL::updateTextValue().

Referenced by INCREMENTAL_TEXT_CTRL::SetStep().

59 {
60  wxASSERT( aMin <= aMax );
61 
62  m_minVal = std::min( aMin, aMax );
63  m_maxVal = std::max( aMin, aMax );
64  m_stepFunc = aStepFunc;
65 
66  // finally, clamp the current value and re-display
68 }
double m_maxVal
Current value of the control.
#define max(a, b)
Definition: auxiliary.h:86
void updateTextValue()
Update the text control value with the current value, clamping to limits as needed.
#define min(a, b)
Definition: auxiliary.h:85
void INCREMENTAL_TEXT_CTRL::SetStep ( double  aMin,
double  aMax,
double  aStep 
)
inlineinherited

Function SetStep()

Shortcut method to set step parameters when the step is constant

Parameters
aMinthe minium value allowed
aMaxthe maximum value allows
aStepthe constant step size

Definition at line 92 of file incremental_text_ctrl.h.

References INCREMENTAL_TEXT_CTRL::SetStep().

93  {
94  SetStep( aMin, aMax,
95  [aStep] ( bool aUp, double aCurrent ) { return aUp ? aStep : -aStep; } );
96  }
void SetStep(double aMin, double aMax, STEP_FUNCTION aNewFunc)
Function SetStep()
void SPIN_INCREMENTAL_TEXT_CTRL::setTextCtrl ( const wxString &  val)
overrideprotectedvirtual

>

Set the text control string value after an increment.

Implements INCREMENTAL_TEXT_CTRL.

Definition at line 176 of file incremental_text_ctrl.cpp.

References m_textCtrl.

177 {
178  m_textCtrl.SetValue( val );
179 }
void INCREMENTAL_TEXT_CTRL::SetValue ( double  aValue)
inherited

Set the value of the text control, but obey the limits currently set.

Parameters
aValuethe control value to set

Definition at line 109 of file incremental_text_ctrl.cpp.

References INCREMENTAL_TEXT_CTRL::m_currentValue, and INCREMENTAL_TEXT_CTRL::updateTextValue().

110 {
111  m_currentValue = aValue;
112  updateTextValue();
113 }
double m_currentValue
Precision to display.
void updateTextValue()
Update the text control value with the current value, clamping to limits as needed.
void INCREMENTAL_TEXT_CTRL::updateTextValue ( )
protectedinherited

Update the text control value with the current value, clamping to limits as needed.

Definition at line 71 of file incremental_text_ctrl.cpp.

References Format(), INCREMENTAL_TEXT_CTRL::m_currentValue, INCREMENTAL_TEXT_CTRL::m_maxVal, INCREMENTAL_TEXT_CTRL::m_minVal, INCREMENTAL_TEXT_CTRL::m_precision, and INCREMENTAL_TEXT_CTRL::setTextCtrl().

Referenced by INCREMENTAL_TEXT_CTRL::incrementCtrlBy(), INCREMENTAL_TEXT_CTRL::SetStep(), and INCREMENTAL_TEXT_CTRL::SetValue().

72 {
73  if( m_currentValue > m_maxVal )
75 
76  if( m_currentValue < m_minVal )
78 
79  wxString fmt = wxString::Format( "%%.%df", m_precision );
81 }
double m_maxVal
Current value of the control.
double m_currentValue
Precision to display.
virtual void setTextCtrl(const wxString &aVal)=0
Set the text control string value after an increment.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
int m_precision
The function used to determine the step.

Member Data Documentation

wxSpinButton& SPIN_INCREMENTAL_TEXT_CTRL::m_spinBtn
private

Definition at line 184 of file incremental_text_ctrl.h.

Referenced by SPIN_INCREMENTAL_TEXT_CTRL().

wxTextCtrl& SPIN_INCREMENTAL_TEXT_CTRL::m_textCtrl
private

The documentation for this class was generated from the following files: