Mostly-merge master.
[dcpomatic.git] / src / wx / video_panel.cc
index bb8476d63b7bbdf1d39624ac28f38b9f29ed887b..fad824727074574ad0ef9f71f23c94f6237de415 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 */
 
 #include <wx/spinctrl.h>
-#include "lib/ratio.h"
 #include "lib/filter.h"
 #include "lib/ffmpeg_content.h"
 #include "lib/colour_conversion.h"
 #include "lib/config.h"
+#include "lib/util.h"
+#include "lib/ratio.h"
 #include "filter_dialog.h"
 #include "video_panel.h"
 #include "wx_util.h"
 #include "film_editor.h"
 #include "content_colour_conversion_dialog.h"
+#include "content_widget.h"
 
 using std::vector;
 using std::string;
@@ -39,6 +41,28 @@ using boost::dynamic_pointer_cast;
 using boost::bind;
 using boost::optional;
 
+static VideoContentScale
+index_to_scale (int n)
+{
+       vector<VideoContentScale> scales = VideoContentScale::all ();
+       assert (n >= 0);
+       assert (n < int (scales.size ()));
+       return scales[n];
+}
+
+static int
+scale_to_index (VideoContentScale scale)
+{
+       vector<VideoContentScale> scales = VideoContentScale::all ();
+       for (size_t i = 0; i < scales.size(); ++i) {
+               if (scales[i] == scale) {
+                       return i;
+               }
+       }
+
+       assert (false);
+}
+
 VideoPanel::VideoPanel (FilmEditor* e)
        : FilmEditorPanel (e, _("Video"))
 {
@@ -48,33 +72,73 @@ VideoPanel::VideoPanel (FilmEditor* e)
        int r = 0;
 
        add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
-       _frame_type = new wxChoice (this, wxID_ANY);
-       grid->Add (_frame_type, wxGBPosition (r, 1));
+       _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
+               this,
+               new wxChoice (this, wxID_ANY),
+               VideoContentProperty::VIDEO_FRAME_TYPE,
+               boost::mem_fn (&VideoContent::video_frame_type),
+               boost::mem_fn (&VideoContent::set_video_frame_type),
+               &caster<int, VideoFrameType>,
+               &caster<VideoFrameType, int>
+               );
+       _frame_type->add (grid, wxGBPosition (r, 1));
        ++r;
        
        add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
-       _left_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
-       grid->Add (_left_crop, wxGBPosition (r, 1));
+       _left_crop = new ContentSpinCtrl<VideoContent> (
+               this,
+               new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
+               VideoContentProperty::VIDEO_CROP,
+               boost::mem_fn (&VideoContent::left_crop),
+               boost::mem_fn (&VideoContent::set_left_crop)
+               );
+       _left_crop->add (grid, wxGBPosition (r, 1));
        ++r;
 
        add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
-       _right_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
-       grid->Add (_right_crop, wxGBPosition (r, 1));
+       _right_crop = new ContentSpinCtrl<VideoContent> (
+               this,
+               new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
+               VideoContentProperty::VIDEO_CROP,
+               boost::mem_fn (&VideoContent::right_crop),
+               boost::mem_fn (&VideoContent::set_right_crop)
+               );
+       _right_crop->add (grid, wxGBPosition (r, 1));
        ++r;
        
        add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
-       _top_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
-       grid->Add (_top_crop, wxGBPosition (r, 1));
+       _top_crop = new ContentSpinCtrl<VideoContent> (
+               this,
+               new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
+               VideoContentProperty::VIDEO_CROP,
+               boost::mem_fn (&VideoContent::top_crop),
+               boost::mem_fn (&VideoContent::set_top_crop)
+               );
+       _top_crop->add (grid, wxGBPosition (r,1 ));
        ++r;
        
        add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
-       _bottom_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
-       grid->Add (_bottom_crop, wxGBPosition (r, 1));
+       _bottom_crop = new ContentSpinCtrl<VideoContent> (
+               this,
+               new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
+               VideoContentProperty::VIDEO_CROP,
+               boost::mem_fn (&VideoContent::bottom_crop),
+               boost::mem_fn (&VideoContent::set_bottom_crop)
+               );
+       _bottom_crop->add (grid, wxGBPosition (r, 1));
        ++r;
 
        add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
