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 <dcp/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)
63 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
64 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
65 dc.DrawRectangle (rect);
67 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
68 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
70 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
71 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
72 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
74 float const value = lexical_cast<float> (wx_to_std (grid.GetCellValue (row, col)));
75 float const value_dB = 20 * log10 (value);
78 if (value_dB > -range) {
79 height = INDICATOR_SIZE * (1 + value_dB / range);
82 height = max (0, height);
85 /* Make sure we get a little bit of the marker if there is any gain */
86 height = max (3, height);
89 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
90 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
93 wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
95 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
98 wxGridCellRenderer* Clone () const
100 return new ValueRenderer;
105 AudioMappingView::AudioMappingView (wxWindow* parent)
106 : wxPanel (parent, wxID_ANY)
109 , _last_tooltip_row (0)
110 , _last_tooltip_column (0)
112 _grid = new wxGrid (this, wxID_ANY);
114 _grid->CreateGrid (0, MAX_AUDIO_CHANNELS + 1);
115 _grid->HideRowLabels ();
116 _grid->DisableDragRowSize ();
117 _grid->DisableDragColSize ();
118 _grid->EnableEditing (false);
119 _grid->SetCellHighlightPenWidth (0);
120 _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
122 set_column_labels ();
124 _sizer = new wxBoxSizer (wxVERTICAL);
125 _sizer->Add (_grid, 1, wxEXPAND | wxALL);
126 SetSizerAndFit (_sizer);
128 Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
129 Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
130 _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1));
133 _menu->Append (ID_off, _("Off"));
134 _menu->Append (ID_full, _("Full"));
135 _menu->Append (ID_minus3dB, _("-3dB"));
136 _menu->Append (ID_edit, _("Edit..."));
138 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
139 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
140 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus3dB, this), ID_minus3dB);
141 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
145 AudioMappingView::map_changed ()
149 _last_tooltip_column = -1;
153 AudioMappingView::left_click (wxGridEvent& ev)
155 if (ev.GetCol() == 0) {
159 dcp::Channel d = static_cast<dcp::Channel> (ev.GetCol() - 1);
161 if (_map.get (ev.GetRow(), d) > 0) {
162 _map.set (ev.GetRow(), d, 0);
164 _map.set (ev.GetRow(), d, 1);
171 AudioMappingView::right_click (wxGridEvent& ev)
173 if (ev.GetCol() == 0) {
177 _menu_row = ev.GetRow ();
178 _menu_column = ev.GetCol ();
179 PopupMenu (_menu, ev.GetPosition ());
183 AudioMappingView::off ()
185 _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 0);
190 AudioMappingView::full ()
192 _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 1);
197 AudioMappingView::minus3dB ()
199 _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 1 / sqrt (2));
204 AudioMappingView::edit ()
206 dcp::Channel d = static_cast<dcp::Channel> (_menu_column - 1);
208 AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
209 if (dialog->ShowModal () == wxID_OK) {
210 _map.set (_menu_row, d, dialog->value ());
218 AudioMappingView::set (AudioMapping map)
225 AudioMappingView::update_cells ()
229 if (_grid->GetNumberRows ()) {
230 _grid->DeleteRows (0, _grid->GetNumberRows ());
233 _grid->InsertRows (0, _map.content_channels ());
235 for (int i = 0; i < _map.content_channels(); ++i) {
236 for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) {
237 _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
241 for (int i = 0; i < _map.content_channels(); ++i) {
242 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
244 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
245 _grid->SetCellValue (i, j, std_to_wx (lexical_cast<string> (_map.get (i, static_cast<dcp::Channel> (j - 1)))));
252 /** @param c Number of DCP channels */
254 AudioMappingView::set_channels (int c)
258 if (c < _grid->GetNumberCols ()) {
259 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
260 } else if (c > _grid->GetNumberCols ()) {
261 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
262 set_column_labels ();
269 AudioMappingView::set_column_labels ()
271 int const c = _grid->GetNumberCols ();
273 _grid->SetColLabelValue (0, _("Content channel"));
275 #if MAX_AUDIO_CHANNELS != 8
276 #warning AudioMappingView::set_column_labels() is expecting the wrong MAX_AUDIO_CHANNELS
280 _grid->SetColLabelValue (1, _("L"));
284 _grid->SetColLabelValue (2, _("R"));
288 _grid->SetColLabelValue (3, _("C"));
292 _grid->SetColLabelValue (4, _("Lfe"));
296 _grid->SetColLabelValue (5, _("Ls"));
300 _grid->SetColLabelValue (6, _("Rs"));
304 _grid->SetColLabelValue (7, _("HI"));
308 _grid->SetColLabelValue (8, _("VI"));
315 AudioMappingView::mouse_moved (wxMouseEvent& ev)
319 _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
321 int const row = _grid->YToRow (yy);
322 int const column = _grid->XToCol (xx);
324 if (row < 0 || column < 1) {
325 _grid->GetGridWindow()->SetToolTip ("");
326 _last_tooltip_row = row;
327 _last_tooltip_column = column;
330 if (row != _last_tooltip_row || column != _last_tooltip_column) {
333 float const gain = _map.get (row, static_cast<dcp::Channel> (column - 1));
335 s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
336 } else if (gain == 1) {
337 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
339 float const dB = 20 * log10 (gain);
340 s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
343 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
344 _last_tooltip_row = row;
345 _last_tooltip_column = column;