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, wxString left_label, wxString from, wxString top_label, wxString to)
64 : wxPanel (parent, wxID_ANY)
67 , _left_label (left_label)
69 , _top_label (top_label)
73 _menu->Append (ID_off, _("Off"));
74 _menu->Append (ID_full, _("Full"));
75 _menu->Append (ID_minus6dB, _("-6dB"));
76 _menu->Append (ID_edit, _("Edit..."));
78 _body = new wxPanel (this, wxID_ANY);
79 _vertical_scroll = new wxScrollBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
80 _horizontal_scroll = new wxScrollBar (this, wxID_ANY);
83 SetDoubleBuffered (true);
86 Bind (wxEVT_SIZE, boost::bind(&AudioMappingView::size, this, _1));
87 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::off, this), ID_off);
88 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::full, this), ID_full);
89 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::minus6dB, this), ID_minus6dB);
90 Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit);
91 Bind (wxEVT_MOUSEWHEEL, boost::bind(&AudioMappingView::mouse_wheel, this, _1));
92 _body->Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this));
93 _body->Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1));
94 _body->Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1));
95 _body->Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1));
96 _vertical_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
97 _vertical_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
98 _vertical_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
99 _vertical_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
100 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
101 _vertical_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
102 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
103 _vertical_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
104 _horizontal_scroll->Bind (wxEVT_SCROLL_TOP, boost::bind(&AudioMappingView::scroll, this));
105 _horizontal_scroll->Bind (wxEVT_SCROLL_BOTTOM, boost::bind(&AudioMappingView::scroll, this));
106 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEUP, boost::bind(&AudioMappingView::scroll, this));
107 _horizontal_scroll->Bind (wxEVT_SCROLL_LINEDOWN, boost::bind(&AudioMappingView::scroll, this));
108 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEUP, boost::bind(&AudioMappingView::scroll, this));
109 _horizontal_scroll->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind(&AudioMappingView::scroll, this));
110 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind(&AudioMappingView::scroll, this));
111 _horizontal_scroll->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind(&AudioMappingView::scroll, this));
115 AudioMappingView::size (wxSizeEvent& ev)
122 AudioMappingView::setup ()
124 wxSize const s = GetSize();
125 int const w = _vertical_scroll->GetSize().GetWidth();
126 int const h = _horizontal_scroll->GetSize().GetHeight();
128 _vertical_scroll->SetPosition (wxPoint(s.GetWidth() - w, 0));
129 _vertical_scroll->SetSize (wxSize(w, max(0, s.GetHeight() - h)));
131 _body->SetSize (wxSize(max(0, s.GetWidth() - w), max(0, s.GetHeight() - h)));
133 _horizontal_scroll->SetPosition (wxPoint(0, s.GetHeight() - h));
134 _horizontal_scroll->SetSize (wxSize(max(0, s.GetWidth() - w), h));
136 _vertical_scroll->SetScrollbar (
137 _vertical_scroll->GetThumbPosition(),
138 s.GetHeight() - h - 8,
139 GRID_SPACING * (2 + _input_channels.size()),
144 _horizontal_scroll->SetScrollbar (
145 _horizontal_scroll->GetThumbPosition(),
146 s.GetWidth() - w - 8,
147 GRID_SPACING * (3 + _output_channels.size()),
153 AudioMappingView::scroll ()
159 AudioMappingView::paint_static (wxDC& dc, wxGraphicsContext* gc)
161 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
162 dc.SetFont (wxSWISS_FONT->Bold());
164 wxCoord label_height;
166 dc.GetTextExtent (_top_label, &label_width, &label_height);
167 dc.DrawText (_top_label, LEFT_WIDTH + (_output_channels.size() * GRID_SPACING - label_width) / 2, (GRID_SPACING - label_height) / 2);
169 dc.GetTextExtent (_left_label, &label_width, &label_height);
172 (GRID_SPACING - label_height) / 2,
173 TOP_HEIGHT + (_input_channels.size() * GRID_SPACING + label_width) / 2,
177 dc.SetFont (*wxSWISS_FONT);
178 gc->SetPen (*wxBLACK_PEN);
182 AudioMappingView::paint_column_labels (wxDC& dc, wxGraphicsContext* gc)
185 wxCoord label_height;
187 BOOST_FOREACH (string i, _output_channels) {
188 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
189 dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2);
193 wxGraphicsPath lines = gc->CreatePath ();
194 lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING);
195 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING);
196 lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING * 2);
197 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, GRID_SPACING * 2);
198 gc->StrokePath (lines);
202 AudioMappingView::paint_column_lines (wxGraphicsContext* gc)
204 wxGraphicsPath lines = gc->CreatePath ();
205 for (size_t i = 0; i < _output_channels.size(); ++i) {
206 lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * i, GRID_SPACING);
207 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
209 lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), GRID_SPACING);
210 lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * _output_channels.size(), TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
211 gc->StrokePath (lines);
215 AudioMappingView::paint_row_labels (wxDC& dc, wxGraphicsContext* gc)
218 wxCoord label_height;
219 wxGraphicsPath lines = gc->CreatePath ();
221 /* Row channel labels */
224 BOOST_FOREACH (string i, _input_channels) {
225 dc.GetTextExtent (std_to_wx(i), &label_width, &label_height);
226 dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2);
230 /* Vertical lines on the left */
232 for (int i = 1; i < 3; ++i) {
233 lines.MoveToPoint (GRID_SPACING * i, TOP_HEIGHT);
234 lines.AddLineToPoint (GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING);
237 /* Group labels and lines */
240 BOOST_FOREACH (Group i, _input_groups) {
241 int const height = (i.to - i.from + 1) * GRID_SPACING;
242 dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height);
243 if (label_width > height) {
244 label_width = height - 8;
249 if ((yp - 2 * GRID_SPACING) < dc.GetLogicalOrigin().y) {
250 yp += dc.GetLogicalOrigin().y;
253 wxCoord old_x, old_y, old_width, old_height;
254 dc.GetClippingBox (&old_x, &old_y, &old_width, &old_height);
255 dc.DestroyClippingRegion ();
256 dc.SetClippingRegion (GRID_SPACING, yp + 4, GRID_SPACING, height - 8);
260 GRID_SPACING + (GRID_SPACING - label_height) / 2,
261 y + (height + label_width) / 2,
265 dc.DestroyClippingRegion ();
266 dc.SetClippingRegion (old_x, old_y, old_width, old_height);
269 lines.MoveToPoint (GRID_SPACING, y);
270 lines.AddLineToPoint (GRID_SPACING * 2, y);
274 lines.MoveToPoint (GRID_SPACING, y);
275 lines.AddLineToPoint (GRID_SPACING * 2, y);
277 gc->StrokePath (lines);
281 AudioMappingView::paint_row_lines (wxGraphicsContext* gc)
283 wxGraphicsPath lines = gc->CreatePath ();
284 for (size_t i = 0; i < _input_channels.size(); ++i) {
285 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * i);
286 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * i);
288 lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
289 lines.AddLineToPoint (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + GRID_SPACING * _input_channels.size());
290 gc->StrokePath (lines);
294 AudioMappingView::paint_indicators (wxDC& dc)
296 /* _{input,output}_channels and _map may not always be in sync, be careful here */
297 size_t const output = min(_output_channels.size(), size_t(_map.output_channels()));
298 size_t const input = min(_input_channels.size(), size_t(_map.input_channels()));
300 for (size_t x = 0; x < output; ++x) {
301 for (size_t y = 0; y < input; ++y) {
302 dc.SetBrush (*wxWHITE_BRUSH);
305 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
306 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
307 INDICATOR_SIZE, INDICATOR_SIZE
311 float const value_dB = 20 * log10 (_map.get(y, x));
312 int const range = 18;
314 if (value_dB > -range) {
315 height = INDICATOR_SIZE * (1 + value_dB / range);
318 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
321 LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2,
322 TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height,
323 INDICATOR_SIZE, height
331 void clip (wxDC& dc, wxGraphicsContext* gc, int x, int y, int w, int h)
333 dc.SetClippingRegion (x, y, w, h);
334 gc->Clip (x, y, w, h);
338 void translate (wxDC& dc, wxGraphicsContext* gc, int x, int y)
341 gc->Translate (-x, -y);
342 dc.SetLogicalOrigin (x, y);
346 void restore (wxDC& dc, wxGraphicsContext* gc)
348 dc.SetLogicalOrigin (0, 0);
350 dc.DestroyClippingRegion ();
355 AudioMappingView::paint ()
357 wxPaintDC dc (_body);
359 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
364 int const hs = _horizontal_scroll->GetThumbPosition ();
365 int const vs = _vertical_scroll->GetThumbPosition ();
367 paint_static (dc, gc);
369 clip (dc, gc, LEFT_WIDTH, 0, GRID_SPACING * _output_channels.size(), GRID_SPACING * (2 + _input_channels.size()));
370 translate (dc, gc, hs, 0);
371 paint_column_labels (dc, gc);
374 clip (dc, gc, 0, TOP_HEIGHT, GRID_SPACING * (3 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
375 translate (dc, gc, 0, vs);
376 paint_row_labels (dc, gc);
379 clip (dc, gc, GRID_SPACING * 2, TOP_HEIGHT, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * _input_channels.size() + 1);
380 translate (dc, gc, hs, vs);
381 paint_row_lines (gc);
384 clip (dc, gc, LEFT_WIDTH, GRID_SPACING, GRID_SPACING * (1 + _output_channels.size()), GRID_SPACING * (1 + _input_channels.size()));
385 translate (dc, gc, hs, vs);
386 paint_column_lines (gc);
389 clip (dc, gc, LEFT_WIDTH, TOP_HEIGHT, GRID_SPACING * _output_channels.size(), GRID_SPACING * _input_channels.size());
390 translate (dc, gc, hs, vs);
391 paint_indicators (dc);
397 optional<pair<int, int> >
398 AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const
400 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
401 int const y = ev.GetY() + _vertical_scroll->GetThumbPosition();
403 if (x <= LEFT_WIDTH || y < TOP_HEIGHT) {
404 return optional<pair<int, int> >();
407 int const input = (y - TOP_HEIGHT) / GRID_SPACING;
408 int const output = (x - LEFT_WIDTH) / GRID_SPACING;
410 if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) {
411 return optional<pair<int, int> >();
414 return make_pair (input, output);
418 AudioMappingView::mouse_event_to_input_group_name (wxMouseEvent& ev) const
420 int const x = ev.GetX() + _horizontal_scroll->GetThumbPosition();
421 if (x < GRID_SPACING || x > (2 * GRID_SPACING)) {
422 return optional<string>();
425 int y = (ev.GetY() + _vertical_scroll->GetThumbPosition() - (GRID_SPACING * 2)) / GRID_SPACING;
426 BOOST_FOREACH (Group i, _input_groups) {
427 if (i.from <= y && y <= i.to) {
432 return optional<string>();
436 AudioMappingView::left_down (wxMouseEvent& ev)
438 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
443 if (_map.get(channels->first, channels->second) > 0) {
444 _map.set (channels->first, channels->second, 0);
446 _map.set (channels->first, channels->second, 1);
449 map_values_changed ();
453 AudioMappingView::right_down (wxMouseEvent& ev)
455 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
460 _menu_input = channels->first;
461 _menu_output = channels->second;
462 PopupMenu (_menu, ev.GetPosition());
466 AudioMappingView::mouse_wheel (wxMouseEvent& ev)
468 if (ev.ShiftDown()) {
469 _horizontal_scroll->SetThumbPosition (
470 _horizontal_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
474 _vertical_scroll->SetThumbPosition (
475 _vertical_scroll->GetThumbPosition() + (ev.GetWheelRotation() > 0 ? -GRID_SPACING : GRID_SPACING)
481 /** Called when any gain value has changed */
483 AudioMappingView::map_values_changed ()
486 _last_tooltip_channels = optional<pair<int, int> >();
491 AudioMappingView::off ()
493 _map.set (_menu_input, _menu_output, 0);
494 map_values_changed ();
498 AudioMappingView::full ()
500 _map.set (_menu_input, _menu_output, 1);
501 map_values_changed ();
505 AudioMappingView::minus6dB ()
507 _map.set (_menu_input, _menu_output, pow (10, -6.0 / 20));
508 map_values_changed ();
512 AudioMappingView::edit ()
514 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output, _map.get(_menu_input, _menu_output));
515 if (dialog->ShowModal() == wxID_OK) {
516 _map.set (_menu_input, _menu_output, dialog->value ());
517 map_values_changed ();
524 AudioMappingView::set (AudioMapping map)
531 AudioMappingView::set_input_channels (vector<string> const & names)
533 _input_channels = names;
539 AudioMappingView::set_output_channels (vector<string> const & names)
541 _output_channels = names;
547 AudioMappingView::safe_input_channel_name (int n) const
549 if (n >= int(_input_channels.size())) {
550 return wxString::Format ("%d", n + 1);
553 optional<wxString> group;
554 BOOST_FOREACH (Group i, _input_groups) {
555 if (i.from <= n && n <= i.to) {
556 group = std_to_wx (i.name);
560 if (group && !group->IsEmpty()) {
561 return wxString::Format ("%s/%s", group->data(), std_to_wx(_input_channels[n]).data());
564 return std_to_wx(_input_channels[n]);
568 AudioMappingView::safe_output_channel_name (int n) const
570 if (n >= int(_output_channels.size())) {
571 return wxString::Format ("%d", n + 1);
574 return std_to_wx(_output_channels[n]);
578 AudioMappingView::motion (wxMouseEvent& ev)
580 optional<pair<int, int> > channels = mouse_event_to_channels (ev);
582 if (channels != _last_tooltip_channels) {
584 float const gain = _map.get(channels->first, channels->second);
586 s = wxString::Format (
587 _("No audio will be passed from %s channel '%s' to %s channel '%s'."),
589 safe_input_channel_name(channels->first),
591 safe_output_channel_name(channels->second)
593 } else if (gain == 1) {
594 s = wxString::Format (
595 _("Audio will be passed from %s channel %s to %s channel %s unaltered."),
597 safe_input_channel_name(channels->first),
599 safe_output_channel_name(channels->second)
602 float const dB = 20 * log10 (gain);
603 s = wxString::Format (
604 _("Audio will be passed from %s channel %s to %s channel %s with gain %.1fdB."),
606 safe_input_channel_name(channels->first),
608 safe_output_channel_name(channels->second),
613 SetToolTip (s + " " + _("Right click to change gain."));
616 optional<string> group = mouse_event_to_input_group_name (ev);
618 SetToolTip (std_to_wx(*group));
624 _last_tooltip_channels = channels;
629 AudioMappingView::set_input_groups (vector<Group> const & groups)
631 _input_groups = groups;