Audacity  2.2.2
AButton.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  AButton.h
6 
7  Dominic Mazzoni
8 
9 
10 **********************************************************************/
11 
12 #ifndef __AUDACITY_BUTTON__
13 #define __AUDACITY_BUTTON__
14 
15 #include "../MemoryX.h"
16 #include <vector>
17 
18 #if wxUSE_ACCESSIBILITY
19 #include <wx/access.h>
20 #include "WindowAccessible.h"
21 #endif
22 
23 #include <wx/image.h>
24 #include <wx/window.h>
25 
26 #include "ImageRoll.h"
27 
28 class AButton final : public wxWindow {
29  friend class AButtonAx;
30  class Listener;
31 
32  public:
33 
34  // Construct button, specifying images (button up, highlight, button down,
35  // and disabled) for the default state
36  AButton(wxWindow * parent,
37  wxWindowID id,
38  const wxPoint & pos,
39  const wxSize & size,
40  ImageRoll up,
41  ImageRoll over,
42  ImageRoll down,
43  ImageRoll overDown,
44  ImageRoll dis,
45  bool toggle);
46 
47  // Construct button, specifying images (button up, highlight, button down,
48  // and disabled) for the default state
49  AButton(wxWindow * parent,
50  wxWindowID id,
51  const wxPoint & pos,
52  const wxSize & size,
53  wxImage up,
54  wxImage over,
55  wxImage down,
56  wxImage overDown,
57  wxImage dis,
58  bool toggle);
59 
60  virtual ~ AButton();
61 
62  bool AcceptsFocus() const override { return s_AcceptsFocus; }
63  bool AcceptsFocusFromKeyboard() const override { return mEnabled; }
64 
65  void SetFocusFromKbd() override;
66 
67  // Associate a set of four images (button up, highlight, button down,
68  // disabled) with one nondefault state of the button
69  void SetAlternateImages(unsigned idx,
70  ImageRoll up,
71  ImageRoll over,
72  ImageRoll down,
73  ImageRoll overDown,
74  ImageRoll dis);
75 
76  // Associate a set of four images (button up, highlight, button down,
77  // disabled) with one nondefault state of the button
78  void SetAlternateImages(unsigned idx,
79  wxImage up,
80  wxImage over,
81  wxImage down,
82  wxImage overDown,
83  wxImage dis);
84 
85  // Choose state of the button
86  void SetAlternateIdx(unsigned idx);
87 
88  // Make the button change appearance with the modifier keys, no matter
89  // where the mouse is:
90  // Use state 2 when CTRL is down, else 1 when SHIFT is down, else 0
91  void FollowModifierKeys();
92 
93  void SetFocusRect(wxRect & r);
94 
95  bool IsEnabled() const { return mEnabled; }
96  void Disable();
97  void Enable();
98  void SetEnabled(bool state) {
99  state ? Enable() : Disable();
100  }
101 
102  void PushDown();
103  void PopUp();
104 
105  void OnErase(wxEraseEvent & event);
106  void OnPaint(wxPaintEvent & event);
107  void OnSize(wxSizeEvent & event);
108  void OnMouseEvent(wxMouseEvent & event);
109 
110  // Update the status bar message if the pointer is in the button.
111  // Else do nothing.
112  void UpdateStatus();
113 
114  void OnCaptureLost(wxMouseCaptureLostEvent & event);
115  void OnKeyDown(wxKeyEvent & event);
116  void OnSetFocus(wxFocusEvent & event);
117  void OnKillFocus(wxFocusEvent & event);
118 
119  bool WasShiftDown(); // returns true if shift was held down
120  // the last time the button was clicked
121  bool WasControlDown(); // returns true if control was held down
122  // the last time the button was clicked
123  bool IsDown(){ return mButtonIsDown;}
124 
125  // Double click is detected, but not automatically cleared.
126  bool IsDoubleClicked() const { return mIsDoubleClicked; }
128 
129  void SetButtonToggles( bool toggler ){ mToggle = toggler;}
130  // When click is over and mouse has moved away, a normal button
131  // should pop up.
132  void InteractionOver(){ if( !mToggle ) PopUp();}
133  void Toggle(){ mButtonIsDown ? PopUp() : PushDown();}
134  void Click();
135  void SetShift(bool shift);
136  void SetControl(bool control);
137 
144  };
145 
147 
148  void UseDisabledAsDownHiliteImage(bool flag);
149 
150  private:
151  static bool s_AcceptsFocus;
152  struct Resetter { void operator () (bool *p) const { if(p) *p = false; } };
153  using TempAllowFocus = std::unique_ptr<bool, Resetter>;
154 
155  public:
157 
158  private:
159 
160  bool HasAlternateImages(unsigned idx);
161 
162  void Init(wxWindow * parent,
163  wxWindowID id,
164  const wxPoint & pos,
165  const wxSize & size,
166  ImageRoll up,
167  ImageRoll over,
168  ImageRoll down,
169  ImageRoll overDown,
170  ImageRoll dis,
171  bool toggle);
172 
173  unsigned mAlternateIdx;
174  bool mToggle; // This bool, if true, makes the button able to
175  // process events when it is in the down state, and
176  // moving to the opposite state when it is clicked.
177  // It is used for the Pause button, and possibly
178  // others. If false, it (should) behave just like
179  // a standard button.
180 
183 
187  bool mEnabled;
190 
191  struct ImageArr { ImageRoll mArr[5]; };
192  std::vector<ImageArr> mImages;
193 
194  wxRect mFocusRect;
196 
197  std::unique_ptr<Listener> mListener;
198 
199 public:
200 
201  DECLARE_EVENT_TABLE()
202 };
203 
204 #if wxUSE_ACCESSIBILITY
205 
206 class AButtonAx final : public WindowAccessible
207 {
208 public:
209  AButtonAx(wxWindow * window);
210 
211  virtual ~ AButtonAx();
212 
213  // Performs the default action. childId is 0 (the action for this object)
214  // or > 0 (the action for a child).
215  // Return wxACC_NOT_SUPPORTED if there is no default action for this
216  // window (e.g. an edit control).
217  wxAccStatus DoDefaultAction(int childId) override;
218 
219  // Retrieves the address of an IDispatch interface for the specified child.
220  // All objects must support this property.
221  wxAccStatus GetChild(int childId, wxAccessible** child) override;
222 
223  // Gets the number of children.
224  wxAccStatus GetChildCount(int* childCount) override;
225 
226  // Gets the default action for this object (0) or > 0 (the action for a child).
227  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
228  // string if there is no action.
229  // The retrieved string describes the action that is performed on an object,
230  // not what the object does as a result. For example, a toolbar button that prints
231  // a document has a default action of "Press" rather than "Prints the current document."
232  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
233 
234  // Returns the description for this object or a child.
235  wxAccStatus GetDescription(int childId, wxString *description) override;
236 
237  // Gets the window with the keyboard focus.
238  // If childId is 0 and child is NULL, no object in
239  // this subhierarchy has the focus.
240  // If this object has the focus, child should be 'this'.
241  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
242 
243  // Returns help text for this object or a child, similar to tooltip text.
244  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
245 
246  // Returns the keyboard shortcut for this object or child.
247  // Return e.g. ALT+K
248  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
249 
250  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
251  // rect is in screen coordinates.
252  wxAccStatus GetLocation(wxRect& rect, int elementId) override;
253 
254  // Gets the name of the specified object.
255  wxAccStatus GetName(int childId, wxString *name) override;
256 
257  // Returns a role constant.
258  wxAccStatus GetRole(int childId, wxAccRole *role) override;
259 
260  // Gets a variant representing the selected children
261  // of this object.
262  // Acceptable values:
263  // - a null variant (IsNull() returns TRUE)
264  // - a list variant (GetType() == wxT("list"))
265  // - an integer representing the selected child element,
266  // or 0 if this object is selected (GetType() == wxT("long"))
267  // - a "void*" pointer to a wxAccessible child object
268  wxAccStatus GetSelections(wxVariant *selections) override;
269 
270  // Returns a state constant.
271  wxAccStatus GetState(int childId, long* state) override;
272 
273  // Returns a localized string representing the value for the object
274  // or child.
275  wxAccStatus GetValue(int childId, wxString* strValue) override;
276 
277 };
278 
279 #endif // wxUSE_ACCESSIBILITY
280 
281 #endif
void InteractionOver()
Definition: AButton.h:132
void SetAlternateIdx(unsigned idx)
Definition: AButton.cpp:248
std::unique_ptr< bool, Resetter > TempAllowFocus
Definition: AButton.h:153
bool mWasShiftDown
Definition: AButton.h:181
bool IsDoubleClicked() const
Definition: AButton.h:126
bool mIsDoubleClicked
Definition: AButton.h:189
void OnKillFocus(wxFocusEvent &event)
Definition: AButton.cpp:480
void SetAlternateImages(unsigned idx, ImageRoll up, ImageRoll over, ImageRoll down, ImageRoll overDown, ImageRoll dis)
Definition: AButton.cpp:232
void PopUp()
Definition: AButton.cpp:525
void ClearDoubleClicked()
Definition: AButton.h:127
bool IsDown()
Definition: AButton.h:123
void OnMouseEvent(wxMouseEvent &event)
Definition: AButton.cpp:357
void SetShift(bool shift)
Definition: AButton.cpp:546
bool WasShiftDown()
Definition: AButton.cpp:485
void SetFocusRect(wxRect &r)
Definition: AButton.cpp:264
friend class AButtonAx
Definition: AButton.h:29
AButtonState
Definition: AButton.h:138
void FollowModifierKeys()
Definition: AButton.cpp:258
void OnSize(wxSizeEvent &event)
Definition: AButton.cpp:332
void Toggle()
Definition: AButton.h:133
bool mToggle
Definition: AButton.h:174
void SetButtonToggles(bool toggler)
Definition: AButton.h:129
bool IsEnabled() const
Definition: AButton.h:95
bool mCursorIsInWindow
Definition: AButton.h:184
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
std::vector< ImageArr > mImages
Definition: AButton.h:192
wxRect mFocusRect
Definition: AButton.h:194
bool mIsClicking
Definition: AButton.h:186
bool mButtonIsDown
Definition: AButton.h:185
ImageRoll mArr[5]
Definition: AButton.h:191
unsigned mAlternateIdx
Definition: AButton.h:173
void SetFocusFromKbd() override
Definition: AButton.cpp:210
void SetControl(bool control)
Definition: AButton.cpp:551
void SetEnabled(bool state)
Definition: AButton.h:98
std::unique_ptr< Listener > mListener
Definition: AButton.h:197
virtual ~AButton()
Definition: AButton.cpp:149
bool mUseDisabledAsDownHiliteImage
Definition: AButton.h:188
void OnPaint(wxPaintEvent &event)
Definition: AButton.cpp:313
bool WasControlDown()
Definition: AButton.cpp:490
bool AcceptsFocus() const override
Definition: AButton.h:62
static bool s_AcceptsFocus
Definition: AButton.h:151
void OnErase(wxEraseEvent &event)
Definition: AButton.cpp:327
void Disable()
Definition: AButton.cpp:502
void OnCaptureLost(wxMouseCaptureLostEvent &event)
Definition: AButton.cpp:437
void Init(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, ImageRoll up, ImageRoll over, ImageRoll down, ImageRoll overDown, ImageRoll dis, bool toggle)
Definition: AButton.cpp:155
const wxChar * name
Definition: Distortion.cpp:94
void Enable()
Definition: AButton.cpp:495
void OnSetFocus(wxFocusEvent &event)
Definition: AButton.cpp:475
void UseDisabledAsDownHiliteImage(bool flag)
Definition: AButton.cpp:202
AButtonState GetState()
Definition: AButton.cpp:270
bool mWasControlDown
Definition: AButton.h:182
AButton(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, ImageRoll up, ImageRoll over, ImageRoll down, ImageRoll overDown, ImageRoll dis, bool toggle)
Definition: AButton.cpp:132
void OnKeyDown(wxKeyEvent &event)
Definition: AButton.cpp:447
bool mEnabled
Definition: AButton.h:187
void PushDown()
Definition: AButton.cpp:517
bool mForceFocusRect
Definition: AButton.h:195
bool AcceptsFocusFromKeyboard() const override
Definition: AButton.h:63
void Click()
Definition: AButton.cpp:537
bool HasAlternateImages(unsigned idx)
Definition: AButton.cpp:343
static TempAllowFocus TemporarilyAllowFocus()
Definition: AButton.cpp:556
An ImageRoll is an image that can be expanded to an arbitrary size; it is made up of both fixed piece...
Definition: ImageRoll.h:29
A wxButton with mouse-over behaviour.
Definition: AButton.h:28
void operator()(bool *p) const
Definition: AButton.h:152
void UpdateStatus()
Definition: AButton.cpp:421