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)
108 _grid = new wxGrid (this, wxID_ANY);
110 _grid->CreateGrid (0, MAX_AUDIO_CHANNELS + 1);
111 _grid->HideRowLabels ();
112 _grid->DisableDragRowSize ();
113 _grid->DisableDragColSize ();
114 _grid->EnableEditing (false);
115 _grid->SetCellHighlightPenWidth (0);
116 _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
118 set_column_labels ();
120 _sizer = new wxBoxSizer (wxVERTICAL);
121 _sizer->Add (_grid, 1, wxEXPAND | wxALL);
122 SetSizerAndFit (_sizer);
124 Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
125 Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
128 _menu->Append (ID_off, _("Off"));
129 _menu->Append (ID_full, _("Full"));
130 _menu->Append (ID_minus3dB, _("-3dB"));
131 _menu->Append (ID_edit, _("Edit..."));
133 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
134 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
135 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus3dB, this), ID_minus3dB);
136 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
140 AudioMappingView::left_click (wxGridEvent& ev)
142 if (ev.GetCol() == 0) {
146 libdcp::Channel d = static_cast<libdcp::Channel> (ev.GetCol() - 1);
148 if (_map.get (ev.GetRow(), d) > 0) {
149 _map.set (ev.GetRow(), d, 0);
151 _map.set (ev.GetRow(), d, 1);
159 AudioMappingView::right_click (wxGridEvent& ev)
161 if (ev.GetCol() == 0) {
165 _menu_row = ev.GetRow ();
166 _menu_column = ev.GetCol ();
167 PopupMenu (_menu, ev.GetPosition ());
171 AudioMappingView::off ()
173 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 0);
179 AudioMappingView::full ()
181 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1);
187 AudioMappingView::minus3dB ()
189 _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1 / sqrt (2));
195 AudioMappingView::edit ()
197 libdcp::Channel d = static_cast<libdcp::Channel> (_menu_column - 1);
199 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
200 if (dialog->ShowModal () == wxID_OK) {
201 _map.set (_menu_row, d, dialog->value ());
210 AudioMappingView::set (AudioMapping map)
217 AudioMappingView::update_cells ()
219 if (_grid->GetNumberRows ()) {
220 _grid->DeleteRows (0, _grid->GetNumberRows ());
223 _grid->InsertRows (0, _map.content_channels ());
225 for (int i = 0; i < _map.content_channels(); ++i) {
226 for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) {
227 _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
231 for (int i = 0; i < _map.content_channels(); ++i) {
232 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
234 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
235 _grid->SetCellValue (i, j, std_to_wx (lexical_cast<string> (_map.get (i, static_cast<libdcp::Channel> (j - 1)))));
242 /** @param c Number of DCP channels */
244 AudioMappingView::set_channels (int c)
248 if (c < _grid->GetNumberCols ()) {
249 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
250 } else if (c > _grid->GetNumberCols ()) {
251 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
252 set_column_labels ();
259 AudioMappingView::set_column_labels ()
261 int const c = _grid->GetNumberCols ();
263 _grid->SetColLabelValue (0, _("Content channel"));
266 _grid->SetColLabelValue (1, _("L"));
270 _grid->SetColLabelValue (2, _("R"));
274 _grid->SetColLabelValue (3, _("C"));
278 _grid->SetColLabelValue (4, _("Lfe"));
282 _grid->SetColLabelValue (5, _("Ls"));
286 _grid->SetColLabelValue (6, _("Rs"));