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