Audacity 3.2.0
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
SpinControl Class Referencefinal

#include <SpinControl.h>

Inheritance diagram for SpinControl:
[legend]
Collaboration diagram for SpinControl:
[legend]

Public Types

using ValueType = double
 

Public Member Functions

 SpinControl (wxWindow *parent, wxWindowID winid=wxID_ANY, ValueType value=0.0, ValueType min=0.0f, ValueType max=100.0, ValueType step=1.0, bool allowFractional=false, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, const TranslatableString &name={})
 
void SetValue (double value)
 
double GetValue () const
 
void SetMinValue (double value)
 
double GetMinValue () const
 
void SetMaxValue (double value)
 
double GetMaxValue () const
 
void SetStep (double step)
 
double GetStep () const
 
void SetFractionalAllowed (bool allow)
 
bool GetFractionalAllowed ()
 
void SetName (const TranslatableString &name)
 

Private Member Functions

void UpdatePrefs () override
 
void CreateUI ()
 
void SetupControls ()
 
void CommitTextControlValue ()
 
void OnCharHook (wxKeyEvent &evt)
 
void SetValue (double value, bool silent)
 
void DoSteps (double direction)
 
void NotifyValueChanged ()
 
- Private Member Functions inherited from PrefsListener
 PrefsListener ()
 
virtual ~PrefsListener ()
 
virtual void UpdatePrefs ()=0
 
virtual void UpdateSelectedPrefs (int id)
 

Private Attributes

ValueType mValue { std::numeric_limits<ValueType>::quiet_NaN() }
 
ValueType mMinValue { -std::numeric_limits<ValueType>::infinity() }
 
ValueType mMaxValue { std::numeric_limits<ValueType>::infinity() }
 
ValueType mStep
 
int mPrecision { 2 }
 
bool mFractionalAllowed
 
wxTextCtrl * mTextControl { nullptr }
 
wxButton * mUpButton { nullptr }
 
wxButton * mDownButton { nullptr }
 

Additional Inherited Members

- Static Private Member Functions inherited from PrefsListener
static void Broadcast (int id=0)
 Call this static function to notify all PrefsListener objects. More...
 

Detailed Description

Definition at line 29 of file SpinControl.h.

Member Typedef Documentation

◆ ValueType

using SpinControl::ValueType = double

Definition at line 34 of file SpinControl.h.

Constructor & Destructor Documentation

◆ SpinControl()

SpinControl::SpinControl ( wxWindow *  parent,
wxWindowID  winid = wxID_ANY,
ValueType  value = 0.0,
ValueType  min = 0.0f,
ValueType  max = 100.0,
ValueType  step = 1.0,
bool  allowFractional = false,
const wxPoint &  pos = wxDefaultPosition,
const wxSize &  size = wxDefaultSize,
const TranslatableString name = {} 
)

Definition at line 63 of file SpinControl.cpp.

69 : wxControl(parent, winid, pos, size, wxBORDER_NONE)
70 , mStep(step)
71 , mFractionalAllowed(allowFractional)
72{
73 CreateUI();
74
75 // Call setter explicitly to ensure that all the clamping happens correctly
77 SetMaxValue(max);
78 SetValue(value);
79
81
83}
int min(int a, int b)
wxString name
Definition: TagsEditor.cpp:166
void SetMaxValue(double value)
void CreateUI()
void SetMinValue(double value)
Definition: SpinControl.cpp:95
ValueType mStep
Definition: SpinControl.h:76
void SetValue(double value)
Definition: SpinControl.cpp:85
void SetName(const TranslatableString &name)
bool mFractionalAllowed
Definition: SpinControl.h:79
void SetupControls()

References CreateUI(), min(), name, SetMaxValue(), SetMinValue(), SetName(), SetupControls(), and SetValue().

Here is the call graph for this function:

Member Function Documentation

◆ CommitTextControlValue()

void SpinControl::CommitTextControlValue ( )
private

Definition at line 311 of file SpinControl.cpp.

312{
313 double value;
314 if (!mTextControl->GetValue().ToDouble(&value))
315 return;
316
317 SetValue(value, false);
318}
wxTextCtrl * mTextControl
Definition: SpinControl.h:81

References mTextControl, and SetValue().

Referenced by CreateUI(), and OnCharHook().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateUI()

void SpinControl::CreateUI ( )
private

Definition at line 161 of file SpinControl.cpp.

