Tidying up of scrolling in the matrix view.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
1 /*
2     Copyright (C) 2013-2018 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 "audio_mapping_view.h"
26 #include "wx_util.h"
27 #include "audio_gain_dialog.h"
28 #include "lib/audio_mapping.h"
29 #include "lib/util.h"
30 #include <dcp/locale_convert.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 using dcp::locale_convert;
49
50 #define INDICATOR_SIZE 16
51 #define LEFT_WIDTH 48
52
53 enum {
54         ID_off = 1,
55         ID_full = 2,
56         ID_minus6dB = 3,
57         ID_edit = 4
58 };
59
60 class NoSelectionStringRenderer : public wxGridCellStringRenderer
61 {
62 public:
63         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
64         {
65                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
66         }
67 };
68
69 /** @class ValueRenderer
70  *  @brief wxGridCellRenderer for a gain value.
71  */
72 class ValueRenderer : public wxGridCellRenderer
73 {
74 public:
75
76         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
77         {
78                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
79                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
80                 dc.DrawRectangle (rect);
81
82                 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
83                 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
84
85                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
86                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
87                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
88
89                 float const value = locale_convert<float> (wx_to_std (grid.GetCellValue (row, col)));
90                 float const value_dB = 20 * log10 (value);
91                 int const range = 18;
92                 int height = 0;
93                 if (value_dB > -range) {
94                         height = INDICATOR_SIZE * (1 + value_dB / range);
95                 }
96
97                 height = max (0, height);
98
99                 if (value > 0) {
100                         /* Make sure we get a little bit of the marker if there is any gain */
101                         height = max (3, height);
102                 }
103
104                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
105                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
106         }
107
108         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
109         {
110                 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
111         }
112
113         wxGridCellRenderer* Clone () const
114         {
115                 return new ValueRenderer;
116         }
117 };
118
119
120 AudioMappingView::AudioMappingView (wxWindow* parent)
121         : wxPanel (parent, wxID_ANY)
122         , _menu_row (0)
123         , _menu_column (1)
124         , _last_tooltip_row (0)
125         , _last_tooltip_column (0)
126 {
127         _left_labels = new wxScrolledCanvas (this, wxID_ANY);
128         _left_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_left_labels, this));
129         _top_labels = new wxScrolledCanvas (this, wxID_ANY);
130         _top_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_top_labels, this));
131
132         _left_labels->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
133         _top_labels->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
134
135         _grid = new wxGrid (this, wxID_ANY);
136
137         _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
138         _grid->HideRowLabels ();
139         _grid->DisableDragRowSize ();
140         _grid->DisableDragColSize ();
141         _grid->EnableEditing (false);
142         _grid->SetCellHighlightPenWidth (0);
143         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
144         _grid->EnableScrolling (true, true);
145         _grid->AutoSize ();
146
147         wxSizer* vertical_sizer = new wxBoxSizer (wxVERTICAL);
148         vertical_sizer->Add (_top_labels);
149         wxSizer* horizontal_sizer = new wxBoxSizer (wxHORIZONTAL);
150         horizontal_sizer->Add (_left_labels);
151         horizontal_sizer->Add (_grid, 1, wxEXPAND | wxALL);
152         vertical_sizer->Add (horizontal_sizer);
153         SetSizerAndFit (vertical_sizer);
154
155         Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
156         Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
157         _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved_grid, this, _1));
158         _grid->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
159         _grid->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
160         _grid->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
161         _grid->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
162         _grid->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
163         _grid->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
164         _grid->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
165         _grid->Bind (wxEVT_SCROLLWIN_THUMBRELEASE, boost::bind (&AudioMappingView::grid_scrolled, this, _1));
166         Bind (wxEVT_SIZE, boost::bind (&AudioMappingView::sized, this, _1));
167
168         _menu = new wxMenu;
169         _menu->Append (ID_off, _("Off"));
170         _menu->Append (ID_full, _("Full"));
171         _menu->Append (ID_minus6dB, _("-6dB"));
172         _menu->Append (ID_edit, _("Edit..."));
173
174         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::off, this), ID_off);
175         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::full, this), ID_full);
176         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
177         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::edit, this), ID_edit);
178
179         _left_labels->Bind (wxEVT_MOTION, bind (&AudioMappingView::mouse_moved_left_labels, this, _1));
180 }
181
182 /** Called when any gain value has changed */
183 void
184 AudioMappingView::map_values_changed ()
185 {
186         update_cells ();
187         Changed (_map);
188         _last_tooltip_column = -1;
189 }
190
191 void
192 AudioMappingView::left_click (wxGridEvent& ev)
193 {
194         if (ev.GetCol() == 0) {
195                 return;
196         }
197
198         int const d = ev.GetCol() - 1;
199
200         if (_map.get (ev.GetRow(), d) > 0) {
201                 _map.set (ev.GetRow(), d, 0);
202         } else {
203                 _map.set (ev.GetRow(), d, 1);
204         }
205
206         map_values_changed ();
207 }
208
209 void
210 AudioMappingView::right_click (wxGridEvent& ev)
211 {
212         if (ev.GetCol() == 0) {
213                 return;
214         }
215
216         _menu_row = ev.GetRow ();
217         _menu_column = ev.GetCol ();
218         PopupMenu (_menu, ev.GetPosition ());
219 }
220
221 void
222 AudioMappingView::off ()
223 {
224         _map.set (_menu_row, _menu_column - 1, 0);
225         map_values_changed ();
226 }
227
228 void
229 AudioMappingView::full ()
230 {
231         _map.set (_menu_row, _menu_column - 1, 1);
232         map_values_changed ();
233 }
234
235 void
236 AudioMappingView::minus6dB ()
237 {
238         _map.set (_menu_row, _menu_column - 1, pow (10, -6.0 / 20));
239         map_values_changed ();
240 }
241
242 void
243 AudioMappingView::edit ()
244 {
245         int const d = _menu_column - 1;
246
247         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
248         if (dialog->ShowModal () == wxID_OK) {
249                 _map.set (_menu_row, d, dialog->value ());
250                 map_values_changed ();
251         }
252
253         dialog->Destroy ();
254 }
255
256 void
257 AudioMappingView::set (AudioMapping map)
258 {
259         _map = map;
260         update_cells ();
261 }
262
263 void
264 AudioMappingView::set_input_channels (vector<string> const & names)
265 {
266         for (int i = 0; i < _grid->GetNumberRows(); ++i) {
267                 _grid->SetCellValue (i, 0, std_to_wx (names[i]));
268         }
269 }
270
271 void
272 AudioMappingView::set_output_channels (vector<string> const & names)
273 {
274         int const o = names.size() + 1;
275         if (o < _grid->GetNumberCols ()) {
276                 _grid->DeleteCols (o, _grid->GetNumberCols() - o);
277         } else if (o > _grid->GetNumberCols ()) {
278                 _grid->AppendCols (o - _grid->GetNumberCols());
279         }
280
281         _grid->SetColLabelValue (0, wxT (""));
282
283         for (size_t i = 0; i < names.size(); ++i) {
284                 _grid->SetColLabelValue (i + 1, std_to_wx (names[i]));
285         }
286
287         update_cells ();
288         setup_sizes ();
289
290         Layout ();
291 }
292
293 void
294 AudioMappingView::update_cells ()
295 {
296         vector<string> row_names;
297         for (int i = 0; i < _grid->GetNumberRows (); ++i) {
298                 row_names.push_back (wx_to_std (_grid->GetCellValue (i, 0)));
299         }
300
301         if (_grid->GetNumberRows ()) {
302                 _grid->DeleteRows (0, _grid->GetNumberRows ());
303         }
304
305         _grid->InsertRows (0, _map.input_channels ());
306
307         for (int i = 0; i < _map.input_channels(); ++i) {
308                 for (int j = 0; j < _map.output_channels(); ++j) {
309                         _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
310                 }
311         }
312
313         for (int i = 0; i < _map.input_channels(); ++i) {
314                 if (i < int (row_names.size ())) {
315                         _grid->SetCellValue (i, 0, std_to_wx (row_names[i]));
316                 }
317                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
318                         _grid->SetCellValue (i, j, std_to_wx (locale_convert<string> (_map.get (i, j - 1))));
319                 }
320         }
321
322         _grid->AutoSize ();
323 }
324
325 void
326 AudioMappingView::mouse_moved_grid (wxMouseEvent& ev)
327 {
328         int xx;
329         int yy;
330         _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
331
332         int const row = _grid->YToRow (yy);
333         int const column = _grid->XToCol (xx);
334
335         if (row < 0 || column < 1) {
336                 _grid->GetGridWindow()->SetToolTip ("");
337                 _last_tooltip_row = row;
338                 _last_tooltip_column = column;
339         }
340
341         if (row != _last_tooltip_row || column != _last_tooltip_column) {
342
343                 wxString s;
344                 float const gain = _map.get (row, column - 1);
345                 if (gain == 0) {
346                         s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
347                 } else if (gain == 1) {
348                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
349                 } else {
350                         float const dB = 20 * log10 (gain);
351                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
352                 }
353
354                 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
355                 _last_tooltip_row = row;
356                 _last_tooltip_column = column;
357         }
358
359         ev.Skip ();
360 }
361
362 void
363 AudioMappingView::sized (wxSizeEvent& ev)
364 {
365         setup_sizes ();
366         ev.Skip ();
367 }
368
369 void
370 AudioMappingView::setup_sizes ()
371 {
372         int const top_height = 24;
373
374         _grid->AutoSize ();
375         _left_labels->SetMinSize (wxSize (LEFT_WIDTH, _grid->GetSize().GetHeight()));
376         /* Allow it to scroll */
377         _left_labels->SetVirtualSize (1024, 1024);
378         _top_labels->SetMinSize (wxSize (_grid->GetSize().GetWidth() + LEFT_WIDTH, top_height));
379         /* Allow it to scroll */
380         _top_labels->SetVirtualSize (1024, 1024);
381         /* Try to make the _top_labels 'actual' size respect the minimum we just set */
382         _top_labels->Fit ();
383         _left_labels->Refresh ();
384         _top_labels->Refresh ();
385
386         int x, y;
387         _grid->GetScrollPixelsPerUnit (&x, &y);
388         _top_labels->SetScrollRate (x, 0);
389         _left_labels->SetScrollRate (0, y);
390 }
391
392 void
393 AudioMappingView::paint_left_labels ()
394 {
395         wxPaintDC dc (_left_labels);
396
397         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
398         if (!gc) {
399                 return;
400         }
401
402         int sx, sy;
403         _left_labels->GetViewStart (&sx, &sy);
404         int rx, ry;
405         _grid->GetScrollPixelsPerUnit (&rx, &ry);
406         gc->Translate (0, -sy * ry);
407
408         wxSize const size = dc.GetSize();
409         int const half = size.GetWidth() / 2;
410
411         gc->SetPen (wxPen (wxColour (0, 0, 0)));
412         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
413
414         wxGraphicsPath lines = gc->CreatePath();
415
416         vector<pair<int, int> >::const_iterator i = _input_group_positions.begin();
417         if (i != _input_group_positions.end()) {
418                 lines.MoveToPoint (half, i->first);
419                 lines.AddLineToPoint (size.GetWidth(), i->first);
420         }
421
422         vector<Group>::const_iterator j = _input_groups.begin();
423         while (i != _input_group_positions.end() && j != _input_groups.end()) {
424
425                 dc.SetClippingRegion (0, i->first + 2, size.GetWidth(), i->second - 4);
426
427                 dc.SetFont (*wxSWISS_FONT);
428                 wxCoord label_width;
429                 wxCoord label_height;
430                 dc.GetTextExtent (std_to_wx (j->name), &label_width, &label_height);
431
432                 dc.DrawRotatedText (
433                         j->name,
434                         half + (half - label_height) / 2,
435                         min (i->second, (i->second + i->first + label_width) / 2),
436                         90
437                         );
438
439                 dc.DestroyClippingRegion ();
440
441                 lines.MoveToPoint (half, i->second);
442                 lines.AddLineToPoint (size.GetWidth(), i->second);
443
444                 gc->StrokePath (lines);
445
446                 ++i;
447                 ++j;
448         }
449
450         /* Overall label */
451         dc.SetFont (wxSWISS_FONT->Bold());
452         wxCoord overall_label_width;
453         wxCoord overall_label_height;
454         dc.GetTextExtent (_("Content"), &overall_label_width, &overall_label_height);
455         dc.DrawRotatedText (
456                 _("Content"),
457                 (half - overall_label_height) / 2,
458                 min (size.GetHeight(), (size.GetHeight() + _grid->GetColLabelSize() + overall_label_width) / 2),
459                 90
460                 );
461
462         delete gc;
463 }
464
465 void
466 AudioMappingView::paint_top_labels ()
467 {
468         wxPaintDC dc (_top_labels);
469         if (_grid->GetNumberCols() == 0) {
470                 return;
471         }
472
473         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
474         if (!gc) {
475                 return;
476         }
477
478         int sx, sy;
479         _top_labels->GetViewStart (&sx, &sy);
480         int rx, ry;
481         _grid->GetScrollPixelsPerUnit (&rx, &ry);
482         int toff = sx * rx;
483         gc->Translate (-toff, 0);
484
485         wxSize const size = dc.GetSize();
486
487         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
488
489         dc.SetFont (wxSWISS_FONT->Bold());
490         wxCoord label_width;
491         wxCoord label_height;
492         dc.GetTextExtent (_("DCP"), &label_width, &label_height);
493
494         dc.DrawText (_("DCP"), (size.GetWidth() + _grid->GetColSize(0) + LEFT_WIDTH - label_width) / 2 - toff, (size.GetHeight() - label_height) / 2);
495
496         gc->SetPen (wxPen (wxColour (0, 0, 0)));
497         wxGraphicsPath lines = gc->CreatePath();
498
499         int lhs = LEFT_WIDTH + _grid->GetColSize(0) - 1;
500         if (toff < (lhs - LEFT_WIDTH)) {
501                 lines.MoveToPoint (lhs, 0);
502                 lines.AddLineToPoint (lhs, size.GetHeight());
503         }
504
505         wxGridSizesInfo si = _grid->GetColSizes();
506         int total = 0;
507         for (int i = 0; i < _grid->GetNumberCols(); ++i) {
508                 total += si.GetSize (i);
509         }
510         lines.MoveToPoint (LEFT_WIDTH + total, 0);
511         lines.AddLineToPoint (LEFT_WIDTH + total, size.GetHeight());
512
513         gc->StrokePath (lines);
514
515         delete gc;
516 }
517
518 void
519 AudioMappingView::set_input_groups (vector<Group> const & groups)
520 {
521         if (_grid->GetNumberRows() == 0) {
522                 return;
523         }
524
525         _input_groups = groups;
526         _input_group_positions.clear ();
527
528         int ypos = _grid->GetColLabelSize() - 1;
529         BOOST_FOREACH (Group const & i, _input_groups) {
530                 int const old_ypos = ypos;
531                 ypos += (i.to - i.from + 1) * _grid->GetRowSize(0);
532                 _input_group_positions.push_back (make_pair (old_ypos, ypos));
533         }
534 }
535
536 void
537 AudioMappingView::mouse_moved_left_labels (wxMouseEvent& event)
538 {
539         bool done = false;
540         for (size_t i = 0; i < _input_group_positions.size(); ++i) {
541                 if (_input_group_positions[i].first <= event.GetY() && event.GetY() < _input_group_positions[i].second) {
542                         _left_labels->SetToolTip (_input_groups[i].name);
543                         done = true;
544                 }
545         }
546
547         if (!done) {
548                 _left_labels->SetToolTip ("");
549         }
550 }
551
552 void
553 AudioMappingView::grid_scrolled (wxScrollWinEvent& ev)
554 {
555         int x, y;
556         _grid->GetViewStart (&x, &y);
557         if (ev.GetOrientation() == wxVERTICAL) {
558                 _left_labels->Scroll (0, ev.GetPosition());
559         } else {
560                 _top_labels->Scroll (ev.GetPosition(), 0);
561                 /* Repaint the whole thing so that the left-hand line disappears where appropriate */
562                 _top_labels->Refresh ();
563         }
564         ev.Skip ();
565 }