Fix audio matrix view labels when DCP audio channel counts change.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/wx/audio_mapping_view.cc
22  *  @brief AudioMappingView class and helpers.
23  */
24
25 #include "lib/audio_mapping.h"
26 #include "lib/util.h"
27 #include "lib/raw_convert.h"
28 #include "audio_mapping_view.h"
29 #include "wx_util.h"
30 #include "audio_gain_dialog.h"
31 #include <dcp/types.h>
32 #include <wx/wx.h>
33 #include <wx/renderer.h>
34 #include <wx/grid.h>
35 #include <wx/graphics.h>
36 #include <boost/foreach.hpp>
37 #include <iostream>
38
39 using std::cout;
40 using std::list;
41 using std::string;
42 using std::min;
43 using std::max;
44 using std::vector;
45 using std::pair;
46 using std::make_pair;
47 using boost::shared_ptr;
48
49 #define INDICATOR_SIZE 16
50 #define LEFT_WIDTH 48
51
52 enum {
53         ID_off = 1,
54         ID_full = 2,
55         ID_minus6dB = 3,
56         ID_edit = 4
57 };
58
59 class NoSelectionStringRenderer : public wxGridCellStringRenderer
60 {
61 public:
62         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
63         {
64                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
65         }
66 };
67
68 /** @class ValueRenderer
69  *  @brief wxGridCellRenderer for a gain value.
70  */
71 class ValueRenderer : public wxGridCellRenderer
72 {
73 public:
74
75         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
76         {
77                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
78                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
79                 dc.DrawRectangle (rect);
80
81                 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
82                 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
83
84                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
85                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
86                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
87
88                 float const value = raw_convert<float> (wx_to_std (grid.GetCellValue (row, col)));
89                 float const value_dB = 20 * log10 (value);
90                 int const range = 18;
91                 int height = 0;
92                 if (value_dB > -range) {
93                         height = INDICATOR_SIZE * (1 + value_dB / range);
94                 }
95
96                 height = max (0, height);
97
98                 if (value > 0) {
99                         /* Make sure we get a little bit of the marker if there is any gain */
100                         height = max (3, height);
101                 }
102
103                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
104                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
105         }
106
107         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
108         {
109                 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
110         }
111
112         wxGridCellRenderer* Clone () const
113         {
114                 return new ValueRenderer;
115         }
116 };
117
118
119 AudioMappingView::AudioMappingView (wxWindow* parent)
120         : wxPanel (parent, wxID_ANY)
121         , _menu_row (0)
122         , _menu_column (1)
123         , _last_tooltip_row (0)
124         , _last_tooltip_column (0)
125 {
126         _left_labels = new wxPanel (this, wxID_ANY);
127         _left_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_left_labels, this));
128         _top_labels = new wxPanel (this, wxID_ANY);
129         _top_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_top_labels, this));
130
131         _grid = new wxGrid (this, wxID_ANY);
132
133         _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
134         _grid->HideRowLabels ();
135         _grid->DisableDragRowSize ();
136         _grid->DisableDragColSize ();
137         _grid->EnableEditing (false);
138         _grid->SetCellHighlightPenWidth (0);
139         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
140         _grid->AutoSize ();
141
142         wxSizer* vertical_sizer = new wxBoxSizer (wxVERTICAL);
143         vertical_sizer->Add (_top_labels);
144         wxSizer* horizontal_sizer = new wxBoxSizer (wxHORIZONTAL);
145         horizontal_sizer->Add (_left_labels);
146         horizontal_sizer->Add (_grid, 1, wxEXPAND | wxALL);
147         vertical_sizer->Add (horizontal_sizer);
148         SetSizerAndFit (vertical_sizer);
149
150         Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
151         Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
152         _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved_grid, this, _1));
153         Bind (wxEVT_SIZE, boost::bind (&AudioMappingView::sized, this, _1));
154
155         _menu = new wxMenu;
156         _menu->Append (ID_off, _("Off"));
157         _menu->Append (ID_full, _("Full"));
158         _menu->Append (ID_minus6dB, _("-6dB"));
159         _menu->Append (ID_edit, _("Edit..."));
160
161         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
162         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
163         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
164         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
165
166         _left_labels->Bind (wxEVT_MOTION, bind (&AudioMappingView::mouse_moved_left_labels, this, _1));
167 }
168
169 /** Called when any gain value has changed */
170 void
171 AudioMappingView::map_values_changed ()
172 {
173         update_cells ();
174         Changed (_map);
175         _last_tooltip_column = -1;
176 }
177
178 void
179 AudioMappingView::left_click (wxGridEvent& ev)
180 {
181         if (ev.GetCol() == 0) {
182                 return;
183         }
184
185         int const d = ev.GetCol() - 1;
186
187         if (_map.get (ev.GetRow(), d) > 0) {
188                 _map.set (ev.GetRow(), d, 0);
189         } else {
190                 _map.set (ev.GetRow(), d, 1);
191         }
192
193         map_values_changed ();
194 }
195
196 void
197 AudioMappingView::right_click (wxGridEvent& ev)
198 {
199         if (ev.GetCol() == 0) {
200                 return;
201         }
202
203         _menu_row = ev.GetRow ();
204         _menu_column = ev.GetCol ();
205         PopupMenu (_menu, ev.GetPosition ());
206 }
207
208 void
209 AudioMappingView::off ()
210 {
211         _map.set (_menu_row, _menu_column - 1, 0);
212         map_values_changed ();
213 }
214
215 void
216 AudioMappingView::full ()
217 {
218         _map.set (_menu_row, _menu_column - 1, 1);
219         map_values_changed ();
220 }
221
222 void
223 AudioMappingView::minus6dB ()
224 {
225         _map.set (_menu_row, _menu_column - 1, pow (10, -6.0 / 20));
226         map_values_changed ();
227 }
228
229 void
230 AudioMappingView::edit ()
231 {
232         int const d = _menu_column - 1;
233
234         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
235         if (dialog->ShowModal () == wxID_OK) {
236                 _map.set (_menu_row, d, dialog->value ());
237                 map_values_changed ();
238         }
239
240         dialog->Destroy ();
241 }
242
243 void
244 AudioMappingView::set (AudioMapping map)
245 {
246         _map = map;
247         update_cells ();
248 }
249
250 void
251 AudioMappingView::set_input_channels (vector<string> const & names)
252 {
253         for (int i = 0; i < _grid->GetNumberRows(); ++i) {
254                 _grid->SetCellValue (i, 0, std_to_wx (names[i]));
255         }
256 }
257
258 void
259 AudioMappingView::set_output_channels (vector<string> const & names)
260 {
261         int const o = names.size() + 1;
262         if (o < _grid->GetNumberCols ()) {
263                 _grid->DeleteCols (o, _grid->GetNumberCols() - o);
264         } else if (o > _grid->GetNumberCols ()) {
265                 _grid->InsertCols (_grid->GetNumberCols(), o - _grid->GetNumberCols());
266         }
267
268         _grid->SetColLabelValue (0, wxT (""));
269
270         for (size_t i = 0; i < names.size(); ++i) {
271                 _grid->SetColLabelValue (i + 1, std_to_wx (names[i]));
272         }
273
274         update_cells ();
275         setup_sizes ();
276 }
277
278 void
279 AudioMappingView::update_cells ()
280 {
281         vector<string> row_names;
282         for (int i = 0; i < _grid->GetNumberRows (); ++i) {
283                 row_names.push_back (wx_to_std (_grid->GetCellValue (i, 0)));
284         }
285
286         if (_grid->GetNumberRows ()) {
287                 _grid->DeleteRows (0, _grid->GetNumberRows ());
288         }
289
290         _grid->InsertRows (0, _map.input_channels ());
291
292         for (int i = 0; i < _map.input_channels(); ++i) {
293                 for (int j = 0; j < _map.output_channels(); ++j) {
294                         _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
295                 }
296         }
297
298         for (int i = 0; i < _map.input_channels(); ++i) {
299                 if (i < int (row_names.size ())) {
300                         _grid->SetCellValue (i, 0, std_to_wx (row_names[i]));
301                 }
302                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
303                         _grid->SetCellValue (i, j, std_to_wx (raw_convert<string> (_map.get (i, j - 1))));
304                 }
305         }
306
307         _grid->AutoSize ();
308 }
309
310 void
311 AudioMappingView::mouse_moved_grid (wxMouseEvent& ev)
312 {
313         int xx;
314         int yy;
315         _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
316
317         int const row = _grid->YToRow (yy);
318         int const column = _grid->XToCol (xx);
319
320         if (row < 0 || column < 1) {
321                 _grid->GetGridWindow()->SetToolTip ("");
322                 _last_tooltip_row = row;
323                 _last_tooltip_column = column;
324         }
325
326         if (row != _last_tooltip_row || column != _last_tooltip_column) {
327
328                 wxString s;
329                 float const gain = _map.get (row, column - 1);
330                 if (gain == 0) {
331                         s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
332                 } else if (gain == 1) {
333                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
334                 } else {
335                         float const dB = 20 * log10 (gain);
336                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
337                 }
338
339                 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
340                 _last_tooltip_row = row;
341                 _last_tooltip_column = column;
342         }
343
344         ev.Skip ();
345 }
346
347 void
348 AudioMappingView::sized (wxSizeEvent& ev)
349 {
350         setup_sizes ();
351         ev.Skip ();
352 }
353
354 void
355 AudioMappingView::setup_sizes ()
356 {
357         int const top_height = 24;
358
359         _grid->AutoSize ();
360         _left_labels->SetMinSize (wxSize (LEFT_WIDTH, _grid->GetSize().GetHeight()));
361         _top_labels->SetMinSize (wxSize (_grid->GetSize().GetWidth() + LEFT_WIDTH, top_height));
362         /* Try to make the _top_labels 'actua' size respect the minimum we just set */
363         _top_labels->Fit ();
364         _left_labels->Refresh ();
365         _top_labels->Refresh ();
366 }
367
368 void
369 AudioMappingView::paint_left_labels ()
370 {
371         wxPaintDC dc (_left_labels);
372
373         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
374         if (!gc) {
375                 return;
376         }
377
378         wxSize const size = dc.GetSize();
379         int const half = size.GetWidth() / 2;
380
381         gc->SetPen (wxPen (wxColour (0, 0, 0)));
382         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
383
384         wxGraphicsPath lines = gc->CreatePath();
385
386         vector<pair<int, int> >::const_iterator i = _input_group_positions.begin();
387         if (i != _input_group_positions.end()) {
388                 lines.MoveToPoint (half, i->first);
389                 lines.AddLineToPoint (size.GetWidth(), i->first);
390         }
391
392         vector<Group>::const_iterator j = _input_groups.begin();
393         while (i != _input_group_positions.end() && j != _input_groups.end()) {
394
395                 dc.SetClippingRegion (0, i->first + 2, size.GetWidth(), i->second - 4);
396
397                 dc.SetFont (*wxSWISS_FONT);
398                 wxCoord label_width;
399                 wxCoord label_height;
400                 dc.GetTextExtent (std_to_wx (j->name), &label_width, &label_height);
401
402                 dc.DrawRotatedText (
403                         j->name,
404                         half + (half - label_height) / 2,
405                         min (i->second, (i->second + i->first + label_width) / 2),
406                         90
407                         );
408
409                 dc.DestroyClippingRegion ();
410
411                 lines.MoveToPoint (half, i->second);
412                 lines.AddLineToPoint (size.GetWidth(), i->second);
413
414                 gc->StrokePath (lines);
415
416                 ++i;
417                 ++j;
418         }
419
420         /* Overall label */
421         dc.SetFont (wxSWISS_FONT->Bold());
422         wxCoord overall_label_width;
423         wxCoord overall_label_height;
424         dc.GetTextExtent (_("Content"), &overall_label_width, &overall_label_height);
425         dc.DrawRotatedText (
426                 _("Content"),
427                 (half - overall_label_height) / 2,
428                 min (size.GetHeight(), (size.GetHeight() + _grid->GetColLabelSize() + overall_label_width) / 2),
429                 90
430                 );
431
432         delete gc;
433 }
434
435 void
436 AudioMappingView::paint_top_labels ()
437 {
438         wxPaintDC dc (_top_labels);
439         if (_grid->GetNumberCols() == 0) {
440                 return;
441         }
442
443         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
444         if (!gc) {
445                 return;
446         }
447
448         wxSize const size = dc.GetSize();
449
450         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
451
452         dc.SetFont (wxSWISS_FONT->Bold());
453         wxCoord label_width;
454         wxCoord label_height;
455         dc.GetTextExtent (_("DCP"), &label_width, &label_height);
456
457         dc.DrawText (_("DCP"), (size.GetWidth() + _grid->GetColSize(0) + LEFT_WIDTH - label_width) / 2, (size.GetHeight() - label_height) / 2);
458
459         gc->SetPen (wxPen (wxColour (0, 0, 0)));
460         wxGraphicsPath lines = gc->CreatePath();
461         lines.MoveToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, 0);
462         lines.AddLineToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, size.GetHeight());
463         lines.MoveToPoint (size.GetWidth() - 1, 0);
464         lines.AddLineToPoint (size.GetWidth() - 1, size.GetHeight());
465         gc->StrokePath (lines);
466
467         delete gc;
468 }
469
470 void
471 AudioMappingView::set_input_groups (vector<Group> const & groups)
472 {
473         _input_groups = groups;
474         _input_group_positions.clear ();
475
476         int ypos = _grid->GetColLabelSize() - 1;
477         BOOST_FOREACH (Group const & i, _input_groups) {
478                 int const old_ypos = ypos;
479                 ypos += (i.to - i.from + 1) * _grid->GetRowSize(0);
480                 _input_group_positions.push_back (make_pair (old_ypos, ypos));
481         }
482 }
483
484 void
485 AudioMappingView::mouse_moved_left_labels (wxMouseEvent& event)
486 {
487         bool done = false;
488         for (size_t i = 0; i < _input_group_positions.size(); ++i) {
489                 if (_input_group_positions[i].first <= event.GetY() && event.GetY() < _input_group_positions[i].second) {
490                         _left_labels->SetToolTip (_input_groups[i].name);
491                         done = true;
492                 }
493         }
494
495         if (!done) {
496                 _left_labels->SetToolTip ("");
497         }
498 }