Restore scrollbars on large audio mapping views on Linux (#970).
[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 "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 wxPanel (this, wxID_ANY);
128         _left_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_left_labels, this));
129         _top_labels = new wxPanel (this, wxID_ANY);
130         _top_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_top_labels, this));
131
132         _grid = new wxGrid (this, wxID_ANY);
133
134         _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
135         _grid->HideRowLabels ();
136         _grid->DisableDragRowSize ();
137         _grid->DisableDragColSize ();
138         _grid->EnableEditing (false);
139         _grid->SetCellHighlightPenWidth (0);
140         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
141         _grid->EnableScrolling (true, true);
142         _grid->AutoSize ();
143
144         wxSizer* vertical_sizer = new wxBoxSizer (wxVERTICAL);
145         vertical_sizer->Add (_top_labels);
146         wxSizer* horizontal_sizer = new wxBoxSizer (wxHORIZONTAL);
147         horizontal_sizer->Add (_left_labels);
148         horizontal_sizer->Add (_grid, 1, wxEXPAND | wxALL);
149         vertical_sizer->Add (horizontal_sizer);
150         SetSizerAndFit (vertical_sizer);
151
152         Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
153         Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
154         _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved_grid, this, _1));
155         _grid->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&AudioMappingView::grid_scrolled, this));
156         _grid->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&AudioMappingView::grid_scrolled, this));
157         _grid->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&AudioMappingView::grid_scrolled, this));
158         _grid->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&AudioMappingView::grid_scrolled, this));
159         _grid->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&AudioMappingView::grid_scrolled, this));
160         _grid->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&AudioMappingView::grid_scrolled, this));
161         _grid->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&AudioMappingView::grid_scrolled, this));
162         _grid->Bind (wxEVT_SCROLLWIN_THUMBRELEASE, boost::bind (&AudioMappingView::grid_scrolled, this));
163         Bind (wxEVT_SIZE, boost::bind (&AudioMappingView::sized, this, _1));
164
165         _menu = new wxMenu;
166         _menu->Append (ID_off, _("Off"));
167         _menu->Append (ID_full, _("Full"));
168         _menu->Append (ID_minus6dB, _("-6dB"));
169         _menu->Append (ID_edit, _("Edit..."));
170
171         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::off, this), ID_off);
172         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::full, this), ID_full);
173         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
174         Bind (wxEVT_MENU, boost::bind (&AudioMappingView::edit, this), ID_edit);
175
176         _left_labels->Bind (wxEVT_MOTION, bind (&AudioMappingView::mouse_moved_left_labels, this, _1));
177 }
178
179 /** Called when any gain value has changed */
180 void
181 AudioMappingView::map_values_changed ()
182 {
183         update_cells ();
184         Changed (_map);
185         _last_tooltip_column = -1;
186 }
187
188 void
189 AudioMappingView::left_click (wxGridEvent& ev)
190 {
191         if (ev.GetCol() == 0) {
192                 return;
193         }
194
195         int const d = ev.GetCol() - 1;
196
197         if (_map.get (ev.GetRow(), d) > 0) {
198                 _map.set (ev.GetRow(), d, 0);
199         } else {
200                 _map.set (ev.GetRow(), d, 1);
201         }
202
203         map_values_changed ();
204 }
205
206 void
207 AudioMappingView::right_click (wxGridEvent& ev)
208 {
209         if (ev.GetCol() == 0) {
210                 return;
211         }
212
213         _menu_row = ev.GetRow ();
214         _menu_column = ev.GetCol ();
215         PopupMenu (_menu, ev.GetPosition ());
216 }
217
218 void
219 AudioMappingView::off ()
220 {
221         _map.set (_menu_row, _menu_column - 1, 0);
222         map_values_changed ();
223 }
224
225 void
226 AudioMappingView::full ()
227 {
228         _map.set (_menu_row, _menu_column - 1, 1);
229         map_values_changed ();
230 }
231
232 void
233 AudioMappingView::minus6dB ()
234 {
235         _map.set (_menu_row, _menu_column - 1, pow (10, -6.0 / 20));
236         map_values_changed ();
237 }
238
239 void
240 AudioMappingView::edit ()
241 {
242         int const d = _menu_column - 1;
243
244         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
245         if (dialog->ShowModal () == wxID_OK) {
246                 _map.set (_menu_row, d, dialog->value ());
247                 map_values_changed ();
248         }
249
250         dialog->Destroy ();
251 }
252
253 void
254 AudioMappingView::set (AudioMapping map)
255 {
256         _map = map;
257         update_cells ();
258 }
259
260 void
261 AudioMappingView::set_input_channels (vector<string> const & names)
262 {
263         for (int i = 0; i < _grid->GetNumberRows(); ++i) {
264                 _grid->SetCellValue (i, 0, std_to_wx (names[i]));
265         }
266 }
267
268 void
269 AudioMappingView::set_output_channels (vector<string> const & names)
270 {
271         int const o = names.size() + 1;
272         if (o < _grid->GetNumberCols ()) {
273                 _grid->DeleteCols (o, _grid->GetNumberCols() - o);
274         } else if (o > _grid->GetNumberCols ()) {
275                 _grid->AppendCols (o - _grid->GetNumberCols());
276         }
277
278         _grid->SetColLabelValue (0, wxT (""));
279
280         for (size_t i = 0; i < names.size(); ++i) {
281                 _grid->SetColLabelValue (i + 1, std_to_wx (names[i]));
282         }
283
284         update_cells ();
285         setup_sizes ();
286
287         Layout ();
288 }
289
290 void
291 AudioMappingView::update_cells ()
292 {
293         vector<string> row_names;
294         for (int i = 0; i < _grid->GetNumberRows (); ++i) {
295                 row_names.push_back (wx_to_std (_grid->GetCellValue (i, 0)));
296         }
297
298         if (_grid->GetNumberRows ()) {
299                 _grid->DeleteRows (0, _grid->GetNumberRows ());
300         }
301
302         _grid->InsertRows (0, _map.input_channels ());
303
304         for (int i = 0; i < _map.input_channels(); ++i) {
305                 for (int j = 0; j < _map.output_channels(); ++j) {
306                         _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
307                 }
308         }
309
310         for (int i = 0; i < _map.input_channels(); ++i) {
311                 if (i < int (row_names.size ())) {
312                         _grid->SetCellValue (i, 0, std_to_wx (row_names[i]));
313                 }
314                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
315                         _grid->SetCellValue (i, j, std_to_wx (locale_convert<string> (_map.get (i, j - 1))));
316                 }
317         }
318
319         _grid->AutoSize ();
320 }
321
322 void
323 AudioMappingView::mouse_moved_grid (wxMouseEvent& ev)
324 {
325         int xx;
326         int yy;
327         _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
328
329         int const row = _grid->YToRow (yy);
330         int const column = _grid->XToCol (xx);
331
332         if (row < 0 || column < 1) {
333                 _grid->GetGridWindow()->SetToolTip ("");
334                 _last_tooltip_row = row;
335                 _last_tooltip_column = column;
336         }
337
338         if (row != _last_tooltip_row || column != _last_tooltip_column) {
339
340                 wxString s;
341                 float const gain = _map.get (row, column - 1);
342                 if (gain == 0) {
343                         s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
344                 } else if (gain == 1) {
345                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
346                 } else {
347                         float const dB = 20 * log10 (gain);
348                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
349                 }
350
351                 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
352                 _last_tooltip_row = row;
353                 _last_tooltip_column = column;
354         }
355
356         ev.Skip ();
357 }
358
359 void
360 AudioMappingView::sized (wxSizeEvent& ev)
361 {
362         setup_sizes ();
363         ev.Skip ();
364 }
365
366 void
367 AudioMappingView::setup_sizes ()
368 {
369         int const top_height = 24;
370
371         _grid->AutoSize ();
372         _left_labels->SetMinSize (wxSize (LEFT_WIDTH, _grid->GetSize().GetHeight()));
373         _top_labels->SetMinSize (wxSize (_grid->GetSize().GetWidth() + LEFT_WIDTH, top_height));
374         /* Try to make the _top_labels 'actual' size respect the minimum we just set */
375         _top_labels->Fit ();
376         _left_labels->Refresh ();
377         _top_labels->Refresh ();
378 }
379
380 void
381 AudioMappingView::paint_left_labels ()
382 {
383         wxPaintDC dc (_left_labels);
384
385         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
386         if (!gc) {
387                 return;
388         }
389
390         int sx, sy;
391         _grid->CalcUnscrolledPosition (0, 0, &sx, &sy);
392         gc->Translate (0, -sy);
393
394         wxSize const size = dc.GetSize();
395         int const half = size.GetWidth() / 2;
396
397         gc->SetPen (wxPen (wxColour (0, 0, 0)));
398         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
399
400         wxGraphicsPath lines = gc->CreatePath();
401
402         vector<pair<int, int> >::const_iterator i = _input_group_positions.begin();
403         if (i != _input_group_positions.end()) {
404                 lines.MoveToPoint (half, i->first);
405                 lines.AddLineToPoint (size.GetWidth(), i->first);
406         }
407
408         vector<Group>::const_iterator j = _input_groups.begin();
409         while (i != _input_group_positions.end() && j != _input_groups.end()) {
410
411                 dc.SetClippingRegion (0, i->first + 2, size.GetWidth(), i->second - 4);
412
413                 dc.SetFont (*wxSWISS_FONT);
414                 wxCoord label_width;
415                 wxCoord label_height;
416                 dc.GetTextExtent (std_to_wx (j->name), &label_width, &label_height);
417
418                 dc.DrawRotatedText (
419                         j->name,
420                         half + (half - label_height) / 2,
421                         min (i->second, (i->second + i->first + label_width) / 2) - sy,
422                         90
423                         );
424
425                 dc.DestroyClippingRegion ();
426
427                 lines.MoveToPoint (half, i->second);
428                 lines.AddLineToPoint (size.GetWidth(), i->second);
429
430                 gc->StrokePath (lines);
431
432                 ++i;
433                 ++j;
434         }
435
436         /* Overall label */
437         dc.SetFont (wxSWISS_FONT->Bold());
438         wxCoord overall_label_width;
439         wxCoord overall_label_height;
440         dc.GetTextExtent (_("Content"), &overall_label_width, &overall_label_height);
441         dc.DrawRotatedText (
442                 _("Content"),
443                 (half - overall_label_height) / 2,
444                 min (size.GetHeight(), (size.GetHeight() + _grid->GetColLabelSize() + overall_label_width) / 2 - sy),
445                 90
446                 );
447
448         delete gc;
449 }
450
451 void
452 AudioMappingView::paint_top_labels ()
453 {
454         wxPaintDC dc (_top_labels);
455         if (_grid->GetNumberCols() == 0) {
456                 return;
457         }
458
459         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
460         if (!gc) {
461                 return;
462         }
463
464         int sx, sy;
465         _grid->CalcUnscrolledPosition (0, 0, &sx, &sy);
466         gc->Translate (-sx, 0);
467
468         wxSize const size = dc.GetSize();
469
470         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
471
472         dc.SetFont (wxSWISS_FONT->Bold());
473         wxCoord label_width;
474         wxCoord label_height;
475         dc.GetTextExtent (_("DCP"), &label_width, &label_height);
476
477         dc.DrawText (_("DCP"), (size.GetWidth() + _grid->GetColSize(0) + LEFT_WIDTH - label_width) / 2 - sx, (size.GetHeight() - label_height) / 2);
478
479         gc->SetPen (wxPen (wxColour (0, 0, 0)));
480         wxGraphicsPath lines = gc->CreatePath();
481         lines.MoveToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, 0);
482         lines.AddLineToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, size.GetHeight());
483         lines.MoveToPoint (size.GetWidth() - 1, 0);
484         lines.AddLineToPoint (size.GetWidth() - 1, size.GetHeight());
485         gc->StrokePath (lines);
486
487         delete gc;
488 }
489
490 void
491 AudioMappingView::set_input_groups (vector<Group> const & groups)
492 {
493         if (_grid->GetNumberRows() == 0) {
494                 return;
495         }
496
497         _input_groups = groups;
498         _input_group_positions.clear ();
499
500         int ypos = _grid->GetColLabelSize() - 1;
501         BOOST_FOREACH (Group const & i, _input_groups) {
502                 int const old_ypos = ypos;
503                 ypos += (i.to - i.from + 1) * _grid->GetRowSize(0);
504                 _input_group_positions.push_back (make_pair (old_ypos, ypos));
505         }
506 }
507
508 void
509 AudioMappingView::mouse_moved_left_labels (wxMouseEvent& event)
510 {
511         bool done = false;
512         for (size_t i = 0; i < _input_group_positions.size(); ++i) {
513                 if (_input_group_positions[i].first <= event.GetY() && event.GetY() < _input_group_positions[i].second) {
514                         _left_labels->SetToolTip (_input_groups[i].name);
515                         done = true;
516                 }
517         }
518
519         if (!done) {
520                 _left_labels->SetToolTip ("");
521         }
522 }
523
524 void
525 AudioMappingView::grid_scrolled ()
526 {
527         _left_labels->Refresh ();
528         _left_labels->Update ();
529         _top_labels->Refresh ();
530         _top_labels->Update ();
531 }