162{
163 mTextControl = safenew wxTextCtrl(this, wxID_ANY);
164#ifndef __WXGTK__
165 const auto editorHeight = mTextControl->GetSize().y;
166#else
167 // GTK requires the buttons to be at least 16x16
168 // TODO: rely on GTK_ICON_SIZE_BUTTON instead of hardcoding the size
169 constexpr auto minGtkSize = 16;
170 const auto editorHeight = std::max(minGtkSize * 2, mTextControl->GetSize().y);
171#endif
172
173 auto boxSizer = safenew wxBoxSizer(wxHORIZONTAL);
174
175 boxSizer->Add(mTextControl, wxSizerFlags().Border(wxALL, 0));
176
177#ifndef __WXGTK__
178 mSpinButton = safenew wxSpinButton(this);
179 mSpinButton->SetMaxSize({ -1, editorHeight });
180
181 // SpinButton is only used to generate the events,
182 // so keep the value between min (0 by default) and max (100 by default)
183 mSpinButton->SetValue(50);
184
185 boxSizer->Add(mSpinButton, wxSizerFlags().Border(wxALL, 0));
186#else
187 auto buttonsSizer = safenew wxBoxSizer(wxVERTICAL);
188
189 const auto buttonSize = wxSize { editorHeight / 2, editorHeight / 2 };
190
191 mUpButton = safenew wxButton(this, wxID_ANY, L"+", wxDefaultPosition, buttonSize);
192 mUpButton->SetMinSize(buttonSize);
193 mUpButton->SetMaxSize(buttonSize);
194 buttonsSizer->Add(mUpButton, wxSizerFlags().Border(wxALL, 0));
195
196 mDownButton = safenew wxButton(this, wxID_ANY, L"-", wxDefaultPosition, buttonSize);
197 mDownButton->SetMinSize(buttonSize);
198 mDownButton->SetMaxSize(buttonSize);
199 buttonsSizer->Add(mDownButton, wxSizerFlags().Border(wxALL, 0));
200
201 boxSizer->Add(buttonsSizer, wxSizerFlags().Border(wxALL, 0));
202#endif
203
204 const auto width = GetSize().x;
205
206 if (width > 0)
207 {
208#ifndef __WXGTK__
209 auto spinWidth = mSpinButton->GetSize().x;
210 const auto editorWidth = std::max(10, width - spinWidth);
211 mTextControl->SetMaxSize({ editorWidth, editorHeight });
212#else
213 const auto editorWidth = std::max(10, width - editorHeight / 2);
214
215 mTextControl->SetMinSize({ editorWidth, editorHeight });
216 mTextControl->SetMaxSize({ editorWidth, editorHeight });
217 mTextControl->SetSize({ editorWidth, editorHeight });
218#endif
219 }
220
221 SetSizerAndFit(boxSizer);
222 Layout();
223
224 Bind(
225 wxEVT_SET_FOCUS,
226 [this](auto& evt)
227 {
228 mTextControl->SetFocus();
229 evt.Skip();
230 });
231
232 Bind(wxEVT_CHAR_HOOK, &SpinControl::OnCharHook, this);
233
234 mTextControl->Bind(
235 wxEVT_KILL_FOCUS,
236 [this](auto& evt)
237 {
239 evt.Skip();
240 });
241
242 mTextControl->Bind(
243 wxEVT_MOUSEWHEEL,
244 [this](auto& evt)
245 {
246 const auto delta = evt.GetWheelDelta();
247 const auto rotation = evt.GetWheelRotation();
248
249 if (rotation >= delta)
250 DoSteps(evt.ShiftDown() ? 10 : 1);
251 else if (rotation <= delta)
252 DoSteps(evt.ShiftDown() ? -10 : -1);
253 });
254
255#ifndef __WXGTK__
256 mSpinButton->Bind(
257 wxEVT_SPIN_UP,
258 [this](auto& evt)
259 {
260 DoSteps(1);
261 evt.Veto();
262 });
263
264 mSpinButton->Bind(
265 wxEVT_SPIN_DOWN,
266 [this](auto& evt)
267 {
268 DoSteps(-1);
269 evt.Veto();
270 });
271#else
272 mUpButton->Bind(
273 wxEVT_BUTTON,
274 [this](auto&)
275 {
276 DoSteps(1);
277 });
278
279 mDownButton->Bind(
280 wxEVT_BUTTON,
281 [this](auto&)
282 {
283 DoSteps(-1);
284 });
285#endif
286}
#define safenew
Definition: MemoryX.h:10
wxButton * mDownButton
Definition: SpinControl.h:86
void DoSteps(double direction)
void CommitTextControlValue()
void OnCharHook(wxKeyEvent &evt)
wxButton * mUpButton
Definition: SpinControl.h:85

