2 Copyright (C) 2013-2020 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)
64 AudioMappingView::AudioMappingView (wxWindow* parent, wxString left_label, wxString from, wxString top_label, wxString to)
65 : wxPanel (parent, wxID_ANY)
68 , _left_label (left_label)
70 , _top_label (top_label)
74 _menu->Append (ID_off, _("Off"));
75 _menu->Append (ID_minus6dB, _("-6dB"));
76 _menu->Append (ID_0dB, _("0dB (unchanged)"));
77 _menu->Append (ID_plus3dB, _("+3dB"));
78 _menu->Append (ID_edit, _("Edit..."));
80 _body = new wxPanel (this, wxID_ANY);
81 _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
82 _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
85 SetDoubleBuffered (true);
88 Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
89 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, 0), ID_off);
90 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, db_to_linear(-6)), ID_minus6dB);
91 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, 1), ID_0dB);
92 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::set_gain_from_menu, this, db_to_linear(3)), ID_plus3dB);
93 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
94 Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
95 _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
96 _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
97 _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
98 _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
99 _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
100 _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
101 _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
102 _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
103 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
104 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
105 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
106 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
107 _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
108 _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
109 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
110 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
111 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
112 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
113 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
114 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
118 AudioMappingView::size (wxSizeEvent& ev)
125 AudioMappingView::setup ()
127 wxSize const s = GetSize();
128 int const w = _vertical_scroll->GetSize().GetWidth();
129 int const h = _horizontal_scroll->GetSize().GetHeight();
131 _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
132 _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
134 _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
136 _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
137 _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
139 _vertical_scroll->SetScrollbar (
140 _vertical_scroll->GetThumbPosition(),
141 s.GetHeight() - h - 8,
142 GRID_SPACING * (2 + _input_channels.size()),
147 _horizontal_scroll->SetScrollbar (
148 _horizontal_scroll->GetThumbPosition(),
149 s.GetWidth() - w - 8,
150 GRID_SPACING * (3 + _output_channels.size()),
156 AudioMappingView::scroll ()
162 AudioMappingView::paint_static (wxDC& dc, wxGraphicsContext* gc)
164 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
165 dc.SetFont (wxSWISS_FONT->Bold());
167 wxCoord label_height;
169 dc.GetTextExtent (_top_label, &label_width, &label_height);
170 dc.DrawText (_top_label, LEFT_WIDTH + (_output_channels.size() * GRID_SPACING - label_width) / 2, (GRID_SPACING - label_height) / 2);
172 dc.GetTextExtent (_left_label, &label_width, &label_height);
175 (GRID_SPACING - label_height) / 2,
176 TOP_HEIGHT + (_input_channels.size() * GRID_SPACING + label_width) / 2,
180 dc.SetFont (*wxSWISS_FONT);
181 gc->SetPen (*wxBLACK_PEN);
185 AudioMappingView::paint_column_labels (wxDC& dc, wxGraphicsContext* gc)
188 wxCoord label_height;
190 BOOST_FOREACH (string i, _output_channels) {
191 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
192 dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2);
196 wxGraphicsPath lines = gc->CreatePath ();
197 lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING);
198 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING);
199 lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING * 2);
200 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING * 2);
201 gc->StrokePath (lines);
205 AudioMappingView::paint_column_lines (wxGraphicsContext* gc)
207 wxGraphicsPath lines = gc->CreatePath ();
208 for (size_t i = 0; i < _output_channels.size(); ++i) {
209 lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * i, GRID_SPACING);
210 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
212 lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), GRID_SPACING);
213 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
214 gc->StrokePath (lines);
218 AudioMappingView::paint_row_labels (wxDC& dc, wxGraphicsContext* gc)
221 wxCoord label_height;
222 wxGraphicsPath lines = gc->CreatePath ();
224 /* Row channel labels */
227 BOOST_FOREACH (string i, _input_channels) {
228 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
229 dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2);
233 /* Vertical lines on the left */
235 for (int i = 1; i < 3; ++i) {
236 lines.MoveToPoint (GRID_SPACING * i, TOP_HEIGHT);
237 lines.AddLineToPoint (GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
240 /* Group labels and lines */
243 BOOST_FOREACH (Group i, _input_groups) {
244 int const height = (i.to - i.from + 1) * GRID_SPACING;
245 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
246 if (label_width > height) {
247 label_width = height - 8;
252 if ((yp - 2 * GRID_SPACING) < dc.GetLogicalOrigin().y) {
253 yp += dc.GetLogicalOrigin().y;
256 wxCoord old_x, old_y, old_width, old_height;
257 dc.GetClippingBox (&old_x, &old_y, &old_width, &old_height);
258 dc.DestroyClippingRegion ();
259 dc.SetClippingRegion (GRID_SPACING, yp + 4, GRID_SPACING, height - 8);
263 GRID_SPACING + (GRID_SPACING - label_height) / 2,
264 y + (height + label_width) / 2,
268 dc.DestroyClippingRegion ();
269 dc.SetClippingRegion (old_x, old_y, old_width, old_height);
272 lines.MoveToPoint (GRID_SPACING, y);
273 lines.AddLineToPoint (GRID_SPACING * 2, y);
277 lines.MoveToPoint (GRID_SPACING, y);
278 lines.AddLineToPoint (GRID_SPACING * 2, y);
280 gc->StrokePath (lines);
284 AudioMappingView::paint_row_lines (wxGraphicsContext* gc)
286 wxGraphicsPath lines = gc->CreatePath ();
287 for (size_t i = 0; i < _input_channels.size(); ++i) {
288 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * i);
289 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * i);
291 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
292 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
293 gc->StrokePath (lines);
297 AudioMappingView::paint_indicators (wxDC& dc)
299 /* _{input,output}_channels and _map may not always be in sync, be careful here */
300 size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
301 size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
303 for (size_t x = 0; x < output; ++x) {
304 for (size_t y = 0; y < input; ++y) {
305 dc.SetBrush (*wxWHITE_BRUSH);
308 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
309 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
310 INDICATOR_SIZE, INDICATOR_SIZE
314 float const value_dB = linear_to_db(_map.get(y, x));
315 wxColour const colour = value_dB <= 0 ? wxColour(0, 255, 0) : wxColour(255, 150, 0);
316 int const range = 18;
318 if (value_dB > -range) {
319 height = min(INDICATOR_SIZE, static_cast<int>(INDICATOR_SIZE * (1 + value_dB / range)));
322 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(colour, wxBRUSHSTYLE_SOLID));
325 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
326 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
327 INDICATOR_SIZE, height
335 void clip (wxDC& dc, wxGraphicsContext* gc, int x, int y, int w, int h)
337 dc.SetClippingRegion (x, y, w, h);
338 gc->Clip (x, y, w, h);
342 void translate (wxDC& dc, wxGraphicsContext* gc, int x, int y)
345 gc->Translate (-x, -y);
346 dc.SetLogicalOrigin (x, y);
350 void restore (wxDC& dc, wxGraphicsContext* gc)
352 dc.SetLogicalOrigin (0, 0);
354 dc.DestroyClippingRegion ();
359 AudioMappingView::paint ()
361 wxPaintDC dc (_body);
363 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
368 int const hs = _horizontal_scroll->GetThumbPosition ();
369 int const vs = _vertical_scroll->GetThumbPosition ();
371 paint_static (dc, gc);
373 clip (dc, gc, LEFT_WIDTH, 0, GRID_SPACING * _output_channels.size(), GRID_SPACING * (2 + _input_channels.size()));
374 translate (dc, gc, hs, 0);
375 paint_column_labels (dc, gc);
378 clip (dc, gc, 0, TOP_HEIGHT, GRID_SPACING * (3 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
379 translate (dc, gc, 0, vs);
380 paint_row_labels (dc, gc);
383 clip (dc, gc, GRID_SPACING * 2, TOP_HEIGHT, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
384 translate (dc, gc, hs, vs);
385 paint_row_lines (gc);
388 clip (dc, gc, LEFT_WIDTH, GRID_SPACING, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * (1 + _input_channels.size()));
389 translate (dc, gc, hs, vs);
390 paint_column_lines (gc);
393 clip (dc, gc, LEFT_WIDTH, TOP_HEIGHT, GRID_SPACING * _output_channels.size(), GRID_SPACING * _input_channels.size());
394 translate (dc, gc, hs, vs);
395 paint_indicators (dc);
401 optional<pair<int, int> >
402 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
404 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
405 int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
407 if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
408 return optional<pair<int, int> >();
411 int const input = (y - TOP_HEIGHT) / GRID_SPACING;
412 int const output = (x - LEFT_WIDTH) / GRID_SPACING;
414 if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
415 return optional<pair<int, int> >();
418 return make_pair (input, output);
422 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
424 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
425 if (x < GRID_SPACING || x > (2 * GRID_SPACING)) {
426 return optional<string>();
429 int y = (ev.GetY() + _vertical_scroll->GetThumbPosition() - (GRID_SPACING * 2)) / GRID_SPACING;
430 BOOST_FOREACH (Group i, _input_groups) {
431 if (i.from <= y && y <= i.to) {
436 return optional<string>();
440 AudioMappingView::left_down (wxMouseEvent& ev)
442 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
447 if (_map.get(channels->first, channels->second) > 0) {
448 _map.set (channels->first, channels->second, 0);
450 _map.set (channels->first, channels->second, 1);
453 map_values_changed ();
457 AudioMappingView::right_down (wxMouseEvent& ev)
459 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
464 _menu_input = channels->first;
465 _menu_output = channels->second;
466 PopupMenu (_menu, ev.GetPosition());
470 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
472 if (ev.ShiftDown()) {
473 _horizontal_scroll->SetThumbPosition (
474 _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
478 _vertical_scroll->SetThumbPosition (
479 _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
485 /** Called when any gain value has changed */
487 AudioMappingView::map_values_changed ()
490 _last_tooltip_channels = optional<pair<int, int> >();
495 AudioMappingView::set_gain_from_menu (double linear)
497 _map.set (_menu_input, _menu_output, linear);
498 map_values_changed ();
502 AudioMappingView::edit ()
504 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output, _map.get(_menu_input, _menu_output));
505 if (dialog->ShowModal() == wxID_OK) {
506 _map.set (_menu_input, _menu_output, dialog->value ());
507 map_values_changed ();
514 AudioMappingView::set (AudioMapping map)
521 AudioMappingView::set_input_channels (vector<string> const & names)
523 _input_channels = names;
529 AudioMappingView::set_output_channels (vector<string> const & names)
531 _output_channels = names;
537 AudioMappingView::safe_input_channel_name (int n) const
539 if (n >= int(_input_channels.size())) {
540 return wxString::Format ("%d", n + 1);
543 optional<wxString> group;
544 BOOST_FOREACH (Group i, _input_groups) {
545 if (i.from <= n && n <= i.to) {
546 group = std_to_wx (i.name);
550 if (group && !group->IsEmpty()) {
551 return wxString::Format ("%s/%s", group->data(), std_to_wx(_input_channels[n]).data());
554 return std_to_wx(_input_channels[n]);
558 AudioMappingView::safe_output_channel_name (int n) const
560 if (n >= int(_output_channels.size())) {
561 return wxString::Format ("%d", n + 1);
564 return std_to_wx(_output_channels[n]);
568 AudioMappingView::motion (wxMouseEvent& ev)
570 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
572 if (channels != _last_tooltip_channels) {
574 float const gain = _map.get(channels->first, channels->second);
576 s = wxString::Format (
577 _("No audio will be passed from %s channel '%s' to %s channel '%s'."),
579 safe_input_channel_name(channels->first),
581 safe_output_channel_name(channels->second)
583 } else if (gain == 1) {
584 s = wxString::Format (
585 _("Audio will be passed from %s channel %s to %s channel %s unaltered."),
587 safe_input_channel_name(channels->first),
589 safe_output_channel_name(channels->second)
592 float const dB = linear_to_db(gain);
593 s = wxString::Format (
594 _("Audio will be passed from %s channel %s to %s channel %s with gain %.1fdB."),
596 safe_input_channel_name(channels->first),
598 safe_output_channel_name(channels->second),
603 SetToolTip (s + " " + _("Right click to change gain."));
606 optional<string> group = mouse_event_to_input_group_name (ev);
608 SetToolTip (std_to_wx(*group));
614 _last_tooltip_channels = channels;
619 AudioMappingView::set_input_groups (vector<Group> const & groups)
621 _input_groups = groups;