Audacity 3.2.0
Grabber.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Grabber.cpp
6
7 Leland Lucius
8
9*******************************************************************//*******************************************************************//**********************************************************************/
22
23
24#include "Grabber.h"
25
26#include <wx/defs.h>
27#include <wx/dcclient.h>
28#include <wx/window.h>
29
30#include "AColor.h"
31#include "AllThemeResources.h"
32#include "Internat.h"
33#include "Theme.h"
34
38
39DEFINE_EVENT_TYPE(EVT_GRABBER_CLICKED)
40
41BEGIN_EVENT_TABLE(Grabber, wxWindow)
42 EVT_ENTER_WINDOW(Grabber::OnEnter)
43 EVT_LEAVE_WINDOW(Grabber::OnLeave)
44 EVT_LEFT_DOWN(Grabber::OnLeftDown)
45 EVT_LEFT_UP(Grabber::OnLeftUp)
46 EVT_ERASE_BACKGROUND( Grabber::OnErase )
47 EVT_PAINT(Grabber::OnPaint)
48 EVT_KEY_DOWN(Grabber::OnKeyDown)
50
51//
52// Constructor
53//
54Grabber::Grabber(wxWindow * parent, Identifier id)
55: wxWindow(parent,
56 wxID_ANY,
57 wxDefaultPosition,
58 wxSize(grabberWidth, 27),
59 wxFULL_REPAINT_ON_RESIZE)
60, mIdentifier{ id }
61{
62 mOver = false;
63 mPressed = false;
64 mAsSpacer = false;
65 SetBackgroundColour( theTheme.Colour( clrMedium ) );
66
67 /* i18n-hint: A 'Grabber' is a region you can click and drag on
68 It's used to drag a track around (when in multi-tool mode) rather
69 than requiring that you use the drag tool. It's shown as a series
70 of horizontal bumps */
71 SetLabel(_("Grabber"));
72 SetName(_("Grabber"));
73}
74
75//
76// Destructor
77//
79{
80}
81
82//
83// Queue a drag event
84//
85void Grabber::SendEvent(wxEventType type, const wxPoint & pos, bool escaping)
86{
87 wxWindow *parent = GetParent();
88
89 // Initialize event and convert mouse coordinates to screen space
90 GrabberEvent e(type, mIdentifier, parent->ClientToScreen(pos), escaping);
91
92 // Set the object of our desire
93 e.SetEventObject(parent);
94
95 // Queue the event
96 parent->GetEventHandler()->AddPendingEvent(e);
97}
98
99void Grabber::SetAsSpacer( bool bIsSpacer ) {
100 if( mAsSpacer != bIsSpacer ){
101 // HACK: Use a wider rectangle to also cover one pixel of space just to the right.
102 wxSize siz = GetSize();
103 siz.IncBy( bIsSpacer ? 1:-1, 0 );
104 SetSize( siz );
105 }
106 mAsSpacer = bIsSpacer;
107};
108
110{
111 wxWindow::SetToolTip( toolTip.Stripped().Translation() );
112}
113
114//
115// Draw the grabber
116//
117void Grabber::DrawGrabber( wxDC & dc )
118{
119 wxRect r = GetRect();
120 // PaintDC positions are relative to the grabber, not the parent window.
121 // So use 0,0 as origin for draw, so that the grabber draws right if
122 // positioned in its parent at some non zero position.
123 r.SetPosition( wxPoint(0,0) );
124 int y, left, right, top, bottom;
125
126 AColor::Medium(&dc, mOver );
127 dc.DrawRectangle(r);
128
129 // HACK: We used a wider rectangle to also cover one pixel of space just to the right.
130 if( mAsSpacer )
131 r.width -= 1;
132
133 // No bumps in a spacer grabber.
134 if( mAsSpacer )
135 return;
136 // Calculate the bump rectangle
137 r.Deflate(2, 2);
138 if ((r.GetHeight() % 4) < 2) {
139 r.Offset(0, 1);
140 }
141
142 // 2-bar toolbars and larger get padding
143 int padding = r.GetHeight() > 32 ? 22 : 6;
144
145 // Cache
146 left = r.GetLeft();
147 right = r.GetRight();
148 top = r.GetTop();
149 bottom = r.GetBottom();
150
151 // Draw the bumps
152 if (mPressed) {
153 AColor::Light(&dc, false);
154 }
155 else {
156 dc.SetPen(wxPen(theTheme.Colour(clrGrabber), 1, wxPENSTYLE_SOLID));
157 }
158
159 for (y = top + padding; y < bottom - padding; y += 5) {
160 dc.DrawRectangle(left, y, 2, 2);
161 dc.DrawRectangle(right, y, 2, 2);
162 }
163}
164
165//
166// Change the button state
167//
168void Grabber::PushButton(bool state )
169{
170 if( mAsSpacer )
171 return;
172 if (!state)
173 mPressed = state;
174 wxRect r = GetRect();
175 mOver = r.Contains(ScreenToClient(wxGetMousePosition()));
176
177 // Redraw button
178 mPressed = state;
179 Refresh(false);
180}
181
182//
183// Handle left button down events
184//
185void Grabber::OnLeftDown(wxMouseEvent & event)
186{
187 // Button should be drawn pushed
188 PushButton(true);
189
190 // Notify parent
191 SendEvent(EVT_GRABBER_CLICKED, event.GetPosition(), false);
192
193 event.Skip();
194}
195
196//
197// Handle left button up events
198//
199void Grabber::OnLeftUp(wxMouseEvent & event)
200{
201 // Normally, "left up" events are handled by the ToolManager::OnMouse() method
202 // but, if the user double clicks a grabber, the "left up" event will come here
203 // instead, so just "unpush" the button.
204 PushButton(false);
205
206 event.Skip();
207}
208
209//
210// Handle mouse enter events
211//
212void Grabber::OnEnter(wxMouseEvent & WXUNUSED(event))
213{
214#if defined(__WXMAC__)
215 // Bug 2416: On Mac, we can get Enter events from grabbers other
216 // than the one being dragged. So, ignore Enter events if another
217 // window has captured the mouse.
218 if (wxWindow::GetCapture() != nullptr)
219 {
220 return;
221 }
222#endif
223
224 // Bug 1201: On Mac, unsetting and re-setting the tooltip may be needed
225 // to make it pop up when we want it.
226 const auto text = GetToolTipText();
227 UnsetToolTip();
228 wxWindow::SetToolTip(text);
229
230 if( mAsSpacer )
231 return;
232
233 // Redraw highlighted
234 mOver = true;
235 Refresh(false);
236}
237
238//
239// Handle mouse leave events
240//
241void Grabber::OnLeave(wxMouseEvent & WXUNUSED(event))
242{
243#if defined(__WXMAC__)
244 // Bug 2416: On Mac, we can get Leave events from grabbers other
245 // than the one being dragged. So, ignore Leave events if another
246 // window has captured the mouse.
247 if (wxWindow::GetCapture() != nullptr)
248 {
249 return;
250 }
251#endif
252
253 if (!GetCapture()) {
254 // Redraw plain
255 mOver = false;
256 Refresh(false);
257 }
258}
259
260void Grabber::OnErase( wxEraseEvent & WXUNUSED(event) )
261{
262 // Ignore it to prevent flashing
263}
264
265//
266// Handle the paint events
267//
268void Grabber::OnPaint(wxPaintEvent & WXUNUSED(event))
269{
270 wxPaintDC dc(this);
271
272 // Redraw the grabber
273 DrawGrabber(dc);
274}
275
276void Grabber::OnKeyDown(wxKeyEvent &event)
277{
278 event.Skip();
279
280 if(event.GetKeyCode() == WXK_ESCAPE) {
281 // We must not only skip this key event, but propagate it up the window
282 // hierarchy, so that ToolFrame detects it too.
283 event.ResumePropagation(wxEVENT_PROPAGATE_MAX);
284 SendEvent(EVT_GRABBER_CLICKED, wxPoint{ -1, -1 }, true);
285 }
286}
287
288// Piggy back in same source file as Grabber.
289// Audacity Flicker-free StaticBitmap.
290BEGIN_EVENT_TABLE(AStaticBitmap,wxStaticBitmap)
291 EVT_ERASE_BACKGROUND(AStaticBitmap::OnErase)
293
294
END_EVENT_TABLE()
DEFINE_EVENT_TYPE(EVT_FREQWINDOW_RECALC)
#define grabberWidth
Definition: Grabber.h:104
#define _(s)
Definition: Internat.h:73
THEME_API Theme theTheme
Definition: Theme.cpp:82
static void Light(wxDC *dc, bool selected, bool highlight=false)
Definition: AColor.cpp:395
static void Medium(wxDC *dc, bool selected)
Definition: AColor.cpp:406
A widget for bitmaps which ignores the erase event for flicker-free use.
Definition: Grabber.h:155
void OnErase(wxEraseEvent &event)
Definition: Grabber.h:173
Grabber Class.
Definition: Grabber.h:48
The widget to the left of a ToolBar that allows it to be dragged around to NEW positions.
Definition: Grabber.h:107
void SendEvent(wxEventType type, const wxPoint &pos, bool escaping)
Definition: Grabber.cpp:85
void OnLeftUp(wxMouseEvent &event)
Definition: Grabber.cpp:199
void SetAsSpacer(bool bIsSpacer)
Definition: Grabber.cpp:99
bool mAsSpacer
Definition: Grabber.h:146
void DrawGrabber(wxDC &dc)
Definition: Grabber.cpp:117
virtual ~Grabber()
Definition: Grabber.cpp:78
void OnLeave(wxMouseEvent &event)
Definition: Grabber.cpp:241
bool mOver
Definition: Grabber.h:144
void OnErase(wxEraseEvent &event)
Definition: Grabber.cpp:260
const Identifier mIdentifier
Definition: Grabber.h:143
void OnPaint(wxPaintEvent &event)
Definition: Grabber.cpp:268
void OnLeftDown(wxMouseEvent &event)
Definition: Grabber.cpp:185
void OnKeyDown(wxKeyEvent &event)
Definition: Grabber.cpp:276
void OnEnter(wxMouseEvent &event)
Definition: Grabber.cpp:212
bool mPressed
Definition: Grabber.h:145
void SetToolTip(const TranslatableString &toolTip)
Definition: Grabber.cpp:109
void PushButton(bool state)
Definition: Grabber.cpp:168
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
wxColour & Colour(int iIndex)
Holds a msgid for the translation catalog; may also bind format arguments.
wxString Translation() const
TranslatableString Stripped(unsigned options=MenuCodes) const
non-mutating, constructs another TranslatableString object