References CommitTextControlValue(), DoSteps(), mDownButton, mTextControl, mUpButton, OnCharHook(), and safenew.

Referenced by SpinControl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DoSteps()

void SpinControl::DoSteps ( double  direction)
private

Definition at line 353 of file SpinControl.cpp.

354{
355 SetValue(mValue + direction * mStep, false);
356}
ValueType mValue
Definition: SpinControl.h:73

References mStep, mValue, and SetValue().

Referenced by CreateUI(), and OnCharHook().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFractionalAllowed()

bool SpinControl::GetFractionalAllowed ( )

Definition at line 139 of file SpinControl.cpp.

140{
141 return mFractionalAllowed;
142}

References mFractionalAllowed.

◆ GetMaxValue()

double SpinControl::GetMaxValue ( ) const

Definition at line 114 of file SpinControl.cpp.

115{
116 return mMaxValue;
117}
ValueType mMaxValue
Definition: SpinControl.h:75

References mMaxValue.

◆ GetMinValue()

double SpinControl::GetMinValue ( ) const

Definition at line 102 of file SpinControl.cpp.

103{
104 return mMinValue;
105}
ValueType mMinValue
Definition: SpinControl.h:74

References mMinValue.

◆ GetStep()

double SpinControl::GetStep ( ) const

Definition at line 125 of file SpinControl.cpp.

126{
127 return mStep;
128}

References mStep.

◆ GetValue()

double SpinControl::GetValue ( ) const

Definition at line 90 of file SpinControl.cpp.

91{
92 return mValue;
93}

References mValue.

Referenced by ShuttleGuiBase::DoTieSpinControl().

Here is the caller graph for this function:

◆ NotifyValueChanged()

void SpinControl::NotifyValueChanged ( )
private

Definition at line 358 of file SpinControl.cpp.

359{
360 wxCommandEvent e(wxEVT_SPINCTRL, GetId());
361 e.SetEventObject(this);
362 ProcessWindowEvent(e);
363}

Referenced by SetValue().

Here is the caller graph for this function:

◆ OnCharHook()

void SpinControl::OnCharHook ( wxKeyEvent &  evt)
private

Definition at line 320 of file SpinControl.cpp.

321{
322 const auto keyCode = evt.GetKeyCode();
323
324 if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
325 DoSteps(evt.ShiftDown() ? 10.0 : 1.0);
326 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
327 DoSteps(10.0);
328 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
329 DoSteps(evt.ShiftDown() ? -10.0 : -1.0);
330 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
331 DoSteps(-10.0);
332 else if (keyCode == WXK_RETURN || keyCode == WXK_NUMPAD_ENTER)
334 else
335 evt.Skip();
336}

References CommitTextControlValue(), and DoSteps().

Referenced by CreateUI().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetFractionalAllowed()

void SpinControl::SetFractionalAllowed ( bool  allow)

Definition at line 130 of file SpinControl.cpp.

131{
132 if (mFractionalAllowed == allow)
133 return;
134
135 mFractionalAllowed = allow;
137}

References mFractionalAllowed, and SetupControls().

Here is the call graph for this function:

◆ SetMaxValue()

void SpinControl::SetMaxValue ( double  value)

Definition at line 107 of file SpinControl.cpp.

108{
109 mMaxValue = std::max(value, mMinValue);
112}

References mMaxValue, mMinValue, mValue, SetupControls(), and SetValue().

Referenced by SpinControl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetMinValue()

void SpinControl::SetMinValue ( double  value)

Definition at line 95 of file SpinControl.cpp.

96{
100}

References min(), mMaxValue, mMinValue, mValue, SetupControls(), and SetValue().

Referenced by SpinControl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetName()

void SpinControl::SetName ( const TranslatableString name)

Definition at line 144 of file SpinControl.cpp.

145{
146#if wxUSE_ACCESSIBILITY
147 if (mWindowAccessible == nullptr)
148 {
149 mWindowAccessible = safenew SpinControlAx(this, mTextControl);
150 mTextControl->SetAccessible(mWindowAccessible);
151 }
152
153 mWindowAccessible->SetName(name);
154#endif
155}

References mTextControl, name, and safenew.

Referenced by SpinControl().

Here is the caller graph for this function:

