Audacity  2.2.0
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  wxString GetMaxTip() 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  ASlider( wxWindow * parent,
248  wxWindowID id,
249  const wxString &name,
250  const wxPoint & pos,
251  const wxSize & size,
252  int style = FRAC_SLIDER,
253  bool popup = true,
254  bool canUseShift = true,
255  float stepValue = STEP_CONTINUOUS,
256  int orientation = wxHORIZONTAL);
257  virtual ~ASlider();
258 
259  bool AcceptsFocus() const override { return s_AcceptsFocus; }
260  bool AcceptsFocusFromKeyboard() const override { return true; }
261 
262  void SetFocusFromKbd() override;
263 
264  void GetScroll(float & line, float & page);
265  void SetScroll(float line, float page);
266 
267  void SetToolTipTemplate(const wxString & tip);
268 
269  float Get( bool convert = true );
270  void Set(float value);
271 
272  void Increase(float steps);
273  void Decrease(float steps);
274  bool ShowDialog(wxPoint pos = wxPoint(-1, -1));
275 
276  void SetSpeed(float speed);
277 
278  void OnErase(wxEraseEvent & event);
279  void OnPaint(wxPaintEvent & event);
280  void OnSize(wxSizeEvent & event);
281  void OnMouseEvent(wxMouseEvent & event);
282  void OnCaptureLost(wxMouseCaptureLostEvent & event);
283  void OnKeyEvent(wxKeyEvent &event);
284  void OnSlider(wxCommandEvent &event);
285  void OnSetFocus(wxFocusEvent & event);
286  void OnKillFocus(wxFocusEvent & event);
287  void OnTimer(wxTimerEvent & event);
288 
289  // Overrides of the wxWindow functions with the same semantics
290  bool Enable(bool enable = true);
291  bool IsEnabled() const;
292 
293 private:
294  static bool s_AcceptsFocus;
295  struct Resetter { void operator () (bool *p) const { if(p) *p = false; } };
296  using TempAllowFocus = std::unique_ptr<bool, Resetter>;
297 
298 public:
300 
301  private:
302  std::unique_ptr<LWSlider> mLWSlider;
304  wxTimer mTimer;
305 
306  protected:
307  int mStyle;
308 
309  public:
310  DECLARE_EVENT_TABLE()
311 };
312 
313 
314 
315 #define SLIDER_DIALOG_TEXTCTRL 100
316 
317 
318 // This is a modal dialog that contains an ASlider
319 // and a text-entry box which can be used to set the
320 // value of a slider.
321 class SliderDialog final : public wxDialogWrapper
322 {
323  public:
324  SliderDialog(wxWindow * parent, wxWindowID id,
325  const wxString & title,
326  wxPoint position,
327  wxSize size,
328  int style,
329  float value,
330  float line,
331  float page);
332  ~SliderDialog();
333 
334  float Get();
335 
336  private:
337  bool TransferDataToWindow() override;
338  bool TransferDataFromWindow() override;
339 
340  void OnSlider(wxCommandEvent &event);
341 
343  wxTextCtrl * mTextCtrl;
344  int mStyle;
345 
346  public:
347  DECLARE_EVENT_TABLE()
348 };
349 
350 
351 #if wxUSE_ACCESSIBILITY
352 
353 class ASliderAx final : public wxWindowAccessible
354 {
355 public:
356  ASliderAx(wxWindow * window);
357 
358  virtual ~ ASliderAx();
359 
360  // Retrieves the address of an IDispatch interface for the specified child.
361  // All objects must support this property.
362  wxAccStatus GetChild(int childId, wxAccessible** child) override;
363 
364  // Gets the number of children.
365  wxAccStatus GetChildCount(int* childCount) override;
366 
367  // Gets the default action for this object (0) or > 0 (the action for a child).
368  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
369  // string if there is no action.
370  // The retrieved string describes the action that is performed on an object,
371  // not what the object does as a result. For example, a toolbar button that prints
372  // a document has a default action of "Press" rather than "Prints the current document."
373  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
374 
375  // Returns the description for this object or a child.
376  wxAccStatus GetDescription(int childId, wxString *description) override;
377 
378  // Gets the window with the keyboard focus.
379  // If childId is 0 and child is NULL, no object in
380  // this subhierarchy has the focus.
381  // If this object has the focus, child should be 'this'.
382  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
383 
384  // Returns help text for this object or a child, similar to tooltip text.
385  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
386 
387  // Returns the keyboard shortcut for this object or child.
388  // Return e.g. ALT+K
389  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
390 
391  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
392  // rect is in screen coordinates.
393  wxAccStatus GetLocation(wxRect& rect, int elementId) override;
394 
395  // Gets the name of the specified object.
396  wxAccStatus GetName(int childId, wxString *name) override;
397 
398  // Returns a role constant.
399  wxAccStatus GetRole(int childId, wxAccRole *role) override;
400 
401  // Gets a variant representing the selected children
402  // of this object.
403  // Acceptable values:
404  // - a null variant (IsNull() returns TRUE)
405  // - a list variant (GetType() == wxT("list"))
406  // - an integer representing the selected child element,
407  // or 0 if this object is selected (GetType() == wxT("long"))
408  // - a "void*" pointer to a wxAccessible child object
409  wxAccStatus GetSelections(wxVariant *selections) override;
410 
411  // Returns a state constant.
412  wxAccStatus GetState(int childId, long* state) override;
413 
414  // Returns a localized string representing the value for the object
415  // or child.
416  wxAccStatus GetValue(int childId, wxString* strValue) override;
417 
418 };
419 
420 #endif // wxUSE_ACCESSIBILITY
421 
422 #endif
void SetScroll(float line, float page)
Definition: ASlider.cpp:501
void OnKillFocus(wxFocusEvent &event)
Definition: ASlider.cpp:1657
void OnMouseEvent(wxMouseEvent &event)
Definition: ASlider.cpp:1097
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:1345
A wxPopupWindow used to give the numerical value of an LWSlider or ASlider.
Definition: ASlider.cpp:89
void OnSetFocus(wxFocusEvent &event)
Definition: ASlider.cpp:1651
ASlider * mSlider
Definition: ASlider.h:342
void SetParent(wxWindow *parent)
Definition: ASlider.h:159
void Decrease(float steps)
Definition: ASlider.cpp:1474
int mLeftX
Definition: ASlider.h:195
int mClickPos
Definition: ASlider.h:211
bool GetEnabled()
Definition: ASlider.cpp:1503
void SetToolTipTemplate(const wxString &tip)
Definition: ASlider.cpp:883
void Increase(float steps)
Definition: ASlider.cpp:1451
#define STEP_CONTINUOUS
Definition: ASlider.h:63
int mStyle
Definition: ASlider.h:344
float mMaxValue
Definition: ASlider.h:214
int mTopY
Definition: ASlider.h:202
void ShowTip(bool show)
Definition: ASlider.cpp:888
std::unique_ptr< LWSlider > mLWSlider
Definition: ASlider.h:302
float mScrollPage
Definition: ASlider.h:219
virtual ~LWSlider()
Definition: ASlider.cpp:470
bool TransferDataToWindow() override
Definition: ASlider.cpp:233
bool mDefaultShortcut
Definition: ASlider.h:223
void SetId(wxWindowID id)
Definition: ASlider.cpp:479
void FormatPopWin()
Definition: ASlider.cpp:944
float Get(bool convert=true)
Definition: ASlider.cpp:1683
static TempAllowFocus TemporarilyAllowFocus()
Definition: ASlider.cpp:1732
void SendUpdate(float newValue)
Definition: ASlider.cpp:1320
bool IsEnabled() const
Definition: ASlider.cpp:1725
static void DeleteSharedTipPanel()
int mWidthX
Definition: ASlider.h:197
wxWindowID mID
Definition: ASlider.h:228
void SetFocusFromKbd() override
Definition: ASlider.cpp:1740
void SetToolTipTemplate(const wxString &tip)
Definition: ASlider.cpp:1678
float DragPositionToValue(int fromPos, bool shiftDown)
Definition: ASlider.cpp:1396
virtual ~ASlider()
Definition: ASlider.cpp:1574
void Set(float value)
Definition: ASlider.cpp:1434
float mSpeed
Definition: ASlider.h:216
int mCenterY
Definition: ASlider.h:193
int mStyle
Definition: ASlider.h:180
void SetPopWinPosition()
Definition: ASlider.cpp:922
void OnPaint(wxDC &dc, bool highlighted)
Definition: ASlider.cpp:549
int mRightX
Definition: ASlider.h:196
void Move(const wxPoint &newpos)
Definition: ASlider.cpp:507
bool AcceptsFocusFromKeyboard() const override
Definition: ASlider.h:260
Pop up dialog used with an LWSlider.
Definition: ASlider.h:321
int mWidth
Definition: ASlider.h:189
Definition: wxPanelWrapper.h:19
bool mHW
Definition: ASlider.h:183
int mStyle
Definition: ASlider.h:307
wxString GetMaxTip() const
Definition: ASlider.cpp:1013
void OnSize(wxSizeEvent &event)
Definition: ASlider.cpp:594
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:349
bool mCanUseShift
Definition: ASlider.h:226
void Draw(wxDC &dc)
Definition: ASlider.cpp:601
void OnCaptureLost(wxMouseCaptureLostEvent &event)
Definition: ASlider.cpp:1640
bool AcceptsFocus() const override
Definition: ASlider.h:259
float Get(bool convert=true)
Definition: ASlider.cpp:1426
void OnMouseEvent(wxMouseEvent &event)
Definition: ASlider.cpp:1626
void SetScroll(float line, float page)
Definition: ASlider.cpp:1673
int mHeight
Definition: ASlider.h:190
std::unique_ptr< wxBitmap > mThumbBitmap
Definition: ASlider.h:235
ASlider(wxWindow *parent, wxWindowID id, const wxString &name, const wxPoint &pos, const wxSize &size, int style=FRAC_SLIDER, bool popup=true, bool canUseShift=true, float stepValue=STEP_CONTINUOUS, int orientation=wxHORIZONTAL)
Definition: ASlider.cpp:1535
float mCurrentValue
Definition: ASlider.h:221
void SetEnabled(bool enabled)
Definition: ASlider.cpp:1508
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:1713
std::unique_ptr< TipPanel > mTipPanel
Definition: ASlider.h:230
wxWindowID GetId()
Definition: ASlider.cpp:474
wxTimer mTimer
Definition: ASlider.h:304
int ValueToPosition(float val)
Definition: ASlider.cpp:1335
friend class ASliderAx
Definition: ASlider.h:244
std::unique_ptr< bool, Resetter > TempAllowFocus
Definition: ASlider.h:296
bool mEnabled
Definition: ASlider.h:239
int mHeightY
Definition: ASlider.h:204
int mBottomY
Definition: ASlider.h:203
void Refresh()
Definition: ASlider.cpp:1497
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:189
wxString mTipTemplate
Definition: ASlider.h:231
void GetScroll(float &line, float &page)
Definition: ASlider.cpp:1668
void CreatePopWin()
Definition: ASlider.cpp:917
static bool s_AcceptsFocus
Definition: ASlider.h:294
void Set(float value)
Definition: ASlider.cpp:1688
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:427
void OnSize(wxSizeEvent &event)
Definition: ASlider.cpp:1596
wxTextCtrl * mTextCtrl
Definition: ASlider.h:343
void OnPaint(wxPaintEvent &event)
Definition: ASlider.cpp:1606
bool mSliderIsFocused
Definition: ASlider.h:303
void operator()(bool *p) const
Definition: ASlider.h:295
void SetDefaultValue(float value)
Definition: ASlider.cpp:484
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:1703
void GetScroll(float &line, float &page)
Definition: ASlider.cpp:495
bool TransferDataFromWindow() override
Definition: ASlider.cpp:241
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:1056
void OnKeyEvent(wxKeyEvent &event)
Definition: ASlider.cpp:1254
int mThumbHeight
Definition: ASlider.h:208
bool mIsDragging
Definition: ASlider.h:233
wxString mName
Definition: ASlider.h:237
bool mPopup
Definition: ASlider.h:184
friend class ASliderAx
Definition: ASlider.h:75
void OnErase(wxEraseEvent &event)
Definition: ASlider.cpp:1601
void Increase(float steps)
Definition: ASlider.cpp:1693
float mScrollLine
Definition: ASlider.h:218
float Get()
Definition: ASlider.cpp:260
void AdjustSize(const wxSize &sz)
Definition: ASlider.cpp:513
float ClickPositionToValue(int fromPos, bool shiftDown)
Definition: ASlider.cpp:1352
int mOrientation
Definition: ASlider.h:181
wxString GetTip(float value) const
Definition: ASlider.cpp:955
void OnSlider(wxCommandEvent &event)
Definition: ASlider.cpp:1580
int mCenterX
Definition: ASlider.h:200
int mTop
Definition: ASlider.h:187
bool DoShowDialog(wxPoint pos)
Definition: ASlider.cpp:1066
#define FRAC_SLIDER
Definition: ASlider.h:43
void SetDefaultShortcut(bool value)
Definition: ASlider.cpp:490
float mStepValue
Definition: ASlider.h:215
int mThumbWidth
Definition: ASlider.h:207
void SetSpeed(float speed)
Definition: ASlider.cpp:1708
int mLeft
Definition: ASlider.h:186
Definition: ASlider.h:295
void OnSlider(wxCommandEvent &event)
Definition: ASlider.cpp:253
void OnTimer(wxTimerEvent &event)
Definition: ASlider.cpp:1663
void OnKeyEvent(wxKeyEvent &event)
Definition: ASlider.cpp:1646
~SliderDialog()
Definition: ASlider.cpp:229
void Decrease(float steps)
Definition: ASlider.cpp:1698