-       _ratio = new wxChoice (this, wxID_ANY);
-       grid->Add (_ratio, wxGBPosition (r, 1));
+       _scale = new ContentChoice<VideoContent, VideoContentScale> (
+               this,
+               new wxChoice (this, wxID_ANY),
+               VideoContentProperty::VIDEO_SCALE,
+               boost::mem_fn (&VideoContent::scale),
+               boost::mem_fn (&VideoContent::set_scale),
+               &index_to_scale,
+               &scale_to_index
+               );
+       _scale->add (grid, wxGBPosition (r, 1));
        ++r;
 
        {
@@ -118,85 +182,33 @@ VideoPanel::VideoPanel (FilmEditor* e)
        _description->SetFont(font);
        ++r;
 
-       _left_crop->SetRange (0, 1024);
-       _top_crop->SetRange (0, 1024);
-       _right_crop->SetRange (0, 1024);
-       _bottom_crop->SetRange (0, 1024);
+       _left_crop->wrapped()->SetRange (0, 1024);
+       _top_crop->wrapped()->SetRange (0, 1024);
+       _right_crop->wrapped()->SetRange (0, 1024);
+       _bottom_crop->wrapped()->SetRange (0, 1024);
 
-       vector<Ratio const *> ratios = Ratio::all ();
-       _ratio->Clear ();
-       for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
-               _ratio->Append (std_to_wx ((*i)->nickname ()));
+       vector<VideoContentScale> scales = VideoContentScale::all ();
+       _scale->wrapped()->Clear ();
+       for (vector<VideoContentScale>::iterator i = scales.begin(); i != scales.end(); ++i) {
+               _scale->wrapped()->Append (std_to_wx (i->name ()));
        }
 
-       _frame_type->Append (_("2D"));
-       _frame_type->Append (_("3D left/right"));
+       _frame_type->wrapped()->Append (_("2D"));
+       _frame_type->wrapped()->Append (_("3D left/right"));
+       _frame_type->wrapped()->Append (_("3D top/bottom"));
+       _frame_type->wrapped()->Append (_("3D alternate"));
 
-       _frame_type->Bind               (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::frame_type_changed, this));
-       _left_crop->Bind                (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::left_crop_changed, this));
-       _right_crop->Bind               (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::right_crop_changed, this));
-       _top_crop->Bind                 (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::top_crop_changed, this));
-       _bottom_crop->Bind              (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::bottom_crop_changed, this));
-       _ratio->Bind                    (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::ratio_changed, this));
        _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
        _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
 }
 
-
-/** Called when the left crop widget has been changed */
-void
-VideoPanel::left_crop_changed ()
-{
-       shared_ptr<VideoContent> c = _editor->selected_video_content ();
-       if (!c) {
-               return;
-       }
-
-       c->set_left_crop (_left_crop->GetValue ());
-}
-
-/** Called when the right crop widget has been changed */
-void
-VideoPanel::right_crop_changed ()
-{
-       shared_ptr<VideoContent> c = _editor->selected_video_content ();
-       if (!c) {
-               return;
-       }
-
-       c->set_right_crop (_right_crop->GetValue ());
-}
-
-/** Called when the top crop widget has been changed */
-void
-VideoPanel::top_crop_changed ()
-{
-       shared_ptr<VideoContent> c = _editor->selected_video_content ();
-       if (!c) {
-               return;
-       }
-
-       c->set_top_crop (_top_crop->GetValue ());
-}
-
-/** Called when the bottom crop value has been changed */
-void
-VideoPanel::bottom_crop_changed ()
-{
-       shared_ptr<VideoContent> c = _editor->selected_video_content ();
-       if (!c) {
-               return;
-       }
-
-       c->set_bottom_crop (_bottom_crop->GetValue ());
-}
-
 void
 VideoPanel::film_changed (Film::Property property)
 {
        switch (property) {
        case Film::CONTAINER:
        case Film::VIDEO_FRAME_RATE:
+       case Film::RESOLUTION:
                setup_description ();
                break;
        default:
@@ -205,53 +217,35 @@ VideoPanel::film_changed (Film::Property property)
 }
 
 void
-VideoPanel::film_content_changed (shared_ptr<Content> c, int property)
+VideoPanel::film_content_changed (int property)
 {
-       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
-       shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
-
+       VideoContentList vc = _editor->selected_video_content ();
+       shared_ptr<VideoContent> vcs;
+       shared_ptr<FFmpegContent> fcs;
+       if (!vc.empty ()) {
+               vcs = vc.front ();
+               fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
+       }
+       
        if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
-               checked_set (_frame_type, vc ? vc->video_frame_type () : VIDEO_FRAME_TYPE_2D);
                setup_description ();
        } else if (property == VideoContentProperty::VIDEO_CROP) {
-               checked_set (_left_crop,   vc ? vc->crop().left : 0);
-               checked_set (_right_crop,  vc ? vc->crop().right        : 0);
-               checked_set (_top_crop,    vc ? vc->crop().top  : 0);
-               checked_set (_bottom_crop, vc ? vc->crop().bottom : 0);
                setup_description ();
-       } else if (property == VideoContentProperty::VIDEO_RATIO) {
-               if (vc) {
-                       int n = 0;
-                       vector<Ratio const *> ratios = Ratio::all ();
-                       vector<Ratio const *>::iterator i = ratios.begin ();
-                       while (i != ratios.end() && *i != vc->ratio()) {
-                               ++i;
-                               ++n;
-                       }
-
-                       if (i == ratios.end()) {
-                               checked_set (_ratio, -1);
-                       } else {
-                               checked_set (_ratio, n);
-                       }
-               } else {
-                       checked_set (_ratio, -1);
-               }
+       } else if (property == VideoContentProperty::VIDEO_SCALE) {
                setup_description ();
        } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
                setup_description ();
        } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
