Audacity  2.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 NumericTextCtrl.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 
15 #ifndef __AUDACITY_TIME_TEXT_CTRL__
16 #define __AUDACITY_TIME_TEXT_CTRL__
17 
18 #include "../MemoryX.h"
19 #include <vector>
20 #include <wx/defs.h>
21 #include <wx/dynarray.h>
22 #include <wx/event.h>
23 #include <wx/panel.h>
24 #include <wx/stattext.h>
25 #include <wx/string.h>
26 #include <wx/textctrl.h>
27 
28 #include "../Audacity.h"
29 
30 #if wxUSE_ACCESSIBILITY
31 #include <wx/access.h>
32 #endif
33 
34 // One event type for each type of control. Event is raised when a control
35 // changes its format. Owners of controls of the same type can listen and
36 // update their formats to agree.
37 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED, -1);
38 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_FREQUENCYTEXTCTRL_UPDATED, -1);
39 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_BANDWIDTHTEXTCTRL_UPDATED,
40  -1);
41 
45 struct BuiltinFormatString;
46 
47 class NumericField;
48 WX_DECLARE_OBJARRAY(NumericField, NumericFieldArray);
49 
50 class DigitInfo;
51 WX_DECLARE_OBJARRAY(DigitInfo, DigitInfoArray);
52 
53 class NumericConverter /* not final */
54 {
55 public:
56 
57  enum Type {
61  };
62 
64  const wxString & formatName = wxEmptyString,
65  double value = 0.0f,
66  double sampleRate = 1.0f /* to prevent div by 0 */);
67 
68  virtual ~NumericConverter();
69 
70  // ValueToControls() formats a raw value (either provided as
71  // argument, or mValue, depending on the version of the function
72  // called). The result is stored to mValueString.
73  virtual void ValueToControls();
74  virtual void ValueToControls(double rawValue, bool nearest = true);
75 
76  // Converts the stored formatted string (mValueString) back to a
77  // raw value (mValue).
78  virtual void ControlsToValue();
79 
80  virtual void ParseFormatString(const wxString & format);
81 
82  void PrintDebugInfo();
83  void SetFormatName(const wxString & formatName);
84  void SetFormatString(const wxString & formatString);
85  void SetSampleRate(double sampleRate);
86  void SetValue(double newValue);
87  void SetMinValue(double minValue);
88  void ResetMinValue();
89  void SetMaxValue(double maxValue);
90  void ResetMaxValue();
91 
92  double GetValue();
93 
94  wxString GetString();
95 
96  wxString GetFormatString();
97  int GetFormatIndex();
98 
99  int GetNumBuiltins();
100  wxString GetBuiltinName(const int index);
101  wxString GetBuiltinFormat(const int index);
102  wxString GetBuiltinFormat(const wxString & name);
103 
104  // Adjust the value by the number "steps" in the active format.
105  // Increment if "dir" is 1, decrement if "dir" is -1.
106  void Adjust(int steps, int dir);
107 
108  void Increment();
109  void Decrement();
110 
111 protected:
113 
114  double mValue;
115 
116  double mMinValue;
117  double mMaxValue;
119 
120  wxString mFormatString;
121 
122  NumericFieldArray mFields;
123  wxString mPrefix;
124  wxString mValueTemplate;
125  wxString mValueMask;
126  // Formatted mValue, by ValueToControls().
127  wxString mValueString;
128 
130  double mSampleRate;
131  bool mNtscDrop;
132 
134  DigitInfoArray mDigits;
135 
136  const std::vector<BuiltinFormatString> &mBuiltinFormatStrings;
139 };
140 
141 class NumericTextCtrl final : public wxControl, public NumericConverter
142 {
143  friend class NumericTextCtrlAx;
144 
145  public:
146  DECLARE_DYNAMIC_CLASS(NumericTextCtrl)
147 
149  wxWindow *parent,
150  wxWindowID id,
151  const wxString &formatName = wxEmptyString,
152  double value = 0.0,
153  double sampleRate = 44100,
154  const wxPoint &pos = wxDefaultPosition,
155  const wxSize &size = wxDefaultSize,
156  bool autoPos = false);
157 
158  virtual ~NumericTextCtrl();
159 
160  bool Layout() override;
161  void Fit() override;
162 
163  void SetSampleRate(double sampleRate);
164  void SetValue(double newValue);
165  void SetFormatString(const wxString & formatString);
166  void SetFormatName(const wxString & formatName);
167 
168  void SetFieldFocus(int /* digit */);
169 
170  void SetReadOnly(bool readOnly = true);
171  void EnableMenu(bool enable = true);
172 
173  // The text control permits typing DELETE to make the value invalid only if this
174  // function has previously been called.
175  // Maybe you want something other than the default of -1 to indicate the invalid value
176  // this control returns to the program, so you can specify.
177  void SetInvalidValue(double invalidValue);
178 
179  int GetFocusedField() { return mLastField; }
180  int GetFocusedDigit() { return mFocusedDigit; }
181 
182 private:
183 
184  void OnCaptureKey(wxCommandEvent &event);
185  void OnKeyDown(wxKeyEvent &event);
186  void OnKeyUp(wxKeyEvent &event);
187  void OnMouse(wxMouseEvent &event);
188  void OnErase(wxEraseEvent &event);
189  void OnPaint(wxPaintEvent &event);
190  void OnFocus(wxFocusEvent &event);
191  void OnContext(wxContextMenuEvent &event);
192 
193  // Formats mValue into mValueString, using the method of the base class.
194  // Triggers a refresh of the wx window only when the value actually
195  // changed since last time a refresh was triggered.
196  void ValueToControls() override;
197  void ControlsToValue() override;
198 
199  // If autoPos was enabled, focus the first non-zero digit
200  void UpdateAutoFocus();
201 
202  void Updated(bool keyup = false);
203 
204 private:
205 
207  bool mReadOnly;
208 
209  std::unique_ptr<wxBitmap> mBackgroundBitmap;
210 
211  std::unique_ptr<wxFont> mDigitFont, mLabelFont;
214  int mDigitW;
215  int mDigitH;
220  int mWidth;
221  int mHeight;
223 
225 
226  // If true, the focus will be set to the first non-zero digit
227  bool mAutoPos;
228 
229  // Keeps track of extra fractional scrollwheel steps
231 
233 
235 
236  DECLARE_EVENT_TABLE()
237 };
238 
239 #if wxUSE_ACCESSIBILITY
240 
241 class NumericTextCtrlAx final : public wxWindowAccessible
242 {
243 public:
245 
246  virtual ~ NumericTextCtrlAx();
247 
248  // Performs the default action. childId is 0 (the action for this object)
249  // or > 0 (the action for a child).
250  // Return wxACC_NOT_SUPPORTED if there is no default action for this
251  // window (e.g. an edit control).
252  wxAccStatus DoDefaultAction(int childId) override;
253 
254  // Retrieves the address of an IDispatch interface for the specified child.
255  // All objects must support this property.
256  wxAccStatus GetChild(int childId, wxAccessible **child) override;
257 
258  // Gets the number of children.
259  wxAccStatus GetChildCount(int *childCount) override;
260 
261  // Gets the default action for this object (0) or > 0 (the action for a child).
262  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
263  // string if there is no action.
264  // The retrieved string describes the action that is performed on an object,
265  // not what the object does as a result. For example, a toolbar button that prints
266  // a document has a default action of "Press" rather than "Prints the current document."
267  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
268 
269  // Returns the description for this object or a child.
270  wxAccStatus GetDescription(int childId, wxString *description) override;
271 
272  // Gets the window with the keyboard focus.
273  // If childId is 0 and child is NULL, no object in
274  // this subhierarchy has the focus.
275  // If this object has the focus, child should be 'this'.
276  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
277 
278  // Returns help text for this object or a child, similar to tooltip text.
279  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
280 
281  // Returns the keyboard shortcut for this object or child.
282  // Return e.g. ALT+K
283  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
284 
285  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
286  // rect is in screen coordinates.
287  wxAccStatus GetLocation(wxRect & rect, int elementId) override;
288 
289  // Gets the name of the specified object.
290  wxAccStatus GetName(int childId, wxString *name) override;
291 
292  // Returns a role constant.
293  wxAccStatus GetRole(int childId, wxAccRole *role) override;
294 
295  // Gets a variant representing the selected children
296  // of this object.
297  // Acceptable values:
298  // - a null variant (IsNull() returns TRUE)
299  // - a list variant (GetType() == wxT("list"))
300  // - an integer representing the selected child element,
301  // or 0 if this object is selected (GetType() == wxT("long"))
302  // - a "void*" pointer to a wxAccessible child object
303  wxAccStatus GetSelections(wxVariant *selections) override;
304 
305  // Returns a state constant.
306  wxAccStatus GetState(int childId, long *state) override;
307 
308  // Returns a localized string representing the value for the object
309  // or child.
310  wxAccStatus GetValue(int childId, wxString *strValue) override;
311 
312 private:
313  NumericTextCtrl *mCtrl;
314  int mLastField;
315  int mLastDigit;
316 };
317 
318 #endif // wxUSE_ACCESSIBILITY
319 
320 #endif // __AUDACITY_TIME_TEXT_CTRL__
int mBorderRight
Definition: NumericTextCtrl.h:218
void SetFormatName(const wxString &formatName)
Definition: NumericTextCtrl.cpp:995
double GetValue()
Definition: NumericTextCtrl.cpp:1052
int GetFormatIndex()
Definition: NumericTextCtrl.cpp:1063
void SetFormatString(const wxString &formatString)
Definition: NumericTextCtrl.cpp:1000
void Adjust(int steps, int dir)
Definition: NumericTextCtrl.cpp:1134
Definition: NumericTextCtrl.h:58
std::unique_ptr< wxFont > mDigitFont
Definition: NumericTextCtrl.h:211
int mBorderBottom
Definition: NumericTextCtrl.h:219
Type mType
Definition: NumericTextCtrl.h:112
std::unique_ptr< wxBitmap > mBackgroundBitmap
Definition: NumericTextCtrl.h:209
void SetFormatName(const wxString &formatName)
Definition: NumericTextCtrl.cpp:1309
wxString mPrefix
Definition: NumericTextCtrl.h:123
void OnContext(wxContextMenuEvent &event)
Definition: NumericTextCtrl.cpp:1551
NumericTextCtrlAx gives the NumericTextCtrl Accessibility.
void SetReadOnly(bool readOnly=true)
Definition: NumericTextCtrl.cpp:1340
bool mAllowInvalidValue
Definition: NumericTextCtrl.h:234
void SetSampleRate(double sampleRate)
Definition: NumericTextCtrl.cpp:1324
bool mAutoPos
Definition: NumericTextCtrl.h:227
void OnKeyDown(wxKeyEvent &event)
Definition: NumericTextCtrl.cpp:1712
int mDigitW
Definition: NumericTextCtrl.h:214
std::unique_ptr< wxFont > mLabelFont
Definition: NumericTextCtrl.h:211
wxString GetFormatString()
Definition: NumericTextCtrl.cpp:1058
void EnableMenu(bool enable=true)
Definition: NumericTextCtrl.cpp:1345
NumericConverter(Type type, const wxString &formatName=wxEmptyString, double value=0.0f, double sampleRate=1.0f)
Definition: NumericTextCtrl.cpp:615
void SetFormatString(const wxString &formatString)
Definition: NumericTextCtrl.cpp:1314
WX_DECLARE_OBJARRAY(NumericField, NumericFieldArray)
virtual ~NumericConverter()
Definition: NumericTextCtrl.cpp:827
int GetNumBuiltins()
Definition: NumericTextCtrl.cpp:1079
NumericField is a class used in NumericTextCtrl.
Definition: NumericTextCtrl.cpp:229
virtual void ControlsToValue()
Definition: NumericTextCtrl.cpp:944
void SetMaxValue(double maxValue)
Definition: NumericTextCtrl.cpp:1037
int mBorderLeft
Definition: NumericTextCtrl.h:216
int mDigitBoxW
Definition: NumericTextCtrl.h:212
void ValueToControls() override
Definition: NumericTextCtrl.cpp:1891
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED,-1)
int mDigitBoxH
Definition: NumericTextCtrl.h:213
void SetFieldFocus(int)
Definition: NumericTextCtrl.cpp:1840
wxString mValueMask
Definition: NumericTextCtrl.h:125
void OnMouse(wxMouseEvent &event)
Definition: NumericTextCtrl.cpp:1608
Definition: NumericTextCtrl.h:59
int format
Definition: ExportPCM.cpp:56
int mLastField
Definition: NumericTextCtrl.h:224
int GetFocusedField()
Definition: NumericTextCtrl.h:179
NumericConverter provides the advanced formatting control used in the selection bar of Audacity...
Definition: NumericTextCtrl.h:53
void PrintDebugInfo()
Definition: NumericTextCtrl.cpp:802
int mButtonWidth
Definition: NumericTextCtrl.h:222
int GetFocusedDigit()
Definition: NumericTextCtrl.h:180
DigitInfo is a class used in NumericTextCtrl.
Definition: NumericTextCtrl.cpp:271
double mMinValue
Definition: NumericTextCtrl.h:116
double mValue
Definition: NumericTextCtrl.h:114
double mMaxValue
Definition: NumericTextCtrl.h:117
bool mReadOnly
Definition: NumericTextCtrl.h:207
void OnErase(wxEraseEvent &event)
Definition: NumericTextCtrl.cpp:1499
void Updated(bool keyup=false)
Definition: NumericTextCtrl.cpp:1869
wxString mValueTemplate
Definition: NumericTextCtrl.h:124
void ControlsToValue() override
Definition: NumericTextCtrl.cpp:1909
void Increment()
Definition: NumericTextCtrl.cpp:1122
wxString GetString()
Definition: NumericTextCtrl.cpp:1115
bool mNtscDrop
Definition: NumericTextCtrl.h:131
int mNBuiltins
Definition: NumericTextCtrl.h:137
void SetValue(double newValue)
Definition: NumericTextCtrl.cpp:1333
double mInvalidValue
Definition: NumericTextCtrl.h:118
Definition: NumericTextCtrl.h:141
Definition: NumericTextCtrl.h:60
wxString GetBuiltinFormat(const int index)
Definition: NumericTextCtrl.cpp:1092
void SetMinValue(double minValue)
Definition: NumericTextCtrl.cpp:1023
int mDefaultNdx
Definition: NumericTextCtrl.h:138
int mDigitH
Definition: NumericTextCtrl.h:215
wxString mFormatString
Definition: NumericTextCtrl.h:120
bool mMenuEnabled
Definition: NumericTextCtrl.h:206
void OnCaptureKey(wxCommandEvent &event)
Definition: NumericTextCtrl.cpp:1660
DigitInfoArray mDigits
Definition: NumericTextCtrl.h:134
void SetInvalidValue(double invalidValue)
Definition: NumericTextCtrl.cpp:1363
double mScrollRemainder
Definition: NumericTextCtrl.h:230
const wxChar * name
Definition: Distortion.cpp:94
void Fit() override
Definition: NumericTextCtrl.cpp:1488
bool Layout() override
Definition: NumericTextCtrl.cpp:1372
wxString mValueString
Definition: NumericTextCtrl.h:127
void ResetMinValue()
Definition: NumericTextCtrl.cpp:1032
double mSampleRate
Definition: NumericTextCtrl.h:130
const std::vector< BuiltinFormatString > & mBuiltinFormatStrings
Definition: NumericTextCtrl.h:136
wxString GetBuiltinName(const int index)
Definition: NumericTextCtrl.cpp:1084
void UpdateAutoFocus()
Definition: NumericTextCtrl.cpp:1294
double mScalingFactor
Definition: NumericTextCtrl.h:129
void SetValue(double newValue)
Definition: NumericTextCtrl.cpp:1016
int mFocusedDigit
Definition: NumericTextCtrl.h:133
Type
Definition: NumericTextCtrl.h:57
NumericConverter::Type mType
Definition: NumericTextCtrl.h:232
void OnKeyUp(wxKeyEvent &event)
Definition: NumericTextCtrl.cpp:1694
void ResetMaxValue()
Definition: NumericTextCtrl.cpp:1047
NumericFieldArray mFields
Definition: NumericTextCtrl.h:122
virtual void ValueToControls()
Definition: NumericTextCtrl.cpp:831
virtual void ParseFormatString(const wxString &format)
Definition: NumericTextCtrl.cpp:652
struct to hold a formatting control string and its user facing name Used in an array to hold the buil...
Definition: NumericTextCtrl.cpp:199
int mWidth
Definition: NumericTextCtrl.h:220
void Decrement()
Definition: NumericTextCtrl.cpp:1128
void SetSampleRate(double sampleRate)
Definition: NumericTextCtrl.cpp:1008
int mHeight
Definition: NumericTextCtrl.h:221
int mBorderTop
Definition: NumericTextCtrl.h:217
void OnPaint(wxPaintEvent &event)
Definition: NumericTextCtrl.cpp:1504
void OnFocus(wxFocusEvent &event)
Definition: NumericTextCtrl.cpp:1648