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