forked from zao/foo_wave_seekbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeekbarWindow.Events.cc
337 lines (294 loc) · 9.27 KB
/
SeekbarWindow.Events.cc
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright Lars Viklund 2008 - 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "PchSeekbar.h"
#include "SeekbarWindow.h"
#include "SeekTooltip.h"
#include "Clipboard.h"
#include "FrontendLoader.h"
#include "util/Profiling.h"
// {EBEABA3F-7A8E-4A54-A902-3DCF716E6A97}
static const GUID guid_seekbar_branch =
{ 0xebeaba3f, 0x7a8e, 0x4a54, { 0xa9, 0x2, 0x3d, 0xcf, 0x71, 0x6e, 0x6a, 0x97 } };
// {E913D7C7-676A-4A4F-A0F4-3DA33622D3D8}
static const GUID guid_seekbar_screenshot_branch =
{ 0xe913d7c7, 0x676a, 0x4a4f, { 0xa0, 0xf4, 0x3d, 0xa3, 0x36, 0x22, 0xd3, 0xd8 } };
// {FA3261D7-671B-4BE4-AA18-75B8EFA7E4D6}
static const GUID guid_seekbar_screenshot_width =
{ 0xfa3261d7, 0x671b, 0x4be4, { 0xaa, 0x18, 0x75, 0xb8, 0xef, 0xa7, 0xe4, 0xd6 } };
// {B3534AC9-609A-4A6E-8B6A-7522D7E9E419}
static const GUID guid_seekbar_screenshot_height =
{ 0xb3534ac9, 0x609a, 0x4a6e, { 0x8b, 0x6a, 0x75, 0x22, 0xd7, 0xe9, 0xe4, 0x19 } };
// {0936311D-C065-4C72-806C-1F68403B385D}
static const GUID guid_seekbar_screenshot_filename_format =
{ 0x936311d, 0xc065, 0x4c72, { 0x80, 0x6c, 0x1f, 0x68, 0x40, 0x3b, 0x38, 0x5d } };
// {BE7CFD4F-C880-4DD1-801C-81740D0A2CEF}
static const GUID guid_scroll_to_seek =
{ 0xbe7cfd4f, 0xc880, 0x4dd1, { 0x80, 0x1c, 0x81, 0x74, 0xd, 0xa, 0x2c, 0xef } };
static advconfig_branch_factory g_seekbar_screenshot_branch("Screenshots", guid_seekbar_screenshot_branch, guid_seekbar_branch, 0.0);
static advconfig_integer_factory g_seekbar_screenshot_width ("Horizontal size (pixels)", guid_seekbar_screenshot_width, guid_seekbar_screenshot_branch, 0.1, 1024, 16, 8192);
static advconfig_integer_factory g_seekbar_screenshot_height("Vertical size (pixels)", guid_seekbar_screenshot_height, guid_seekbar_screenshot_branch, 0.2, 1024, 16, 8192);
static advconfig_string_factory g_seekbar_screenshot_filename_format("File format template (either absolute path+filename or just filename)", guid_seekbar_screenshot_filename_format, guid_seekbar_screenshot_branch, 0.3, "%artist% - %tracknumber%. %title%.png");
static advconfig_checkbox_factory g_seekbar_scroll_to_seek("Scroll mouse wheel to seek", guid_scroll_to_seek, guid_seekbar_branch, 0.0, true);
static bool is_outside(CPoint point, CRect r, int N, bool horizontal)
{
if (!horizontal)
{
std::swap(point.x, point.y);
std::swap(r.right, r.bottom);
std::swap(r.left, r.top);
}
return point.y < -2 * N || point.y > r.bottom - r.top + 2 * N ||
point.x < -N || point.x > r.right - r.left + N;
}
struct menu_item_info
{
menu_item_info(UINT f_mask, UINT f_type, UINT f_state, LPTSTR dw_type_data, UINT w_id, HMENU h_submenu = 0, HBITMAP hbmp_checked = 0, HBITMAP hbmp_unchecked = 0, ULONG_PTR dw_item_data = 0, HBITMAP hbmp_item = 0)
{
MENUITEMINFO mi =
{
sizeof(mi), f_mask | MIIM_TYPE | MIIM_STATE | MIIM_ID, f_type, f_state, w_id, h_submenu, hbmp_checked, hbmp_unchecked, dw_item_data, dw_type_data, _tcslen(dw_type_data), hbmp_item
};
this->mi = mi;
}
mutable MENUITEMINFO mi;
operator LPMENUITEMINFO () const
{
return &mi;
}
};
namespace wave
{
void seekbar_window::on_wm_paint(HDC dc)
{
GetClientRect(client_rect);
if (!(client_rect.right > 1 && client_rect.bottom > 1))
{
ValidateRect(0);
return;
}
if (!fe->frontend && !initializing_graphics)
{
initialize_frontend();
}
if (fe->frontend)
{
fe->frontend->clear();
fe->frontend->draw();
fe->frontend->present();
}
ValidateRect(0);
}
void seekbar_window::on_wm_size(UINT wparam, CSize size)
{
if (size.cx < 1 || size.cy < 1)
return;
set_orientation(size.cx >= size.cy
? config::orientation_horizontal
: config::orientation_vertical);
lock_guard<recursive_mutex> lk(fe->mutex);
fe->callback->set_size(wave::size(size.cx, size.cy));
if (fe->frontend)
fe->frontend->on_state_changed((visual_frontend::state)(visual_frontend::state_size | visual_frontend::state_orientation));
}
void seekbar_window::on_wm_timer(UINT_PTR wparam)
{
if (wparam == REPAINT_TIMER_ID && core_api::are_services_available())
{
static_api_ptr_t<playback_control> pc;
double t = pc->playback_get_position();
set_playback_time(t);
}
}
LRESULT seekbar_window::on_wm_create(LPCREATESTRUCT)
{
util::ScopedEvent se("Windowing", "seekbar creation");
fe->callback.reset(new frontend_callback_impl);
fe->conf.reset(new frontend_config_impl(settings));
fe->callback->set_waveform(placeholder_waveform);
wait_for_frontend_module_load();
decltype(deferred_init) v;
v.swap(deferred_init);
for (auto f : v) {
f();
}
apply_settings();
static_api_ptr_t<player> p;
p->register_waveform_listener(this);
return 0;
}
void seekbar_window::on_wm_destroy()
{
static_api_ptr_t<player> p;
p->deregister_waveform_listener(this);
if (repaint_timer_id)
KillTimer(repaint_timer_id);
repaint_timer_id = 0;
lock_guard<recursive_mutex> lk(fe->mutex);
fe->clear();
}
LRESULT seekbar_window::on_wm_erasebkgnd(HDC dc)
{
SetDCBrushColor(dc, color_to_xbgr(fe->callback->get_color(config::color_background)));
auto sz = fe->callback->get_size();
CRect r(0, 0, sz.cx, sz.cy);
FillRect(dc, r, GetStockBrush(DC_BRUSH));
return 1;
}
void seekbar_window::on_wm_lbuttondown(UINT wparam, CPoint point)
{
drag_state = (wparam & MK_CONTROL) ? MouseDragSelection : MouseDragSeeking;
if (!tooltip)
{
tooltip.reset(new seek_tooltip(*this));
seek_callbacks.push_back(tooltip);
}
lock_guard<recursive_mutex> lk(fe->mutex);
if (drag_state == MouseDragSeeking)
{
fe->callback->set_seeking(true);
for each(auto cb in seek_callbacks)
if (auto p = cb.lock())
p->on_seek_begin();
set_seek_position(point);
if (fe->frontend)
{
fe->frontend->on_state_changed(visual_frontend::state_position);
}
}
else
{
drag_data.to = drag_data.from = compute_position(point);
}
SetCapture();
}
void seekbar_window::on_wm_lbuttonup(UINT wparam, CPoint point)
{
lock_guard<recursive_mutex> lk(fe->mutex);
ReleaseCapture();
if (drag_state == MouseDragSeeking)
{
bool completed = fe->callback->is_seeking();
if (completed)
{
fe->callback->set_seeking(false);
set_seek_position(point);
if (fe->frontend)
fe->frontend->on_state_changed(visual_frontend::state_position);
static_api_ptr_t<playback_control> pc;
pc->playback_seek(fe->callback->get_seek_position());
}
for each(auto cb in seek_callbacks)
if (auto p = cb.lock())
p->on_seek_end(!completed);
}
else if (drag_state == MouseDragSelection)
{
auto source = fe->displayed_song;
if (source.is_valid() && drag_data.to >= 0)
{
drag_data.to = compute_position(point);
auto from = std::min(drag_data.from, drag_data.to);
auto to = std::max(drag_data.from, drag_data.to);
if (clipboard::render_audio(source, from, to)) {
console::formatter() << "seekbar: rendered waveform from " <<
from << " through " << to << " to the clipboard of track " <<
source->get_location();
}
else {
console::formatter() << "seekbar: failed to render waveform to clipboard";
}
}
}
drag_state = MouseDragNone;
}
void seekbar_window::on_wm_mousemove(UINT wparam, CPoint point)
{
if (drag_state != MouseDragNone)
{
if (last_seek_point == point)
return;
last_seek_point = point;
lock_guard<recursive_mutex> lk(fe->mutex);
CRect r;
GetWindowRect(r);
int const N = 40;
bool horizontal = fe->callback->get_orientation() == config::orientation_horizontal;
bool outside = is_outside(point, r, N, horizontal);
if (drag_state == MouseDragSeeking)
{
fe->callback->set_seeking(!outside);
set_seek_position(point);
if (fe->frontend)
{
fe->frontend->on_state_changed(visual_frontend::state_position);
}
}
else if (drag_state == MouseDragSelection)
{
drag_data.to = outside
? -1.0f
: compute_position(point);
}
}
}
LRESULT seekbar_window::on_wm_mousewheel(UINT, short z_delta, CPoint)
{
std::pair<int64_t, double> const skip_table[] = {
{ 50, 1 },
{ 100, 2 },
{ 250, 5 },
{ 500, 10 },
{ 1500, 30 },
{ 3000, 1*60 },
{ 6000, 2*60 },
{ 15000, 5*60 },
{ 30000, 10*60 },
{ 90000, 30*60 }
};
if (!g_seekbar_scroll_to_seek)
return 0;
static_api_ptr_t<playback_control> pc;
auto pos = pc->playback_get_position();
auto length = pc->playback_get_length();
double skip_size = 1*60*60;
for (auto p : skip_table) {
if ((int64_t)length < p.first) {
skip_size = p.second;
break;
}
}
pos += skip_size * z_delta / 120.0f;
pc->playback_seek(pos);
return 0;
}
void seekbar_window::on_wm_rbuttonup(UINT wparam, CPoint point)
{
if (forward_rightclick())
{
SetMsgHandled(FALSE);
return;
}
WTL::CMenu m;
m.CreatePopupMenu();
m.InsertMenu(-1, MF_BYPOSITION | MF_STRING, 3, L"Configure");
ClientToScreen(&point);
BOOL ans = m.TrackPopupMenu(TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, *this, 0);
config::frontend old_kind = settings.active_frontend_kind;
switch(ans)
{
case 3:
if (config_dialog)
config_dialog->BringWindowToTop();
else
{
config_dialog.reset(new configuration_dialog(*this));
config_dialog->Create(*this);
}
break;
default:
return;
}
}
}