Supporters update.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
1 /*
2     Copyright (C) 2013-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/wx/audio_mapping_view.cc
22  *  @brief AudioMappingView class and helpers.
23  */
24
25 #include "audio_mapping_view.h"
26 #include "wx_util.h"
27 #include "audio_gain_dialog.h"
28 #include "lib/audio_mapping.h"
29 #include "lib/util.h"
30 #include <dcp/locale_convert.h>
31 #include <dcp/types.h>
32 #include <wx/wx.h>
33 #include <wx/renderer.h>
34 #include <wx/grid.h>
35 #include <wx/graphics.h>
36 #include <boost/foreach.hpp>
37 #include <iostream>
38
39 using std::cout;
40 using std::list;
41 using std::string;
42 using std::min;
43 using std::max;
44 using std::vector;
45 using std::pair;
46 using std::make_pair;
47 using boost::shared_ptr;
48 using boost::optional;
49 #if BOOST_VERSION >= 106100
50 using namespace boost::placeholders;
51 #endif
52 using dcp::locale_convert;
53
54 #define INDICATOR_SIZE 20
55 #define GRID_SPACING 32
56 #define LEFT_WIDTH (GRID_SPACING * 3)
57 #define TOP_HEIGHT (GRID_SPACING * 2)
58
59 enum {
60         ID_off = 1,
61         ID_full = 2,
62         ID_minus6dB = 3,
63         ID_edit = 4
64 };
65
66 AudioMappingView::AudioMappingView (wxWindow* parent)
67         : wxPanel (parent, wxID_ANY)
68         , _menu_input (0)
69         , _menu_output (1)
70 {
71         _menu = new wxMenu;
72         _menu->Append (ID_off, _("Off"));
73         _menu->Append (ID_full, _("Full"));
74         _menu->Append (ID_minus6dB, _("-6dB"));
75         _menu->Append (ID_edit, _("Edit..."));
76
77         _body = new wxPanel (this, wxID_ANY);
78         _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
79         _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
80
81         Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
82         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::off, this), ID_off);
83         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::full, this), ID_full);
84         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::minus6dB, this), ID_minus6dB);
85         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
86         Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
87         _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
88         _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
89         _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
90         _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
91         _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
92         _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
93         _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
94         _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
95         _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
96         _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
97         _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
98         _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
99         _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
100         _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
101         _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
102         _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
103         _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
104         _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
105         _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
106         _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
107 }
108
109 void
110 AudioMappingView::size (wxSizeEvent& ev)
111 {
112         setup ();
113         ev.Skip ();
114 }
115
116 void
117 AudioMappingView::setup ()
118 {
119         wxSize const s = GetSize();
120         int const w = _vertical_scroll->GetSize().GetWidth();
121         int const h = _horizontal_scroll->GetSize().GetHeight();
122
123         _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
124         _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
125
126         _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
127
128         _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
129         _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
130
131         _vertical_scroll->SetScrollbar (
132                 _vertical_scroll->GetThumbPosition(),
133                 s.GetHeight() - h - 8,
134                 GRID_SPACING * (2 + _input_channels.size()),
135                 GRID_SPACING,
136                 true
137                 );
138
139         _horizontal_scroll->SetScrollbar (
140                 _horizontal_scroll->GetThumbPosition(),
141                 s.GetWidth() - w - 8,
142                 GRID_SPACING * (3 + _output_channels.size()),
143                 GRID_SPACING,
144                 true);
145 }
146
147 void
148 AudioMappingView::scroll ()
149 {
150         Refresh ();
151 }
152
153 void
154 AudioMappingView::paint_static (wxDC& dc)
155 {
156         dc.SetFont (wxSWISS_FONT->Bold());
157         wxCoord label_width;
158         wxCoord label_height;
159
160         /* DCP label at the top */
161
162         dc.GetTextExtent (_("DCP"), &label_width, &label_height);
163         dc.DrawText (_("DCP"), LEFT_WIDTH + (_output_channels.size() * GRID_SPACING - label_width) / 2, (GRID_SPACING - label_height) / 2);
164
165         /* Content label on the left */
166
167         dc.GetTextExtent (_("Content"), &label_width, &label_height);
168         dc.DrawRotatedText (
169                 _("Content"),
170                 (GRID_SPACING - label_height) / 2,
171                 TOP_HEIGHT + (_input_channels.size() * GRID_SPACING + label_width) / 2,
172                 90
173                 );
174
175         dc.SetFont (*wxSWISS_FONT);
176 }
177
178 void
179 AudioMappingView::paint_column_labels (wxDC& dc)
180 {
181         wxCoord label_width;
182         wxCoord label_height;
183         int N = 0;
184         BOOST_FOREACH (string i, _output_channels) {
185                 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
186                 dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2);
187                 ++N;
188         }
189
190         dc.DrawLine(wxPoint(LEFT_WIDTH, GRID_SPACING), wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING));
191         dc.DrawLine(wxPoint(LEFT_WIDTH, GRID_SPACING * 2), wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING * 2));
192 }
193
194 void
195 AudioMappingView::paint_column_lines (wxDC& dc)
196 {
197         for (size_t i = 0; i < _output_channels.size(); ++i) {
198                 dc.DrawLine (
199                         wxPoint(LEFT_WIDTH + GRID_SPACING * i, GRID_SPACING),
200                         wxPoint(LEFT_WIDTH + GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING)
201                         );
202         }
203
204         dc.DrawLine (
205                 wxPoint(LEFT_WIDTH + GRID_SPACING * _output_channels.size(), GRID_SPACING),
206                 wxPoint(LEFT_WIDTH + GRID_SPACING * _output_channels.size(), TOP_HEIGHT + _input_channels.size() * GRID_SPACING)
207                 );
208 }
209
210 void
211 AudioMappingView::paint_row_labels (wxDC& dc)
212 {
213         wxCoord label_width;
214         wxCoord label_height;
215
216         /* Row channel labels */
217
218         int N = 0;
219         BOOST_FOREACH (string i, _input_channels) {
220                 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
221                 dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2);
222                 ++N;
223         }
224
225         /* Vertical lines on the left */
226
227         for (int i = 1; i < 3; ++i) {
228                 dc.DrawLine (
229                         wxPoint(GRID_SPACING * i, TOP_HEIGHT),
230                         wxPoint(GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING)
231                         );
232         }
233
234         /* Group labels and lines */
235
236         int y = TOP_HEIGHT;
237         BOOST_FOREACH (Group i, _input_groups) {
238                 int const height = (i.to - i.from + 1) * GRID_SPACING;
239                 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
240                 if (label_width > height) {
241                         label_width = height - 8;
242                 }
243
244                 {
245                         int yp = y;
246                         if ((yp - 2 * GRID_SPACING) < dc.GetLogicalOrigin().y) {
247                                 yp += dc.GetLogicalOrigin().y;
248                         }
249
250                         wxCoord old_x, old_y, old_width, old_height;
251                         dc.GetClippingBox (&old_x, &old_y, &old_width, &old_height);
252                         dc.DestroyClippingRegion ();
253                         dc.SetClippingRegion (GRID_SPACING, yp + 4, GRID_SPACING, height - 8);
254
255                         dc.DrawRotatedText (
256                                 std_to_wx(i.name),
257                                 GRID_SPACING + (GRID_SPACING - label_height) / 2,
258                                 y + (height + label_width) / 2,
259                                 90
260                                 );
261
262                         dc.DestroyClippingRegion ();
263                         dc.SetClippingRegion (old_x, old_y, old_width, old_height);
264                 }
265
266                 dc.DrawLine (wxPoint(GRID_SPACING, y), wxPoint(GRID_SPACING * 2, y));
267                 y += height;
268         }
269
270         dc.DrawLine (wxPoint(GRID_SPACING, y), wxPoint(GRID_SPACING * 2, y));
271         }
272
273 void
274 AudioMappingView::paint_row_lines (wxDC& dc)
275 {
276         for (size_t i = 0; i < _input_channels.size(); ++i) {
277                 dc.DrawLine (
278                         wxPoint(GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * i),
279                         wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * i)
280                         );
281         }
282         dc.DrawLine (
283                 wxPoint(GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * _input_channels.size()),
284                 wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * _input_channels.size())
285                 );
286 }
287
288 void
289 AudioMappingView::paint_indicators (wxDC& dc)
290 {
291         /* _{input,output}_channels and _map may not always be in sync, be careful here */
292         size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
293         size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
294
295         for (size_t x = 0; x < output; ++x) {
296                 for (size_t y = 0; y < input; ++y) {
297                         dc.SetBrush (*wxWHITE_BRUSH);
298                         dc.DrawRectangle (
299                                 wxRect(
300                                         LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
301                                         TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
302                                         INDICATOR_SIZE, INDICATOR_SIZE
303                                         )
304                                 );
305
306                         float const value_dB = 20 * log10 (_map.get(y, x));
307                         int const range = 18;
308                         int height = 0;
309                         if (value_dB > -range) {
310                                 height = INDICATOR_SIZE * (1 + value_dB / range);
311                         }
312
313                         dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
314                         dc.DrawRectangle (
315                                 wxRect(
316                                         LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
317                                         TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
318                                         INDICATOR_SIZE, height
319                                         )
320                                 );
321                 }
322         }
323 }
324
325 static
326 void clip (wxDC& dc, int x, int y, int w, int h)
327 {
328         dc.SetClippingRegion (x, y, w, h);
329 }
330
331 static
332 void translate (wxDC& dc, int x, int y)
333 {
334         dc.SetLogicalOrigin (x, y);
335 }
336
337 static
338 void restore (wxDC& dc)
339 {
340         dc.SetLogicalOrigin (0, 0);
341         dc.DestroyClippingRegion ();
342 }
343
344 void
345 AudioMappingView::paint ()
346 {
347         wxPaintDC dc (_body);
348
349         int const hs = _horizontal_scroll->GetThumbPosition ();
350         int const vs = _vertical_scroll->GetThumbPosition ();
351
352         paint_static (dc);
353
354         clip (dc, LEFT_WIDTH, 0, GRID_SPACING * _output_channels.size(), GRID_SPACING * (2 + _input_channels.size()));
355         translate (dc, hs, 0);
356         paint_column_labels (dc);
357         restore (dc);
358
359         clip (dc, 0, TOP_HEIGHT, GRID_SPACING * (3 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
360         translate (dc, 0, vs);
361         paint_row_labels (dc);
362         restore (dc);
363
364         clip (dc, GRID_SPACING * 2, TOP_HEIGHT, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
365         translate (dc, hs, vs);
366         paint_row_lines (dc);
367         restore (dc);
368
369         clip (dc, LEFT_WIDTH, GRID_SPACING, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * (1 + _input_channels.size()));
370         translate (dc, hs, vs);
371         paint_column_lines (dc);
372         restore (dc);
373
374         clip (dc, LEFT_WIDTH, TOP_HEIGHT, GRID_SPACING * _output_channels.size(), GRID_SPACING * _input_channels.size());
375         translate (dc, hs, vs);
376         paint_indicators (dc);
377         restore (dc);
378 }
379
380 optional<pair<int, int> >
381 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
382 {
383         int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
384         int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
385
386         if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
387                 return optional<pair<int, int> >();
388         }
389
390         int const input = (y - TOP_HEIGHT) / GRID_SPACING;
391         int const output = (x - LEFT_WIDTH) / GRID_SPACING;
392
393         if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
394                 return optional<pair<int, int> >();
395         }
396
397         return make_pair (input, output);
398 }
399
400 optional<string>
401 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
402 {
403         int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
404         if (x < GRID_SPACING || x > (2 * GRID_SPACING)) {
405                 return optional<string>();
406         }
407
408         int y = (ev.GetY() + _vertical_scroll->GetThumbPosition() - (GRID_SPACING * 2)) / GRID_SPACING;
409         BOOST_FOREACH (Group i, _input_groups) {
410                 if (i.from <= y && y <= i.to) {
411                         return i.name;
412                 }
413         }
414
415         return optional<string>();
416 }
417
418 void
419 AudioMappingView::left_down (wxMouseEvent& ev)
420 {
421         optional<pair<int, int> > channels = mouse_event_to_channels (ev);
422         if (!channels) {
423                 return;
424         }
425
426         if (_map.get(channels->first, channels->second) > 0) {
427                 _map.set (channels->first, channels->second, 0);
428         } else {
429                 _map.set (channels->first, channels->second, 1);
430         }
431
432         map_values_changed ();
433 }
434
435 void
436 AudioMappingView::right_down (wxMouseEvent& ev)
437 {
438         optional<pair<int, int> > channels = mouse_event_to_channels (ev);
439         if (!channels) {
440                 return;
441         }
442
443         _menu_input = channels->first;
444         _menu_output = channels->second;
445         PopupMenu (_menu, ev.GetPosition());
446 }
447
448 void
449 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
450 {
451         if (ev.ShiftDown()) {
452                 _horizontal_scroll->SetThumbPosition (
453                         _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
454                         );
455
456         } else {
457                 _vertical_scroll->SetThumbPosition (
458                         _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
459                         );
460         }
461         Refresh ();
462 }
463
464 /** Called when any gain value has changed */
465 void
466 AudioMappingView::map_values_changed ()
467 {
468         Changed (_map);
469         _last_tooltip_channels = optional<pair<int, int> >();
470         Refresh ();
471 }
472
473 void
474 AudioMappingView::off ()
475 {
476         _map.set (_menu_input, _menu_output, 0);
477         map_values_changed ();
478 }
479
480 void
481 AudioMappingView::full ()
482 {
483         _map.set (_menu_input, _menu_output, 1);
484         map_values_changed ();
485 }
486
487 void
488 AudioMappingView::minus6dB ()
489 {
490         _map.set (_menu_input, _menu_output, pow (10, -6.0 / 20));
491         map_values_changed ();
492 }
493
494 void
495 AudioMappingView::edit ()
496 {
497         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output, _map.get(_menu_input, _menu_output));
498         if (dialog->ShowModal() == wxID_OK) {
499                 _map.set (_menu_input, _menu_output, dialog->value ());
500                 map_values_changed ();
501         }
502
503         dialog->Destroy ();
504 }
505
506 void
507 AudioMappingView::set (AudioMapping map)
508 {
509         _map = map;
510         Refresh ();
511 }
512
513 void
514 AudioMappingView::set_input_channels (vector<string> const & names)
515 {
516         _input_channels = names;
517         setup ();
518         Refresh ();
519 }
520
521 void
522 AudioMappingView::set_output_channels (vector<string> const & names)
523 {
524         _output_channels = names;
525         setup ();
526         Refresh ();
527 }
528
529 void
530 AudioMappingView::motion (wxMouseEvent& ev)
531 {
532         optional<pair<int, int> > channels = mouse_event_to_channels (ev);
533         if (channels) {
534                 if (channels != _last_tooltip_channels) {
535                         wxString s;
536                         float const gain = _map.get(channels->first, channels->second);
537                         if (gain == 0) {
538                                 s = wxString::Format (
539                                         _("No audio will be passed from content channel %d to DCP channel %d."),
540                                         channels->first + 1, channels->second + 1
541                                         );
542                         } else if (gain == 1) {
543                                 s = wxString::Format (
544                                         _("Audio will be passed from content channel %d to DCP channel %d unaltered."),
545                                         channels->first + 1, channels->second + 1
546                                         );
547                         } else {
548                                 float const dB = 20 * log10 (gain);
549                                 s = wxString::Format (
550                                         _("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."),
551                                         channels->first + 1, channels->second + 1, dB
552                                         );
553                         }
554
555                         SetToolTip (s + " " + _("Right click to change gain."));
556                 }
557         } else {
558                 optional<string> group = mouse_event_to_input_group_name (ev);
559                 if (group) {
560                         SetToolTip (std_to_wx(*group));
561                 } else {
562                         SetToolTip ("");
563                 }
564         }
565
566         _last_tooltip_channels = channels;
567         ev.Skip ();
568 }
569
570 void
571 AudioMappingView::set_input_groups (vector<Group> const & groups)
572 {
573         _input_groups = groups;
574 }