Remove scrolling in AudioMappingView.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
1 /*
2     Copyright (C) 2013-2021 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
22 /** @file  src/wx/audio_mapping_view.cc
23  *  @brief AudioMappingView class and helpers.
24  */
25
26
27 #include "audio_gain_dialog.h"
28 #include "audio_mapping_view.h"
29 #include "wx_ptr.h"
30 #include "wx_util.h"
31 #include "lib/audio_mapping.h"
32 #include "lib/maths_util.h"
33 #include <dcp/locale_convert.h>
34 #include <dcp/types.h>
35 #include <dcp/warnings.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <wx/graphics.h>
38 #include <wx/grid.h>
39 #include <wx/renderer.h>
40 #include <wx/wx.h>
41 LIBDCP_ENABLE_WARNINGS
42
43
44 using std::list;
45 using std::make_pair;
46 using std::max;
47 using std::min;
48 using std::pair;
49 using std::shared_ptr;
50 using std::string;
51 using std::vector;
52 using boost::optional;
53 #if BOOST_VERSION >= 106100
54 using namespace boost::placeholders;
55 #endif
56 using dcp::locale_convert;
57
58
59 static constexpr auto INDICATOR_SIZE = 20;
60 static constexpr auto ROW_HEIGHT = 32;
61 static constexpr auto MINIMUM_COLUMN_WIDTH = 32;
62 static constexpr auto LEFT_WIDTH = MINIMUM_COLUMN_WIDTH * 3;
63 static constexpr auto TOP_HEIGHT = ROW_HEIGHT * 2;
64 static constexpr auto COLUMN_PADDING = 16;
65 static constexpr auto HORIZONTAL_PAGE_SIZE = 32;
66
67
68 enum {
69         ID_off = 1,
70         ID_minus6dB = 2,
71         ID_0dB = 3,
72         ID_plus3dB = 4,
73         ID_edit = 5
74 };
75
76
77 AudioMappingView::AudioMappingView (wxWindow* parent, wxString left_label, wxString from, wxString top_label, wxString to)
78         : wxPanel (parent, wxID_ANY)
79         , _menu_input (0)
80         , _menu_output (1)
81         , _left_label (left_label)
82         , _from (from)
83         , _top_label (top_label)
84         , _to (to)
85 {
86         _menu = new wxMenu;
87         _menu->Append (ID_off, _("Off"));
88         _menu->Append (ID_minus6dB, _("-6dB"));
89         _menu->Append (ID_0dB, _("0dB (unchanged)"));
90         _menu->Append (ID_plus3dB, _("+3dB"));
91         _menu->Append (ID_edit, _("Edit..."));
92
93 #ifndef __WXOSX__
94         SetDoubleBuffered (true);
95 #endif
96
97         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, 0), ID_off);
98         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, db_to_linear(-6)), ID_minus6dB);
99         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, 1), ID_0dB);
100         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, db_to_linear(3)), ID_plus3dB);
101         Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
102         Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
103         Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
104         Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
105         Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
106 }
107
108
109 void
110 AudioMappingView::setup ()
111 {
112         wxClientDC dc (GetParent());
113         dc.SetFont (wxSWISS_FONT->Bold());
114
115         _column_widths.clear ();
116         _column_widths.reserve (_output_channels.size());
117         _column_widths_total = 0;
118
119         for (auto const& i: _output_channels) {
120                 wxCoord width;
121                 wxCoord height;
122                 dc.GetTextExtent (std_to_wx(i.name), &width, &height);
123                 auto const this_width = max(width + COLUMN_PADDING, MINIMUM_COLUMN_WIDTH);
124                 _column_widths.push_back (this_width);
125                 _column_widths_total += this_width;
126         }
127
128         SetMinSize({8 + LEFT_WIDTH + _column_widths_total, static_cast<int>(8 + TOP_HEIGHT + ROW_HEIGHT * _input_channels.size())});
129 }
130
131
132 void
133 AudioMappingView::paint_static (wxDC& dc)
134 {
135         dc.SetFont (wxSWISS_FONT->Bold());
136         wxCoord label_width;
137         wxCoord label_height;
138
139         dc.GetTextExtent (_top_label, &label_width, &label_height);
140         dc.DrawText (_top_label, LEFT_WIDTH + (_column_widths_total - label_width) / 2, (ROW_HEIGHT - label_height) / 2);
141
142         dc.GetTextExtent (_left_label, &label_width, &label_height);
143         dc.DrawRotatedText (
144                 _left_label,
145                 (ROW_HEIGHT - label_height) / 2,
146                 TOP_HEIGHT + (_input_channels.size() * ROW_HEIGHT + label_width) / 2,
147                 90
148                 );
149
150         dc.SetFont (*wxSWISS_FONT);
151 }
152
153
154 void
155 AudioMappingView::paint_column_labels (wxDC& dc)
156 {
157         wxCoord label_width;
158         wxCoord label_height;
159         int x = LEFT_WIDTH;
160         for (auto i = 0U; i < _output_channels.size(); ++i) {
161                 auto const name = std_to_wx(_output_channels[i].name);
162                 dc.GetTextExtent (name, &label_width, &label_height);
163                 dc.DrawText (name, x + (_column_widths[i] - label_width) / 2, ROW_HEIGHT + (ROW_HEIGHT - label_height) / 2);
164                 x += _column_widths[i];
165         }
166
167         dc.DrawLine(wxPoint(LEFT_WIDTH, ROW_HEIGHT), wxPoint(LEFT_WIDTH + _column_widths_total, ROW_HEIGHT));
168         dc.DrawLine(wxPoint(LEFT_WIDTH, ROW_HEIGHT * 2), wxPoint(LEFT_WIDTH + _column_widths_total, ROW_HEIGHT * 2));
169 }
170
171
172 void
173 AudioMappingView::paint_column_lines (wxDC& dc)
174 {
175         int x = LEFT_WIDTH;
176         for (size_t i = 0; i < _output_channels.size(); ++i) {
177                 dc.DrawLine (
178                         wxPoint(x, ROW_HEIGHT),
179                         wxPoint(x, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
180                         );
181                 x += _column_widths[i];
182         }
183
184         dc.DrawLine (
185                 wxPoint(LEFT_WIDTH + _column_widths_total, ROW_HEIGHT),
186                 wxPoint(LEFT_WIDTH + _column_widths_total, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
187                 );
188 }
189
190
191 void
192 AudioMappingView::paint_row_labels (wxDC& dc)
193 {
194         wxCoord label_width;
195         wxCoord label_height;
196
197         /* Row channel labels */
198
199         for (auto i = 0U; i < _input_channels.size(); ++i) {
200                 auto const name = std_to_wx(_input_channels[i].name);
201                 dc.GetTextExtent (name, &label_width, &label_height);
202                 dc.DrawText (name, LEFT_WIDTH - MINIMUM_COLUMN_WIDTH + (MINIMUM_COLUMN_WIDTH - label_width) / 2, TOP_HEIGHT + ROW_HEIGHT * i + (ROW_HEIGHT - label_height) / 2);
203         }
204
205         /* Vertical lines on the left */
206
207         for (int i = 1; i < 3; ++i) {
208                 dc.DrawLine (
209                         wxPoint(MINIMUM_COLUMN_WIDTH * i, TOP_HEIGHT),
210                         wxPoint(MINIMUM_COLUMN_WIDTH * i, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
211                         );
212         }
213
214         int y = TOP_HEIGHT;
215         for (auto const& i: _input_groups) {
216                 dc.DrawLine (wxPoint(MINIMUM_COLUMN_WIDTH, y), wxPoint(MINIMUM_COLUMN_WIDTH * 2, y));
217                 y += (i.to - i.from + 1) * ROW_HEIGHT;
218         }
219         dc.DrawLine (wxPoint(MINIMUM_COLUMN_WIDTH, y), wxPoint(MINIMUM_COLUMN_WIDTH * 2, y));
220
221         if (_input_groups.empty()) {
222                 auto const bottom = TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT;
223                 dc.DrawLine(wxPoint(MINIMUM_COLUMN_WIDTH, bottom), wxPoint(MINIMUM_COLUMN_WIDTH * 2, bottom));
224         }
225
226         /* Group labels and lines; be careful here as wxDCClipper does not restore the old
227          * clipping rectangle after it is destroyed
228          */
229         y = TOP_HEIGHT;
230         for (auto const& i: _input_groups) {
231                 int const height = (i.to - i.from + 1) * ROW_HEIGHT;
232                 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
233                 if (label_width > height) {
234                         label_width = height - 8;
235                 }
236
237                 {
238                         wxDCClipper clip (dc, wxRect(MINIMUM_COLUMN_WIDTH, y, ROW_HEIGHT, height));
239                         int yp = y;
240                         if ((yp - 2 * ROW_HEIGHT) < dc.GetLogicalOrigin().y) {
241                                 yp += dc.GetLogicalOrigin().y;
242                         }
243
244                         dc.DrawRotatedText (
245                                 std_to_wx(i.name),
246                                 ROW_HEIGHT + (ROW_HEIGHT - label_height) / 2,
247                                 y + (height + label_width) / 2,
248                                 90
249                                 );
250                 }
251
252                 y += height;
253         }
254 }
255
256
257 void
258 AudioMappingView::paint_row_lines (wxDC& dc)
259 {
260         for (size_t i = 0; i < _input_channels.size() + 1; ++i) {
261                 dc.DrawLine (
262                         wxPoint(MINIMUM_COLUMN_WIDTH * 2, TOP_HEIGHT + ROW_HEIGHT * i),
263                         wxPoint(LEFT_WIDTH + _column_widths_total, TOP_HEIGHT + ROW_HEIGHT * i)
264                         );
265         }
266 }
267
268
269 void
270 AudioMappingView::paint_indicators (wxDC& dc)
271 {
272         /* _{input,output}_channels and _map may not always be in sync, be careful here */
273         size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
274         size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
275
276         int xp = LEFT_WIDTH;
277         for (size_t x = 0; x < output; ++x) {
278                 for (size_t y = 0; y < input; ++y) {
279                         dc.SetBrush (*wxWHITE_BRUSH);
280                         dc.DrawRectangle (
281                                 wxRect(
282                                         xp + (_column_widths[x] - INDICATOR_SIZE) / 2,
283                                         TOP_HEIGHT + y * ROW_HEIGHT + (ROW_HEIGHT - INDICATOR_SIZE) / 2,
284                                         INDICATOR_SIZE, INDICATOR_SIZE
285                                         )
286                                 );
287
288                         float const value_dB = linear_to_db(_map.get(_input_channels[y].index, _output_channels[x].index));
289                         auto const colour = value_dB <= 0 ? wxColour(0, 255, 0) : wxColour(255, 150, 0);
290                         int const range = 18;
291                         int height = 0;
292                         if (value_dB > -range) {
293                                 height = min(INDICATOR_SIZE, static_cast<int>(INDICATOR_SIZE * (1 + value_dB / range)));
294                         }
295
296                         dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(colour, wxBRUSHSTYLE_SOLID));
297                         dc.DrawRectangle (
298                                 wxRect(
299                                         xp + (_column_widths[x] - INDICATOR_SIZE) / 2,
300                                         TOP_HEIGHT + y * ROW_HEIGHT + (ROW_HEIGHT - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
301                                         INDICATOR_SIZE, height
302                                         )
303                                 );
304
305                 }
306                 xp += _column_widths[x];
307         }
308 }
309
310
311 static
312 void restore (wxDC& dc)
313 {
314         dc.SetLogicalOrigin (0, 0);
315         dc.DestroyClippingRegion ();
316 }
317
318
319 void
320 AudioMappingView::paint ()
321 {
322         wxPaintDC dc(this);
323
324         paint_static (dc);
325
326         dc.SetClippingRegion (
327                 LEFT_WIDTH,
328                 0,
329                 _column_widths_total,
330                 ROW_HEIGHT * (2 + _input_channels.size())
331                 );
332         paint_column_labels (dc);
333         restore (dc);
334
335         dc.SetClippingRegion(
336                 0,
337                 TOP_HEIGHT,
338                 LEFT_WIDTH,
339                 min(int(ROW_HEIGHT * _input_channels.size()), GetSize().GetHeight() - TOP_HEIGHT) + 1
340              );
341         paint_row_labels (dc);
342         restore (dc);
343
344         dc.SetClippingRegion(
345                 MINIMUM_COLUMN_WIDTH * 2,
346                 TOP_HEIGHT,
347                 MINIMUM_COLUMN_WIDTH + _column_widths_total,
348                 min(int(ROW_HEIGHT * (2 + _input_channels.size())), GetSize().GetHeight() - TOP_HEIGHT)
349              );
350         paint_row_lines (dc);
351         restore (dc);
352
353         dc.SetClippingRegion(
354                 LEFT_WIDTH,
355                 MINIMUM_COLUMN_WIDTH,
356                 MINIMUM_COLUMN_WIDTH + _column_widths_total,
357                 min(int(ROW_HEIGHT * (1 + _input_channels.size())), GetSize().GetHeight() - ROW_HEIGHT)
358              );
359         paint_column_lines (dc);
360         restore (dc);
361
362         dc.SetClippingRegion (
363                 LEFT_WIDTH,
364                 TOP_HEIGHT,
365                 _column_widths_total,
366                 min(int(ROW_HEIGHT * _input_channels.size()), GetSize().GetHeight() - TOP_HEIGHT)
367              );
368         paint_indicators (dc);
369         restore (dc);
370 }
371
372
373 optional<pair<NamedChannel, NamedChannel>>
374 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
375 {
376         int x = ev.GetX();
377         int const y = ev.GetY();
378
379         if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
380                 return {};
381         }
382
383         int const input = (y - TOP_HEIGHT) / ROW_HEIGHT;
384
385         x -= LEFT_WIDTH;
386         int output = 0;
387         for (auto const i: _column_widths) {
388                 x -= i;
389                 if (x < 0) {
390                         break;
391                 }
392                 ++output;
393         }
394
395         if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
396                 return {};
397         }
398
399         return make_pair(_input_channels[input], _output_channels[output]);
400 }
401
402 optional<string>
403 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
404 {
405         int const x = ev.GetX();
406         if (x < MINIMUM_COLUMN_WIDTH || x > (2 * MINIMUM_COLUMN_WIDTH)) {
407                 return {};
408         }
409
410         int const y = (ev.GetY() - TOP_HEIGHT) / ROW_HEIGHT;
411         for (auto i: _input_groups) {
412                 if (i.from <= y && y <= i.to) {
413                         return i.name;
414                 }
415         }
416
417         return {};
418 }
419
420 void
421 AudioMappingView::left_down (wxMouseEvent& ev)
422 {
423         auto channels = mouse_event_to_channels (ev);
424         if (!channels) {
425                 return;
426         }
427
428         if (_map.get(channels->first.index, channels->second.index) > 0) {
429                 _map.set (channels->first.index, channels->second.index, 0);
430         } else {
431                 _map.set (channels->first.index, channels->second.index, 1);
432         }
433
434         map_values_changed ();
435 }
436
437 void
438 AudioMappingView::right_down (wxMouseEvent& ev)
439 {
440         auto channels = mouse_event_to_channels (ev);
441         if (!channels) {
442                 return;
443         }
444
445         _menu_input = channels->first.index;
446         _menu_output = channels->second.index;
447         PopupMenu (_menu, ev.GetPosition());
448 }
449
450
451 /** Called when any gain value has changed */
452 void
453 AudioMappingView::map_values_changed ()
454 {
455         Changed (_map);
456         _last_tooltip_channels = boost::none;
457         Refresh ();
458 }
459
460 void
461 AudioMappingView::set_gain_from_menu (double linear)
462 {
463         _map.set (_menu_input, _menu_output, linear);
464         map_values_changed ();
465 }
466
467 void
468 AudioMappingView::edit ()
469 {
470         auto dialog = make_wx<AudioGainDialog>(this, _menu_input, _menu_output, _map.get(_menu_input, _menu_output));
471         if (dialog->ShowModal() == wxID_OK) {
472                 _map.set (_menu_input, _menu_output, dialog->value ());
473                 map_values_changed ();
474         }
475 }
476
477 void
478 AudioMappingView::set (AudioMapping map)
479 {
480         _map = map;
481         Refresh ();
482 }
483
484 void
485 AudioMappingView::set_input_channels (vector<NamedChannel> const& channels)
486 {
487         _input_channels = channels;
488         setup ();
489         Refresh ();
490 }
491
492 void
493 AudioMappingView::set_output_channels (vector<NamedChannel> const & channels)
494 {
495         _output_channels = channels;
496         setup ();
497         Refresh ();
498 }
499
500
501 wxString
502 AudioMappingView::input_channel_name_with_group (NamedChannel const& n) const
503 {
504         optional<wxString> group;
505         for (auto i: _input_groups) {
506                 if (i.from <= n.index && n.index <= i.to) {
507                         group = std_to_wx (i.name);
508                 }
509         }
510
511         if (group && !group->IsEmpty()) {
512                 return wxString::Format ("%s/%s", group->data(), std_to_wx(n.name).data());
513         }
514
515         return std_to_wx(n.name);
516 }
517
518
519 void
520 AudioMappingView::motion (wxMouseEvent& ev)
521 {
522         auto channels = mouse_event_to_channels (ev);
523         if (channels) {
524                 if (channels != _last_tooltip_channels) {
525                         wxString s;
526                         auto const gain = _map.get(channels->first.index, channels->second.index);
527                         if (gain == 0) {
528                                 s = wxString::Format (
529                                         _("No audio will be passed from %s channel '%s' to %s channel '%s'."),
530                                         _from,
531                                         input_channel_name_with_group(channels->first),
532                                         _to,
533                                         std_to_wx(channels->second.name)
534                                         );
535                         } else if (gain == 1) {
536                                 s = wxString::Format (
537                                         _("Audio will be passed from %s channel %s to %s channel %s unaltered."),
538                                         _from,
539                                         input_channel_name_with_group(channels->first),
540                                         _to,
541                                         std_to_wx(channels->second.name)
542                                         );
543                         } else {
544                                 auto const dB = linear_to_db(gain);
545                                 s = wxString::Format (
546                                         _("Audio will be passed from %s channel %s to %s channel %s with gain %.1fdB."),
547                                         _from,
548                                         input_channel_name_with_group(channels->first),
549                                         _to,
550                                         std_to_wx(channels->second.name),
551                                         dB
552                                         );
553                         }
554
555                         SetToolTip (s + " " + _("Right click to change gain."));
556                 }
557         } else {
558                 auto 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 }