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