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 TOP_HEIGHT = ROW_HEIGHT * 2;
62 static constexpr auto COLUMN_PADDING = 16;
74 AudioMappingView::AudioMappingView (wxWindow* parent, wxString left_label, wxString from, wxString top_label, wxString to)
75 : wxPanel (parent, wxID_ANY)
78 , _left_label (left_label)
80 , _top_label (top_label)
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..."));
91 SetDoubleBuffered (true);
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));
107 AudioMappingView::setup ()
109 wxClientDC dc (GetParent());
110 dc.SetFont (wxSWISS_FONT->Bold());
112 _column_widths.clear ();
113 _column_widths.reserve (_output_channels.size());
114 _column_widths_total = 0;
116 for (auto const& i: _output_channels) {
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;
125 SetMinSize({8 + left_width() + _column_widths_total, static_cast<int>(8 + TOP_HEIGHT + ROW_HEIGHT * _input_channels.size())});
130 AudioMappingView::paint_static (wxDC& dc)
132 dc.SetFont (wxSWISS_FONT->Bold());
134 wxCoord label_height;
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);
139 dc.GetTextExtent (_left_label, &label_width, &label_height);
142 (ROW_HEIGHT - label_height) / 2,
143 TOP_HEIGHT + (_input_channels.size() * ROW_HEIGHT + label_width) / 2,
147 dc.SetFont (*wxSWISS_FONT);
152 AudioMappingView::paint_column_labels (wxDC& dc)
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];
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));
170 AudioMappingView::paint_column_lines (wxDC& dc)
172 int x = left_width();
173 for (size_t i = 0; i < _output_channels.size(); ++i) {
175 wxPoint(x, ROW_HEIGHT),
176 wxPoint(x, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
178 x += _column_widths[i];
182 wxPoint(left_width() + _column_widths_total, ROW_HEIGHT),
183 wxPoint(left_width() + _column_widths_total, TOP_HEIGHT + _input_channels.size() * ROW_HEIGHT)
189 AudioMappingView::paint_row_labels (wxDC& dc)
192 wxCoord label_height;
194 /* Row channel labels */
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);
202 /* Vertical lines on the left */
204 for (int i = 1; i < 3; ++i) {
206 wxPoint(MINIMUM_COLUMN_WIDTH * i, TOP_HEIGHT),
207 wxPoint(MINIMUM_COLUMN_WIDTH * i, TOP_HEIGHT + _input_channels.size() * ROW_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;
216 dc.DrawLine (wxPoint(MINIMUM_COLUMN_WIDTH, y), wxPoint(MINIMUM_COLUMN_WIDTH * 2, y));
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));
223 /* Group labels and lines; be careful here as wxDCClipper does not restore the old
224 * clipping rectangle after it is destroyed
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;
235 wxDCClipper clip (dc, wxRect(MINIMUM_COLUMN_WIDTH, y, ROW_HEIGHT, height));
237 if ((yp - 2 * ROW_HEIGHT) < dc.GetLogicalOrigin().y) {
238 yp += dc.GetLogicalOrigin().y;
243 ROW_HEIGHT + (ROW_HEIGHT - label_height) / 2,
244 y + (height + label_width) / 2,
255 AudioMappingView::paint_row_lines (wxDC& dc)
257 for (size_t i = 0; i < _input_channels.size() + 1; ++i) {
259 wxPoint(MINIMUM_COLUMN_WIDTH * 2, TOP_HEIGHT + ROW_HEIGHT * i),
260 wxPoint(left_width() + _column_widths_total, TOP_HEIGHT + ROW_HEIGHT * i)
267 AudioMappingView::paint_indicators (wxDC& dc)
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()));
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);
279 xp + (_column_widths[x] - INDICATOR_SIZE) / 2,
280 TOP_HEIGHT + y * ROW_HEIGHT + (ROW_HEIGHT - INDICATOR_SIZE) / 2,
281 INDICATOR_SIZE, INDICATOR_SIZE
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;
289 if (value_dB > -range) {
290 height = min(INDICATOR_SIZE, static_cast<int>(INDICATOR_SIZE * (1 + value_dB / range)));
293 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(colour, wxBRUSHSTYLE_SOLID));
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
303 xp += _column_widths[x];
309 void restore (wxDC& dc)
311 dc.SetLogicalOrigin (0, 0);
312 dc.DestroyClippingRegion ();
317 AudioMappingView::paint ()
323 dc.SetClippingRegion (
326 _column_widths_total,
327 ROW_HEIGHT * (2 + _input_channels.size())
329 paint_column_labels (dc);
332 dc.SetClippingRegion(
336 min(int(ROW_HEIGHT * _input_channels.size()), GetSize().GetHeight() - TOP_HEIGHT) + 1
338 paint_row_labels (dc);
341 dc.SetClippingRegion(
342 MINIMUM_COLUMN_WIDTH * 2,
344 MINIMUM_COLUMN_WIDTH + _column_widths_total,
345 min(int(ROW_HEIGHT * (2 + _input_channels.size())), GetSize().GetHeight() - TOP_HEIGHT)
347 paint_row_lines (dc);
350 dc.SetClippingRegion(
352 MINIMUM_COLUMN_WIDTH,
353 MINIMUM_COLUMN_WIDTH + _column_widths_total,
354 min(int(ROW_HEIGHT * (1 + _input_channels.size())), GetSize().GetHeight() - ROW_HEIGHT)
356 paint_column_lines (dc);
359 dc.SetClippingRegion (
362 _column_widths_total,
363 min(int(ROW_HEIGHT * _input_channels.size()), GetSize().GetHeight() - TOP_HEIGHT)
365 paint_indicators (dc);
370 optional<pair<NamedChannel, NamedChannel>>
371 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
374 int const y = ev.GetY();
376 if (x <= left_width() || y < TOP_HEIGHT) {
380 int const input = (y - TOP_HEIGHT) / ROW_HEIGHT;
384 for (auto const i: _column_widths) {
392 if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
396 return make_pair(_input_channels[input], _output_channels[output]);
400 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
402 int const x = ev.GetX();
403 if (x < MINIMUM_COLUMN_WIDTH || x > (2 * MINIMUM_COLUMN_WIDTH)) {
407 int const y = (ev.GetY() - TOP_HEIGHT) / ROW_HEIGHT;
408 for (auto i: _input_groups) {
409 if (i.from <= y && y <= i.to) {
418 AudioMappingView::left_down (wxMouseEvent& ev)
420 auto channels = mouse_event_to_channels (ev);
425 if (_map.get(channels->first.index, channels->second.index) > 0) {
426 _map.set (channels->first.index, channels->second.index, 0);
428 _map.set (channels->first.index, channels->second.index, 1);
431 map_values_changed ();
435 AudioMappingView::right_down (wxMouseEvent& ev)
437 auto channels = mouse_event_to_channels (ev);
442 _menu_input = channels->first.index;
443 _menu_output = channels->second.index;
444 PopupMenu (_menu, ev.GetPosition());
448 /** Called when any gain value has changed */
450 AudioMappingView::map_values_changed ()
453 _last_tooltip_channels = boost::none;
458 AudioMappingView::set_gain_from_menu (double linear)
460 _map.set (_menu_input, _menu_output, linear);
461 map_values_changed ();
465 AudioMappingView::edit ()
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 ();
475 AudioMappingView::set (AudioMapping map)
482 AudioMappingView::set_input_channels (vector<NamedChannel> const& channels)
484 _input_channels = channels;
490 AudioMappingView::set_output_channels (vector<NamedChannel> const & channels)
492 _output_channels = channels;
499 AudioMappingView::input_channel_name_with_group (NamedChannel const& n) const
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);
508 if (group && !group->IsEmpty()) {
509 return wxString::Format ("%s/%s", group->data(), std_to_wx(n.name).data());
512 return std_to_wx(n.name);
517 AudioMappingView::motion (wxMouseEvent& ev)
519 auto channels = mouse_event_to_channels (ev);
521 if (channels != _last_tooltip_channels) {
523 auto const gain = _map.get(channels->first.index, channels->second.index);
525 s = wxString::Format (
526 _("No audio will be passed from %s channel '%s' to %s channel '%s'."),
528 input_channel_name_with_group(channels->first),
530 std_to_wx(channels->second.name)
532 } else if (gain == 1) {
533 s = wxString::Format (
534 _("Audio will be passed from %s channel %s to %s channel %s unaltered."),
536 input_channel_name_with_group(channels->first),
538 std_to_wx(channels->second.name)
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."),
545 input_channel_name_with_group(channels->first),
547 std_to_wx(channels->second.name),
552 SetToolTip (s + " " + _("Right click to change gain."));
555 auto group = mouse_event_to_input_group_name (ev);
557 SetToolTip (std_to_wx(*group));
563 _last_tooltip_channels = channels;
568 AudioMappingView::set_input_groups (vector<Group> const & groups)
570 _input_groups = groups;
575 AudioMappingView::left_width() const
577 return _input_groups.empty() ? (MINIMUM_COLUMN_WIDTH * 2) : (MINIMUM_COLUMN_WIDTH * 3);