More noncopyable.
[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 "lib/audio_mapping.h"
25 #include "audio_mapping_view.h"
26 #include "wx_util.h"
27
28 using std::cout;
29 using std::list;
30 using boost::shared_ptr;
31
32 /* This could go away with wxWidgets 2.9, which has an API call
33    to find these values.
34 */
35
36 #ifdef __WXMSW__
37 #define CHECKBOX_WIDTH 16
38 #define CHECKBOX_HEIGHT 16
39 #else
40 #define CHECKBOX_WIDTH 20
41 #define CHECKBOX_HEIGHT 20
42 #endif
43
44 class NoSelectionStringRenderer : public wxGridCellStringRenderer
45 {
46 public:
47         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
48         {
49                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
50         }
51 };
52
53 class CheckBoxRenderer : public wxGridCellRenderer
54 {
55 public:
56
57         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
58         {
59                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 0, wxPENSTYLE_SOLID));
60                 dc.DrawRectangle (rect);
61                 
62                 wxRendererNative::Get().DrawCheckBox (
63                         &grid,
64                         dc, rect,
65                         grid.GetCellValue (row, col) == wxT("1") ? static_cast<int>(wxCONTROL_CHECKED) : 0
66                         );
67         }
68
69         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
70         {
71                 return wxSize (CHECKBOX_WIDTH + 4, CHECKBOX_HEIGHT + 4);
72         }
73         
74         wxGridCellRenderer* Clone () const
75         {
76                 return new CheckBoxRenderer;
77         }
78 };
79
80
81 AudioMappingView::AudioMappingView (wxWindow* parent)
82         : wxPanel (parent, wxID_ANY)
83 {
84         _grid = new wxGrid (this, wxID_ANY);
85
86         _grid->CreateGrid (0, 7);
87         _grid->HideRowLabels ();
88         _grid->DisableDragRowSize ();
89         _grid->DisableDragColSize ();
90         _grid->EnableEditing (false);
91         _grid->SetCellHighlightPenWidth (0);
92         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
93
94         set_column_labels ();
95
96         _sizer = new wxBoxSizer (wxVERTICAL);
97         _sizer->Add (_grid, 1, wxEXPAND | wxALL);
98         SetSizerAndFit (_sizer);
99
100         Connect (wxID_ANY, wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler (AudioMappingView::left_click), 0, this);
101 }
102
103 void
104 AudioMappingView::left_click (wxGridEvent& ev)
105 {
106         if (ev.GetCol() == 0) {
107                 return;
108         }
109         
110         if (_grid->GetCellValue (ev.GetRow(), ev.GetCol()) == wxT("1")) {
111                 _grid->SetCellValue (ev.GetRow(), ev.GetCol(), wxT("0"));
112         } else {
113                 _grid->SetCellValue (ev.GetRow(), ev.GetCol(), wxT("1"));
114         }
115
116         _map = AudioMapping ();
117         for (int i = 0; i < _grid->GetNumberRows(); ++i) {
118                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
119                         if (_grid->GetCellValue (i, j) == wxT ("1")) {
120                                 _map.add (i, static_cast<libdcp::Channel> (j - 1));
121                         }
122                 }
123         }
124
125         Changed (_map);
126 }
127
128 void
129 AudioMappingView::set (AudioMapping map)
130 {
131         _map = map;
132         
133         if (_grid->GetNumberRows ()) {
134                 _grid->DeleteRows (0, _grid->GetNumberRows ());
135         }
136
137         list<int> content_channels = _map.content_channels ();
138         _grid->InsertRows (0, content_channels.size ());
139
140         for (size_t r = 0; r < content_channels.size(); ++r) {
141                 for (int c = 1; c < 7; ++c) {
142                         _grid->SetCellRenderer (r, c, new CheckBoxRenderer);
143                 }
144         }
145         
146         int n = 0;
147         for (list<int>::iterator i = content_channels.begin(); i != content_channels.end(); ++i) {
148                 _grid->SetCellValue (n, 0, wxString::Format (wxT("%d"), *i + 1));
149
150                 list<libdcp::Channel> const d = _map.content_to_dcp (*i);
151                 for (list<libdcp::Channel>::const_iterator j = d.begin(); j != d.end(); ++j) {
152                         int const c = static_cast<int>(*j) + 1;
153                         if (c < _grid->GetNumberCols ()) {
154                                 _grid->SetCellValue (n, c, wxT("1"));
155                         }
156                 }
157                 ++n;
158         }
159 }
160
161 void
162 AudioMappingView::set_channels (int c)
163 {
164         c++;
165
166         if (c < _grid->GetNumberCols ()) {
167                 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
168         } else if (c > _grid->GetNumberCols ()) {
169                 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
170                 set_column_labels ();
171         }
172
173         set (_map);
174 }
175
176 void
177 AudioMappingView::set_column_labels ()
178 {
179         int const c = _grid->GetNumberCols ();
180         
181         _grid->SetColLabelValue (0, _("Content channel"));
182         
183         if (c > 0) {
184                 _grid->SetColLabelValue (1, _("L"));
185         }
186         
187         if (c > 1) {
188                 _grid->SetColLabelValue (2, _("R"));
189         }
190         
191         if (c > 2) {
192                 _grid->SetColLabelValue (3, _("C"));
193         }
194         
195         if (c > 3) {
196                 _grid->SetColLabelValue (4, _("Lfe"));
197         }
198         
199         if (c > 4) {
200                 _grid->SetColLabelValue (5, _("Ls"));
201         }
202         
203         if (c > 5) {
204                 _grid->SetColLabelValue (6, _("Rs"));
205         }
206
207         _grid->AutoSize ();
208 }