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