2 Copyright (C) 2013-2016 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 "lib/audio_mapping.h"
27 #include "audio_mapping_view.h"
29 #include "audio_gain_dialog.h"
30 #include <dcp/raw_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 dcp::raw_convert;
50 #define INDICATOR_SIZE 16
60 class NoSelectionStringRenderer : public wxGridCellStringRenderer
63 void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
65 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
69 /** @class ValueRenderer
70 * @brief wxGridCellRenderer for a gain value.
72 class ValueRenderer : public wxGridCellRenderer
76 void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
78 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
79 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
80 dc.DrawRectangle (rect);
82 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
83 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
85 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
86 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
87 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
89 float const value = raw_convert<float> (wx_to_std (grid.GetCellValue (row, col)));
90 float const value_dB = 20 * log10 (value);
93 if (value_dB > -range) {
94 height = INDICATOR_SIZE * (1 + value_dB / range);
97 height = max (0, height);
100 /* Make sure we get a little bit of the marker if there is any gain */
101 height = max (3, height);
104 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
105 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
108 wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
110 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
113 wxGridCellRenderer* Clone () const
115 return new ValueRenderer;
120 AudioMappingView::AudioMappingView (wxWindow* parent)
121 : wxPanel (parent, wxID_ANY)
124 , _last_tooltip_row (0)
125 , _last_tooltip_column (0)
127 _left_labels = new wxPanel (this, wxID_ANY);
128 _left_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_left_labels, this));
129 _top_labels = new wxPanel (this, wxID_ANY);
130 _top_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_top_labels, this));
132 _grid = new wxGrid (this, wxID_ANY);
134 _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
135 _grid->HideRowLabels ();
136 _grid->DisableDragRowSize ();
137 _grid->DisableDragColSize ();
138 _grid->EnableEditing (false);
139 _grid->SetCellHighlightPenWidth (0);
140 _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
143 wxSizer* vertical_sizer = new wxBoxSizer (wxVERTICAL);
144 vertical_sizer->Add (_top_labels);
145 wxSizer* horizontal_sizer = new wxBoxSizer (wxHORIZONTAL);
146 horizontal_sizer->Add (_left_labels);
147 horizontal_sizer->Add (_grid, 1, wxEXPAND | wxALL);
148 vertical_sizer->Add (horizontal_sizer);
149 SetSizerAndFit (vertical_sizer);
151 Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
152 Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
153 _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved_grid, this, _1));
154 Bind (wxEVT_SIZE, boost::bind (&AudioMappingView::sized, this, _1));
157 _menu->Append (ID_off, _("Off"));
158 _menu->Append (ID_full, _("Full"));
159 _menu->Append (ID_minus6dB, _("-6dB"));
160 _menu->Append (ID_edit, _("Edit..."));
162 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
163 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
164 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
165 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
167 _left_labels->Bind (wxEVT_MOTION, bind (&AudioMappingView::mouse_moved_left_labels, this, _1));
170 /** Called when any gain value has changed */
172 AudioMappingView::map_values_changed ()
176 _last_tooltip_column = -1;
180 AudioMappingView::left_click (wxGridEvent& ev)
182 if (ev.GetCol() == 0) {
186 int const d = ev.GetCol() - 1;
188 if (_map.get (ev.GetRow(), d) > 0) {
189 _map.set (ev.GetRow(), d, 0);
191 _map.set (ev.GetRow(), d, 1);
194 map_values_changed ();
198 AudioMappingView::right_click (wxGridEvent& ev)
200 if (ev.GetCol() == 0) {
204 _menu_row = ev.GetRow ();
205 _menu_column = ev.GetCol ();
206 PopupMenu (_menu, ev.GetPosition ());
210 AudioMappingView::off ()
212 _map.set (_menu_row, _menu_column - 1, 0);
213 map_values_changed ();
217 AudioMappingView::full ()
219 _map.set (_menu_row, _menu_column - 1, 1);
220 map_values_changed ();
224 AudioMappingView::minus6dB ()
226 _map.set (_menu_row, _menu_column - 1, pow (10, -6.0 / 20));
227 map_values_changed ();
231 AudioMappingView::edit ()
233 int const d = _menu_column - 1;
235 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
236 if (dialog->ShowModal () == wxID_OK) {
237 _map.set (_menu_row, d, dialog->value ());
238 map_values_changed ();
245 AudioMappingView::set (AudioMapping map)
252 AudioMappingView::set_input_channels (vector<string> const & names)
254 for (int i = 0; i < _grid->GetNumberRows(); ++i) {
255 _grid->SetCellValue (i, 0, std_to_wx (names[i]));
260 AudioMappingView::set_output_channels (vector<string> const & names)
262 int const o = names.size() + 1;
263 if (o < _grid->GetNumberCols ()) {
264 _grid->DeleteCols (o, _grid->GetNumberCols() - o);
265 } else if (o > _grid->GetNumberCols ()) {
266 _grid->InsertCols (_grid->GetNumberCols(), o - _grid->GetNumberCols());
269 _grid->SetColLabelValue (0, wxT (""));
271 for (size_t i = 0; i < names.size(); ++i) {
272 _grid->SetColLabelValue (i + 1, std_to_wx (names[i]));
280 AudioMappingView::update_cells ()
282 vector<string> row_names;
283 for (int i = 0; i < _grid->GetNumberRows (); ++i) {
284 row_names.push_back (wx_to_std (_grid->GetCellValue (i, 0)));
287 if (_grid->GetNumberRows ()) {
288 _grid->DeleteRows (0, _grid->GetNumberRows ());
291 _grid->InsertRows (0, _map.input_channels ());
293 for (int i = 0; i < _map.input_channels(); ++i) {
294 for (int j = 0; j < _map.output_channels(); ++j) {
295 _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
299 for (int i = 0; i < _map.input_channels(); ++i) {
300 if (i < int (row_names.size ())) {
301 _grid->SetCellValue (i, 0, std_to_wx (row_names[i]));
303 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
304 _grid->SetCellValue (i, j, std_to_wx (raw_convert<string> (_map.get (i, j - 1))));
312 AudioMappingView::mouse_moved_grid (wxMouseEvent& ev)
316 _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
318 int const row = _grid->YToRow (yy);
319 int const column = _grid->XToCol (xx);
321 if (row < 0 || column < 1) {
322 _grid->GetGridWindow()->SetToolTip ("");
323 _last_tooltip_row = row;
324 _last_tooltip_column = column;
327 if (row != _last_tooltip_row || column != _last_tooltip_column) {
330 float const gain = _map.get (row, column - 1);
332 s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
333 } else if (gain == 1) {
334 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
336 float const dB = 20 * log10 (gain);
337 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
340 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
341 _last_tooltip_row = row;
342 _last_tooltip_column = column;
349 AudioMappingView::sized (wxSizeEvent& ev)
356 AudioMappingView::setup_sizes ()
358 int const top_height = 24;
361 _left_labels->SetMinSize (wxSize (LEFT_WIDTH, _grid->GetSize().GetHeight()));
362 _top_labels->SetMinSize (wxSize (_grid->GetSize().GetWidth() + LEFT_WIDTH, top_height));
363 /* Try to make the _top_labels 'actua' size respect the minimum we just set */
365 _left_labels->Refresh ();
366 _top_labels->Refresh ();
370 AudioMappingView::paint_left_labels ()
372 wxPaintDC dc (_left_labels);
374 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
379 wxSize const size = dc.GetSize();
380 int const half = size.GetWidth() / 2;
382 gc->SetPen (wxPen (wxColour (0, 0, 0)));
383 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
385 wxGraphicsPath lines = gc->CreatePath();
387 vector<pair<int, int> >::const_iterator i = _input_group_positions.begin();
388 if (i != _input_group_positions.end()) {
389 lines.MoveToPoint (half, i->first);
390 lines.AddLineToPoint (size.GetWidth(), i->first);
393 vector<Group>::const_iterator j = _input_groups.begin();
394 while (i != _input_group_positions.end() && j != _input_groups.end()) {
396 dc.SetClippingRegion (0, i->first + 2, size.GetWidth(), i->second - 4);
398 dc.SetFont (*wxSWISS_FONT);
400 wxCoord label_height;
401 dc.GetTextExtent (std_to_wx (j->name), &label_width, &label_height);
405 half + (half - label_height) / 2,
406 min (i->second, (i->second + i->first + label_width) / 2),
410 dc.DestroyClippingRegion ();
412 lines.MoveToPoint (half, i->second);
413 lines.AddLineToPoint (size.GetWidth(), i->second);
415 gc->StrokePath (lines);
422 dc.SetFont (wxSWISS_FONT->Bold());
423 wxCoord overall_label_width;
424 wxCoord overall_label_height;
425 dc.GetTextExtent (_("Content"), &overall_label_width, &overall_label_height);
428 (half - overall_label_height) / 2,
429 min (size.GetHeight(), (size.GetHeight() + _grid->GetColLabelSize() + overall_label_width) / 2),
437 AudioMappingView::paint_top_labels ()
439 wxPaintDC dc (_top_labels);
440 if (_grid->GetNumberCols() == 0) {
444 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
449 wxSize const size = dc.GetSize();
451 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
453 dc.SetFont (wxSWISS_FONT->Bold());
455 wxCoord label_height;
456 dc.GetTextExtent (_("DCP"), &label_width, &label_height);
458 dc.DrawText (_("DCP"), (size.GetWidth() + _grid->GetColSize(0) + LEFT_WIDTH - label_width) / 2, (size.GetHeight() - label_height) / 2);
460 gc->SetPen (wxPen (wxColour (0, 0, 0)));
461 wxGraphicsPath lines = gc->CreatePath();
462 lines.MoveToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, 0);
463 lines.AddLineToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, size.GetHeight());
464 lines.MoveToPoint (size.GetWidth() - 1, 0);
465 lines.AddLineToPoint (size.GetWidth() - 1, size.GetHeight());
466 gc->StrokePath (lines);
472 AudioMappingView::set_input_groups (vector<Group> const & groups)
474 _input_groups = groups;
475 _input_group_positions.clear ();
477 int ypos = _grid->GetColLabelSize() - 1;
478 BOOST_FOREACH (Group const & i, _input_groups) {
479 int const old_ypos = ypos;
480 ypos += (i.to - i.from + 1) * _grid->GetRowSize(0);
481 _input_group_positions.push_back (make_pair (old_ypos, ypos));
486 AudioMappingView::mouse_moved_left_labels (wxMouseEvent& event)
489 for (size_t i = 0; i < _input_group_positions.size(); ++i) {
490 if (_input_group_positions[i].first <= event.GetY() && event.GetY() < _input_group_positions[i].second) {
491 _left_labels->SetToolTip (_input_groups[i].name);
497 _left_labels->SetToolTip ("");