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