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