Add a new "Advanced settings" dialog for content, accessible
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "filter_dialog.h"
22 #include "video_panel.h"
23 #include "wx_util.h"
24 #include "content_colour_conversion_dialog.h"
25 #include "content_widget.h"
26 #include "content_panel.h"
27 #include "static_text.h"
28 #include "check_box.h"
29 #include "custom_scale_dialog.h"
30 #include "dcpomatic_button.h"
31 #include "lib/filter.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/colour_conversion.h"
34 #include "lib/config.h"
35 #include "lib/util.h"
36 #include "lib/ratio.h"
37 #include "lib/frame_rate_change.h"
38 #include "lib/dcp_content.h"
39 #include "lib/video_content.h"
40 #include <wx/spinctrl.h>
41 #include <wx/tglbtn.h>
42 #include <boost/foreach.hpp>
43 #include <boost/unordered_set.hpp>
44 #include <boost/functional/hash.hpp>
45 #include <set>
46 #include <iostream>
47
48 using std::vector;
49 using std::string;
50 using std::pair;
51 using std::cout;
52 using std::list;
53 using std::set;
54 using boost::shared_ptr;
55 using boost::dynamic_pointer_cast;
56 using boost::bind;
57 using boost::optional;
58 using namespace dcpomatic;
59
60
61 VideoPanel::VideoPanel (ContentPanel* p)
62         : ContentSubPanel (p, _("Video"))
63 {
64         _reference = new CheckBox (this, _("Use this DCP's video as OV and make VF"));
65         _reference_note = new StaticText (this, wxT(""));
66         _reference_note->Wrap (200);
67         wxFont font = _reference_note->GetFont();
68         font.SetStyle(wxFONTSTYLE_ITALIC);
69         font.SetPointSize(font.GetPointSize() - 1);
70         _reference_note->SetFont(font);
71
72         _type_label = create_label (this, _("Type"), true);
73         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
74                 this,
75                 new wxChoice (this, wxID_ANY),
76                 VideoContentProperty::FRAME_TYPE,
77                 &Content::video,
78                 boost::mem_fn (&VideoContent::frame_type),
79                 boost::mem_fn (&VideoContent::set_frame_type),
80                 &caster<int, VideoFrameType>,
81                 &caster<VideoFrameType, int>
82                 );
83
84         _crop_label = create_label (this, _("Crop"), true);
85
86         int const crop_width = 56;
87         int const link_height = 28;
88
89         _left_crop_label = create_label (this, _("Left"), true);
90         _left_crop = new ContentSpinCtrl<VideoContent> (
91                 this,
92                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(crop_width, -1)),
93                 VideoContentProperty::CROP,
94                 &Content::video,
95                 boost::mem_fn (&VideoContent::left_crop),
96                 boost::mem_fn (&VideoContent::set_left_crop),
97                 boost::bind (&VideoPanel::left_crop_changed, this)
98                 );
99
100         _left_right_link = new wxToggleButton (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(22, link_height));
101         _left_right_link->SetBitmap (wxBitmap(bitmap_path("link"), wxBITMAP_TYPE_PNG));
102
103         _right_crop_label = create_label (this, _("Right"), true);
104         _right_crop = new ContentSpinCtrl<VideoContent> (
105                 this,
106                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(crop_width, -1)),
107                 VideoContentProperty::CROP,
108                 &Content::video,
109                 boost::mem_fn (&VideoContent::right_crop),
110                 boost::mem_fn (&VideoContent::set_right_crop),
111                 boost::bind (&VideoPanel::right_crop_changed, this)
112                 );
113
114         _top_crop_label = create_label (this, _("Top"), true);
115         _top_crop = new ContentSpinCtrl<VideoContent> (
116                 this,
117                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(crop_width, -1)),
118                 VideoContentProperty::CROP,
119                 &Content::video,
120                 boost::mem_fn (&VideoContent::top_crop),
121                 boost::mem_fn (&VideoContent::set_top_crop),
122                 boost::bind (&VideoPanel::top_crop_changed, this)
123                 );
124
125         _top_bottom_link = new wxToggleButton (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(22, link_height));
126         _top_bottom_link->SetBitmap (wxBitmap(bitmap_path("link"), wxBITMAP_TYPE_PNG));
127
128         _bottom_crop_label = create_label (this, _("Bottom"), true);
129         _bottom_crop = new ContentSpinCtrl<VideoContent> (
130                 this,
131                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(crop_width, -1)),
132                 VideoContentProperty::CROP,
133                 &Content::video,
134                 boost::mem_fn (&VideoContent::bottom_crop),
135                 boost::mem_fn (&VideoContent::set_bottom_crop),
136                 boost::bind (&VideoPanel::bottom_crop_changed, this)
137                 );
138
139         _fade_in_label = create_label (this, _("Fade in"), true);
140         _fade_in = new Timecode<ContentTime> (this);
141
142         _fade_out_label = create_label (this, _("Fade out"), true);
143         _fade_out = new Timecode<ContentTime> (this);
144
145         wxClientDC dc (this);
146         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
147         size.SetHeight (-1);
148
149         _scale_label = create_label (this, _("Scale"), true);
150         _scale_fit = new wxRadioButton (this, wxID_ANY, _("to fit DCP"));
151         _scale_custom = new wxRadioButton (this, wxID_ANY, _("custom"), wxDefaultPosition, size);
152         _scale_custom_edit = new Button (this, _("Edit..."));
153
154         _filters_label = create_label (this, _("Filters"), true);
155         _filters = new StaticText (this, _("None"), wxDefaultPosition, size);
156         _filters_button = new Button (this, _("Edit..."));
157
158         _colour_conversion_label = create_label (this, _("Colour conversion"), true);
159         _colour_conversion = new wxChoice (this, wxID_ANY, wxDefaultPosition, size);
160         _colour_conversion->Append (_("None"));
161         BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all()) {
162                 _colour_conversion->Append (std_to_wx (i.name));
163         }
164
165         /// TRANSLATORS: translate the word "Custom" here; do not include the "Colour|" prefix
166         _colour_conversion->Append (S_("Colour|Custom"));
167         _edit_colour_conversion_button = new Button (this, _("Edit..."));
168
169         _range_label = create_label (this, _("Range"), true);
170         _range = new wxChoice (this, wxID_ANY);
171         _range->Append (_("Full (JPEG, 0-255)"));
172         _range->Append (_("Video (MPEG, 16-235)"));
173
174         _description = new StaticText (this, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
175         _description->SetFont(font);
176
177         _left_crop->wrapped()->SetRange (0, 4096);
178         _top_crop->wrapped()->SetRange (0, 4096);
179         _right_crop->wrapped()->SetRange (0, 4096);
180         _bottom_crop->wrapped()->SetRange (0, 4096);
181
182         _frame_type->wrapped()->Append (_("2D"));
183         _frame_type->wrapped()->Append (_("3D"));
184         _frame_type->wrapped()->Append (_("3D left/right"));
185         _frame_type->wrapped()->Append (_("3D top/bottom"));
186         _frame_type->wrapped()->Append (_("3D alternate"));
187         _frame_type->wrapped()->Append (_("3D left only"));
188         _frame_type->wrapped()->Append (_("3D right only"));
189
190         content_selection_changed ();
191
192         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
193         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
194
195         _reference->Bind                     (wxEVT_CHECKBOX, boost::bind (&VideoPanel::reference_clicked, this));
196         _filters_button->Bind                (wxEVT_BUTTON,   boost::bind (&VideoPanel::edit_filters_clicked, this));
197         _scale_fit->Bind                     (wxEVT_RADIOBUTTON, boost::bind (&VideoPanel::scale_fit_clicked, this));
198         _scale_custom->Bind                  (wxEVT_RADIOBUTTON, boost::bind (&VideoPanel::scale_custom_clicked, this));
199         _scale_custom_edit->Bind             (wxEVT_BUTTON,   boost::bind (&VideoPanel::scale_custom_edit_clicked, this));
200         _colour_conversion->Bind             (wxEVT_CHOICE,   boost::bind (&VideoPanel::colour_conversion_changed, this));
201         _range->Bind                         (wxEVT_CHOICE,   boost::bind (&VideoPanel::range_changed, this));
202         _edit_colour_conversion_button->Bind (wxEVT_BUTTON,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
203         _left_right_link->Bind               (wxEVT_TOGGLEBUTTON, boost::bind(&VideoPanel::left_right_link_clicked, this));
204         _top_bottom_link->Bind               (wxEVT_TOGGLEBUTTON, boost::bind(&VideoPanel::top_bottom_link_clicked, this));
205
206         add_to_grid ();
207 }
208
209 void
210 VideoPanel::add_to_grid ()
211 {
212         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
213
214         int r = 0;
215
216         _reference->Show (full);
217         _reference_note->Show (full && !_reference_note->GetLabel().IsEmpty());
218
219         if (full) {
220                 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
221                 reference_sizer->Add (_reference, 0);
222                 reference_sizer->Add (_reference_note, 0);
223                 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 3));
224                 ++r;
225         }
226
227         add_label_to_sizer (_grid, _type_label, true, wxGBPosition(r, 0));
228         _frame_type->add (_grid, wxGBPosition(r, 1), wxGBSpan(1, 2));
229         ++r;
230
231         int cr = 0;
232         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
233         add_label_to_sizer (crop, _left_crop_label, true, wxGBPosition (cr, 0));
234         _left_crop->add (crop, wxGBPosition (cr, 1));
235         crop->Add (_left_right_link, wxGBPosition(cr, 2));
236         add_label_to_sizer (crop, _right_crop_label, true, wxGBPosition (cr, 3));
237         _right_crop->add (crop, wxGBPosition (cr, 4));
238         ++cr;
239         add_label_to_sizer (crop, _top_crop_label, true, wxGBPosition (cr, 0));
240         _top_crop->add (crop, wxGBPosition (cr, 1));
241         crop->Add (_top_bottom_link, wxGBPosition(cr, 2));
242         add_label_to_sizer (crop, _bottom_crop_label, true, wxGBPosition (cr, 3));
243         _bottom_crop->add (crop, wxGBPosition (cr, 4));
244         add_label_to_sizer (_grid, _crop_label, true, wxGBPosition(r, 0));
245         _grid->Add (crop, wxGBPosition(r, 1));
246         ++r;
247
248         _scale_label->Show (full);
249         _scale_fit->Show (full);
250         _scale_custom->Show (full);
251         _scale_custom_edit->Show (full);
252         _filters_label->Show (full);
253         _filters->Show (full);
254         _filters_button->Show (full);
255         _colour_conversion_label->Show (full);
256         _colour_conversion->Show (full);
257         _edit_colour_conversion_button->Show (full);
258         _range_label->Show (full);
259         _range->Show (full);
260
261         add_label_to_sizer (_grid, _fade_in_label, true, wxGBPosition (r, 0));
262         _grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
263         ++r;
264
265         add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition (r, 0));
266         _grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
267         ++r;
268
269         if (full) {
270                 add_label_to_sizer (_grid, _scale_label, true, wxGBPosition (r, 0));
271                 {
272                         wxSizer* v = new wxBoxSizer (wxVERTICAL);
273                         v->Add (_scale_fit, 0, wxBOTTOM, 4);
274                         wxSizer* h = new wxBoxSizer (wxHORIZONTAL);
275                         h->Add (_scale_custom, 1, wxRIGHT, 6);
276                         h->Add (_scale_custom_edit, 0);
277                         v->Add (h, 0);
278                         _grid->Add (v, wxGBPosition(r, 1));
279                 }
280                 ++r;
281
282                 add_label_to_sizer (_grid, _filters_label, true, wxGBPosition (r, 0));
283                 {
284                         wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
285                         s->Add (_filters, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
286                         s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
287                         _grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
288                 }
289                 ++r;
290
291                 add_label_to_sizer (_grid, _colour_conversion_label, true, wxGBPosition(r, 0));
292                 {
293                         wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
294                         s->Add (_colour_conversion, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
295                         s->Add (_edit_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
296                         _grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
297                 }
298                 ++r;
299
300                 add_label_to_sizer (_grid, _range_label, true, wxGBPosition(r, 0));
301                 _grid->Add (_range, wxGBPosition(r, 1), wxGBSpan(1, 2), wxALIGN_CENTER_VERTICAL);
302                 ++r;
303         }
304
305         _grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL, 6);
306         ++r;
307 }
308
309 void
310 VideoPanel::range_changed ()
311 {
312         ContentList vc = _parent->selected_video ();
313         if (vc.size() != 1) {
314                 return;
315         }
316
317         switch (_range->GetSelection()) {
318         case 0:
319                 vc.front()->video->set_range (VIDEO_RANGE_FULL);
320                 break;
321         case 1:
322                 vc.front()->video->set_range (VIDEO_RANGE_VIDEO);
323                 break;
324         default:
325                 DCPOMATIC_ASSERT (false);
326         }
327 }
328
329
330 void
331 VideoPanel::film_changed (Film::Property property)
332 {
333         switch (property) {
334         case Film::VIDEO_FRAME_RATE:
335         case Film::CONTAINER:
336         case Film::RESOLUTION:
337                 setup_description ();
338                 setup_sensitivity ();
339                 break;
340         case Film::REEL_TYPE:
341         case Film::INTEROP:
342                 setup_sensitivity ();
343                 break;
344         default:
345                 break;
346         }
347 }
348
349 std::size_t
350 hash_value (boost::optional<ColourConversion> const & c)
351 {
352         boost::hash<string> hasher;
353         if (!c) {
354                 return hasher ("none");
355         }
356         return hasher (c->identifier());
357 }
358
359
360 void
361 VideoPanel::film_content_changed (int property)
362 {
363         ContentList vc = _parent->selected_video ();
364         shared_ptr<Content> vcs;
365         shared_ptr<FFmpegContent> fcs;
366         if (!vc.empty ()) {
367                 vcs = vc.front ();
368                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
369         }
370
371         if (property == ContentProperty::VIDEO_FRAME_RATE ||
372             property == VideoContentProperty::FRAME_TYPE ||
373             property == VideoContentProperty::CROP ||
374             property == VideoContentProperty::SCALE) {
375                 setup_description ();
376         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
377                 boost::unordered_set<optional<ColourConversion> > check;
378                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
379                         check.insert (i->video->colour_conversion());
380                 }
381
382                 /* Remove any "Many" entry that we might have added previously.  There should
383                  * be entries for each preset plus one for "None" and one for "Custom".
384                  */
385                 vector<PresetColourConversion> cc = PresetColourConversion::all ();
386                 if (_colour_conversion->GetCount() > cc.size() + 2) {
387                         _colour_conversion->Delete (_colour_conversion->GetCount() - 1);
388                 }
389
390                 if (check.size() == 1) {
391                         if (vcs && vcs->video->colour_conversion ()) {
392                                 optional<size_t> preset = vcs->video->colour_conversion().get().preset ();
393                                 if (preset) {
394                                         checked_set (_colour_conversion, preset.get() + 1);
395                                 } else {
396                                         checked_set (_colour_conversion, cc.size() + 1);
397                                 }
398                         } else {
399                                 checked_set (_colour_conversion, 0);
400                         }
401                 } else if (check.size() > 1) {
402                         /* Add a "many" entry and select it as an indication that multiple different
403                          * colour conversions are present in the selection.
404                          */
405                         _colour_conversion->Append (_("Many"));
406                         checked_set (_colour_conversion, _colour_conversion->GetCount() - 1);
407                 }
408
409                 setup_sensitivity ();
410
411         } else if (property == FFmpegContentProperty::FILTERS) {
412                 if (fcs) {
413                         string p = Filter::ffmpeg_string (fcs->filters ());
414                         if (p.empty ()) {
415                                 checked_set (_filters, _("None"));
416                         } else {
417                                 if (p.length() > 25) {
418                                         p = p.substr (0, 25) + "...";
419                                 }
420                                 checked_set (_filters, p);
421                         }
422                 }
423         } else if (property == VideoContentProperty::USE) {
424                 setup_sensitivity ();
425         } else if (property == VideoContentProperty::FADE_IN) {
426                 set<Frame> check;
427                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
428                         check.insert (i->video->fade_in ());
429                 }
430
431                 if (check.size() == 1) {
432                         _fade_in->set (
433                                 ContentTime::from_frames (vc.front()->video->fade_in(), vc.front()->active_video_frame_rate(_parent->film())),
434                                 vc.front()->active_video_frame_rate(_parent->film())
435                                 );
436                 } else {
437                         _fade_in->clear ();
438                 }
439         } else if (property == VideoContentProperty::FADE_OUT) {
440                 set<Frame> check;
441                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
442                         check.insert (i->video->fade_out ());
443                 }
444
445                 if (check.size() == 1) {
446                         _fade_out->set (
447                                 ContentTime::from_frames (vc.front()->video->fade_out(), vc.front()->active_video_frame_rate(_parent->film())),
448                                 vc.front()->active_video_frame_rate(_parent->film())
449                                 );
450                 } else {
451                         _fade_out->clear ();
452                 }
453         } else if (property == DCPContentProperty::REFERENCE_VIDEO) {
454                 if (vc.size() == 1) {
455                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (vc.front ());
456                         checked_set (_reference, dcp ? dcp->reference_video () : false);
457                 } else {
458                         checked_set (_reference, false);
459                 }
460
461                 setup_sensitivity ();
462         } else if (property == VideoContentProperty::RANGE) {
463                 if (vcs) {
464                         checked_set (_range, vcs->video->range() == VIDEO_RANGE_FULL ? 0 : 1);
465                 } else {
466                         checked_set (_range, 0);
467                 }
468
469                 setup_sensitivity ();
470         } else if (property == VideoContentProperty::CUSTOM_RATIO || property == VideoContentProperty::CUSTOM_SIZE) {
471                 set<Frame> check;
472                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
473                         check.insert (i->video->custom_ratio() || i->video->custom_size());
474                 }
475
476                 if (check.size() == 1) {
477                         checked_set (_scale_fit, !vc.front()->video->custom_ratio() && !vc.front()->video->custom_size());
478                         checked_set (_scale_custom, vc.front()->video->custom_ratio() || vc.front()->video->custom_size());
479                 } else {
480                         checked_set (_scale_fit, true);
481                         checked_set (_scale_custom, false);
482                 }
483                 setup_sensitivity ();
484         }
485 }
486
487 /** Called when the `Edit filters' button has been clicked */
488 void
489 VideoPanel::edit_filters_clicked ()
490 {
491         FFmpegContentList c = _parent->selected_ffmpeg ();
492         if (c.size() != 1) {
493                 return;
494         }
495
496         FilterDialog* d = new FilterDialog (this, c.front()->filters());
497         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
498         d->ShowModal ();
499         d->Destroy ();
500 }
501
502 void
503 VideoPanel::setup_description ()
504 {
505         ContentList vc = _parent->selected_video ();
506         if (vc.empty ()) {
507                 checked_set (_description, wxT (""));
508                 return;
509         } else if (vc.size() > 1) {
510                 checked_set (_description, _("Multiple content selected"));
511                 return;
512         }
513
514         string d = vc.front()->video->processing_description (_parent->film());
515         size_t lines = count (d.begin(), d.end(), '\n');
516
517         for (int i = lines; i < 6; ++i) {
518                 d += "\n ";
519         }
520
521         checked_set (_description, d);
522         _sizer->Layout ();
523 }
524
525 void
526 VideoPanel::colour_conversion_changed ()
527 {
528         ContentList vc = _parent->selected_video ();
529
530         int const s = _colour_conversion->GetSelection ();
531         vector<PresetColourConversion> all = PresetColourConversion::all ();
532
533         if (s == int(all.size() + 1)) {
534                 edit_colour_conversion_clicked ();
535         } else {
536                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
537                         if (s == 0) {
538                                 i->video->unset_colour_conversion ();
539                         } else if (s != int(all.size() + 2)) {
540                                 i->video->set_colour_conversion (all[s - 1].conversion);
541                         }
542                 }
543         }
544 }
545
546 void
547 VideoPanel::edit_colour_conversion_clicked ()
548 {
549         ContentList vc = _parent->selected_video ();
550
551         ContentColourConversionDialog* d = new ContentColourConversionDialog (this, vc.front()->video->yuv ());
552         d->set (vc.front()->video->colour_conversion().get_value_or (PresetColourConversion::all().front().conversion));
553         if (d->ShowModal() == wxID_OK) {
554                 BOOST_FOREACH (shared_ptr<Content> i, vc) {
555                         i->video->set_colour_conversion (d->get ());
556                 }
557         } else {
558                 /* Reset the colour conversion choice */
559                 film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
560         }
561         d->Destroy ();
562 }
563
564 void
565 VideoPanel::content_selection_changed ()
566 {
567         ContentList video_sel = _parent->selected_video ();
568
569         _frame_type->set_content (video_sel);
570         _left_crop->set_content (video_sel);
571         _right_crop->set_content (video_sel);
572         _top_crop->set_content (video_sel);
573         _bottom_crop->set_content (video_sel);
574
575         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
576         film_content_changed (VideoContentProperty::CROP);
577         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
578         film_content_changed (VideoContentProperty::FADE_IN);
579         film_content_changed (VideoContentProperty::FADE_OUT);
580         film_content_changed (VideoContentProperty::RANGE);
581         film_content_changed (VideoContentProperty::USE);
582         film_content_changed (VideoContentProperty::CUSTOM_RATIO);
583         film_content_changed (VideoContentProperty::CUSTOM_SIZE);
584         film_content_changed (FFmpegContentProperty::FILTERS);
585         film_content_changed (DCPContentProperty::REFERENCE_VIDEO);
586
587         setup_sensitivity ();
588 }
589
590 void
591 VideoPanel::setup_sensitivity ()
592 {
593         ContentList sel = _parent->selected ();
594
595         shared_ptr<DCPContent> dcp;
596         if (sel.size() == 1) {
597                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
598         }
599
600         string why_not;
601         bool const can_reference = dcp && dcp->can_reference_video (_parent->film(), why_not);
602         wxString cannot;
603         if (why_not.empty()) {
604                 cannot = _("Cannot reference this DCP's video.");
605         } else {
606                 cannot = _("Cannot reference this DCP's video: ") + std_to_wx(why_not);
607         }
608         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
609
610         bool any_use = false;
611         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
612                 if (i->video && i->video->use()) {
613                         any_use = true;
614                 }
615         }
616
617         bool const enable = !_reference->GetValue() && any_use;
618
619         if (!enable) {
620                 _frame_type->wrapped()->Enable (false);
621                 _left_crop->wrapped()->Enable (false);
622                 _right_crop->wrapped()->Enable (false);
623                 _top_crop->wrapped()->Enable (false);
624                 _bottom_crop->wrapped()->Enable (false);
625                 _fade_in->Enable (false);
626                 _fade_out->Enable (false);
627                 _scale_fit->Enable (false);
628                 _scale_custom->Enable (false);
629                 _scale_custom_edit->Enable (false);
630                 _description->Enable (false);
631                 _filters->Enable (false);
632                 _filters_button->Enable (false);
633                 _colour_conversion->Enable (false);
634                 _range->Enable (false);
635         } else {
636                 ContentList video_sel = _parent->selected_video ();
637                 FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
638                 bool const single = video_sel.size() == 1;
639
640                 _frame_type->wrapped()->Enable (true);
641                 _left_crop->wrapped()->Enable (true);
642                 _right_crop->wrapped()->Enable (true);
643                 _top_crop->wrapped()->Enable (true);
644                 _bottom_crop->wrapped()->Enable (true);
645                 _fade_in->Enable (!video_sel.empty ());
646                 _fade_out->Enable (!video_sel.empty ());
647                 _scale_fit->Enable (true);
648                 _scale_custom->Enable (true);
649                 _scale_custom_edit->Enable (_scale_custom->GetValue());
650                 _description->Enable (true);
651                 _filters->Enable (true);
652                 _filters_button->Enable (single && !ffmpeg_sel.empty ());
653                 _colour_conversion->Enable (!video_sel.empty());
654                 _range->Enable (single && !video_sel.empty());
655         }
656
657         ContentList vc = _parent->selected_video ();
658         shared_ptr<Content> vcs;
659         if (!vc.empty ()) {
660                 vcs = vc.front ();
661         }
662
663         if (vcs && vcs->video->colour_conversion ()) {
664                 _edit_colour_conversion_button->Enable (!vcs->video->colour_conversion().get().preset());
665         } else {
666                 _edit_colour_conversion_button->Enable (false);
667         }
668 }
669
670 void
671 VideoPanel::fade_in_changed ()
672 {
673         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video ()) {
674                 double const vfr = i->active_video_frame_rate (_parent->film());
675                 i->video->set_fade_in (_fade_in->get(vfr).frames_round(vfr));
676         }
677 }
678
679 void
680 VideoPanel::fade_out_changed ()
681 {
682         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video ()) {
683                 double const vfr = i->active_video_frame_rate (_parent->film());
684                 i->video->set_fade_out (_fade_out->get(vfr).frames_round(vfr));
685         }
686 }
687
688
689 void
690 VideoPanel::reference_clicked ()
691 {
692         ContentList c = _parent->selected ();
693         if (c.size() != 1) {
694                 return;
695         }
696
697         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
698         if (!d) {
699                 return;
700         }
701
702         d->set_reference_video (_reference->GetValue ());
703 }
704
705
706 void
707 VideoPanel::scale_fit_clicked ()
708 {
709         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
710                 i->video->set_custom_ratio (optional<float>());
711         }
712 }
713
714
715 void
716 VideoPanel::scale_custom_clicked ()
717 {
718         if (!scale_custom_edit_clicked()) {
719                 _scale_fit->SetValue (true);
720         }
721 }
722
723
724 bool
725 VideoPanel::scale_custom_edit_clicked ()
726 {
727         shared_ptr<const VideoContent> vc = _parent->selected_video().front()->video;
728         CustomScaleDialog* d = new CustomScaleDialog (this, vc->size(), _parent->film()->frame_size(), vc->custom_ratio(), vc->custom_size());
729         int const r = d->ShowModal ();
730         if (r == wxID_OK) {
731                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
732                         i->video->set_custom_ratio (d->custom_ratio());
733                         i->video->set_custom_size (d->custom_size());
734                 }
735         }
736         d->Destroy ();
737         return r == wxID_OK;
738 }
739
740
741 void
742 VideoPanel::left_right_link_clicked ()
743 {
744         right_crop_changed ();
745 }
746
747
748 void
749 VideoPanel::top_bottom_link_clicked ()
750 {
751         bottom_crop_changed ();
752 }
753
754
755 void
756 VideoPanel::left_crop_changed ()
757 {
758         if (_left_right_link->GetValue()) {
759                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
760                         i->video->set_right_crop (i->video->left_crop());
761                 }
762         }
763 }
764
765
766 void
767 VideoPanel::right_crop_changed ()
768 {
769         if (_left_right_link->GetValue()) {
770                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
771                         i->video->set_left_crop (i->video->right_crop());
772                 }
773         }
774 }
775
776
777 void
778 VideoPanel::top_crop_changed ()
779 {
780         if (_top_bottom_link->GetValue()) {
781                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
782                         i->video->set_bottom_crop (i->video->top_crop());
783                 }
784         }
785 }
786
787
788 void
789 VideoPanel::bottom_crop_changed ()
790 {
791         if (_top_bottom_link->GetValue()) {
792                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video()) {
793                         i->video->set_top_crop (i->video->bottom_crop());
794                 }
795         }
796 }
797
798
799