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