2 Copyright (C) 2013-2019 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/>.
21 /** @file src/wx/audio_mapping_view.cc
22 * @brief AudioMappingView class and helpers.
25 #include "audio_mapping_view.h"
27 #include "audio_gain_dialog.h"
28 #include "lib/audio_mapping.h"
30 #include <dcp/locale_convert.h>
31 #include <dcp/types.h>
33 #include <wx/renderer.h>
35 #include <wx/graphics.h>
36 #include <boost/foreach.hpp>
47 using boost::shared_ptr;
48 using boost::optional;
49 #if BOOST_VERSION >= 106100
50 using namespace boost::placeholders;
52 using dcp::locale_convert;
54 #define INDICATOR_SIZE 20
55 #define GRID_SPACING 32
56 #define LEFT_WIDTH (GRID_SPACING * 3)
57 #define TOP_HEIGHT (GRID_SPACING * 2)
66 AudioMappingView::AudioMappingView (wxWindow* parent)
67 : wxPanel (parent, wxID_ANY)
72 _menu->Append (ID_off, _("Off"));
73 _menu->Append (ID_full, _("Full"));
74 _menu->Append (ID_minus6dB, _("-6dB"));
75 _menu->Append (ID_edit, _("Edit..."));
77 _body = new wxPanel (this, wxID_ANY);
78 _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
79 _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
81 Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
82 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::off, this), ID_off);
83 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::full, this), ID_full);
84 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::minus6dB, this), ID_minus6dB);
85 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
86 Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
87 _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
88 _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
89 _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
90 _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
91 _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
92 _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
93 _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
94 _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
95 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
96 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
97 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
98 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
99 _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
100 _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
101 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
102 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
103 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
104 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
105 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
106 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
110 AudioMappingView::size (wxSizeEvent& ev)
117 AudioMappingView::setup ()
119 wxSize const s = GetSize();
120 int const w = _vertical_scroll->GetSize().GetWidth();
121 int const h = _horizontal_scroll->GetSize().GetHeight();
123 _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
124 _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
126 _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
128 _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
129 _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
131 _vertical_scroll->SetScrollbar (
132 _vertical_scroll->GetThumbPosition(),
133 s.GetHeight() - h - 8,
134 GRID_SPACING * (2 + _input_channels.size()),
139 _horizontal_scroll->SetScrollbar (
140 _horizontal_scroll->GetThumbPosition(),
141 s.GetWidth() - w - 8,
142 GRID_SPACING * (3 + _output_channels.size()),
148 AudioMappingView::scroll ()
154 AudioMappingView::paint_static (wxDC& dc)
156 dc.SetFont (wxSWISS_FONT->Bold());
158 wxCoord label_height;
160 /* DCP label at the top */
162 dc.GetTextExtent (_("DCP"), &label_width, &label_height);
163 dc.DrawText (_("DCP"), LEFT_WIDTH + (_output_channels.size() * GRID_SPACING - label_width) / 2, (GRID_SPACING - label_height) / 2);
165 /* Content label on the left */
167 dc.GetTextExtent (_("Content"), &label_width, &label_height);
170 (GRID_SPACING - label_height) / 2,
171 TOP_HEIGHT + (_input_channels.size() * GRID_SPACING + label_width) / 2,
175 dc.SetFont (*wxSWISS_FONT);
179 AudioMappingView::paint_column_labels (wxDC& dc)
182 wxCoord label_height;
184 BOOST_FOREACH (string i, _output_channels) {
185 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
186 dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2);
190 dc.DrawLine(wxPoint(LEFT_WIDTH, GRID_SPACING), wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING));
191 dc.DrawLine(wxPoint(LEFT_WIDTH, GRID_SPACING * 2), wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING * 2));
195 AudioMappingView::paint_column_lines (wxDC& dc)
197 for (size_t i = 0; i < _output_channels.size(); ++i) {
199 wxPoint(LEFT_WIDTH + GRID_SPACING * i, GRID_SPACING),
200 wxPoint(LEFT_WIDTH + GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING)
205 wxPoint(LEFT_WIDTH + GRID_SPACING * _output_channels.size(), GRID_SPACING),
206 wxPoint(LEFT_WIDTH + GRID_SPACING * _output_channels.size(), TOP_HEIGHT + _input_channels.size() * GRID_SPACING)
211 AudioMappingView::paint_row_labels (wxDC& dc)
214 wxCoord label_height;
216 /* Row channel labels */
219 BOOST_FOREACH (string i, _input_channels) {
220 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
221 dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2);
225 /* Vertical lines on the left */
227 for (int i = 1; i < 3; ++i) {
229 wxPoint(GRID_SPACING * i, TOP_HEIGHT),
230 wxPoint(GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING)
234 /* Group labels and lines */
237 BOOST_FOREACH (Group i, _input_groups) {
238 int const height = (i.to - i.from + 1) * GRID_SPACING;
239 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
240 if (label_width > height) {
241 label_width = height - 8;
246 if ((yp - 2 * GRID_SPACING) < dc.GetLogicalOrigin().y) {
247 yp += dc.GetLogicalOrigin().y;
250 wxCoord old_x, old_y, old_width, old_height;
251 dc.GetClippingBox (&old_x, &old_y, &old_width, &old_height);
252 dc.DestroyClippingRegion ();
253 dc.SetClippingRegion (GRID_SPACING, yp + 4, GRID_SPACING, height - 8);
257 GRID_SPACING + (GRID_SPACING - label_height) / 2,
258 y + (height + label_width) / 2,
262 dc.DestroyClippingRegion ();
263 dc.SetClippingRegion (old_x, old_y, old_width, old_height);
266 dc.DrawLine (wxPoint(GRID_SPACING, y), wxPoint(GRID_SPACING * 2, y));
270 dc.DrawLine (wxPoint(GRID_SPACING, y), wxPoint(GRID_SPACING * 2, y));
274 AudioMappingView::paint_row_lines (wxDC& dc)
276 for (size_t i = 0; i < _input_channels.size(); ++i) {
278 wxPoint(GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * i),
279 wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * i)
283 wxPoint(GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * _input_channels.size()),
284 wxPoint(LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * _input_channels.size())
289 AudioMappingView::paint_indicators (wxDC& dc)
291 /* _{input,output}_channels and _map may not always be in sync, be careful here */
292 size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
293 size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
295 for (size_t x = 0; x < output; ++x) {
296 for (size_t y = 0; y < input; ++y) {
297 dc.SetBrush (*wxWHITE_BRUSH);
300 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
301 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
302 INDICATOR_SIZE, INDICATOR_SIZE
306 float const value_dB = 20 * log10 (_map.get(y, x));
307 int const range = 18;
309 if (value_dB > -range) {
310 height = INDICATOR_SIZE * (1 + value_dB / range);
313 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
316 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
317 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
318 INDICATOR_SIZE, height
326 void clip (wxDC& dc, int x, int y, int w, int h)
328 dc.SetClippingRegion (x, y, w, h);
332 void translate (wxDC& dc, int x, int y)
334 dc.SetLogicalOrigin (x, y);
338 void restore (wxDC& dc)
340 dc.SetLogicalOrigin (0, 0);
341 dc.DestroyClippingRegion ();
345 AudioMappingView::paint ()
347 wxPaintDC dc (_body);
349 int const hs = _horizontal_scroll->GetThumbPosition ();
350 int const vs = _vertical_scroll->GetThumbPosition ();
354 clip (dc, LEFT_WIDTH, 0, GRID_SPACING * _output_channels.size(), GRID_SPACING * (2 + _input_channels.size()));
355 translate (dc, hs, 0);
356 paint_column_labels (dc);
359 clip (dc, 0, TOP_HEIGHT, GRID_SPACING * (3 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
360 translate (dc, 0, vs);
361 paint_row_labels (dc);
364 clip (dc, GRID_SPACING * 2, TOP_HEIGHT, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
365 translate (dc, hs, vs);
366 paint_row_lines (dc);
369 clip (dc, LEFT_WIDTH, GRID_SPACING, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * (1 + _input_channels.size()));
370 translate (dc, hs, vs);
371 paint_column_lines (dc);
374 clip (dc, LEFT_WIDTH, TOP_HEIGHT, GRID_SPACING * _output_channels.size(), GRID_SPACING * _input_channels.size());
375 translate (dc, hs, vs);
376 paint_indicators (dc);
380 optional<pair<int, int> >
381 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
383 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
384 int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
386 if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
387 return optional<pair<int, int> >();
390 int const input = (y - TOP_HEIGHT) / GRID_SPACING;
391 int const output = (x - LEFT_WIDTH) / GRID_SPACING;
393 if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
394 return optional<pair<int, int> >();
397 return make_pair (input, output);
401 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
403 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
404 if (x < GRID_SPACING || x > (2 * GRID_SPACING)) {
405 return optional<string>();
408 int y = (ev.GetY() + _vertical_scroll->GetThumbPosition() - (GRID_SPACING * 2)) / GRID_SPACING;
409 BOOST_FOREACH (Group i, _input_groups) {
410 if (i.from <= y && y <= i.to) {
415 return optional<string>();
419 AudioMappingView::left_down (wxMouseEvent& ev)
421 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
426 if (_map.get(channels->first, channels->second) > 0) {
427 _map.set (channels->first, channels->second, 0);
429 _map.set (channels->first, channels->second, 1);
432 map_values_changed ();
436 AudioMappingView::right_down (wxMouseEvent& ev)
438 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
443 _menu_input = channels->first;
444 _menu_output = channels->second;
445 PopupMenu (_menu, ev.GetPosition());
449 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
451 if (ev.ShiftDown()) {
452 _horizontal_scroll->SetThumbPosition (
453 _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
457 _vertical_scroll->SetThumbPosition (
458 _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
464 /** Called when any gain value has changed */
466 AudioMappingView::map_values_changed ()
469 _last_tooltip_channels = optional<pair<int, int> >();
474 AudioMappingView::off ()
476 _map.set (_menu_input, _menu_output, 0);
477 map_values_changed ();
481 AudioMappingView::full ()
483 _map.set (_menu_input, _menu_output, 1);
484 map_values_changed ();
488 AudioMappingView::minus6dB ()
490 _map.set (_menu_input, _menu_output, pow (10, -6.0 / 20));
491 map_values_changed ();
495 AudioMappingView::edit ()
497 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output, _map.get(_menu_input, _menu_output));
498 if (dialog->ShowModal() == wxID_OK) {
499 _map.set (_menu_input, _menu_output, dialog->value ());
500 map_values_changed ();
507 AudioMappingView::set (AudioMapping map)
514 AudioMappingView::set_input_channels (vector<string> const & names)
516 _input_channels = names;
522 AudioMappingView::set_output_channels (vector<string> const & names)
524 _output_channels = names;
530 AudioMappingView::motion (wxMouseEvent& ev)
532 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
534 if (channels != _last_tooltip_channels) {
536 float const gain = _map.get(channels->first, channels->second);
538 s = wxString::Format (
539 _("No audio will be passed from content channel %d to DCP channel %d."),
540 channels->first + 1, channels->second + 1
542 } else if (gain == 1) {
543 s = wxString::Format (
544 _("Audio will be passed from content channel %d to DCP channel %d unaltered."),
545 channels->first + 1, channels->second + 1
548 float const dB = 20 * log10 (gain);
549 s = wxString::Format (
550 _("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."),
551 channels->first + 1, channels->second + 1, dB
555 SetToolTip (s + " " + _("Right click to change gain."));
558 optional<string> group = mouse_event_to_input_group_name (ev);
560 SetToolTip (std_to_wx(*group));
566 _last_tooltip_channels = channels;
571 AudioMappingView::set_input_groups (vector<Group> const & groups)
573 _input_groups = groups;