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 using dcp::locale_convert;
51 #define INDICATOR_SIZE 20
52 #define GRID_SPACING 32
53 #define LEFT_WIDTH (GRID_SPACING * 3)
54 #define TOP_HEIGHT (GRID_SPACING * 2)
63 AudioMappingView::AudioMappingView (wxWindow* parent)
64 : wxPanel (parent, wxID_ANY)
69 _menu->Append (ID_off, _("Off"));
70 _menu->Append (ID_full, _("Full"));
71 _menu->Append (ID_minus6dB, _("-6dB"));
72 _menu->Append (ID_edit, _("Edit..."));
74 _body = new wxPanel (this, wxID_ANY);
75 _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
76 _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
78 Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
79 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::off, this), ID_off);
80 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::full, this), ID_full);
81 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::minus6dB, this), ID_minus6dB);
82 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
83 Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
84 _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
85 _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
86 _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
87 _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
88 _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
89 _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
90 _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
91 _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
92 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
93 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
94 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
95 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
96 _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
97 _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
98 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
99 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
100 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
101 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
102 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
103 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
107 AudioMappingView::size (wxSizeEvent& ev)
114 AudioMappingView::setup ()
116 wxSize const s = GetSize();
117 int const w = _vertical_scroll->GetSize().GetWidth();
118 int const h = _horizontal_scroll->GetSize().GetHeight();
120 _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
121 _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
123 _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
125 _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
126 _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
128 _vertical_scroll->SetScrollbar (
129 _vertical_scroll->GetThumbPosition(),
130 s.GetHeight() - h - 8,
131 GRID_SPACING * (2 + _input_channels.size()),
136 _horizontal_scroll->SetScrollbar (
137 _horizontal_scroll->GetThumbPosition(),
138 s.GetWidth() - w - 8,
139 GRID_SPACING * (3 + _output_channels.size()),
145 AudioMappingView::scroll ()
151 AudioMappingView::paint_static (wxDC& dc, wxGraphicsContext* gc)
153 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
154 dc.SetFont (wxSWISS_FONT->Bold());
156 wxCoord label_height;
158 /* DCP label at the top */
160 dc.GetTextExtent (_("DCP"), &label_width, &label_height);
161 dc.DrawText (_("DCP"), LEFT_WIDTH + (_output_channels.size() * GRID_SPACING - label_width) / 2, (GRID_SPACING - label_height) / 2);
163 /* Content label on the left */
165 dc.GetTextExtent (_("Content"), &label_width, &label_height);
168 (GRID_SPACING - label_height) / 2,
169 TOP_HEIGHT + (_input_channels.size() * GRID_SPACING + label_width) / 2,
173 dc.SetFont (*wxSWISS_FONT);
174 gc->SetPen (*wxBLACK_PEN);
178 AudioMappingView::paint_column_labels (wxDC& dc, wxGraphicsContext* gc)
181 wxCoord label_height;
183 BOOST_FOREACH (string i, _output_channels) {
184 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
185 dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2);
189 wxGraphicsPath lines = gc->CreatePath ();
190 lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING);
191 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING);
192 lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING * 2);
193 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING * 2);
194 gc->StrokePath (lines);
198 AudioMappingView::paint_column_lines (wxGraphicsContext* gc)
200 wxGraphicsPath lines = gc->CreatePath ();
201 for (size_t i = 0; i < _output_channels.size(); ++i) {
202 lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * i, GRID_SPACING);
203 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
205 lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), GRID_SPACING);
206 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
207 gc->StrokePath (lines);
211 AudioMappingView::paint_row_labels (wxDC& dc, wxGraphicsContext* gc)
214 wxCoord label_height;
215 wxGraphicsPath lines = gc->CreatePath ();
217 /* Row channel labels */
220 BOOST_FOREACH (string i, _input_channels) {
221 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
222 dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2);
226 /* Vertical lines on the left */
228 for (int i = 1; i < 3; ++i) {
229 lines.MoveToPoint (GRID_SPACING * i, TOP_HEIGHT);
230 lines.AddLineToPoint (GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
233 /* Group labels and lines */
236 BOOST_FOREACH (Group i, _input_groups) {
237 int const height = (i.to - i.from + 1) * GRID_SPACING;
238 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
239 if (label_width > height) {
240 label_width = height - 8;
245 if ((yp - 2 * GRID_SPACING) < dc.GetLogicalOrigin().y) {
246 yp += dc.GetLogicalOrigin().y;
249 wxCoord old_x, old_y, old_width, old_height;
250 dc.GetClippingBox (&old_x, &old_y, &old_width, &old_height);
251 dc.DestroyClippingRegion ();
252 dc.SetClippingRegion (GRID_SPACING, yp + 4, GRID_SPACING, height - 8);
256 GRID_SPACING + (GRID_SPACING - label_height) / 2,
257 y + (height + label_width) / 2,
261 dc.DestroyClippingRegion ();
262 dc.SetClippingRegion (old_x, old_y, old_width, old_height);
265 lines.MoveToPoint (GRID_SPACING, y);
266 lines.AddLineToPoint (GRID_SPACING * 2, y);
270 lines.MoveToPoint (GRID_SPACING, y);
271 lines.AddLineToPoint (GRID_SPACING * 2, y);
273 gc->StrokePath (lines);
277 AudioMappingView::paint_row_lines (wxGraphicsContext* gc)
279 wxGraphicsPath lines = gc->CreatePath ();
280 for (size_t i = 0; i < _input_channels.size(); ++i) {
281 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * i);
282 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * i);
284 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
285 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
286 gc->StrokePath (lines);
290 AudioMappingView::paint_indicators (wxDC& dc)
292 /* _{input,output}_channels and _map may not always be in sync, be careful here */
293 size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
294 size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
296 for (size_t x = 0; x < output; ++x) {
297 for (size_t y = 0; y < input; ++y) {
298 dc.SetBrush (*wxWHITE_BRUSH);
301 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
302 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
303 INDICATOR_SIZE, INDICATOR_SIZE
307 float const value_dB = 20 * log10 (_map.get(y, x));
308 int const range = 18;
310 if (value_dB > -range) {
311 height = INDICATOR_SIZE * (1 + value_dB / range);
314 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
317 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
318 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
319 INDICATOR_SIZE, height
327 void clip (wxDC& dc, wxGraphicsContext* gc, int x, int y, int w, int h)
329 dc.SetClippingRegion (x, y, w, h);
330 gc->Clip (x, y, w, h);
334 void translate (wxDC& dc, wxGraphicsContext* gc, int x, int y)
337 gc->Translate (-x, -y);
338 dc.SetLogicalOrigin (x, y);
342 void restore (wxDC& dc, wxGraphicsContext* gc)
344 dc.SetLogicalOrigin (0, 0);
346 dc.DestroyClippingRegion ();
351 AudioMappingView::paint ()
353 wxPaintDC dc (_body);
355 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
360 int const hs = _horizontal_scroll->GetThumbPosition ();
361 int const vs = _vertical_scroll->GetThumbPosition ();
363 paint_static (dc, gc);
365 clip (dc, gc, LEFT_WIDTH, 0, GRID_SPACING * _output_channels.size(), GRID_SPACING * (2 + _input_channels.size()));
366 translate (dc, gc, hs, 0);
367 paint_column_labels (dc, gc);
370 clip (dc, gc, 0, TOP_HEIGHT, GRID_SPACING * (3 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
371 translate (dc, gc, 0, vs);
372 paint_row_labels (dc, gc);
375 clip (dc, gc, GRID_SPACING * 2, TOP_HEIGHT, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
376 translate (dc, gc, hs, vs);
377 paint_row_lines (gc);
380 clip (dc, gc, LEFT_WIDTH, GRID_SPACING, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * (1 + _input_channels.size()));
381 translate (dc, gc, hs, vs);
382 paint_column_lines (gc);
385 clip (dc, gc, LEFT_WIDTH, TOP_HEIGHT, GRID_SPACING * _output_channels.size(), GRID_SPACING * _input_channels.size());
386 translate (dc, gc, hs, vs);
387 paint_indicators (dc);
393 optional<pair<int, int> >
394 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
396 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
397 int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
399 if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
400 return optional<pair<int, int> >();
403 int const input = (y - TOP_HEIGHT) / GRID_SPACING;
404 int const output = (x - LEFT_WIDTH) / GRID_SPACING;
406 if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
407 return optional<pair<int, int> >();
410 return make_pair (input, output);
414 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
416 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
417 if (x < GRID_SPACING || x > (2 * GRID_SPACING)) {
418 return optional<string>();
421 int y = (ev.GetY() + _vertical_scroll->GetThumbPosition() - (GRID_SPACING * 2)) / GRID_SPACING;
422 BOOST_FOREACH (Group i, _input_groups) {
423 if (i.from <= y && y <= i.to) {
428 return optional<string>();
432 AudioMappingView::left_down (wxMouseEvent& ev)
434 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
439 if (_map.get(channels->first, channels->second) > 0) {
440 _map.set (channels->first, channels->second, 0);
442 _map.set (channels->first, channels->second, 1);
445 map_values_changed ();
449 AudioMappingView::right_down (wxMouseEvent& ev)
451 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
456 _menu_input = channels->first;
457 _menu_output = channels->second;
458 PopupMenu (_menu, ev.GetPosition());
462 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
464 if (ev.ShiftDown()) {
465 _horizontal_scroll->SetThumbPosition (
466 _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
470 _vertical_scroll->SetThumbPosition (
471 _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
477 /** Called when any gain value has changed */
479 AudioMappingView::map_values_changed ()
482 _last_tooltip_channels = optional<pair<int, int> >();
487 AudioMappingView::off ()
489 _map.set (_menu_input, _menu_output, 0);
490 map_values_changed ();
494 AudioMappingView::full ()
496 _map.set (_menu_input, _menu_output, 1);
497 map_values_changed ();
501 AudioMappingView::minus6dB ()
503 _map.set (_menu_input, _menu_output, pow (10, -6.0 / 20));
504 map_values_changed ();
508 AudioMappingView::edit ()
510 int const d = _menu_output - 1;
512 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output - 1, _map.get(_menu_input, d));
513 if (dialog->ShowModal() == wxID_OK) {
514 _map.set (_menu_input, d, dialog->value ());
515 map_values_changed ();
522 AudioMappingView::set (AudioMapping map)
529 AudioMappingView::set_input_channels (vector<string> const & names)
531 _input_channels = names;
537 AudioMappingView::set_output_channels (vector<string> const & names)
539 _output_channels = names;
545 AudioMappingView::motion (wxMouseEvent& ev)
547 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
549 if (channels != _last_tooltip_channels) {
551 float const gain = _map.get(channels->first, channels->second);
553 s = wxString::Format (
554 _("No audio will be passed from content channel %d to DCP channel %d."),
555 channels->first + 1, channels->second + 1
557 } else if (gain == 1) {
558 s = wxString::Format (
559 _("Audio will be passed from content channel %d to DCP channel %d unaltered."),
560 channels->first + 1, channels->second + 1
563 float const dB = 20 * log10 (gain);
564 s = wxString::Format (
565 _("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."),
566 channels->first + 1, channels->second + 1, dB
570 SetToolTip (s + " " + _("Right click to change gain."));
573 optional<string> group = mouse_event_to_input_group_name (ev);
575 SetToolTip (std_to_wx(*group));
581 _last_tooltip_channels = channels;
586 AudioMappingView::set_input_groups (vector<Group> const & groups)
588 _input_groups = groups;