-               optional<size_t> preset = vc ? vc->colour_conversion().preset () : optional<size_t> ();
+               optional<size_t> preset = vcs ? vcs->colour_conversion().preset () : optional<size_t> ();
                vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
                _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
        } else if (property == FFmpegContentProperty::FILTERS) {
-               if (fc) {
-                       pair<string, string> p = Filter::ffmpeg_strings (fc->filters ());
-                       if (p.first.empty () && p.second.empty ()) {
+               if (fcs) {
+                       string const p = Filter::ffmpeg_string (fcs->filters ());
+                       if (p.empty ()) {
                                _filters->SetLabel (_("None"));
                        } else {
-                               string const b = p.first + " " + p.second;
-                               _filters->SetLabel (std_to_wx (b));
+                               _filters->SetLabel (std_to_wx (p));
                        }
                }
        }
@@ -261,18 +255,13 @@ VideoPanel::film_content_changed (shared_ptr<Content> c, int property)
 void
 VideoPanel::edit_filters_clicked ()
 {
-       shared_ptr<Content> c = _editor->selected_content ();
-       if (!c) {
+       FFmpegContentList c = _editor->selected_ffmpeg_content ();
+       if (c.size() != 1) {
                return;
        }
 
-       shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
-       if (!fc) {
-               return;
-       }
-       
-       FilterDialog* d = new FilterDialog (this, fc->filters());
-       d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, fc, _1));
+       FilterDialog* d = new FilterDialog (this, c.front()->filters());
+       d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
        d->ShowModal ();
        d->Destroy ();
 }
@@ -280,64 +269,67 @@ VideoPanel::edit_filters_clicked ()
 void
 VideoPanel::setup_description ()
 {
-       shared_ptr<VideoContent> vc = _editor->selected_video_content ();
-       if (!vc) {
+       VideoContentList vc = _editor->selected_video_content ();
+       if (vc.empty ()) {
                _description->SetLabel ("");
                return;
+       } else if (vc.size() > 1) {
+               _description->SetLabel (_("Multiple content selected"));
+               return;
        }
 
+       shared_ptr<VideoContent> vcs = vc.front ();
+
        wxString d;
 
        int lines = 0;
 
-       if (vc->video_size().width && vc->video_size().height) {
+       if (vcs->video_size().width && vcs->video_size().height) {
                d << wxString::Format (
                        _("Content video is %dx%d (%.2f:1)\n"),
-                       vc->video_size_after_3d_split().width, vc->video_size_after_3d_split().height,
-                       float (vc->video_size_after_3d_split().width) / vc->video_size_after_3d_split().height
+                       vcs->video_size_after_3d_split().width,
+                       vcs->video_size_after_3d_split().height,
+                       vcs->video_size_after_3d_split().ratio ()
                        );
                ++lines;
        }
 
-       Crop const crop = vc->crop ();
-       if ((crop.left || crop.right || crop.top || crop.bottom) && vc->video_size() != libdcp::Size (0, 0)) {
-               libdcp::Size cropped = vc->video_size_after_3d_split ();
-               cropped.width -= crop.left + crop.right;
-               cropped.height -= crop.top + crop.bottom;
+       Crop const crop = vcs->crop ();
+       if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != dcp::Size (0, 0)) {
+               dcp::Size cropped = vcs->video_size_after_crop ();
                d << wxString::Format (
                        _("Cropped to %dx%d (%.2f:1)\n"),
                        cropped.width, cropped.height,
-                       float (cropped.width) / cropped.height
+                       cropped.ratio ()
                        );
                ++lines;
        }
 
-       Ratio const * ratio = vc->ratio ();
-       if (ratio) {
-               libdcp::Size container_size = _editor->film()->container()->size (_editor->film()->full_frame ());
-               
-               libdcp::Size const scaled = ratio->size (container_size);
+       dcp::Size const container_size = _editor->film()->frame_size ();
+       dcp::Size const scaled = vcs->scale().size (vcs, container_size, container_size);
+
+       if (scaled != vcs->video_size_after_crop ()) {
                d << wxString::Format (
                        _("Scaled to %dx%d (%.2f:1)\n"),
                        scaled.width, scaled.height,
-                       float (scaled.width) / scaled.height
+                       scaled.ratio ()
+                       );
+               ++lines;
+       }
+       
+       if (scaled != container_size) {
+               d << wxString::Format (
+                       _("Padded with black to %dx%d (%.2f:1)\n"),
+                       container_size.width, container_size.height,
+                       container_size.ratio ()
                        );
                ++lines;
-
-               if (scaled != container_size) {
-                       d << wxString::Format (
-                               _("Padded with black to %dx%d (%.2f:1)\n"),
-                               container_size.width, container_size.height,
-                               float (container_size.width) / container_size.height
-                               );
-                       ++lines;
-               }
        }
 
-       d << wxString::Format (_("Content frame rate %.4f\n"), vc->video_frame_rate ());
+       d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
        ++lines;
-       FrameRateConversion frc (vc->video_frame_rate(), _editor->film()->video_frame_rate ());
-       d << frc.description << "\n";
+       FrameRateChange frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
+       d << std_to_wx (frc.description) << "\n";
        ++lines;
 
        for (int i = lines; i < 6; ++i) {
@@ -348,46 +340,42 @@ VideoPanel::setup_description ()
        _sizer->Layout ();
 }
 
-
-void
-VideoPanel::ratio_changed ()
-{
-       if (!_editor->film ()) {
-               return;
-       }
-
-       shared_ptr<VideoContent> vc = _editor->selected_video_content ();
-       
-       int const n = _ratio->GetSelection ();
-       if (n >= 0) {
-               vector<Ratio const *> ratios = Ratio::all ();
-               assert (n < int (ratios.size()));
-               vc->set_ratio (ratios[n]);
-       }
-}
-
-void
-VideoPanel::frame_type_changed ()
-{
-       shared_ptr<VideoContent> vc = _editor->selected_video_content ();
-       if (vc) {
-               vc->set_video_frame_type (static_cast<VideoFrameType> (_frame_type->GetSelection ()));
-       }
-}
-
 void
 VideoPanel::edit_colour_conversion_clicked ()
 {
-       shared_ptr<VideoContent> vc = _editor->selected_video_content ();
-       if (!vc) {
+       VideoContentList vc = _editor->selected_video_content ();
+       if (vc.size() != 1) {
                return;
        }
 
-       ColourConversion conversion = vc->colour_conversion ();
+       ColourConversion conversion = vc.front()->colour_conversion ();
        ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
        d->set (conversion);
        d->ShowModal ();
 
-       vc->set_colour_conversion (d->get ());
+       vc.front()->set_colour_conversion (d->get ());
        d->Destroy ();
 }
+
+void
+VideoPanel::content_selection_changed ()
+{
+       VideoContentList sel = _editor->selected_video_content ();
+       bool const single = sel.size() == 1;
+
+       _left_crop->set_content (sel);
+       _right_crop->set_content (sel);
+       _top_crop->set_content (sel);
+       _bottom_crop->set_content (sel);
+       _frame_type->set_content (sel);
+       _scale->set_content (sel);
+
+       /* Things that are only allowed with single selections */
+       _filters_button->Enable (single);
+       _colour_conversion_button->Enable (single);
+
+       film_content_changed (VideoContentProperty::VIDEO_CROP);
+       film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
+       film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
+       film_content_changed (FFmpegContentProperty::FILTERS);
+}