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 "lib/audio_mapping.h"
26 #include "audio_mapping_view.h"
28 #include "audio_gain_dialog.h"
34 using boost::shared_ptr;
35 using boost::lexical_cast;
37 #define INDICATOR_SIZE 20
46 class NoSelectionStringRenderer : public wxGridCellStringRenderer
49 void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
51 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
55 class ValueRenderer : public wxGridCellRenderer
59 void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
61 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
62 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
63 dc.DrawRectangle (rect);
65 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
66 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
68 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
69 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
70 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
72 float const value = lexical_cast<float> (wx_to_std (grid.GetCellValue (row, col)));
73 float const value_dB = 20 * log10 (value);
76 if (value_dB > -range) {
77 height = INDICATOR_SIZE * (1 + value_dB / range);
80 height = max (0, height);
83 /* Make sure we get a little bit of the marker if there is any gain */
84 height = max (3, height);
87 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
88 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
91 wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
93 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
96 wxGridCellRenderer* Clone () const
98 return new ValueRenderer;
103 AudioMappingView::AudioMappingView (wxWindow* parent)
104 : wxPanel (parent, wxID_ANY)
107 , _last_tooltip_row (0)
108 , _last_tooltip_column (0)
110 _grid = new wxGrid (this, wxID_ANY);
112 _grid->CreateGrid (0, MAX_AUDIO_CHANNELS + 1);
113 _grid->HideRowLabels ();
114 _grid->DisableDragRowSize ();
115 _grid->DisableDragColSize ();
116 _grid->EnableEditing (false);
117 _grid->SetCellHighlightPenWidth (0);
118 _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
120 set_column_labels ();
122 _sizer = new wxBoxSizer (wxVERTICAL);
123 _sizer->Add (_grid, 1, wxEXPAND | wxALL);
124 SetSizerAndFit (_sizer);
126 Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
127 Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
128 _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1));
131 _menu->Append (ID_off, _("Off"));
132 _menu->Append (ID_full, _("Full"));
133 _menu->Append (ID_minus3dB, _("-3dB"));
134 _menu->Append (ID_edit, _("Edit..."));
136 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
137 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
138 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus3dB, this), ID_minus3dB);
139 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
143 AudioMappingView::map_changed ()
147 _last_tooltip_column = -1;
151 AudioMappingView::left_click (wxGridEvent& ev)
153 if (ev.GetCol() == 0) {
157 libdcp::Channel d = static_cast<libdcp::Channel> (ev.GetCol() - 1);
159 if (_map.get (ev.GetRow(), d) > 0) {
160 _map.set (ev.GetRow(), d, 0);
162 _map.set (ev.GetRow(), d, 1);
169 AudioMappingView::right_click (wxGridEvent& ev)
171 if (ev.GetCol() == 0) {
175 _menu_row = ev.GetRow ();
176 _menu_column = ev.GetCol ();
177 PopupMenu (_menu, ev.GetPosition ());
181 AudioMappingView::off ()
183 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 0);
188 AudioMappingView::full ()
190 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1);
195 AudioMappingView::minus3dB ()
197 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1 / sqrt (2));
202 AudioMappingView::edit ()
204 libdcp::Channel d = static_cast<libdcp::Channel> (_menu_column - 1);
206 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
207 if (dialog->ShowModal () == wxID_OK) {
208 _map.set (_menu_row, d, dialog->value ());
216 AudioMappingView::set (AudioMapping map)
223 AudioMappingView::update_cells ()
225 if (_grid->GetNumberRows ()) {
226 _grid->DeleteRows (0, _grid->GetNumberRows ());
229 _grid->InsertRows (0, _map.content_channels ());
231 for (int i = 0; i < _map.content_channels(); ++i) {
232 for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) {
233 _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
237 for (int i = 0; i < _map.content_channels(); ++i) {
238 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
240 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
241 _grid->SetCellValue (i, j, std_to_wx (lexical_cast<string> (_map.get (i, static_cast<libdcp::Channel> (j - 1)))));
248 /** @param c Number of DCP channels */
250 AudioMappingView::set_channels (int c)
254 if (c < _grid->GetNumberCols ()) {
255 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
256 } else if (c > _grid->GetNumberCols ()) {
257 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
258 set_column_labels ();
265 AudioMappingView::set_column_labels ()
267 int const c = _grid->GetNumberCols ();
269 _grid->SetColLabelValue (0, _("Content channel"));
272 _grid->SetColLabelValue (1, _("L"));
276 _grid->SetColLabelValue (2, _("R"));
280 _grid->SetColLabelValue (3, _("C"));
284 _grid->SetColLabelValue (4, _("Lfe"));
288 _grid->SetColLabelValue (5, _("Ls"));
292 _grid->SetColLabelValue (6, _("Rs"));
299 AudioMappingView::mouse_moved (wxMouseEvent& ev)
303 _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
305 int const row = _grid->YToRow (yy);
306 int const column = _grid->XToCol (xx);
308 if (row < 0 || column < 1) {
309 _grid->GetGridWindow()->SetToolTip ("");
310 _last_tooltip_row = row;
311 _last_tooltip_column = column;
314 if (row != _last_tooltip_row || column != _last_tooltip_column) {
317 float const gain = _map.get (row, static_cast<libdcp::Channel> (column - 1));
319 s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
320 } else if (gain == 1) {
321 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
323 float const dB = 20 * log10 (gain);
324 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
327 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
328 _last_tooltip_row = row;
329 _last_tooltip_column = column;