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