2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file src/wx/audio_mapping_view.cc
23 * @brief AudioMappingView class and helpers.
27 #include "audio_gain_dialog.h"
28 #include "audio_mapping_view.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>
38 #include <wx/renderer.h>
40 LIBDCP_ENABLE_WARNINGS
48 using std::shared_ptr;
51 using boost::optional;
52 #if BOOST_VERSION >= 106100
53 using namespace boost::placeholders;
55 using dcp::locale_convert;
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 LEFT_WIDTH = MINIMUM_COLUMN_WIDTH * 3;
62 static constexpr auto TOP_HEIGHT = ROW_HEIGHT * 2;
63 static constexpr auto COLUMN_PADDING = 16;
64 static constexpr auto HORIZONTAL_PAGE_SIZE = 32;
76 AudioMappingView::AudioMappingView (wxWindow* parent, wxString left_label, wxString from, wxString top_label, wxString to)
77 : wxPanel (parent, wxID_ANY)
80 , _left_label (left_label)
82 , _top_label (top_label)
86 _menu->Append (ID_off, _("Off"));
87 _menu->Append (ID_minus6dB, _("-6dB"));
88 _menu->Append (ID_0dB, _("0dB (unchanged)"));
89 _menu->Append (ID_plus3dB, _("+3dB"));
90 _menu->Append (ID_edit, _("Edit..."));
92 _body = new wxPanel (this, wxID_ANY);
93 _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
94 _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
97 SetDoubleBuffered (true);
100 Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
101 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, 0), ID_off);
102 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, db_to_linear(-6)), ID_minus6dB);
103 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, 1), ID_0dB);
104 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, db_to_linear(3)), ID_plus3dB);
105 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
106 Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
107 _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
108 _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
109 _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
110 _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
111 _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
112 _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
113 _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
114 _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
115 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
116 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
117 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
118 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
119 _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
120 _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
121 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
122 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
123 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
124 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
125 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
126 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
131 AudioMappingView::size (wxSizeEvent& ev)
139 AudioMappingView::setup ()
141 auto const s = GetSize();
142 int const w = _vertical_scroll->GetSize().GetWidth();
143 int const h = _horizontal_scroll->GetSize().GetHeight();
145 _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
146 _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
148 _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
150 _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
151 _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
153 _vertical_scroll->SetScrollbar (
154 _vertical_scroll->GetThumbPosition(),
155 s.GetHeight() - h - 8,
156 ROW_HEIGHT * (2 + _input_channels.size()),
161 wxClientDC dc (GetParent());
162 dc.SetFont (wxSWISS_FONT->Bold());
164 _column_widths.clear ();
165 _column_widths.reserve (_output_channels.size());
166 _column_widths_total = 0;
168 for (auto const& i: _output_channels) {
171 dc.GetTextExtent (std_to_wx(i.name), &width, &height);
172 auto const this_width = max(width + COLUMN_PADDING, MINIMUM_COLUMN_WIDTH);
173 _column_widths.push_back (this_width);
174 _column_widths_total += this_width;
177 _horizontal_scroll->SetScrollbar (
178 _horizontal_scroll->GetThumbPosition(),
179 s.GetWidth() - w - 8,
180 LEFT_WIDTH + _column_widths_total,
181 HORIZONTAL_PAGE_SIZE,
187 AudioMappingView::scroll ()
194 AudioMappingView::paint_static (wxDC& dc)
196 dc.SetFont (wxSWISS_FONT->Bold());
198 wxCoord label_height;
200 dc.GetTextExtent (_top_label, &label_width, &label_height);
201 dc.DrawText (_top_label, LEFT_WIDTH + (_column_widths_total - label_width) / 2, (ROW_HEIGHT - label_height) / 2);
203 dc.GetTextExtent (_left_label, &label_width, &label_height);
206 (ROW_HEIGHT - label_height) / 2,
207 TOP_HEIGHT + (_input_channels.size() * ROW_HEIGHT + label_width) / 2,
211 dc.SetFont (*wxSWISS_FONT);
216 AudioMappingView::paint_column_labels (wxDC& dc)
219 wxCoord label_height;
221 for (auto i = 0U; i < _output_channels.size(); ++i) {
222 auto const name = std_to_wx(_output_channels[i].name);
223 dc.GetTextExtent (name, &label_width, &label_height);
224 dc.DrawText (name, x + (_column_widths[i] - label_width) / 2, ROW_HEIGHT + (ROW_HEIGHT - label_height) / 2);
225 x += _column_widths[i];
228 dc.DrawLine(wxPoint(LEFT_WIDTH, ROW_HEIGHT), wxPoint(LEFT_WIDTH + _column_widths_total, ROW_HEIGHT));
229 dc.DrawLine(wxPoint(LEFT_WIDTH, ROW_HEIGHT * 2), wxPoint(LEFT_WIDTH + _column_widths_total, ROW_HEIGHT * 2));
234 AudioMappingView::paint_column_lines (wxDC& dc)
237 for (size_t i = 0; i < _output_channels.size(); ++i) {
239 wxPoint(x, ROW_HEIGHT),
240 wxPoint(x, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
242 x += _column_widths[i];
246 wxPoint(LEFT_WIDTH + _column_widths_total, ROW_HEIGHT),
247 wxPoint(LEFT_WIDTH + _column_widths_total, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
253 AudioMappingView::paint_row_labels (wxDC& dc)
256 wxCoord label_height;
258 /* Row channel labels */
260 for (auto i = 0U; i < _input_channels.size(); ++i) {
261 auto const name = std_to_wx(_input_channels[i].name);
262 dc.GetTextExtent (name, &label_width, &label_height);
263 dc.DrawText (name, LEFT_WIDTH - MINIMUM_COLUMN_WIDTH + (MINIMUM_COLUMN_WIDTH - label_width) / 2, TOP_HEIGHT + ROW_HEIGHT * i + (ROW_HEIGHT - label_height) / 2);
266 /* Vertical lines on the left */
268 for (int i = 1; i < 3; ++i) {
270 wxPoint(MINIMUM_COLUMN_WIDTH * i, TOP_HEIGHT),
271 wxPoint(MINIMUM_COLUMN_WIDTH * i, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
276 for (auto const& i: _input_groups) {
277 dc.DrawLine (wxPoint(MINIMUM_COLUMN_WIDTH, y), wxPoint(MINIMUM_COLUMN_WIDTH * 2, y));
278 y += (i.to - i.from + 1) * ROW_HEIGHT;
280 dc.DrawLine (wxPoint(MINIMUM_COLUMN_WIDTH, y), wxPoint(MINIMUM_COLUMN_WIDTH * 2, y));
282 /* Group labels and lines; be careful here as wxDCClipper does not restore the old
283 * clipping rectangle after it is destroyed
286 for (auto const& i: _input_groups) {
287 int const height = (i.to - i.from + 1) * ROW_HEIGHT;
288 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
289 if (label_width > height) {
290 label_width = height - 8;
294 wxDCClipper clip (dc, wxRect(MINIMUM_COLUMN_WIDTH, y, ROW_HEIGHT, height));
296 if ((yp - 2 * ROW_HEIGHT) < dc.GetLogicalOrigin().y) {
297 yp += dc.GetLogicalOrigin().y;
302 ROW_HEIGHT + (ROW_HEIGHT - label_height) / 2,
303 y + (height + label_width) / 2,
314 AudioMappingView::paint_row_lines (wxDC& dc)
316 for (size_t i = 0; i < _input_channels.size(); ++i) {
318 wxPoint(MINIMUM_COLUMN_WIDTH * 2, TOP_HEIGHT + ROW_HEIGHT * i),
319 wxPoint(LEFT_WIDTH + _column_widths_total, TOP_HEIGHT + ROW_HEIGHT * i)
323 wxPoint(MINIMUM_COLUMN_WIDTH * 2, TOP_HEIGHT + ROW_HEIGHT * _input_channels.size()),
324 wxPoint(LEFT_WIDTH + _column_widths_total, TOP_HEIGHT + ROW_HEIGHT * _input_channels.size())
330 AudioMappingView::paint_indicators (wxDC& dc)
332 /* _{input,output}_channels and _map may not always be in sync, be careful here */
333 size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
334 size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
337 for (size_t x = 0; x < output; ++x) {
338 for (size_t y = 0; y < input; ++y) {
339 dc.SetBrush (*wxWHITE_BRUSH);
342 xp + (_column_widths[x] - INDICATOR_SIZE) / 2,
343 TOP_HEIGHT + y * ROW_HEIGHT + (ROW_HEIGHT - INDICATOR_SIZE) / 2,
344 INDICATOR_SIZE, INDICATOR_SIZE
348 float const value_dB = linear_to_db(_map.get(_input_channels[y].index, _output_channels[x].index));
349 auto const colour = value_dB <= 0 ? wxColour(0, 255, 0) : wxColour(255, 150, 0);
350 int const range = 18;
352 if (value_dB > -range) {
353 height = min(INDICATOR_SIZE, static_cast<int>(INDICATOR_SIZE * (1 + value_dB / range)));
356 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(colour, wxBRUSHSTYLE_SOLID));
359 xp + (_column_widths[x] - INDICATOR_SIZE) / 2,
360 TOP_HEIGHT + y * ROW_HEIGHT + (ROW_HEIGHT - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
361 INDICATOR_SIZE, height
366 xp += _column_widths[x];
372 void restore (wxDC& dc)
374 dc.SetLogicalOrigin (0, 0);
375 dc.DestroyClippingRegion ();
380 AudioMappingView::paint ()
382 wxPaintDC dc (_body);
384 int const hs = _horizontal_scroll->GetThumbPosition ();
385 int const vs = _vertical_scroll->GetThumbPosition ();
389 dc.SetClippingRegion (
392 _column_widths_total,
393 ROW_HEIGHT * (2 + _input_channels.size())
395 dc.SetLogicalOrigin (hs, 0);
396 paint_column_labels (dc);
399 dc.SetClippingRegion(
403 min(int(ROW_HEIGHT * _input_channels.size()), GetSize().GetHeight() - TOP_HEIGHT) + 1
405 dc.SetLogicalOrigin (0, vs);
406 paint_row_labels (dc);
409 dc.SetClippingRegion(
410 MINIMUM_COLUMN_WIDTH * 2,
412 MINIMUM_COLUMN_WIDTH + _column_widths_total,
413 min(int(ROW_HEIGHT * (2 + _input_channels.size())), GetSize().GetHeight() - TOP_HEIGHT)
415 dc.SetLogicalOrigin (hs, vs);
416 paint_row_lines (dc);
419 dc.SetClippingRegion(
421 MINIMUM_COLUMN_WIDTH,
422 MINIMUM_COLUMN_WIDTH + _column_widths_total,
423 min(int(ROW_HEIGHT * (1 + _input_channels.size())), GetSize().GetHeight() - ROW_HEIGHT)
425 dc.SetLogicalOrigin(hs, vs);
426 paint_column_lines (dc);
429 dc.SetClippingRegion (
432 _column_widths_total,
433 min(int(ROW_HEIGHT * _input_channels.size()), GetSize().GetHeight() - TOP_HEIGHT)
435 dc.SetLogicalOrigin(hs, vs);
436 paint_indicators (dc);
441 optional<pair<NamedChannel, NamedChannel>>
442 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
444 int x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
445 int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
447 if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
451 int const input = (y - TOP_HEIGHT) / ROW_HEIGHT;
455 for (auto const i: _column_widths) {
463 if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
467 return make_pair(_input_channels[input], _output_channels[output]);
471 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
473 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
474 if (x < MINIMUM_COLUMN_WIDTH || x > (2 * MINIMUM_COLUMN_WIDTH)) {
478 int const y = (ev.GetY() + _vertical_scroll->GetThumbPosition() - TOP_HEIGHT) / ROW_HEIGHT;
479 for (auto i: _input_groups) {
480 if (i.from <= y && y <= i.to) {
489 AudioMappingView::left_down (wxMouseEvent& ev)
491 auto channels = mouse_event_to_channels (ev);
496 if (_map.get(channels->first.index, channels->second.index) > 0) {
497 _map.set (channels->first.index, channels->second.index, 0);
499 _map.set (channels->first.index, channels->second.index, 1);
502 map_values_changed ();
506 AudioMappingView::right_down (wxMouseEvent& ev)
508 auto channels = mouse_event_to_channels (ev);
513 _menu_input = channels->first.index;
514 _menu_output = channels->second.index;
515 PopupMenu (_menu, ev.GetPosition());
519 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
521 if (ev.ShiftDown()) {
522 _horizontal_scroll->SetThumbPosition (
523 _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -HORIZONTAL_PAGE_SIZE : HORIZONTAL_PAGE_SIZE)
527 _vertical_scroll->SetThumbPosition (
528 _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -ROW_HEIGHT : ROW_HEIGHT)
534 /** Called when any gain value has changed */
536 AudioMappingView::map_values_changed ()
539 _last_tooltip_channels = boost::none;
544 AudioMappingView::set_gain_from_menu (double linear)
546 _map.set (_menu_input, _menu_output, linear);
547 map_values_changed ();
551 AudioMappingView::edit ()
553 auto dialog = new AudioGainDialog (this, _menu_input, _menu_output, _map.get(_menu_input, _menu_output));
554 if (dialog->ShowModal() == wxID_OK) {
555 _map.set (_menu_input, _menu_output, dialog->value ());
556 map_values_changed ();
563 AudioMappingView::set (AudioMapping map)
570 AudioMappingView::set_input_channels (vector<NamedChannel> const& channels)
572 _input_channels = channels;
578 AudioMappingView::set_output_channels (vector<NamedChannel> const & channels)
580 _output_channels = channels;
587 AudioMappingView::input_channel_name_with_group (NamedChannel const& n) const
589 optional<wxString> group;
590 for (auto i: _input_groups) {
591 if (i.from <= n.index && n.index <= i.to) {
592 group = std_to_wx (i.name);
596 if (group && !group->IsEmpty()) {
597 return wxString::Format ("%s/%s", group->data(), std_to_wx(n.name).data());
600 return std_to_wx(n.name);
605 AudioMappingView::motion (wxMouseEvent& ev)
607 auto channels = mouse_event_to_channels (ev);
609 if (channels != _last_tooltip_channels) {
611 auto const gain = _map.get(channels->first.index, channels->second.index);
613 s = wxString::Format (
614 _("No audio will be passed from %s channel '%s' to %s channel '%s'."),
616 input_channel_name_with_group(channels->first),
618 std_to_wx(channels->second.name)
620 } else if (gain == 1) {
621 s = wxString::Format (
622 _("Audio will be passed from %s channel %s to %s channel %s unaltered."),
624 input_channel_name_with_group(channels->first),
626 std_to_wx(channels->second.name)
629 auto const dB = linear_to_db(gain);
630 s = wxString::Format (
631 _("Audio will be passed from %s channel %s to %s channel %s with gain %.1fdB."),
633 input_channel_name_with_group(channels->first),
635 std_to_wx(channels->second.name),
640 SetToolTip (s + " " + _("Right click to change gain."));
643 auto group = mouse_event_to_input_group_name (ev);
645 SetToolTip (std_to_wx(*group));
651 _last_tooltip_channels = channels;
656 AudioMappingView::set_input_groups (vector<Group> const & groups)
658 _input_groups = groups;