Fix AudioMappingView scrolling so that only the body of the grid is scrolled.
[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 using dcp::locale_convert;
50
51 #define INDICATOR_SIZE 20
52 #define GRID_SPACING 32
53 #define LEFT_WIDTH (GRID_SPACING * 3)
54 #define TOP_HEIGHT (GRID_SPACING * 2)
55
56 enum {
57         ID_off = 1,
58         ID_full = 2,
59         ID_minus6dB = 3,
60         ID_edit = 4
61 };
62
63 AudioMappingView::AudioMappingView (wxWindow* parent)
64         : wxPanel (parent, wxID_ANY)
65         , _menu_input (0)
66         , _menu_output (1)
67 {
68         _menu = new wxMenu;
69         _menu->Append (ID_off, _("Off"));
70         _menu->Append (ID_full, _("Full"));
71         _menu->Append (ID_minus6dB, _("-6dB"));
72         _menu->Append (ID_edit, _("Edit..."));
73
74         _body = new wxPanel (this, wxID_ANY);
75         _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
76         _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
77
78         Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
79         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::off, this), ID_off);
80         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::full, this), ID_full);
81         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::minus6dB, this), ID_minus6dB);
82         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
83         Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
84         _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
85         _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
86         _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
87         _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
88         _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
89         _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
90         _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
91         _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
92         _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
93         _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
94         _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
95         _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
96         _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
97         _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
98         _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
99         _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
100         _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
101         _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
102         _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
103         _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
104 }
105
106 void
107 AudioMappingView::size (wxSizeEvent& ev)
108 {
109         setup ();
110         ev.Skip ();
111 }
112
113 void
114 AudioMappingView::setup ()
115 {
116         wxSize const s = GetSize();
117         int const w = _vertical_scroll->GetSize().GetWidth();
118         int const h = _horizontal_scroll->GetSize().GetHeight();
119
120         _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
121         _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
122
123         _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
124
125         _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
126         _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
127
128         _vertical_scroll->SetScrollbar (
129                 _vertical_scroll->GetThumbPosition(),
130                 s.GetHeight() - h - 8,
131                 GRID_SPACING * (2 + _input_channels.size()),
132                 GRID_SPACING,
133                 true
134                 );
135
136         _horizontal_scroll->SetScrollbar (
137                 _horizontal_scroll->GetThumbPosition(),
138                 s.GetWidth() - w - 8,
139                 GRID_SPACING * (3 + _output_channels.size()),
140                 GRID_SPACING,
141                 true);
142 }
143
144 void
145 AudioMappingView::scroll ()
146 {
147         Refresh ();
148 }
149
150 void
151 AudioMappingView::paint_static (wxDC& dc, wxGraphicsContext* gc)
152 {
153         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
154         dc.SetFont (wxSWISS_FONT->Bold());
155         wxCoord label_width;
156         wxCoord label_height;
157
158         /* DCP label at the top */
159
160         dc.GetTextExtent (_("DCP"), &label_width, &label_height);
161         dc.DrawText (_("DCP"), LEFT_WIDTH + (_output_channels.size() * GRID_SPACING - label_width) / 2, (GRID_SPACING - label_height) / 2);
162
163         /* Content label on the left */
164
165         dc.GetTextExtent (_("Content"), &label_width, &label_height);
166         dc.DrawRotatedText (
167                 _("Content"),
168                 (GRID_SPACING - label_height) / 2,
169                 TOP_HEIGHT + (_input_channels.size() * GRID_SPACING + label_width) / 2,
170                 90
171                 );
172
173         dc.SetFont (*wxSWISS_FONT);
174         gc->SetPen (*wxBLACK_PEN);
175 }
176
177 void
178 AudioMappingView::paint_column_labels (wxDC& dc, wxGraphicsContext* gc)
179 {
180         wxCoord label_width;
181         wxCoord label_height;
182         int N = 0;
183         BOOST_FOREACH (string i, _output_channels) {
184                 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
185                 dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2);
186                 ++N;
187         }
188
189         wxGraphicsPath lines = gc->CreatePath ();
190         lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING);
191         lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING);
192         lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING * 2);
193         lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING * 2);
194         gc->StrokePath (lines);
195 }
196
197 void
198 AudioMappingView::paint_column_lines (wxGraphicsContext* gc)
199 {
200         wxGraphicsPath lines = gc->CreatePath ();
201         for (size_t i = 0; i < _output_channels.size(); ++i) {
202                 lines.MoveToPoint    (LEFT_WIDTH + GRID_SPACING * i, GRID_SPACING);
203                 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
204         }
205         lines.MoveToPoint    (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), GRID_SPACING);
206         lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
207         gc->StrokePath (lines);
208 }
209
210 void
211 AudioMappingView::paint_row_labels (wxDC& dc, wxGraphicsContext* gc)
212 {
213         wxCoord label_width;
214         wxCoord label_height;
215         wxGraphicsPath lines = gc->CreatePath ();
216
217         /* Row channel labels */
218
219         int N = 0;
220         BOOST_FOREACH (string i, _input_channels) {
221                 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
222                 dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2);
223                 ++N;
224         }
225
226         /* Vertical lines on the left */
227
228         for (int i = 1; i < 3; ++i) {
229                 lines.MoveToPoint    (GRID_SPACING * i, TOP_HEIGHT);
230                 lines.AddLineToPoint (GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
231         }
232
233         /* Group labels and lines */
234
235         int y = TOP_HEIGHT;
236         BOOST_FOREACH (Group i, _input_groups) {
237                 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
238                 int const height = (i.to - i.from + 1) * GRID_SPACING;
239                 dc.DrawRotatedText (
240                         std_to_wx(i.name),
241                         GRID_SPACING + (GRID_SPACING - label_height) / 2,
242                         y + (height + label_width) / 2,
243                         90
244                         );
245                 lines.MoveToPoint    (GRID_SPACING,     y);
246                 lines.AddLineToPoint (GRID_SPACING * 2, y);
247                 y += height;
248         }
249
250         lines.MoveToPoint    (GRID_SPACING,     y);
251         lines.AddLineToPoint (GRID_SPACING * 2, y);
252
253         gc->StrokePath (lines);
254 }
255
256 void
257 AudioMappingView::paint_row_lines (wxGraphicsContext* gc)
258 {
259         wxGraphicsPath lines = gc->CreatePath ();
260         for (size_t i = 0; i < _input_channels.size(); ++i) {
261                 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * i);
262                 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * i);
263         }
264         lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
265         lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
266         gc->StrokePath (lines);
267 }
268
269 void
270 AudioMappingView::paint_indicators (wxDC& dc)
271 {
272         for (size_t x = 0; x < _output_channels.size(); ++x) {
273                 for (size_t y = 0; y < _input_channels.size(); ++y) {
274                         dc.SetBrush (*wxWHITE_BRUSH);
275                         dc.DrawRectangle (
276                                 wxRect(
277                                         LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
278                                         TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
279                                         INDICATOR_SIZE, INDICATOR_SIZE
280                                         )
281                                 );
282
283                         float const value_dB = 20 * log10 (_map.get(y, x));
284                         int const range = 18;
285                         int height = 0;
286                         if (value_dB > -range) {
287                                 height = INDICATOR_SIZE * (1 + value_dB / range);
288                         }
289
290                         dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
291                         dc.DrawRectangle (
292                                 wxRect(
293                                         LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
294                                         TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
295                                         INDICATOR_SIZE, height
296                                         )
297                                 );
298                 }
299         }
300 }
301
302 static
303 void clip (wxDC& dc, wxGraphicsContext* gc, int x, int y, int w, int h)
304 {
305         dc.SetClippingRegion (x, y, w, h);
306         gc->Clip (x, y, w, h);
307 }
308
309 static
310 void translate (wxDC& dc, wxGraphicsContext* gc, int x, int y)
311 {
312         gc->PushState ();
313         gc->Translate (-x, -y);
314         dc.SetLogicalOrigin (x, y);
315 }
316
317 static
318 void restore (wxDC& dc, wxGraphicsContext* gc)
319 {
320         dc.SetLogicalOrigin (0, 0);
321         gc->PopState ();
322         dc.DestroyClippingRegion ();
323         gc->ResetClip ();
324 }
325
326 void
327 AudioMappingView::paint ()
328 {
329         wxPaintDC dc (_body);
330
331         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
332         if (!gc) {
333                 return;
334         }
335
336         int const hs = _horizontal_scroll->GetThumbPosition ();
337         int const vs = _vertical_scroll->GetThumbPosition ();
338
339         paint_static (dc, gc);
340
341         clip (dc, gc, LEFT_WIDTH, 0, GRID_SPACING * _output_channels.size(), GRID_SPACING * (2 + _input_channels.size()));
342         translate (dc, gc, hs, 0);
343         paint_column_labels (dc, gc);
344         restore (dc, gc);
345
346         clip (dc, gc, 0, TOP_HEIGHT, GRID_SPACING * (3 + _output_channels.size()), GRID_SPACING * _input_channels.size());
347         translate (dc, gc, 0, vs);
348         paint_row_labels (dc, gc);
349         restore (dc, gc);
350
351         clip (dc, gc, GRID_SPACING * 2, TOP_HEIGHT, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size());
352         translate (dc, gc, hs, vs);
353         paint_row_lines (gc);
354         restore (dc, gc);
355
356         clip (dc, gc, LEFT_WIDTH, GRID_SPACING, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size());
357         translate (dc, gc, hs, vs);
358         paint_column_lines (gc);
359         restore (dc, gc);
360
361         clip (dc, gc, LEFT_WIDTH, TOP_HEIGHT, GRID_SPACING * _output_channels.size(), GRID_SPACING * _input_channels.size());
362         translate (dc, gc, hs, vs);
363         paint_indicators (dc);
364         restore (dc, gc);
365
366         delete gc;
367 }
368
369 optional<pair<int, int> >
370 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
371 {
372         int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
373         int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
374
375         if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
376                 return optional<pair<int, int> >();
377         }
378
379         int const input = (y - TOP_HEIGHT) / GRID_SPACING;
380         int const output = (x - LEFT_WIDTH) / GRID_SPACING;
381
382         if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
383                 return optional<pair<int, int> >();
384         }
385
386         return make_pair (input, output);
387 }
388
389 void
390 AudioMappingView::left_down (wxMouseEvent& ev)
391 {
392         optional<pair<int, int> > channels = mouse_event_to_channels (ev);
393         if (!channels) {
394                 return;
395         }
396
397         if (_map.get(channels->first, channels->second) > 0) {
398                 _map.set (channels->first, channels->second, 0);
399         } else {
400                 _map.set (channels->first, channels->second, 1);
401         }
402
403         map_values_changed ();
404 }
405
406 void
407 AudioMappingView::right_down (wxMouseEvent& ev)
408 {
409         optional<pair<int, int> > channels = mouse_event_to_channels (ev);
410         if (!channels) {
411                 return;
412         }
413
414         _menu_input = channels->first;
415         _menu_output = channels->second;
416         PopupMenu (_menu, ev.GetPosition());
417 }
418
419 void
420 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
421 {
422         if (ev.ShiftDown()) {
423                 _horizontal_scroll->SetThumbPosition (
424                         _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
425                         );
426
427         } else {
428                 _vertical_scroll->SetThumbPosition (
429                         _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
430                         );
431         }
432         Refresh ();
433 }
434
435 /** Called when any gain value has changed */
436 void
437 AudioMappingView::map_values_changed ()
438 {
439         Changed (_map);
440         _last_tooltip_channels = optional<pair<int, int> >();
441         Refresh ();
442 }
443
444 void
445 AudioMappingView::off ()
446 {
447         _map.set (_menu_input, _menu_output, 0);
448         map_values_changed ();
449 }
450
451 void
452 AudioMappingView::full ()
453 {
454         _map.set (_menu_input, _menu_output, 1);
455         map_values_changed ();
456 }
457
458 void
459 AudioMappingView::minus6dB ()
460 {
461         _map.set (_menu_input, _menu_output, pow (10, -6.0 / 20));
462         map_values_changed ();
463 }
464
465 void
466 AudioMappingView::edit ()
467 {
468         int const d = _menu_output - 1;
469
470         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output - 1, _map.get(_menu_input, d));
471         if (dialog->ShowModal() == wxID_OK) {
472                 _map.set (_menu_input, d, dialog->value ());
473                 map_values_changed ();
474         }
475
476         dialog->Destroy ();
477 }
478
479 void
480 AudioMappingView::set (AudioMapping map)
481 {
482         _map = map;
483         Refresh ();
484 }
485
486 void
487 AudioMappingView::set_input_channels (vector<string> const & names)
488 {
489         _input_channels = names;
490         setup ();
491         Refresh ();
492 }
493
494 void
495 AudioMappingView::set_output_channels (vector<string> const & names)
496 {
497         _output_channels = names;
498         setup ();
499         Refresh ();
500 }
501
502 void
503 AudioMappingView::motion (wxMouseEvent& ev)
504 {
505         optional<pair<int, int> > channels = mouse_event_to_channels (ev);
506         if (!channels) {
507                 SetToolTip ("");
508                 _last_tooltip_channels = channels;
509                 return;
510         }
511
512         if (channels != _last_tooltip_channels) {
513                 wxString s;
514                 float const gain = _map.get(channels->first, channels->second);
515                 if (gain == 0) {
516                         s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), channels->first + 1, channels->second + 1);
517                 } else if (gain == 1) {
518                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), channels->first + 1, channels->second + 1);
519                 } else {
520                         float const dB = 20 * log10 (gain);
521                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), channels->first + 1, channels->second + 1, dB);
522                 }
523
524                 SetToolTip (s + " " + _("Right click to change gain."));
525                 _last_tooltip_channels = channels;
526         }
527
528         ev.Skip ();
529 }
530
531 void
532 AudioMappingView::set_input_groups (vector<Group> const & groups)
533 {
534         _input_groups = groups;
535 }