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 NumericFormatID 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 NumericFormatID &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 NumericFormatID& formatName);
93 // returns true iff the format name really changed:
94 bool SetFormatName(const NumericFormatID &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 GetFocusedDigit() { return mFocusedDigit; }
112
113private:
114 void OnFormatUpdated(bool resetFocus) override;
115 void HandleFormatterChanged(bool resetFocus);
116
117 void OnCaptureKey(wxCommandEvent &event);
118 void OnKeyDown(wxKeyEvent &event);
119 void OnKeyUp(wxKeyEvent &event);
120 void OnMouse(wxMouseEvent &event);
121 void OnErase(wxEraseEvent &event);
122 void OnPaint(wxPaintEvent &event);
123 void OnFocus(wxFocusEvent &event);
124 void OnContext(wxContextMenuEvent &event);
125
126 // Formats mValue into mValueString, using the method of the base class.
127 // Triggers a refresh of the wx window only when the value actually
128 // changed since last time a refresh was triggered.
129 void ValueToControls() override;
130 void ControlsToValue() override;
131
132 // If autoPos was enabled, focus the first non-zero digit
133 void UpdateAutoFocus();
134
135 void Updated(bool keyup = false);
136
137private:
138 struct FieldPosition final
139 {
140 int fieldX; // x-position of the field on-screen
141 int fieldW; // width of the field on-screen
142 int labelX; // x-position of the label on-screen
143 };
144
145 std::vector<wxRect> mBoxes;
146 std::vector<FieldPosition> mFieldPositions;
147
148 wxRect GetBox(size_t ii) const;
149
152
153 std::unique_ptr<wxBitmap> mBackgroundBitmap;
154
155 std::unique_ptr<wxFont> mDigitFont, mLabelFont;
167
168 int mFocusedDigit { 0 };
169
170 // If true, the focus will be set to the first non-zero digit
172
173 // Keeps track of extra fractional scrollwheel steps
175
177
179
180 DECLARE_EVENT_TABLE()
181};
182
183#endif // __AUDACITY_TIME_TEXT_CTRL__
const TranslatableString name
Definition: Distortion.cpp:76
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_TIMETEXTCTRL_UPDATED, -1)
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()
virtual void OnFormatUpdated(bool resetFocus)
bool SetCustomFormat(const TranslatableString &customFormat)
void SetValue(double newValue)
bool SetTypeAndFormatName(const NumericConverterType &type, const NumericFormatID &formatName)
bool SetFormatName(const NumericFormatID &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 & Value(bool has, double v)
Options & CustomFormat(const TranslatableString &f)
Options & AutoPos(bool enable)
Options & InvalidValue(bool has, double v=-1.0)
Options & FormatSymbol(const NumericFormatID &f)
Options & ReadOnly(bool enable)