Audacity  3.0.3
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 
19 
20 #include "MemoryX.h"
21 #include "ComponentInterface.h"
23 #include <vector>
24 #include <wx/setup.h> // for wxUSE_* macros
25 #include <wx/defs.h>
26 #include <wx/control.h> // to inherit
27 
28 #include "Internat.h"
29 
30 // One event type for each type of control. Event is raised when a control
31 // changes its format. Owners of controls of the same type can listen and
32 // update their formats to agree.
33 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED, -1);
34 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_FREQUENCYTEXTCTRL_UPDATED, -1);
35 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_BANDWIDTHTEXTCTRL_UPDATED,
36  -1);
37 
41 struct BuiltinFormatString;
42 
43 class NumericField;
44 
45 class DigitInfo;
46 
47 class AUDACITY_DLL_API NumericConverter /* not final */
48 {
49 public:
50 
51  enum Type {
53  ATIME, // for Audio time control.
56  };
57 
58  struct FormatStrings {
60  // How to name the fraction of the unit; not necessary for time formats
61  // or when the format string has no decimal point
63 
65  const TranslatableString &format = {},
66  const TranslatableString &fraction = {})
67  : formatStr{ format }, fraction{ fraction }
68  {}
69 
70  friend bool operator == ( const FormatStrings &x, const FormatStrings &y )
71  { return x.formatStr == y.formatStr && x.fraction == y.fraction; }
72  friend bool operator != ( const FormatStrings &x, const FormatStrings &y )
73  { return !(x == y); }
74  };
75 
76  static NumericFormatSymbol DefaultSelectionFormat();
77  static NumericFormatSymbol TimeAndSampleFormat();
78  static NumericFormatSymbol SecondsFormat();
79  static NumericFormatSymbol HoursMinsSecondsFormat();
80  static NumericFormatSymbol HundredthsFormat();
81  static NumericFormatSymbol HertzFormat();
82 
83  static NumericFormatSymbol LookupFormat( Type type, const wxString& id);
84 
85  NumericConverter(Type type,
86  const NumericFormatSymbol & formatName = {},
87  double value = 0.0f,
88  double sampleRate = 1.0f /* to prevent div by 0 */);
90 
91  virtual ~NumericConverter();
92 
93  // ValueToControls() formats a raw value (either provided as
94  // argument, or mValue, depending on the version of the function
95  // called). The result is stored to mValueString.
96  virtual void ValueToControls();
97  virtual void ValueToControls(double rawValue, bool nearest = true);
98 
99  // Converts the stored formatted string (mValueString) back to a
100  // raw value (mValue).
101  virtual void ControlsToValue();
102 
103 private:
104  void ParseFormatString(const TranslatableString & untranslatedFormat);
105 
106 public:
107  void PrintDebugInfo();
108 
109  // returns true iff the format name really changed:
110  bool SetFormatName(const NumericFormatSymbol & formatName);
111 
112  // returns true iff the format string really changed:
113  bool SetFormatString(const FormatStrings & formatString);
114 
115  void SetSampleRate(double sampleRate);
116  void SetValue(double newValue);
117  void SetMinValue(double minValue);
118  void ResetMinValue();
119  void SetMaxValue(double maxValue);
120  void ResetMaxValue();
121 
122  double GetValue();
123 
124  wxString GetString();
125 
126  int GetFormatIndex();
127 
128  int GetNumBuiltins();
129  NumericFormatSymbol GetBuiltinName(const int index);
130  FormatStrings GetBuiltinFormat(const int index);
131  FormatStrings GetBuiltinFormat(const NumericFormatSymbol & name);
132 
133  // Adjust the value by the number "steps" in the active format.
134  // Increment if "dir" is 1, decrement if "dir" is -1.
135  void Adjust(int steps, int dir);
136 
137  void Increment();
138  void Decrement();
139 
140 protected:
142 
143  double mValue;
144 
145  double mMinValue;
146  double mMaxValue;
148 
150 
151  std::vector<NumericField> mFields;
152  wxString mPrefix;
153  wxString mValueTemplate;
154  wxString mValueMask;
155  // Formatted mValue, by ValueToControls().
156  wxString mValueString;
157 
159  double mSampleRate;
160  bool mNtscDrop;
161 
163  std::vector<DigitInfo> mDigits;
164 
166  const size_t mNBuiltins;
168 };
169 
170 class AUDACITY_DLL_API NumericTextCtrl final
171  : public wxControl, public NumericConverter
172 {
173  friend class NumericTextCtrlAx;
174 
175  public:
176  DECLARE_DYNAMIC_CLASS(NumericTextCtrl)
177 
178  struct Options {
179  bool autoPos { true };
180  bool readOnly { false };
181  bool menuEnabled { true };
182  bool hasInvalidValue { false };
183  double invalidValue { -1.0 };
185  bool hasValue { false };
186  double value{ -1.0 };
187 
188  Options() {}
189 
190  Options &AutoPos (bool enable) { autoPos = enable; return *this; }
191  Options &ReadOnly (bool enable) { readOnly = enable; return *this; }
192  Options &MenuEnabled (bool enable) { menuEnabled = enable; return *this; }
193  Options &InvalidValue (bool has, double v = -1.0)
194  { hasInvalidValue = has, invalidValue = v; return *this; }
195  // use a custom format not in the tables:
197  { format = f; return *this; }
198  Options &Value (bool has, double v)
199  { hasValue = has, value = v; return *this; }
200  };
201 
202  NumericTextCtrl(wxWindow *parent, wxWindowID winid,
204  const NumericFormatSymbol &formatName = {},
205  double value = 0.0,
206  double sampleRate = 44100,
207  const Options &options = {},
208  const wxPoint &pos = wxDefaultPosition,
209  const wxSize &size = wxDefaultSize);
210 
211  virtual ~NumericTextCtrl();
212 
213  // Hide the inherited function that takes wxString
214  void SetName( const TranslatableString &name );
215 
216  wxSize ComputeSizing(bool update = true, wxCoord digitW = 0, wxCoord digitH = 0);
217  bool Layout() override;
218  void Fit() override;
219 
220  void SetSampleRate(double sampleRate);
221  void SetValue(double newValue);
222 
223  // returns true iff the format string really changed:
224  bool SetFormatString(const FormatStrings & formatString);
225 
226  // returns true iff the format name really changed:
227  bool SetFormatName(const NumericFormatSymbol & formatName);
228 
229  void SetFieldFocus(int /* digit */);
230 
231  wxSize GetDimensions() { return wxSize(mWidth + mButtonWidth, mHeight); }
232  wxSize GetDigitSize() { return wxSize(mDigitBoxW, mDigitBoxH); }
233  void SetDigitSize(int width, int height);
234  void SetReadOnly(bool readOnly = true);
235  void EnableMenu(bool enable = true);
236 
237  // The text control permits typing DELETE to make the value invalid only if this
238  // function has previously been called.
239  // Maybe you want something other than the default of -1 to indicate the invalid value
240  // this control returns to the program, so you can specify.
241  void SetInvalidValue(double invalidValue);
242 
243  int GetFocusedField() { return mLastField; }
244  int GetFocusedDigit() { return mFocusedDigit; }
245 
246 private:
247 
248  void OnCaptureKey(wxCommandEvent &event);
249  void OnKeyDown(wxKeyEvent &event);
250  void OnKeyUp(wxKeyEvent &event);
251  void OnMouse(wxMouseEvent &event);
252  void OnErase(wxEraseEvent &event);
253  void OnPaint(wxPaintEvent &event);
254  void OnFocus(wxFocusEvent &event);
255  void OnContext(wxContextMenuEvent &event);
256 
257  // Formats mValue into mValueString, using the method of the base class.
258  // Triggers a refresh of the wx window only when the value actually
259  // changed since last time a refresh was triggered.
260  void ValueToControls() override;
261  void ControlsToValue() override;
262 
263  // If autoPos was enabled, focus the first non-zero digit
264  void UpdateAutoFocus();
265 
266  void Updated(bool keyup = false);
267 
268 private:
269 
271  bool mReadOnly;
272 
273  std::unique_ptr<wxBitmap> mBackgroundBitmap;
274 
275  std::unique_ptr<wxFont> mDigitFont, mLabelFont;
278  int mDigitW;
279  int mDigitH;
284  int mWidth;
285  int mHeight;
287 
289 
290  // If true, the focus will be set to the first non-zero digit
291  bool mAutoPos;
292 
293  // Keeps track of extra fractional scrollwheel steps
295 
297 
299 
300  DECLARE_EVENT_TABLE()
301 };
302 
303 #endif // __AUDACITY_TIME_TEXT_CTRL__
size
size_t size
Definition: ffmpeg-2.3.6-single-header.h:412
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
NumericConverter::SetFormatString
bool SetFormatString(const FormatStrings &formatString)
Definition: NumericTextCtrl.cpp:1115
DigitInfo
DigitInfo is a class used in NumericTextCtrl.
Definition: NumericTextCtrl.cpp:347
NumericTextCtrl::Options::AutoPos
Options & AutoPos(bool enable)
Definition: NumericTextCtrl.h:190
NumericConverter::SetFormatName
bool SetFormatName(const NumericFormatSymbol &formatName)
Definition: NumericTextCtrl.cpp:1109
NumericField
NumericField is a class used in NumericTextCtrl.
Definition: NumericTextCtrl.cpp:301
NumericTextCtrl::mLastField
int mLastField
Definition: NumericTextCtrl.h:288
NumericTextCtrl::mBorderTop
int mBorderTop
Definition: NumericTextCtrl.h:281
operator!=
bool operator!=(const Tags &lhs, const Tags &rhs)
Definition: Tags.h:140
BuiltinFormatString
struct to hold a formatting control string and its user facing name Used in an array to hold the buil...
Definition: NumericTextCtrl.cpp:286
NumericConverter::FormatStrings::fraction
TranslatableString fraction
Definition: NumericTextCtrl.h:62
NumericTextCtrl::GetDigitSize
wxSize GetDigitSize()
Definition: NumericTextCtrl.h:232
NumericConverter::mFields
std::vector< NumericField > mFields
Definition: NumericTextCtrl.h:151
NumericTextCtrl::Options::Value
Options & Value(bool has, double v)
Definition: NumericTextCtrl.h:198
NumericConverter::mScalingFactor
double mScalingFactor
Definition: NumericTextCtrl.h:158
DECLARE_EXPORTED_EVENT_TYPE
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED, -1)
NumericConverter::ATIME
@ ATIME
Definition: NumericTextCtrl.h:53
ComponentInterfaceSymbol.h
NumericConverter::mNBuiltins
const size_t mNBuiltins
Definition: NumericTextCtrl.h:166
NumericConverter::mValue
double mValue
Definition: NumericTextCtrl.h:143
NumericConverter::mValueMask
wxString mValueMask
Definition: NumericTextCtrl.h:154
NumericTextCtrl::mBorderBottom
int mBorderBottom
Definition: NumericTextCtrl.h:283
NumericTextCtrl::mBorderLeft
int mBorderLeft
Definition: NumericTextCtrl.h:280
NumericConverter::ValueToControls
virtual void ValueToControls()
Definition: NumericTextCtrl.cpp:947
NumericTextCtrl
Definition: NumericTextCtrl.h:172
NumericConverter::mDigits
std::vector< DigitInfo > mDigits
Definition: NumericTextCtrl.h:163
NumericConverter::mBuiltinFormatStrings
const BuiltinFormatString * mBuiltinFormatStrings
Definition: NumericTextCtrl.h:165
NumericTextCtrl::GetFocusedDigit
int GetFocusedDigit()
Definition: NumericTextCtrl.h:244
NumericConverter::mPrefix
wxString mPrefix
Definition: NumericTextCtrl.h:152
NumericTextCtrlAx
NumericTextCtrlAx gives the NumericTextCtrl Accessibility.
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
NumericTextCtrl::Options
Definition: NumericTextCtrl.h:178
NumericConverter::BANDWIDTH
@ BANDWIDTH
Definition: NumericTextCtrl.h:55
operator==
bool operator==(const Tags &lhs, const Tags &rhs)
Definition: Tags.cpp:361
NumericConverter::FormatStrings::formatStr
TranslatableString formatStr
Definition: NumericTextCtrl.h:59
NumericConverter::mNtscDrop
bool mNtscDrop
Definition: NumericTextCtrl.h:160
NumericTextCtrl::mReadOnly
bool mReadOnly
Definition: NumericTextCtrl.h:271
NumericConverter::NumericConverter
NumericConverter(const NumericConverter &)
NumericConverter::mInvalidValue
double mInvalidValue
Definition: NumericTextCtrl.h:147
NumericTextCtrl::mType
NumericConverter::Type mType
Definition: NumericTextCtrl.h:296
NumericTextCtrl::Options::Options
Options()
Definition: NumericTextCtrl.h:188
NumericTextCtrl::Options::InvalidValue
Options & InvalidValue(bool has, double v=-1.0)
Definition: NumericTextCtrl.h:193
NumericConverter::mSampleRate
double mSampleRate
Definition: NumericTextCtrl.h:159
ComponentInterface.h
NumericTextCtrl::mWidth
int mWidth
Definition: NumericTextCtrl.h:284
NumericConverter::ControlsToValue
virtual void ControlsToValue()
Definition: NumericTextCtrl.cpp:1058
NumericTextCtrl::mHeight
int mHeight
Definition: NumericTextCtrl.h:285
NumericTextCtrl::mDigitH
int mDigitH
Definition: NumericTextCtrl.h:279
NumericConverter::mType
Type mType
Definition: NumericTextCtrl.h:141
NumericTextCtrl::mDigitBoxW
int mDigitBoxW
Definition: NumericTextCtrl.h:276
NumericTextCtrl::mDigitBoxH
int mDigitBoxH
Definition: NumericTextCtrl.h:277
NumericConverter::mMinValue
double mMinValue
Definition: NumericTextCtrl.h:145
name
const TranslatableString name
Definition: Distortion.cpp:98
NumericConverter::TIME
@ TIME
Definition: NumericTextCtrl.h:52
format
int format
Definition: ExportPCM.cpp:56
NumericTextCtrl::Options::Format
Options & Format(const FormatStrings &f)
Definition: NumericTextCtrl.h:196
NumericTextCtrl::mMenuEnabled
bool mMenuEnabled
Definition: NumericTextCtrl.h:270
NumericTextCtrl::Options::MenuEnabled
Options & MenuEnabled(bool enable)
Definition: NumericTextCtrl.h:192
NumericConverter::mMaxValue
double mMaxValue
Definition: NumericTextCtrl.h:146
NumericConverter::FREQUENCY
@ FREQUENCY
Definition: NumericTextCtrl.h:54
Internat.h
NumericConverter::FormatStrings::FormatStrings
FormatStrings(const TranslatableString &format={}, const TranslatableString &fraction={})
Definition: NumericTextCtrl.h:64
NumericConverter::FormatStrings
Definition: NumericTextCtrl.h:58
NumericTextCtrl::GetDimensions
wxSize GetDimensions()
Definition: NumericTextCtrl.h:231
NumericTextCtrl::mScrollRemainder
double mScrollRemainder
Definition: NumericTextCtrl.h:294
NumericConverter::mDefaultNdx
int mDefaultNdx
Definition: NumericTextCtrl.h:167
NumericConverter
NumericConverter provides the advanced formatting control used in the selection bar of Audacity.
Definition: NumericTextCtrl.h:48
NumericConverter::mFormatString
FormatStrings mFormatString
Definition: NumericTextCtrl.h:149
NumericConverter::mValueTemplate
wxString mValueTemplate
Definition: NumericTextCtrl.h:153
NumericTextCtrl::mLabelFont
std::unique_ptr< wxFont > mLabelFont
Definition: NumericTextCtrl.h:275
NumericConverter::SetValue
void SetValue(double newValue)
Definition: NumericTextCtrl.cpp:1136
MemoryX.h
NumericTextCtrl::Options::ReadOnly
Options & ReadOnly(bool enable)
Definition: NumericTextCtrl.h:191
NumericTextCtrl::mBackgroundBitmap
std::unique_ptr< wxBitmap > mBackgroundBitmap
Definition: NumericTextCtrl.h:273
NumericTextCtrl::GetFocusedField
int GetFocusedField()
Definition: NumericTextCtrl.h:243
KeyboardCapture::OnFocus
void OnFocus(wxWindow &window, wxFocusEvent &event)
a function useful to implement a focus event handler The window releases the keyboard if the event is...
Definition: KeyboardCapture.cpp:95
NumericConverter::mFocusedDigit
int mFocusedDigit
Definition: NumericTextCtrl.h:162
NumericConverter::SetSampleRate
void SetSampleRate(double sampleRate)
Definition: NumericTextCtrl.cpp:1128
NumericTextCtrl::mAllowInvalidValue
bool mAllowInvalidValue
Definition: NumericTextCtrl.h:298
NumericConverter::Type
Type
Definition: NumericTextCtrl.h:51
NumericTextCtrl::mAutoPos
bool mAutoPos
Definition: NumericTextCtrl.h:291
NumericTextCtrl::mDigitW
int mDigitW
Definition: NumericTextCtrl.h:278
anonymous_namespace{Menus.cpp}::Options
std::vector< CommandFlagOptions > & Options()
Definition: Menus.cpp:527
NumericTextCtrl::mButtonWidth
int mButtonWidth
Definition: NumericTextCtrl.h:286
NumericConverter::mValueString
wxString mValueString
Definition: NumericTextCtrl.h:156
NumericTextCtrl::mBorderRight
int mBorderRight
Definition: NumericTextCtrl.h:282