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