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