2 Copyright (C) 2013 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.
21 #include <wx/renderer.h>
23 #include <libdcp/types.h>
24 #include <libdcp/raw_convert.h>
25 #include "lib/audio_mapping.h"
27 #include "audio_mapping_view.h"
29 #include "audio_gain_dialog.h"
35 using boost::shared_ptr;
36 using boost::lexical_cast;
38 #define INDICATOR_SIZE 16
47 class NoSelectionStringRenderer : public wxGridCellStringRenderer
50 void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
52 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
56 class ValueRenderer : public wxGridCellRenderer
60 void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
62 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
63 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
64 dc.DrawRectangle (rect);
66 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
67 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
69 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
70 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
71 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
73 float const value = lexical_cast<float> (wx_to_std (grid.GetCellValue (row, col)));
74 float const value_dB = 20 * log10 (value);
77 if (value_dB > -range) {
78 height = INDICATOR_SIZE * (1 + value_dB / range);
81 height = max (0, height);
84 /* Make sure we get a little bit of the marker if there is any gain */
85 height = max (3, height);
88 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
89 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
92 wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
94 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
97 wxGridCellRenderer* Clone () const
99 return new ValueRenderer;
104 AudioMappingView::AudioMappingView (wxWindow* parent)
105 : wxPanel (parent, wxID_ANY)
108 , _last_tooltip_row (0)
109 , _last_tooltip_column (0)
111 _grid = new wxGrid (this, wxID_ANY);
113 _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
114 _grid->HideRowLabels ();
115 _grid->DisableDragRowSize ();
116 _grid->DisableDragColSize ();
117 _grid->EnableEditing (false);
118 _grid->SetCellHighlightPenWidth (0);
119 _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
121 set_column_labels ();
123 _sizer = new wxBoxSizer (wxVERTICAL);
124 _sizer->Add (_grid, 1, wxEXPAND | wxALL);
125 SetSizerAndFit (_sizer);
127 Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
128 Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
129 _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1));
132 _menu->Append (ID_off, _("Off"));
133 _menu->Append (ID_full, _("Full"));
134 _menu->Append (ID_minus6dB, _("-6dB"));
135 _menu->Append (ID_edit, _("Edit..."));
137 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
138 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
139 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
140 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
144 AudioMappingView::map_changed ()
148 _last_tooltip_column = -1;
152 AudioMappingView::left_click (wxGridEvent& ev)
154 if (ev.GetCol() == 0) {
158 libdcp::Channel d = static_cast<libdcp::Channel> (ev.GetCol() - 1);
160 if (_map.get (ev.GetRow(), d) > 0) {
161 _map.set (ev.GetRow(), d, 0);
163 _map.set (ev.GetRow(), d, 1);
170 AudioMappingView::right_click (wxGridEvent& ev)
172 if (ev.GetCol() == 0) {
176 _menu_row = ev.GetRow ();
177 _menu_column = ev.GetCol ();
178 PopupMenu (_menu, ev.GetPosition ());
182 AudioMappingView::off ()
184 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 0);
189 AudioMappingView::full ()
191 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1);
196 AudioMappingView::minus6dB ()
198 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), pow (10, -6.0 / 20));
203 AudioMappingView::edit ()
205 libdcp::Channel d = static_cast<libdcp::Channel> (_menu_column - 1);
207 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
208 if (dialog->ShowModal () == wxID_OK) {
209 _map.set (_menu_row, d, dialog->value ());
217 AudioMappingView::set (AudioMapping map)
224 AudioMappingView::update_cells ()
226 if (_grid->GetNumberRows ()) {
227 _grid->DeleteRows (0, _grid->GetNumberRows ());
230 _grid->InsertRows (0, _map.content_channels ());
232 for (int i = 0; i < _map.content_channels(); ++i) {
233 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
234 _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
238 for (int i = 0; i < _map.content_channels(); ++i) {
239 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
241 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
242 _grid->SetCellValue (i, j, std_to_wx (libdcp::raw_convert<string> (_map.get (i, static_cast<libdcp::Channel> (j - 1)))));
249 /** @param c Number of DCP channels */
251 AudioMappingView::set_channels (int c)
255 if (c < _grid->GetNumberCols ()) {
256 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
257 } else if (c > _grid->GetNumberCols ()) {
258 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
259 set_column_labels ();
266 AudioMappingView::set_column_labels ()
268 int const c = _grid->GetNumberCols ();
270 _grid->SetColLabelValue (0, _("Content"));
272 #if MAX_DCP_AUDIO_CHANNELS != 12
273 #warning AudioMappingView::set_column_labels() is expecting the wrong MAX_DCP_AUDIO_CHANNELS
277 _grid->SetColLabelValue (1, _("L"));
281 _grid->SetColLabelValue (2, _("R"));
285 _grid->SetColLabelValue (3, _("C"));
289 _grid->SetColLabelValue (4, _("Lfe"));
293 _grid->SetColLabelValue (5, _("Ls"));
297 _grid->SetColLabelValue (6, _("Rs"));
301 _grid->SetColLabelValue (7, _("HI"));
305 _grid->SetColLabelValue (8, _("VI"));
309 _grid->SetColLabelValue (9, _("Lc"));
313 _grid->SetColLabelValue (10, _("Rc"));
317 _grid->SetColLabelValue (11, _("BsL"));
321 _grid->SetColLabelValue (12, _("BsR"));
328 AudioMappingView::mouse_moved (wxMouseEvent& ev)
332 _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
334 int const row = _grid->YToRow (yy);
335 int const column = _grid->XToCol (xx);
337 if (row < 0 || column < 1) {
338 _grid->GetGridWindow()->SetToolTip ("");
339 _last_tooltip_row = row;
340 _last_tooltip_column = column;
343 if (row != _last_tooltip_row || column != _last_tooltip_column) {
346 float const gain = _map.get (row, static_cast<libdcp::Channel> (column - 1));
348 s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
349 } else if (gain == 1) {
350 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
352 float const dB = 20 * log10 (gain);
353 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
356 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
357 _last_tooltip_row = row;
358 _last_tooltip_column = column;