◆ SetStep()

void SpinControl::SetStep ( double  step)

Definition at line 119 of file SpinControl.cpp.

120{
121 mStep = step;
123}

References mStep, and SetupControls().

Here is the call graph for this function:

◆ SetupControls()

void SpinControl::SetupControls ( )
private

Definition at line 288 of file SpinControl.cpp.

289{
291 {
292 auto validator = wxFloatingPointValidator<ValueType>(
293 mPrecision, nullptr, wxNUM_VAL_NO_TRAILING_ZEROES);
294
295 validator.SetMin(mMinValue);
296 validator.SetMax(mMaxValue);
297
298 mTextControl->SetValidator(validator);
299 }
300 else
301 {
302 auto validator = wxIntegerValidator<int>();
303
304 validator.SetMin(static_cast<int>(std::ceil(mMinValue)));
305 validator.SetMax(static_cast<int>(std::floor(mMaxValue)));
306
307 mTextControl->SetValidator(validator);
308 }
309}
int mPrecision
Definition: SpinControl.h:77

References mFractionalAllowed, mMaxValue, mMinValue, mPrecision, and mTextControl.

Referenced by SetFractionalAllowed(), SetMaxValue(), SetMinValue(), SetStep(), and SpinControl().

Here is the caller graph for this function:

◆ SetValue() [1/2]

void SpinControl::SetValue ( double  value)

Definition at line 85 of file SpinControl.cpp.

86{
87 SetValue(value, true);
88}

References SetValue().

Referenced by CommitTextControlValue(), DoSteps(), ShuttleGuiBase::DoTieSpinControl(), SetMaxValue(), SetMinValue(), SetValue(), and SpinControl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetValue() [2/2]

void SpinControl::SetValue ( double  value,
bool  silent 
)
private

Definition at line 338 of file SpinControl.cpp.

339{
340 value = std::clamp(value, mMinValue, mMaxValue);
341
342 // Should some epsilon be used here?
343 if (value == mValue)
344 return;
345
346 mValue = value;
347 mTextControl->SetValue(wxString::FromDouble(value));
348
349 if (!silent)
351}
void NotifyValueChanged()

References mMaxValue, mMinValue, mTextControl, mValue, and NotifyValueChanged().

Here is the call graph for this function:

◆ UpdatePrefs()

void SpinControl::UpdatePrefs ( )
overrideprivatevirtual

Implements PrefsListener.

Definition at line 157 of file SpinControl.cpp.

158{
159}

Member Data Documentation

◆ mDownButton

wxButton* SpinControl::mDownButton { nullptr }
private

Definition at line 86 of file SpinControl.h.

Referenced by CreateUI().

◆ mFractionalAllowed

bool SpinControl::mFractionalAllowed
private

Definition at line 79 of file SpinControl.h.

Referenced by GetFractionalAllowed(), SetFractionalAllowed(), and SetupControls().

◆ mMaxValue

ValueType SpinControl::mMaxValue { std::numeric_limits<ValueType>::infinity() }
private

Definition at line 75 of file SpinControl.h.

Referenced by GetMaxValue(), SetMaxValue(), SetMinValue(), SetupControls(), and SetValue().

◆ mMinValue

ValueType SpinControl::mMinValue { -std::numeric_limits<ValueType>::infinity() }
private

Definition at line 74 of file SpinControl.h.

Referenced by GetMinValue(), SetMaxValue(), SetMinValue(), SetupControls(), and SetValue().

◆ mPrecision

int SpinControl::mPrecision { 2 }
private

Definition at line 77 of file SpinControl.h.

Referenced by SetupControls().

◆ mStep

ValueType SpinControl::mStep
private

Definition at line 76 of file SpinControl.h.

Referenced by DoSteps(), GetStep(), and SetStep().

◆ mTextControl

wxTextCtrl* SpinControl::mTextControl { nullptr }
private

Definition at line 81 of file SpinControl.h.

Referenced by CommitTextControlValue(), CreateUI(), SetName(), SetupControls(), and SetValue().

◆ mUpButton

wxButton* SpinControl::mUpButton { nullptr }
private

Definition at line 85 of file SpinControl.h.

Referenced by CreateUI().

◆ mValue

ValueType SpinControl::mValue { std::numeric_limits<ValueType>::quiet_NaN() }
private

Definition at line 73 of file SpinControl.h.

Referenced by DoSteps(), GetValue(), SetMaxValue(), SetMinValue(), and SetValue().


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