Audacity 3.2.0
NumericTextCtrl.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 NumericTextCtrl.h
6
7 Dominic Mazzoni
8
9 See NumericConverter.cpp for documentation on how to use the
10 format string to specify how a NumericTextCtrl's fields are
11 laid out.
12
13**********************************************************************/
14#ifndef __AUDACITY_TIME_TEXT_CTRL__
15#define __AUDACITY_TIME_TEXT_CTRL__
16
17#include "NumericConverter.h"
18
19#include "MemoryX.h"
20#include "ComponentInterface.h"
21#include <vector>
22#include <wx/setup.h> // for wxUSE_* macros
23#include <wx/defs.h>
24#include <wx/control.h> // to inherit
25
26// One event type for each type of control. Event is raised when a control
27// changes its format. Owners of controls of the same type can listen and
28// update their formats to agree.
29DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED, -1);
30DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_FREQUENCYTEXTCTRL_UPDATED, -1);
31DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_BANDWIDTHTEXTCTRL_UPDATED,
32 -1);
33
34class AUDACITY_DLL_API NumericTextCtrl final
35 : public wxControl, public NumericConverter
36{
37 friend class NumericTextCtrlAx;
38
39 public:
40 DECLARE_DYNAMIC_CLASS(NumericTextCtrl)
41
42 struct Options {
43 bool autoPos { true };
44 bool readOnly { false };
45 bool menuEnabled { true };
46 bool hasInvalidValue { false };
47 double invalidValue { -1.0 };
48 NumericFormatSymbol formatSymbol {};
49 TranslatableString customFormat {};
50 bool hasValue { false };
51 double value{ -1.0 };
52
54
55 Options &AutoPos (bool enable) { autoPos = enable; return *this; }
56 Options &ReadOnly (bool enable) { readOnly = enable; return *this; }
57 Options &MenuEnabled (bool enable) { menuEnabled = enable; return *this; }
58 Options &InvalidValue (bool has, double v = -1.0)
59 { hasInvalidValue = has, invalidValue = v; return *this; }
60 // use a custom format not in the tables:
62 { formatSymbol = f; return *this; }
64 { customFormat = f; return *this; }
65 Options &Value (bool has, double v)
66 { hasValue = has, value = v; return *this; }
67 };
68
70 const FormatterContext& context, wxWindow* parent, wxWindowID winid,
72 const NumericFormatSymbol &formatName = {},
73 double value = 0.0,
74 const Options &options = {},
75 const wxPoint &pos = wxDefaultPosition,
76 const wxSize &size = wxDefaultSize);
77
78 virtual ~NumericTextCtrl();
79
80 // Hide the inherited function that takes wxString
81 void SetName( const TranslatableString &name );
82
83 wxSize ComputeSizing(bool update = true, wxCoord digitW = 0, wxCoord digitH = 0);
84
85 bool Layout() override;
86 void Fit() override;
87
88 void SetValue(double newValue);
89
90 // returns true if the format type really changed:
92 const NumericConverterType& type, const NumericFormatSymbol& formatName);
93 // returns true iff the format name really changed:
94 bool SetFormatName(const NumericFormatSymbol & formatName);
95 bool SetCustomFormat(const TranslatableString& customFormat);
96
97 void SetFieldFocus(int /* digit */);
98
99 wxSize GetDimensions() { return wxSize(mWidth + mButtonWidth, mHeight); }
100 wxSize GetDigitSize() { return wxSize(mDigitBoxW, mDigitBoxH); }
101 void SetDigitSize(int width, int height);
102 void SetReadOnly(bool readOnly = true);
103 void EnableMenu(bool enable = true);
104
105 // The text control permits typing DELETE to make the value invalid only if this
106 // function has previously been called.
107 // Maybe you want something other than the default of -1 to indicate the invalid value
108 // this control returns to the program, so you can specify.
109 void SetInvalidValue(double invalidValue);
110
111 int GetFocusedField() { return mLastField; }
112 int GetFocusedDigit() { return mFocusedDigit; }
113
114private:
115 void OnFormatUpdated() override;
116 void HandleFormatterChanged();
117
118 void OnCaptureKey(wxCommandEvent &event);
119 void OnKeyDown(wxKeyEvent &event);
120 void OnKeyUp(wxKeyEvent &event);
121 void OnMouse(wxMouseEvent &event);
122 void OnErase(wxEraseEvent &event);
123 void OnPaint(wxPaintEvent &event);
124 void OnFocus(wxFocusEvent &event);
125 void OnContext(wxContextMenuEvent &event);
126
127 // Formats mValue into mValueString, using the method of the base class.
128 // Triggers a refresh of the wx window only when the value actually
129 // changed since last time a refresh was triggered.
130 void ValueToControls() override;
131 void ControlsToValue() override;
132
133 // If autoPos was enabled, focus the first non-zero digit
134 void UpdateAutoFocus();
135
136 void Updated(bool keyup = false);
137
138private:
139 struct FieldPosition final
140 {
141 int fieldX; // x-position of the field on-screen
142 int fieldW; // width of the field on-screen
143 int labelX; // x-position of the label on-screen
144 };
145
146 std::vector<wxRect> mBoxes;
147 std::vector<FieldPosition> mFieldPositions;
148
149 wxRect GetBox(size_t ii) const;
150
153
154 std::unique_ptr<wxBitmap> mBackgroundBitmap;
155
156 std::unique_ptr<wxFont> mDigitFont, mLabelFont;
168
170
171 int mFocusedDigit { 0 };
172
173 // If true, the focus will be set to the first non-zero digit
175
176 // Keeps track of extra fractional scrollwheel steps
178
180
182
183 DECLARE_EVENT_TABLE()
184};
185
186#endif // __AUDACITY_TIME_TEXT_CTRL__
const TranslatableString name
Definition: Distortion.cpp:76
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED, -1)
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
A context in which formatter operates.
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
NumericConverter provides the advanced formatting control used in the selection bar of Audacity.
virtual void ControlsToValue()
virtual void ValueToControls()
bool SetCustomFormat(const TranslatableString &customFormat)
virtual void OnFormatUpdated()
void SetValue(double newValue)
bool SetFormatName(const NumericFormatSymbol &formatName)
bool SetTypeAndFormatName(const NumericConverterType &type, const NumericFormatSymbol &formatName)
wxSize GetDimensions()
std::unique_ptr< wxFont > mDigitFont
std::vector< wxRect > mBoxes
std::unique_ptr< wxBitmap > mBackgroundBitmap
NumericConverterType mType
std::vector< FieldPosition > mFieldPositions
Holds a msgid for the translation catalog; may also bind format arguments.
void OnFocus(wxWindow &window, wxFocusEvent &event)
a function useful to implement a focus event handler The window releases the keyboard if the event is...
Options & MenuEnabled(bool enable)
Options & FormatSymbol(const NumericFormatSymbol &f)
Options & Value(bool has, double v)
Options & CustomFormat(const TranslatableString &f)
Options & AutoPos(bool enable)
Options & InvalidValue(bool has, double v=-1.0)
Options & ReadOnly(bool enable)