Merge master.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/wx/audio_mapping_view.cc
21  *  @brief AudioMappingView class and helpers.
22  */
23
24 #include <wx/wx.h>
25 #include <wx/renderer.h>
26 #include <wx/grid.h>
27 #include <dcp/types.h>
28 #include "lib/audio_mapping.h"
29 #include "lib/util.h"
30 #include "audio_mapping_view.h"
31 #include "wx_util.h"
32 #include "audio_gain_dialog.h"
33 #include <boost/lexical_cast.hpp>
34
35 using std::cout;
36 using std::list;
37 using std::string;
38 using std::max;
39 using boost::shared_ptr;
40 using boost::lexical_cast;
41
42 #define INDICATOR_SIZE 16
43
44 enum {
45         ID_off = 1,
46         ID_full = 2,
47         ID_minus6dB = 3,
48         ID_edit = 4
49 };
50
51 class NoSelectionStringRenderer : public wxGridCellStringRenderer
52 {
53 public:
54         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
55         {
56                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
57         }
58 };
59
60 /** @class ValueRenderer
61  *  @brief wxGridCellRenderer for a gain value.
62  */
63 class ValueRenderer : public wxGridCellRenderer
64 {
65 public:
66
67         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
68         {
69                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
70                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
71                 dc.DrawRectangle (rect);
72
73                 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
74                 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
75
76                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
77                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
78                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
79
80                 float const value = lexical_cast<float> (wx_to_std (grid.GetCellValue (row, col)));
81                 float const value_dB = 20 * log10 (value);
82                 int const range = 18;
83                 int height = 0;
84                 if (value_dB > -range) {
85                         height = INDICATOR_SIZE * (1 + value_dB / range);
86                 }
87
88                 height = max (0, height);
89                 
90                 if (value > 0) {
91                         /* Make sure we get a little bit of the marker if there is any gain */
92                         height = max (3, height);
93                 }
94
95                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
96                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
97         }
98
99         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
100         {
101                 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
102         }
103         
104         wxGridCellRenderer* Clone () const
105         {
106                 return new ValueRenderer;
107         }
108 };
109
110
111 AudioMappingView::AudioMappingView (wxWindow* parent)
112         : wxPanel (parent, wxID_ANY)
113         , _menu_row (0)
114         , _menu_column (1)
115         , _last_tooltip_row (0)
116         , _last_tooltip_column (0)
117 {
118         _grid = new wxGrid (this, wxID_ANY);
119
120         _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
121         _grid->HideRowLabels ();
122         _grid->DisableDragRowSize ();
123         _grid->DisableDragColSize ();
124         _grid->EnableEditing (false);
125         _grid->SetCellHighlightPenWidth (0);
126         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
127
128         set_column_labels ();
129
130         _sizer = new wxBoxSizer (wxVERTICAL);
131         _sizer->Add (_grid, 1, wxEXPAND | wxALL);
132         SetSizerAndFit (_sizer);
133
134         Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
135         Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
136         _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1));
137
138         _menu = new wxMenu;
139         _menu->Append (ID_off, _("Off"));
140         _menu->Append (ID_full, _("Full"));
141         _menu->Append (ID_minus6dB, _("-6dB"));
142         _menu->Append (ID_edit, _("Edit..."));
143
144         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
145         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
146         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
147         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
148 }
149
150 void
151 AudioMappingView::map_changed ()
152 {
153         update_cells ();
154         Changed (_map);
155         _last_tooltip_column = -1;
156 }       
157
158 void
159 AudioMappingView::left_click (wxGridEvent& ev)
160 {
161         if (ev.GetCol() == 0) {
162                 return;
163         }
164
165         dcp::Channel d = static_cast<dcp::Channel> (ev.GetCol() - 1);
166         
167         if (_map.get (ev.GetRow(), d) > 0) {
168                 _map.set (ev.GetRow(), d, 0);
169         } else {
170                 _map.set (ev.GetRow(), d, 1);
171         }
172
173         map_changed ();
174 }
175
176 void
177 AudioMappingView::right_click (wxGridEvent& ev)
178 {
179         if (ev.GetCol() == 0) {
180                 return;
181         }
182
183         _menu_row = ev.GetRow ();
184         _menu_column = ev.GetCol ();
185         PopupMenu (_menu, ev.GetPosition ());
186 }
187
188 void
189 AudioMappingView::off ()
190 {
191         _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 0);
192         map_changed ();
193 }
194
195 void
196 AudioMappingView::full ()
197 {
198         _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), 1);
199         map_changed ();
200 }
201
202 void
203 AudioMappingView::minus6dB ()
204 {
205         _map.set (_menu_row, static_cast<dcp::Channel> (_menu_column - 1), pow (10, -6.0 / 20));
206         map_changed ();
207 }
208
209 void
210 AudioMappingView::edit ()
211 {
212         dcp::Channel d = static_cast<dcp::Channel> (_menu_column - 1);
213         
214         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
215         if (dialog->ShowModal () == wxID_OK) {
216                 _map.set (_menu_row, d, dialog->value ());
217                 map_changed ();
218         }
219         
220         dialog->Destroy ();
221 }
222
223 void
224 AudioMappingView::set (AudioMapping map)
225 {
226         _map = map;
227         update_cells ();
228 }
229
230 void
231 AudioMappingView::update_cells ()
232 {
233         if (_grid->GetNumberRows ()) {
234                 _grid->DeleteRows (0, _grid->GetNumberRows ());
235         }
236
237         _grid->InsertRows (0, _map.content_channels ());
238
239         for (int i = 0; i < _map.content_channels(); ++i) {
240                 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
241                         _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
242                 }
243         }
244         
245         for (int i = 0; i < _map.content_channels(); ++i) {
246                 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
247
248                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
249                         _grid->SetCellValue (i, j, std_to_wx (lexical_cast<string> (_map.get (i, static_cast<dcp::Channel> (j - 1)))));
250                 }
251         }
252
253         _grid->AutoSize ();
254 }
255
256 /** @param c Number of DCP channels */
257 void
258 AudioMappingView::set_channels (int c)
259 {
260         c++;
261
262         if (c < _grid->GetNumberCols ()) {
263                 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
264         } else if (c > _grid->GetNumberCols ()) {
265                 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
266                 set_column_labels ();
267         }
268
269         update_cells ();
270 }
271
272 void
273 AudioMappingView::set_column_labels ()
274 {
275         int const c = _grid->GetNumberCols ();
276         
277         _grid->SetColLabelValue (0, _("Content"));
278
279 #if MAX_DCP_AUDIO_CHANNELS != 12
280 #warning AudioMappingView::set_column_labels() is expecting the wrong MAX_DCP_AUDIO_CHANNELS
281 #endif  
282         
283         if (c > 0) {
284                 _grid->SetColLabelValue (1, _("L"));
285         }
286         
287         if (c > 1) {
288                 _grid->SetColLabelValue (2, _("R"));
289         }
290         
291         if (c > 2) {
292                 _grid->SetColLabelValue (3, _("C"));
293         }
294         
295         if (c > 3) {
296                 _grid->SetColLabelValue (4, _("Lfe"));
297         }
298         
299         if (c > 4) {
300                 _grid->SetColLabelValue (5, _("Ls"));
301         }
302         
303         if (c > 5) {
304                 _grid->SetColLabelValue (6, _("Rs"));
305         }
306
307         if (c > 6) {
308                 _grid->SetColLabelValue (7, _("HI"));
309         }
310
311         if (c > 7) {
312                 _grid->SetColLabelValue (8, _("VI"));
313         }
314
315         if (c > 8) {
316                 _grid->SetColLabelValue (9, _("Lc"));
317         }
318
319         if (c > 9) {
320                 _grid->SetColLabelValue (10, _("Rc"));
321         }
322
323         if (c > 10) {
324                 _grid->SetColLabelValue (11, _("BsL"));
325         }
326
327         if (c > 11) {
328                 _grid->SetColLabelValue (12, _("BsR"));
329         }
330
331         _grid->AutoSize ();
332 }
333
334 void
335 AudioMappingView::mouse_moved (wxMouseEvent& ev)
336 {
337         int xx;
338         int yy;
339         _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
340
341         int const row = _grid->YToRow (yy);
342         int const column = _grid->XToCol (xx);
343
344         if (row < 0 || column < 1) {
345                 _grid->GetGridWindow()->SetToolTip ("");
346                 _last_tooltip_row = row;
347                 _last_tooltip_column = column;
348         }
349
350         if (row != _last_tooltip_row || column != _last_tooltip_column) {
351
352                 wxString s;
353                 float const gain = _map.get (row, static_cast<dcp::Channel> (column - 1));
354                 if (gain == 0) {
355                         s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
356                 } else if (gain == 1) {
357                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
358                 } else {
359                         float const dB = 20 * log10 (gain);
360                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
361                 }
362                 
363                 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
364                 _last_tooltip_row = row;
365                 _last_tooltip_column = column;
366         }
367
368         ev.Skip ();
369 }