Audacity  2.2.2
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/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 
48 class DigitInfo;
49 
50 class NumericConverter /* not final */
51 {
52 public:
53 
54  enum Type {
58  };
59 
61  const wxString & formatName = wxEmptyString,
62  double value = 0.0f,
63  double sampleRate = 1.0f /* to prevent div by 0 */);
64 
65  virtual ~NumericConverter();
66 
67  // ValueToControls() formats a raw value (either provided as
68  // argument, or mValue, depending on the version of the function
69  // called). The result is stored to mValueString.
70  virtual void ValueToControls();
71  virtual void ValueToControls(double rawValue, bool nearest = true);
72 
73  // Converts the stored formatted string (mValueString) back to a
74  // raw value (mValue).
75  virtual void ControlsToValue();
76 
77  virtual void ParseFormatString(const wxString & format);
78 
79  void PrintDebugInfo();
80  void SetFormatName(const wxString & formatName);
81  void SetFormatString(const wxString & formatString);
82  void SetSampleRate(double sampleRate);
83  void SetValue(double newValue);
84  void SetMinValue(double minValue);
85  void ResetMinValue();
86  void SetMaxValue(double maxValue);
87  void ResetMaxValue();
88 
89  double GetValue();
90 
91  wxString GetString();
92 
93  wxString GetFormatString();
94  int GetFormatIndex();
95 
96  int GetNumBuiltins();
97  wxString GetBuiltinName(const int index);
98  wxString GetBuiltinFormat(const int index);
99  wxString GetBuiltinFormat(const wxString & name);
100 
101  // Adjust the value by the number "steps" in the active format.
102  // Increment if "dir" is 1, decrement if "dir" is -1.
103  void Adjust(int steps, int dir);
104 
105  void Increment();
106  void Decrement();
107 
108 protected:
110 
111  double mValue;
112 
113  double mMinValue;
114  double mMaxValue;
116 
117  wxString mFormatString;
118 
119  std::vector<NumericField> mFields;
120  wxString mPrefix;
121  wxString mValueTemplate;
122  wxString mValueMask;
123  // Formatted mValue, by ValueToControls().
124  wxString mValueString;
125 
127  double mSampleRate;
128  bool mNtscDrop;
129 
131  std::vector<DigitInfo> mDigits;
132 
133  const std::vector<BuiltinFormatString> &mBuiltinFormatStrings;
136 };
137 
138 class NumericTextCtrl final : public wxControl, public NumericConverter
139 {
140  friend class NumericTextCtrlAx;
141 
142  public:
143  DECLARE_DYNAMIC_CLASS(NumericTextCtrl)
144 
145  struct Options {
146  bool autoPos { false };
147  bool readOnly { false };
148  bool menuEnabled { true };
149  bool hasInvalidValue { false };
150  double invalidValue { -1.0 };
151  wxString format {};
152  bool hasValue { false };
153  double value{ -1.0 };
154 
155  Options() {}
156 
157  Options &AutoPos (bool value) { autoPos = value; return *this; }
158  Options &ReadOnly (bool value) { readOnly = value; return *this; }
159  Options &MenuEnabled (bool value) { menuEnabled = value; return *this; }
160  Options &InvalidValue (bool has, double value = -1.0)
161  { hasInvalidValue = has, invalidValue = value; return *this; }
162  Options &Format (const wxString &value) { format = value; return *this; }
163  Options &Value (bool has, double v)
164  { hasValue = has, value = v; return *this; }
165  };
166 
167  NumericTextCtrl(wxWindow *parent, wxWindowID winid,
169  const wxString &formatName = wxEmptyString,
170  double value = 0.0,
171  double sampleRate = 44100,
172  const Options &options = {},
173  const wxPoint &pos = wxDefaultPosition,
174  const wxSize &size = wxDefaultSize);
175 
176  virtual ~NumericTextCtrl();
177 
178  bool Layout() override;
179  void Fit() override;
180 
181  void SetSampleRate(double sampleRate);
182  void SetValue(double newValue);
183  void SetFormatString(const wxString & formatString);
184  void SetFormatName(const wxString & formatName);
185 
186  void SetFieldFocus(int /* digit */);
187 
188  void SetReadOnly(bool readOnly = true);
189  void EnableMenu(bool enable = true);
190 
191  // The text control permits typing DELETE to make the value invalid only if this
192  // function has previously been called.
193  // Maybe you want something other than the default of -1 to indicate the invalid value
194  // this control returns to the program, so you can specify.
195  void SetInvalidValue(double invalidValue);
196 
197  int GetFocusedField() { return mLastField; }
198  int GetFocusedDigit() { return mFocusedDigit; }
199 
200 private:
201 
202  void OnCaptureKey(wxCommandEvent &event);
203  void OnKeyDown(wxKeyEvent &event);
204  void OnKeyUp(wxKeyEvent &event);
205  void OnMouse(wxMouseEvent &event);
206  void OnErase(wxEraseEvent &event);
207  void OnPaint(wxPaintEvent &event);
208  void OnFocus(wxFocusEvent &event);
209  void OnContext(wxContextMenuEvent &event);
210 
211  // Formats mValue into mValueString, using the method of the base class.
212  // Triggers a refresh of the wx window only when the value actually
213  // changed since last time a refresh was triggered.
214  void ValueToControls() override;
215  void ControlsToValue() override;
216 
217  // If autoPos was enabled, focus the first non-zero digit
218  void UpdateAutoFocus();
219 
220  void Updated(bool keyup = false);
221 
222 private:
223 
225  bool mReadOnly;
226 
227  std::unique_ptr<wxBitmap> mBackgroundBitmap;
228 
229  std::unique_ptr<wxFont> mDigitFont, mLabelFont;
232  int mDigitW;
233  int mDigitH;
238  int mWidth;
239  int mHeight;
241 
243 
244  // If true, the focus will be set to the first non-zero digit
245  bool mAutoPos;
246 
247  // Keeps track of extra fractional scrollwheel steps
249 
251 
253 
254  DECLARE_EVENT_TABLE()
255 };
256 
257 #if wxUSE_ACCESSIBILITY
258 
259 class NumericTextCtrlAx final : public wxWindowAccessible
260 {
261 public:
263 
264  virtual ~ NumericTextCtrlAx();
265 
266  // Performs the default action. childId is 0 (the action for this object)
267  // or > 0 (the action for a child).
268  // Return wxACC_NOT_SUPPORTED if there is no default action for this
269  // window (e.g. an edit control).
270  wxAccStatus DoDefaultAction(int childId) override;
271 
272  // Retrieves the address of an IDispatch interface for the specified child.
273  // All objects must support this property.
274  wxAccStatus GetChild(int childId, wxAccessible **child) override;
275 
276  // Gets the number of children.
277  wxAccStatus GetChildCount(int *childCount) override;
278 
279  // Gets the default action for this object (0) or > 0 (the action for a child).
280  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
281  // string if there is no action.
282  // The retrieved string describes the action that is performed on an object,
283  // not what the object does as a result. For example, a toolbar button that prints
284  // a document has a default action of "Press" rather than "Prints the current document."
285  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
286 
287  // Returns the description for this object or a child.
288  wxAccStatus GetDescription(int childId, wxString *description) override;
289 
290  // Gets the window with the keyboard focus.
291  // If childId is 0 and child is NULL, no object in
292  // this subhierarchy has the focus.
293  // If this object has the focus, child should be 'this'.
294  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
295 
296  // Returns help text for this object or a child, similar to tooltip text.
297  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
298 
299  // Returns the keyboard shortcut for this object or child.
300  // Return e.g. ALT+K
301  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
302 
303  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
304  // rect is in screen coordinates.
305  wxAccStatus GetLocation(wxRect & rect, int elementId) override;
306 
307  // Gets the name of the specified object.
308  wxAccStatus GetName(int childId, wxString *name) override;
309 
310  // Returns a role constant.
311  wxAccStatus GetRole(int childId, wxAccRole *role) override;
312 
313  // Gets a variant representing the selected children
314  // of this object.
315  // Acceptable values:
316  // - a null variant (IsNull() returns TRUE)
317  // - a list variant (GetType() == wxT("list"))
318  // - an integer representing the selected child element,
319  // or 0 if this object is selected (GetType() == wxT("long"))
320  // - a "void*" pointer to a wxAccessible child object
321  wxAccStatus GetSelections(wxVariant *selections) override;
322 
323  // Returns a state constant.
324  wxAccStatus GetState(int childId, long *state) override;
325 
326  // Returns a localized string representing the value for the object
327  // or child.
328  wxAccStatus GetValue(int childId, wxString *strValue) override;
329 
330 private:
331  NumericTextCtrl *mCtrl;
332  int mLastField;
333  int mLastDigit;
334 };
335 
336 #endif // wxUSE_ACCESSIBILITY
337 
338 #endif // __AUDACITY_TIME_TEXT_CTRL__
void SetFormatName(const wxString &formatName)
void SetFormatString(const wxString &formatString)
Options & InvalidValue(bool has, double value=-1.0)
void Adjust(int steps, int dir)
std::vector< NumericField > mFields
std::unique_ptr< wxFont > mDigitFont
Options & Value(bool has, double v)
NumericTextCtrl(wxWindow *parent, wxWindowID winid, NumericConverter::Type type, const wxString &formatName=wxEmptyString, double value=0.0, double sampleRate=44100, const Options &options={}, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize)
std::unique_ptr< wxBitmap > mBackgroundBitmap
void SetFormatName(const wxString &formatName)
void OnContext(wxContextMenuEvent &event)
NumericTextCtrlAx gives the NumericTextCtrl Accessibility.
void SetReadOnly(bool readOnly=true)
void SetSampleRate(double sampleRate)
void OnKeyDown(wxKeyEvent &event)
std::unique_ptr< wxFont > mLabelFont
wxString GetFormatString()
void EnableMenu(bool enable=true)
NumericConverter(Type type, const wxString &formatName=wxEmptyString, double value=0.0f, double sampleRate=1.0f)
void SetFormatString(const wxString &formatString)
std::vector< DigitInfo > mDigits
virtual ~NumericConverter()
NumericField is a class used in NumericTextCtrl.
virtual void ControlsToValue()
void SetMaxValue(double maxValue)
void ValueToControls() override
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED,-1)
void OnMouse(wxMouseEvent &event)
int format
Definition: ExportPCM.cpp:56
NumericConverter provides the advanced formatting control used in the selection bar of Audacity...
Options & MenuEnabled(bool value)
DigitInfo is a class used in NumericTextCtrl.
void OnErase(wxEraseEvent &event)
void Updated(bool keyup=false)
void ControlsToValue() override
void SetValue(double newValue)
wxString GetBuiltinFormat(const int index)
void SetMinValue(double minValue)
void OnCaptureKey(wxCommandEvent &event)
Options & ReadOnly(bool value)
void SetInvalidValue(double invalidValue)
const wxChar * name
Definition: Distortion.cpp:94
void Fit() override
bool Layout() override
const std::vector< BuiltinFormatString > & mBuiltinFormatStrings
wxString GetBuiltinName(const int index)
Options & Format(const wxString &value)
Options & AutoPos(bool value)
void SetValue(double newValue)
NumericConverter::Type mType
void OnKeyUp(wxKeyEvent &event)
virtual void ValueToControls()
virtual void ParseFormatString(const wxString &format)
struct to hold a formatting control string and its user facing name Used in an array to hold the buil...
virtual ~NumericTextCtrl()
void SetSampleRate(double sampleRate)
void OnPaint(wxPaintEvent &event)
void OnFocus(wxFocusEvent &event)