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