10157eb2c6460e6fea4177f15c57471c3c323173
[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 #endif
40
41 #ifdef __WXGTK__
42 #define CHECKBOX_WIDTH 20
43 #define CHECKBOX_HEIGHT 20
44 #endif
45
46
47 class NoSelectionStringRenderer : public wxGridCellStringRenderer
48 {
49 public:
50         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
51         {
52                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
53         }
54 };
55
56 class CheckBoxRenderer : public wxGridCellRenderer
57 {
58 public:
59
60         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
61         {
62 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
63                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 0, wxPENSTYLE_SOLID));
64 #else           
65                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 0, wxSOLID));
66 #endif          
67                 dc.DrawRectangle (rect);
68                 
69                 wxRendererNative::Get().DrawCheckBox (
70                         &grid,
71                         dc, rect,
72                         grid.GetCellValue (row, col) == wxT("1") ? static_cast<int>(wxCONTROL_CHECKED) : 0
73                         );
74         }
75
76         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
77         {
78                 return wxSize (CHECKBOX_WIDTH + 4, CHECKBOX_HEIGHT + 4);
79         }
80         
81         wxGridCellRenderer* Clone () const
82         {
83                 return new CheckBoxRenderer;
84         }
85 };
86
87
88 AudioMappingView::AudioMappingView (wxWindow* parent)
89         : wxPanel (parent, wxID_ANY)
90 {
91         _grid = new wxGrid (this, wxID_ANY);
92
93         _grid->CreateGrid (0, 7);
94 #if wxMINOR_VERSION == 9
95         _grid->HideRowLabels ();
96 #else
97         _grid->SetRowLabelSize (0);
98 #endif  
99         _grid->DisableDragRowSize ();
100         _grid->DisableDragColSize ();
101         _grid->EnableEditing (false);
102         _grid->SetCellHighlightPenWidth (0);
103         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
104
105         _grid->SetColLabelValue (0, _("Content channel"));
106         _grid->SetColLabelValue (1, _("L"));
107         _grid->SetColLabelValue (2, _("R"));
108         _grid->SetColLabelValue (3, _("C"));
109         _grid->SetColLabelValue (4, _("Lfe"));
110         _grid->SetColLabelValue (5, _("Ls"));
111         _grid->SetColLabelValue (6, _("Rs"));
112
113         _grid->AutoSize ();
114
115         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
116         s->Add (_grid, 1, wxEXPAND);
117         SetSizerAndFit (s);
118
119         Connect (wxID_ANY, wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler (AudioMappingView::left_click), 0, this);
120 }
121
122 void
123 AudioMappingView::left_click (wxGridEvent& ev)
124 {
125         if (ev.GetCol() == 0) {
126                 return;
127         }
128         
129         if (_grid->GetCellValue (ev.GetRow(), ev.GetCol()) == wxT("1")) {
130                 _grid->SetCellValue (ev.GetRow(), ev.GetCol(), wxT("0"));
131         } else {
132                 _grid->SetCellValue (ev.GetRow(), ev.GetCol(), wxT("1"));
133         }
134 }
135
136 void
137 AudioMappingView::set_mapping (AudioMapping map)
138 {
139         if (_grid->GetNumberRows ()) {
140                 _grid->DeleteRows (0, _grid->GetNumberRows ());
141         }
142
143         list<int> content_channels = map.content_channels ();
144         _grid->InsertRows (0, content_channels.size ());
145
146         for (size_t r = 0; r < content_channels.size(); ++r) {
147                 for (int c = 1; c < 7; ++c) {
148                         _grid->SetCellRenderer (r, c, new CheckBoxRenderer);
149                 }
150         }
151         
152         int n = 0;
153         for (list<int>::iterator i = content_channels.begin(); i != content_channels.end(); ++i) {
154                 _grid->SetCellValue (n, 0, wxString::Format (wxT("%d"), *i + 1));
155
156                 list<libdcp::Channel> const d = map.content_to_dcp (*i);
157                 for (list<libdcp::Channel>::const_iterator j = d.begin(); j != d.end(); ++j) {
158                         _grid->SetCellValue (n, static_cast<int> (*j) + 1, wxT("1"));
159                 }
160                 ++n;
161         }
162
163         _grid->AutoSize ();
164 }
165