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