Merge master.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
index a5dacdfc258bda49aeeee8371ba240b1cceb96eb..3136b8679a6140902c225239832148a51f08326f 100644 (file)
@@ -22,6 +22,7 @@
 #include <wx/grid.h>
 #include <libdcp/types.h>
 #include "lib/audio_mapping.h"
+#include "lib/util.h"
 #include "audio_mapping_view.h"
 #include "wx_util.h"
 
@@ -36,14 +37,11 @@ using boost::shared_ptr;
 #ifdef __WXMSW__
 #define CHECKBOX_WIDTH 16
 #define CHECKBOX_HEIGHT 16
-#endif
-
-#ifdef __WXGTK__
+#else
 #define CHECKBOX_WIDTH 20
 #define CHECKBOX_HEIGHT 20
 #endif
 
-
 class NoSelectionStringRenderer : public wxGridCellStringRenderer
 {
 public:
@@ -59,6 +57,9 @@ public:
 
        void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
        {
+               dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 0, wxPENSTYLE_SOLID));
+               dc.DrawRectangle (rect);
+               
                wxRendererNative::Get().DrawCheckBox (
                        &grid,
                        dc, rect,
@@ -84,32 +85,20 @@ AudioMappingView::AudioMappingView (wxWindow* parent)
        _grid = new wxGrid (this, wxID_ANY);
 
        _grid->CreateGrid (0, 7);
-#if wxMINOR_VERSION == 9
        _grid->HideRowLabels ();
-#else
-       _grid->SetRowLabelSize (0);
-#endif 
        _grid->DisableDragRowSize ();
        _grid->DisableDragColSize ();
        _grid->EnableEditing (false);
        _grid->SetCellHighlightPenWidth (0);
        _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
 
-       _grid->SetColLabelValue (0, _("Content channel"));
-       _grid->SetColLabelValue (1, _("L"));
-       _grid->SetColLabelValue (2, _("R"));
-       _grid->SetColLabelValue (3, _("C"));
-       _grid->SetColLabelValue (4, _("Lfe"));
-       _grid->SetColLabelValue (5, _("Ls"));
-       _grid->SetColLabelValue (6, _("Rs"));
-
-       _grid->AutoSize ();
+       set_column_labels ();
 
-       wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
-       s->Add (_grid, 1, wxEXPAND);
-       SetSizerAndFit (s);
+       _sizer = new wxBoxSizer (wxVERTICAL);
+       _sizer->Add (_grid, 1, wxEXPAND | wxALL);
+       SetSizerAndFit (_sizer);
 
-       Connect (wxID_ANY, wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler (AudioMappingView::left_click), 0, this);
+       Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
 }
 
 void
@@ -124,37 +113,95 @@ AudioMappingView::left_click (wxGridEvent& ev)
        } else {
                _grid->SetCellValue (ev.GetRow(), ev.GetCol(), wxT("1"));
        }
+
+       _map = AudioMapping (_map.content_channels ());
+       
+       for (int i = 0; i < _grid->GetNumberRows(); ++i) {
+               for (int j = 1; j < _grid->GetNumberCols(); ++j) {
+                       if (_grid->GetCellValue (i, j) == wxT ("1")) {
+                               _map.add (i, static_cast<libdcp::Channel> (j - 1));
+                       }
+               }
+       }
+
+       Changed (_map);
 }
 
 void
-AudioMappingView::set_mapping (AudioMapping map)
+AudioMappingView::set (AudioMapping map)
 {
+       _map = map;
+       
        if (_grid->GetNumberRows ()) {
                _grid->DeleteRows (0, _grid->GetNumberRows ());
        }
 
-       list<AudioMapping::Channel> content_channels = map.content_channels ();
-       _grid->InsertRows (0, content_channels.size ());
+       _grid->InsertRows (0, _map.content_channels ());
 
-       for (size_t r = 0; r < content_channels.size(); ++r) {
+       for (int r = 0; r < _map.content_channels(); ++r) {
                for (int c = 1; c < 7; ++c) {
                        _grid->SetCellRenderer (r, c, new CheckBoxRenderer);
                }
        }
        
-       int n = 0;
-       for (list<AudioMapping::Channel>::iterator i = content_channels.begin(); i != content_channels.end(); ++i) {
-               shared_ptr<const AudioContent> ac = i->content.lock ();
-               assert (ac);
-               _grid->SetCellValue (n, 0, wxString::Format (wxT("%s %d"), std_to_wx (ac->file().filename().string()).data(), i->index + 1));
+       for (int i = 0; i < _map.content_channels(); ++i) {
+               _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
 
-               list<libdcp::Channel> const d = map.content_to_dcp (*i);
+               list<libdcp::Channel> const d = _map.content_to_dcp (i);
                for (list<libdcp::Channel>::const_iterator j = d.begin(); j != d.end(); ++j) {
-                       _grid->SetCellValue (n, static_cast<int> (*j) + 1, wxT("1"));
+                       int const c = static_cast<int>(*j) + 1;
+                       if (c < _grid->GetNumberCols ()) {
+                               _grid->SetCellValue (i, c, wxT("1"));
+                       }
                }
-               ++n;
        }
+}
 
-       _grid->AutoSize ();
+void
+AudioMappingView::set_channels (int c)
+{
+       c++;
+
+       if (c < _grid->GetNumberCols ()) {
+               _grid->DeleteCols (c, _grid->GetNumberCols() - c);
+       } else if (c > _grid->GetNumberCols ()) {
+               _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
+               set_column_labels ();
+       }
+
+       set (_map);
 }
 
+void
+AudioMappingView::set_column_labels ()
+{
+       int const c = _grid->GetNumberCols ();
+       
+       _grid->SetColLabelValue (0, _("Content channel"));
+       
+       if (c > 0) {
+               _grid->SetColLabelValue (1, _("L"));
+       }
+       
+       if (c > 1) {
+               _grid->SetColLabelValue (2, _("R"));
+       }
+       
+       if (c > 2) {
+               _grid->SetColLabelValue (3, _("C"));
+       }
+       
+       if (c > 3) {
+               _grid->SetColLabelValue (4, _("Lfe"));
+       }
+       
+       if (c > 4) {
+               _grid->SetColLabelValue (5, _("Ls"));
+       }
+       
+       if (c > 5) {
+               _grid->SetColLabelValue (6, _("Rs"));
+       }
+
+       _grid->AutoSize ();
+}