Audacity  2.3.1
ASlider.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  ASlider.h
6 
7  Dominic Mazzoni
8 
9  This class is a custom slider.
10 
11 **********************************************************************/
12 
13 #ifndef __AUDACITY_SLIDER__
14 #define __AUDACITY_SLIDER__
15 
16 #include "../Experimental.h"
17 
18 #include "../MemoryX.h"
19 #include <wx/defs.h>
20 #include <wx/window.h>
21 #include <wx/panel.h>
22 #include <wx/timer.h>
23 #include "widgets/wxPanelWrapper.h"
24 
25 #if wxUSE_ACCESSIBILITY
26 #include <wx/access.h>
27 #include "WindowAccessible.h"
28 #endif
29 
30 class wxBitmap;
31 class wxCursor;
32 class wxImage;
33 class wxSize;
34 class wxPoint;
35 class wxTextCtrl;
36 class wxButton;
37 
38 class Ruler;
39 class TipPanel;
40 
41 //
42 // Predefined slider types (mStyle)
43 //
44 #define FRAC_SLIDER 1 // 0.0...1.0
45 #define DB_SLIDER 2 // -36...36 dB
46 #define PAN_SLIDER 3 // -1.0...1.0
47 #define SPEED_SLIDER 4 // 0.01 ..3.0
48 
49 #define VEL_SLIDER 5 // -50..50
50 
51 #define DB_MIN -36.0f
52 #define DB_MAX 36.0f
53 #define FRAC_MIN 0.0f
54 #define FRAC_MAX 1.0f
55 #define SPEED_MIN 0.01f
56 #define SPEED_MAX 3.0f
57 #define VEL_MIN -50.0f
58 #define VEL_MAX 50.0f
59 
60 // Customizable slider only: If stepValue is STEP_CONTINUOUS,
61 // every value on the slider between minValue and maxValue
62 // will be possible
63 //
64 #define STEP_CONTINUOUS 0.0f
65 
66 //
67 // Lightweight slider - i.e. a slider that doesn't appear in
68 // its own window, but rather draws itself inside an existing
69 // window (used inside Track Labels). The ASlider class,
70 // which uses this class, is below.
71 //
72 
73 class LWSlider
74 {
75  friend class ASlider;
76  friend class ASliderAx;
77 
78  public:
79 
80  // MM: Construct customizable slider
81  LWSlider(wxWindow * parent,
82  const wxString &name,
83  const wxPoint &pos,
84  const wxSize &size,
85  float minValue,
86  float maxValue,
87  float stepValue,
88  bool canUseShift,
89  int style,
90  bool heavyweight=false,
91  bool popup=true,
92  int orientation = wxHORIZONTAL); // wxHORIZONTAL or wxVERTICAL. wxVERTICAL is currently only for DB_SLIDER.
93 
94  // Construct predefined slider
95  LWSlider(wxWindow * parent,
96  const wxString &name,
97  const wxPoint &pos,
98  const wxSize &size,
99  int style,
100  bool heavyweight=false,
101  bool popup=true,
102  int orientation = wxHORIZONTAL); // wxHORIZONTAL or wxVERTICAL. wxVERTICAL is currently only for DB_SLIDER.
103 
104  void Init(wxWindow * parent,
105  const wxString &name,
106  const wxPoint &pos,
107  const wxSize &size,
108  float minValue,
109  float maxValue,
110  float stepValue,
111  bool canUseShift,
112  int style,
113  bool heavyweight,
114  bool popup,
115  float speed,
116  int orientation = wxHORIZONTAL); // wxHORIZONTAL or wxVERTICAL. wxVERTICAL is currently only for DB_SLIDER.
117 
118  virtual ~LWSlider();
119 
120  wxWindowID GetId();
121  void SetId(wxWindowID id);
122 
123  void SetDefaultValue(float value);
124  void SetDefaultShortcut(bool value);
125 
126  void GetScroll(float & line, float & page);
127  void SetScroll(float line, float page);
128 
129  void ShowTip(bool show);
130  void SetToolTipTemplate(const wxString & tip);
131 
132  float Get(bool convert = true);
133  void Set(float value);
134 
135  void Increase(float steps);
136  void Decrease(float steps);
137 
138  // If set to less than 1.0, moving the mouse one pixel will move
139  // the slider by less than 1 unit
140  void SetSpeed(float speed);
141 
142  void Move(const wxPoint &newpos);
143 
144  void AdjustSize(const wxSize & sz);
145 
146  void OnPaint(wxDC &dc, bool highlighted);
147  void OnSize(wxSizeEvent & event);
148  void OnMouseEvent(wxMouseEvent & event);
149  void OnKeyEvent(wxKeyEvent & event);
150  void Refresh();
151 
152  bool ShowDialog();
153  bool ShowDialog(wxPoint pos);
154 
155  void SetEnabled(bool enabled);
156  bool GetEnabled();
157 
158  static void DeleteSharedTipPanel();
159 
160  void SetParent(wxWindow *parent) { mParent = parent; }
161  void SendUpdate(float newValue);
162 
163  private:
164 
165  wxString GetTip(float value) const;
166  wxArrayString GetWidestTips() const;
167  void FormatPopWin();
168  void SetPopWinPosition();
169  void CreatePopWin();
170  void DrawToBitmap(wxDC & dc);
171 
172  bool DoShowDialog(wxPoint pos);
173 
174 
175  int ValueToPosition(float val);
176  float DragPositionToValue(int fromPos, bool shiftDown);
177  float ClickPositionToValue(int fromPos, bool shiftDown);
178 
179  wxWindow *mParent;
180 
181  int mStyle;
182  int mOrientation; // wxHORIZONTAL or wxVERTICAL. wxVERTICAL is currently only for DB_SLIDER.
183 
184  bool mHW; // is it really heavyweight (in a window)
185  bool mPopup; // should display dialog on double click
186 
187  int mLeft;
188  int mTop;
189 
190  int mWidth; //In pixels
191  int mHeight; //In pixels
192 
193  // for (mOrientation == wxHORIZONTAL)
194  int mCenterY;
195 
196  int mLeftX;
197  int mRightX;
198  int mWidthX;
199 
200  // for (mOrientation == wxVERTICAL) //v Vertical PAN_SLIDER currently not handled, forced to horizontal.
201  int mCenterX;
202 
203  int mTopY;
204  int mBottomY; // low values at bottom
205  int mHeightY;
206 
207 
208  int mThumbWidth; //In pixels
209  int mThumbHeight; //In pixels
210 
211  float mClickValue;
212  int mClickPos; // position in x if (mOrientation == wxHORIZONTAL), else in y
213 
214  float mMinValue;
215  float mMaxValue;
216  float mStepValue;
217  float mSpeed;
218 
219  float mScrollLine;
220  float mScrollPage;
221 
223 
226 
228 
229  wxWindowID mID;
230 
231  std::unique_ptr<TipPanel> mTipPanel;
232  wxString mTipTemplate;
233 
235 
236  std::unique_ptr<wxBitmap> mBitmap, mThumbBitmap, mThumbBitmapHilited;
237 
238  wxString mName;
239 
240  bool mEnabled;
241 };
242 
243 class ASlider /* not final */ : public wxPanel
244 {
245  friend class ASliderAx;
246 
247  public:
248  struct Options {
249  Options() {}
250 
252  wxOrientation orientation{ wxHORIZONTAL };
253  bool popup{ true };
254  bool canUseShift{ true };
256 
257  float line{ 1.0 };
258  float page{ 5.0 };
259 
260  Options& Style( int s ) { style = s; return *this; }
261  Options& Orientation( wxOrientation o )
262  { orientation = o; return *this; }
263  Options& Popup( bool p ) { popup = p; return *this; }
264  Options& CanUseShift( bool c ) { canUseShift = c; return *this; }
265  Options& StepValue( float v ) { stepValue = v; return *this; }
266 
267  Options& Line( float l ) { line = l; return *this; }
268  Options& Page( float p ) { page = p; return *this; }
269  };
270 
271  ASlider( wxWindow * parent,
272  wxWindowID id,
273  const wxString &name,
274  const wxPoint & pos,
275  const wxSize & size,
276  const Options &options = Options{});
277  virtual ~ASlider();
278 
279  bool AcceptsFocus() const override { return s_AcceptsFocus; }
280  bool AcceptsFocusFromKeyboard() const override { return true; }
281 
282  void SetFocusFromKbd() override;
283 
284  void GetScroll(float & line, float & page);
285  void SetScroll(float line, float page);
286 
287  void SetToolTipTemplate(const wxString & tip);
288 
289  float Get( bool convert = true );
290  void Set(float value);
291 
292  void Increase(float steps);
293  void Decrease(float steps);
294  bool ShowDialog(wxPoint pos = wxPoint(-1, -1));
295 
296  void SetSpeed(float speed);
297 
298  void OnErase(wxEraseEvent & event);
299  void OnPaint(wxPaintEvent & event);
300  void OnSize(wxSizeEvent & event);
301  void OnMouseEvent(wxMouseEvent & event);
302  void OnCaptureLost(wxMouseCaptureLostEvent & event);
303  void OnKeyEvent(wxKeyEvent &event);
304  void OnSlider(wxCommandEvent &event);
305  void OnSetFocus(wxFocusEvent & event);
306  void OnKillFocus(wxFocusEvent & event);
307  void OnTimer(wxTimerEvent & event);
308 
309  // Overrides of the wxWindow functions with the same semantics
310  bool Enable(bool enable = true) override;
311  bool IsEnabled() const;
312 
313 private:
314  static bool s_AcceptsFocus;
315  struct Resetter { void operator () (bool *p) const { if(p) *p = false; } };
316  using TempAllowFocus = std::unique_ptr<bool, Resetter>;
317 
318 public:
320 
321  private:
322  std::unique_ptr<LWSlider> mLWSlider;
324  wxTimer mTimer;
325 
326  protected:
327  int mStyle;
328 
329  public:
330  DECLARE_EVENT_TABLE()
331 };
332 
333 
334 
335 #define SLIDER_DIALOG_TEXTCTRL 100
336 
337 
338 // This is a modal dialog that contains an ASlider
339 // and a text-entry box which can be used to set the
340 // value of a slider.
341 class SliderDialog final : public wxDialogWrapper
342 {
343  public:
344  SliderDialog(wxWindow * parent, wxWindowID id,
345  const wxString & title,
346  wxPoint position,
347  wxSize size,
348  int style,
349  float value,
350  float line,
351  float page,
352  LWSlider * pSlider=nullptr);
353  ~SliderDialog();
354 
355  float Get();
356 
357  private:
358  bool TransferDataToWindow() override;
359  bool TransferDataFromWindow() override;
360 
361  void OnSlider(wxCommandEvent &event);
362  void OnTextChange(wxCommandEvent &event);
363 
365  wxTextCtrl * mTextCtrl;
366  int mStyle;
368 
369  public:
370  DECLARE_EVENT_TABLE()
371 };
372 
373 
374 #if wxUSE_ACCESSIBILITY
375 
376 class ASliderAx final : public WindowAccessible
377 {
378 public:
379  ASliderAx(wxWindow * window);
380 
381  virtual ~ ASliderAx();
382 
383  // Retrieves the address of an IDispatch interface for the specified child.
384  // All objects must support this property.
385  wxAccStatus GetChild(int childId, wxAccessible** child) override;
386 
387  // Gets the number of children.
388  wxAccStatus GetChildCount(int* childCount) override;
389 
390  // Gets the default action for this object (0) or > 0 (the action for a child).
391  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
392  // string if there is no action.
393  // The retrieved string describes the action that is performed on an object,
394  // not what the object does as a result. For example, a toolbar button that prints
395  // a document has a default action of "Press" rather than "Prints the current document."
396  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
397 
398  // Returns the description for this object or a child.
399  wxAccStatus GetDescription(int childId, wxString *description) override;
400 
401  // Gets the window with the keyboard focus.
402  // If childId is 0 and child is NULL, no object in
403  // this subhierarchy has the focus.
404  // If this object has the focus, child should be 'this'.
405  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
406 
407  // Returns help text for this object or a child, similar to tooltip text.
408  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
409 
410  // Returns the keyboard shortcut for this object or child.
411  // Return e.g. ALT+K
412  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
413 
414  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
415  // rect is in screen coordinates.
416  wxAccStatus GetLocation(wxRect& rect, int elementId) override;
417 
418  // Gets the name of the specified object.
419  wxAccStatus GetName(int childId, wxString *name) override;
420 
421  // Returns a role constant.
422  wxAccStatus GetRole(int childId, wxAccRole *role) override;
423 
424  // Gets a variant representing the selected children
425  // of this object.
426  // Acceptable values:
427  // - a null variant (IsNull() returns TRUE)
428  // - a list variant (GetType() == wxT("list"))
429  // - an integer representing the selected child element,
430  // or 0 if this object is selected (GetType() == wxT("long"))
431  // - a "void*" pointer to a wxAccessible child object
432  wxAccStatus GetSelections(wxVariant *selections) override;
433 
434  // Returns a state constant.
435  wxAccStatus GetState(int childId, long* state) override;
436 
437  // Returns a localized string representing the value for the object
438  // or child.
439  wxAccStatus GetValue(int childId, wxString* strValue) override;
440 
441 };
442 
443 #endif // wxUSE_ACCESSIBILITY
444 
445 #endif
void SetScroll(float line, float page)
Definition: ASlider.cpp:513
void OnKillFocus(wxFocusEvent &event)
Definition: ASlider.cpp:1565
void OnMouseEvent(wxMouseEvent &event)
Definition: ASlider.cpp:1008
float mMinValue
Definition: ASlider.h:214
wxWindow * mParent
Definition: ASlider.h:179
std::unique_ptr< wxBitmap > mBitmap
Definition: ASlider.h:236
void SetSpeed(float speed)
Definition: ASlider.cpp:1256
void DrawToBitmap(wxDC &dc)
Definition: ASlider.cpp:616
A wxPopupWindow used to give the numerical value of an LWSlider or ASlider.
Definition: ASlider.cpp:82
void OnSetFocus(wxFocusEvent &event)
Definition: ASlider.cpp:1559
ASlider * mSlider
Definition: ASlider.h:364
void SetParent(wxWindow *parent)
Definition: ASlider.h:160
void Decrease(float steps)
Definition: ASlider.cpp:1385
int mLeftX
Definition: ASlider.h:196
int mClickPos
Definition: ASlider.h:212
bool GetEnabled()
Definition: ASlider.cpp:1414
void SetToolTipTemplate(const wxString &tip)
Definition: ASlider.cpp:784
void Increase(float steps)
Definition: ASlider.cpp:1362
#define STEP_CONTINUOUS
Definition: ASlider.h:64
int mStyle
Definition: ASlider.h:366
float mMaxValue
Definition: ASlider.h:215
wxArrayString GetWidestTips() const
Definition: ASlider.cpp:918
int mTopY
Definition: ASlider.h:203
void ShowTip(bool show)
Definition: ASlider.cpp:789
std::unique_ptr< LWSlider > mLWSlider
Definition: ASlider.h:322
float mScrollPage
Definition: ASlider.h:220
virtual ~LWSlider()
Definition: ASlider.cpp:482
bool TransferDataToWindow() override
Definition: ASlider.cpp:232
bool mDefaultShortcut
Definition: ASlider.h:224
void SetId(wxWindowID id)
Definition: ASlider.cpp:491
void FormatPopWin()
Definition: ASlider.cpp:845
SliderDialog(wxWindow *parent, wxWindowID id, const wxString &title, wxPoint position, wxSize size, int style, float value, float line, float page, LWSlider *pSlider=nullptr)
Definition: ASlider.cpp:187
float Get(bool convert=true)
Definition: ASlider.cpp:1591
static TempAllowFocus TemporarilyAllowFocus()
Definition: ASlider.cpp:1640
void SendUpdate(float newValue)
Definition: ASlider.cpp:1231
bool IsEnabled() const
Definition: ASlider.cpp:1633
static void DeleteSharedTipPanel()
int mWidthX
Definition: ASlider.h:198
wxWindowID mID
Definition: ASlider.h:229
void SetFocusFromKbd() override
Definition: ASlider.cpp:1648
void SetToolTipTemplate(const wxString &tip)
Definition: ASlider.cpp:1586
float DragPositionToValue(int fromPos, bool shiftDown)
Definition: ASlider.cpp:1307
virtual ~ASlider()
Definition: ASlider.cpp:1482
void Set(float value)
Definition: ASlider.cpp:1345
float mSpeed
Definition: ASlider.h:217
int mCenterY
Definition: ASlider.h:194
Options & Orientation(wxOrientation o)
Definition: ASlider.h:261
Options & StepValue(float v)
Definition: ASlider.h:265
int mStyle
Definition: ASlider.h:181
void SetPopWinPosition()
Definition: ASlider.cpp:823
void OnPaint(wxDC &dc, bool highlighted)
Definition: ASlider.cpp:561
int mRightX
Definition: ASlider.h:197
void Move(const wxPoint &newpos)
Definition: ASlider.cpp:519
bool AcceptsFocusFromKeyboard() const override
Definition: ASlider.h:280
Pop up dialog used with an LWSlider.
Definition: ASlider.h:341
int mWidth
Definition: ASlider.h:190
bool mHW
Definition: ASlider.h:184
void OnTextChange(wxCommandEvent &event)
Definition: ASlider.cpp:266
int mStyle
Definition: ASlider.h:327
Options & Page(float p)
Definition: ASlider.h:268
void OnSize(wxSizeEvent &event)
Definition: ASlider.cpp:607
LWSlider(wxWindow *parent, const wxString &name, const wxPoint &pos, const wxSize &size, float minValue, float maxValue, float stepValue, bool canUseShift, int style, bool heavyweight=false, bool popup=true, int orientation=wxHORIZONTAL)
Definition: ASlider.cpp:361
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
bool mCanUseShift
Definition: ASlider.h:227
void OnCaptureLost(wxMouseCaptureLostEvent &event)
Definition: ASlider.cpp:1548
Options & CanUseShift(bool c)
Definition: ASlider.h:264
ASlider(wxWindow *parent, wxWindowID id, const wxString &name, const wxPoint &pos, const wxSize &size, const Options &options=Options{})
Definition: ASlider.cpp:1446
bool AcceptsFocus() const override
Definition: ASlider.h:279
float Get(bool convert=true)
Definition: ASlider.cpp:1337
void OnMouseEvent(wxMouseEvent &event)
Definition: ASlider.cpp:1534
void SetScroll(float line, float page)
Definition: ASlider.cpp:1581
int mHeight
Definition: ASlider.h:191
std::unique_ptr< wxBitmap > mThumbBitmap
Definition: ASlider.h:236
float mCurrentValue
Definition: ASlider.h:222
void SetEnabled(bool enabled)
Definition: ASlider.cpp:1419
Lightweight version of ASlider. In other words it does not have a window permanently associated with ...
Definition: ASlider.h:73
std::unique_ptr< TipPanel > mTipPanel
Definition: ASlider.h:231
wxOrientation orientation
Definition: ASlider.h:252
wxWindowID GetId()
Definition: ASlider.cpp:486
bool canUseShift
Definition: ASlider.h:254
wxTimer mTimer
Definition: ASlider.h:324
int ValueToPosition(float val)
Definition: ASlider.cpp:1246
friend class ASliderAx
Definition: ASlider.h:245
std::unique_ptr< bool, Resetter > TempAllowFocus
Definition: ASlider.h:316
bool mEnabled
Definition: ASlider.h:240
int mHeightY
Definition: ASlider.h:205
int mBottomY
Definition: ASlider.h:204
Options & Popup(bool p)
Definition: ASlider.h:263
void Refresh()
Definition: ASlider.cpp:1408
float mDefaultValue
Definition: ASlider.h:225
wxString mTipTemplate
Definition: ASlider.h:232
void GetScroll(float &line, float &page)
Definition: ASlider.cpp:1576
void CreatePopWin()
Definition: ASlider.cpp:818
static bool s_AcceptsFocus
Definition: ASlider.h:314
void Set(float value)
Definition: ASlider.cpp:1596
Options & Line(float l)
Definition: ASlider.h:267
void Init(wxWindow *parent, const wxString &name, const wxPoint &pos, const wxSize &size, float minValue, float maxValue, float stepValue, bool canUseShift, int style, bool heavyweight, bool popup, float speed, int orientation=wxHORIZONTAL)
Definition: ASlider.cpp:439
bool Enable(bool enable=true) override
Definition: ASlider.cpp:1621
void OnSize(wxSizeEvent &event)
Definition: ASlider.cpp:1504
wxTextCtrl * mTextCtrl
Definition: ASlider.h:365
void OnPaint(wxPaintEvent &event)
Definition: ASlider.cpp:1514
bool mSliderIsFocused
Definition: ASlider.h:323
void operator()(bool *p) const
Definition: ASlider.h:315
LWSlider * mpOrigin
Definition: ASlider.h:367
void SetDefaultValue(float value)
Definition: ASlider.cpp:496
float mClickValue
Definition: ASlider.h:211
std::unique_ptr< wxBitmap > mThumbBitmapHilited
Definition: ASlider.h:236
bool ShowDialog(wxPoint pos=wxPoint(-1,-1))
Definition: ASlider.cpp:1611
void GetScroll(float &line, float &page)
Definition: ASlider.cpp:507
bool TransferDataFromWindow() override
Definition: ASlider.cpp:245
ASlider is a custom slider, allowing for a slicker look and feel.
Definition: ASlider.h:243
const wxChar * name
Definition: Distortion.cpp:94
Used to display a Ruler.
Definition: Ruler.h:30
bool ShowDialog()
Definition: ASlider.cpp:963
void OnKeyEvent(wxKeyEvent &event)
Definition: ASlider.cpp:1165
int mThumbHeight
Definition: ASlider.h:209
bool mIsDragging
Definition: ASlider.h:234
wxString mName
Definition: ASlider.h:238
bool mPopup
Definition: ASlider.h:185
float stepValue
Definition: ASlider.h:255
friend class ASliderAx
Definition: ASlider.h:76
void OnErase(wxEraseEvent &event)
Definition: ASlider.cpp:1509
void Increase(float steps)
Definition: ASlider.cpp:1601
float mScrollLine
Definition: ASlider.h:219
float Get()
Definition: ASlider.cpp:272
void AdjustSize(const wxSize &sz)
Definition: ASlider.cpp:525
float ClickPositionToValue(int fromPos, bool shiftDown)
Definition: ASlider.cpp:1263
int mOrientation
Definition: ASlider.h:182
wxString GetTip(float value) const
Definition: ASlider.cpp:856
void OnSlider(wxCommandEvent &event)
Definition: ASlider.cpp:1488
Options & Style(int s)
Definition: ASlider.h:260
int mCenterX
Definition: ASlider.h:201
int mTop
Definition: ASlider.h:188
bool DoShowDialog(wxPoint pos)
Definition: ASlider.cpp:973
#define FRAC_SLIDER
Definition: ASlider.h:44
void SetDefaultShortcut(bool value)
Definition: ASlider.cpp:502
float mStepValue
Definition: ASlider.h:216
int mThumbWidth
Definition: ASlider.h:208
void SetSpeed(float speed)
Definition: ASlider.cpp:1616
int mLeft
Definition: ASlider.h:187
void OnSlider(wxCommandEvent &event)
Definition: ASlider.cpp:260
void OnTimer(wxTimerEvent &event)
Definition: ASlider.cpp:1571
void OnKeyEvent(wxKeyEvent &event)
Definition: ASlider.cpp:1554
void Decrease(float steps)
Definition: ASlider.cpp:1606