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