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 FrameTextHButton
117 };
118
125
126 AButtonStateCount
127 };
128
129 AButton(wxWindow* parent = nullptr,
130 wxWindowID id = wxID_ANY,
131 const wxPoint& pos = wxDefaultPosition,
132 const wxSize& size = wxDefaultSize,
133 bool toggle = false);
134
135 // Construct button, specifying images (button up, highlight, button down,
136 // and disabled) for the default state
137 AButton(wxWindow * parent,
138 wxWindowID id,
139 const wxPoint & pos,
140 const wxSize & size,
141 const wxImage& up,
142 const wxImage& over,
143 const wxImage& down,
144 const wxImage& overDown,
145 const wxImage& dis,
146 bool toggle);
147
148 virtual ~ AButton();
149
150 void SetButtonType(Type type);
151 void SetFrameMid(int mid);
152
153 // hide the inherited function that takes naked wxString:
154 void SetToolTip(const TranslatableString &toolTip);
155
156 // hide the inherited function that takes naked wxString:
157 void SetLabel(const TranslatableString &label);
158
159 bool AcceptsFocus() const override { return s_AcceptsFocus; }
160 bool AcceptsFocusFromKeyboard() const override { return mEnabled; }
161
162 void SetFocusFromKbd() override;
163
164 //Same as SetAlternateImages(0, ...);
165 void SetImages(const wxImage& up,
166 const wxImage& over,
167 const wxImage& down,
168 const wxImage& overDown,
169 const wxImage& dis);
170
171 // Associate a set of four images (button up, highlight, button down,
172 // disabled) with one nondefault state of the button
173 void SetAlternateImages(unsigned idx,
174 const wxImage& up,
175 const wxImage& over,
176 const wxImage& down,
177 const wxImage& overDown,
178 const wxImage& dis);
179
180 void SetIcon(const wxImage& icon);
181 void SetIcon(AButtonState state, const wxImage& icon);
182 void SetIcons(const wxImage& up, const wxImage& down, const wxImage& disabled);
183
184 void SetAlternateIcon(unsigned idx, const wxImage& icon);
185 void SetAlternateIcon(unsigned idx, AButtonState state, const wxImage& icon);
186 void SetAlternateIcons(unsigned idx, const wxImage& up, const wxImage& down, const wxImage& disabled);
187
188 // Choose state of the button
189 void SetAlternateIdx(unsigned idx);
190
191 // Make the button change appearance with the modifier keys, no matter
192 // where the mouse is:
193 // Use state 2 when CTRL is down, else 1 when SHIFT is down, else 0
194 void FollowModifierKeys();
195
196 void SetFocusRect(const wxRect & r);
197
198 bool IsEnabled() const { return mEnabled; }
199 void Disable();
200 void Enable();
201 void SetEnabled(bool state) {
202 state ? Enable() : Disable();
203 }
204
205 void PushDown();
206 void PopUp();
207
208 void OnErase(wxEraseEvent & event);
209 void OnPaint(wxPaintEvent & event);
210 void OnSize(wxSizeEvent & event);
211 void OnMouseEvent(wxMouseEvent & event);
212
213 // Update the status bar message if the pointer is in the button.
214 // Else do nothing.
215 void UpdateStatus();
216
217 void OnCaptureLost(wxMouseCaptureLostEvent & event);
218 void OnKeyDown(wxKeyEvent & event);
219 void OnSetFocus(wxFocusEvent & event);
220 void OnKillFocus(wxFocusEvent & event);
221 void OnCharHook(wxKeyEvent& event);
222
223 bool WasShiftDown(); // returns true if shift was held down
224 // the last time the button was clicked
225 bool WasControlDown(); // returns true if control was held down
226 // the last time the button was clicked
227 bool IsDown(){ return mButtonIsDown;}
228
229 // Double click is detected, but not automatically cleared.
230 bool IsDoubleClicked() const { return mIsDoubleClicked; }
231 void ClearDoubleClicked() { mIsDoubleClicked = false; }
232
233 void SetButtonToggles( bool toggler ){ mToggle = toggler;}
234 bool IsToggle() const noexcept;
235 // When click is over and mouse has moved away, a normal button
236 // should pop up.
237 void InteractionOver(){ if( !mToggle ) PopUp();}
238 void Toggle(){ mButtonIsDown ? PopUp() : PushDown();}
239 void Click();
240 void SetShift(bool shift);
241 void SetControl(bool control);
242
243 wxSize DoGetBestClientSize() const override;
244
245 AButtonState GetState();
246
247 void UseDisabledAsDownHiliteImage(bool flag);
248
249 private:
250 static bool s_AcceptsFocus;
251 struct Resetter { void operator () (bool *p) const { if(p) *p = false; } };
252 using TempAllowFocus = std::unique_ptr<bool, Resetter>;
253
254 public:
255 static TempAllowFocus TemporarilyAllowFocus();
256
257 private:
258
259 bool HasAlternateImages(unsigned idx) const;
260
261 void Init(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, bool toggle);
262
263 unsigned mAlternateIdx{0};
264 bool mToggle{false}; // This bool, if true, makes the button able to
265 // process events when it is in the down state, and
266 // moving to the opposite state when it is clicked.
267 // It is used for the Pause button, and possibly
268 // others. If false, it (should) behave just like
269 // a standard button.
270
271 bool mWasShiftDown {false};
272 bool mWasControlDown {false};
273
274 bool mCursorIsInWindow{false};
275 bool mButtonIsDown{false};
276 bool mIsClicking{false};
277 bool mEnabled{true};
278 bool mUseDisabledAsDownHiliteImage{false};
279 bool mIsDoubleClicked{false};
280
281 std::vector<std::array<wxImage, AButtonStateCount>> mIcons;
282 std::vector<std::array<wxImage, AButtonStateCount>> mImages;
283
285 bool mForceFocusRect{false};
286
287 std::unique_ptr<Listener> mListener;
288
289 Type mType{ImageButton};
290 int mFrameMid{1};
291
292public:
293
294 DECLARE_EVENT_TABLE()
295};
296
297#endif
wxImage(22, 22)
wxString name
Definition: TagsEditor.cpp:166
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:284
AButtonState
Definition: AButton.h:119
@ AButtonUp
Definition: AButton.h:120
@ AButtonDis
Definition: AButton.h:124
@ AButtonOver
Definition: AButton.h:121
@ AButtonDown
Definition: AButton.h:122
@ AButtonOverDown
Definition: AButton.h:123
bool IsDoubleClicked() const
Definition: AButton.h:230
std::unique_ptr< bool, Resetter > TempAllowFocus
Definition: AButton.h:252
bool IsDown()
Definition: AButton.h:227
void SetButtonToggles(bool toggler)
Definition: AButton.h:233
bool IsToggle() const noexcept
static bool s_AcceptsFocus
Definition: AButton.h:250
void ClearDoubleClicked()
Definition: AButton.h:231
std::unique_ptr< Listener > mListener
Definition: AButton.h:287
bool IsEnabled() const
Definition: AButton.h:198
@ TextButton
Definition: AButton.h:112
@ FrameTextVButton
Definition: AButton.h:115
@ FrameButton
Definition: AButton.h:114
@ ImageButton
Definition: AButton.h:113
std::vector< std::array< wxImage, AButtonStateCount > > mIcons
Definition: AButton.h:281
bool AcceptsFocusFromKeyboard() const override
Definition: AButton.h:160
void Toggle()
Definition: AButton.h:238
void SetEnabled(bool state)
Definition: AButton.h:201
std::vector< std::array< wxImage, AButtonStateCount > > mImages
Definition: AButton.h:282
bool AcceptsFocus() const override
Definition: AButton.h:159
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 ...