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