2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/wx/audio_mapping_view.cc
21 * @brief AudioMappingView class and helpers.
25 #include <wx/renderer.h>
27 #include <dcp/types.h>
28 #include <dcp/raw_convert.h>
29 #include "lib/audio_mapping.h"
31 #include "audio_mapping_view.h"
33 #include "audio_gain_dialog.h"
34 #include <boost/lexical_cast.hpp>
40 using boost::shared_ptr;
41 using boost::lexical_cast;
43 #define INDICATOR_SIZE 16
52 class NoSelectionStringRenderer : public wxGridCellStringRenderer
55 void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
57 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
61 /** @class ValueRenderer
62 * @brief wxGridCellRenderer for a gain value.
64 class ValueRenderer : public wxGridCellRenderer
68 void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
70 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
71 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
72 dc.DrawRectangle (rect);
74 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
75 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
77 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
78 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
79 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
81 float const value = lexical_cast<float> (wx_to_std (grid.GetCellValue (row, col)));
82 float const value_dB = 20 * log10 (value);
85 if (value_dB > -range) {
86 height = INDICATOR_SIZE * (1 + value_dB / range);
89 height = max (0, height);
92 /* Make sure we get a little bit of the marker if there is any gain */
93 height = max (3, height);
96 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
97 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
100 wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
102 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
105 wxGridCellRenderer* Clone () const
107 return new ValueRenderer;
112 AudioMappingView::AudioMappingView (wxWindow* parent)
113 : wxPanel (parent, wxID_ANY)
116 , _last_tooltip_row (0)
117 , _last_tooltip_column (0)
119 _grid = new wxGrid (this, wxID_ANY);
121 _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
122 _grid->HideRowLabels ();
123 _grid->DisableDragRowSize ();
124 _grid->DisableDragColSize ();
125 _grid->EnableEditing (false);
126 _grid->SetCellHighlightPenWidth (0);
127 _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
129 set_column_labels ();
131 _sizer = new wxBoxSizer (wxVERTICAL);
132 _sizer->Add (_grid, 1, wxEXPAND | wxALL);
133 SetSizerAndFit (_sizer);
135 Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
136 Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
137 _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1));
140 _menu->Append (ID_off, _("Off"));
141 _menu->Append (ID_full, _("Full"));
142 _menu->Append (ID_minus6dB, _("-6dB"));
143 _menu->Append (ID_edit, _("Edit..."));
145 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
146 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
147 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
148 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
152 AudioMappingView::map_changed ()
156 _last_tooltip_column = -1;
160 AudioMappingView::left_click (wxGridEvent& ev)
162 if (ev.GetCol() == 0) {
166 dcp::Channel d = static_cast<dcp::Channel> (ev.GetCol() - 1);
168 if (_map.get (ev.GetRow(), d) > 0) {
169 _map.set (ev.GetRow(), d, 0);
171 _map.set (ev.GetRow(), d, 1);
178 AudioMappingView::right_click (wxGridEvent& ev)
180 if (ev.GetCol() == 0) {
184 _menu_row = ev.GetRow ();
185 _menu_column = ev.GetCol ();
186 PopupMenu (_menu, ev.GetPosition ());
190 AudioMappingView::off ()
192 _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 0);
197 AudioMappingView::full ()
199 _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 1);
204 AudioMappingView::minus6dB ()
206 _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), pow (10, -6.0 / 20));
211 AudioMappingView::edit ()
213 dcp::Channel d = static_cast<dcp::Channel> (_menu_column - 1);
215 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
216 if (dialog->ShowModal () == wxID_OK) {
217 _map.set (_menu_row, d, dialog->value ());
225 AudioMappingView::set (AudioMapping map)
232 AudioMappingView::update_cells ()
234 if (_grid->GetNumberRows ()) {
235 _grid->DeleteRows (0, _grid->GetNumberRows ());
238 _grid->InsertRows (0, _map.content_channels ());
240 for (int i = 0; i < _map.content_channels(); ++i) {
241 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
242 _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
246 for (int i = 0; i < _map.content_channels(); ++i) {
247 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
249 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
250 _grid->SetCellValue (i, j, std_to_wx (dcp::raw_convert<string> (_map.get (i, static_cast<dcp::Channel> (j - 1)))));
257 /** @param c Number of DCP channels */
259 AudioMappingView::set_channels (int c)
263 if (c < _grid->GetNumberCols ()) {
264 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
265 } else if (c > _grid->GetNumberCols ()) {
266 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
267 set_column_labels ();
274 AudioMappingView::set_column_labels ()
276 int const c = _grid->GetNumberCols ();
278 _grid->SetColLabelValue (0, _("Content"));
280 #if MAX_DCP_AUDIO_CHANNELS != 12
281 #warning AudioMappingView::set_column_labels() is expecting the wrong MAX_DCP_AUDIO_CHANNELS
285 _grid->SetColLabelValue (1, _("L"));
289 _grid->SetColLabelValue (2, _("R"));
293 _grid->SetColLabelValue (3, _("C"));
297 _grid->SetColLabelValue (4, _("Lfe"));
301 _grid->SetColLabelValue (5, _("Ls"));
305 _grid->SetColLabelValue (6, _("Rs"));
309 _grid->SetColLabelValue (7, _("HI"));
313 _grid->SetColLabelValue (8, _("VI"));
317 _grid->SetColLabelValue (9, _("Lc"));
321 _grid->SetColLabelValue (10, _("Rc"));
325 _grid->SetColLabelValue (11, _("BsL"));
329 _grid->SetColLabelValue (12, _("BsR"));
336 AudioMappingView::mouse_moved (wxMouseEvent& ev)
340 _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
342 int const row = _grid->YToRow (yy);
343 int const column = _grid->XToCol (xx);
345 if (row < 0 || column < 1) {
346 _grid->GetGridWindow()->SetToolTip ("");
347 _last_tooltip_row = row;
348 _last_tooltip_column = column;
351 if (row != _last_tooltip_row || column != _last_tooltip_column) {
354 float const gain = _map.get (row, static_cast<dcp::Channel> (column - 1));
356 s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
357 } else if (gain == 1) {
358 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
360 float const dB = 20 * log10 (gain);
361 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
364 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
365 _last_tooltip_row = row;
366 _last_tooltip_column = column;