3e121054326473fd4017285f1cb23868cfcef1cc
[dcpomatic.git] / src / wx / dcp_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 "audio_dialog.h"
23 #include "check_box.h"
24 #include "check_box.h"
25 #include "dcp_panel.h"
26 #include "dcpomatic_button.h"
27 #include "dcpomatic_choice.h"
28 #include "dcpomatic_spin_ctrl.h"
29 #include "focus_manager.h"
30 #include "interop_metadata_dialog.h"
31 #include "language_tag_dialog.h"
32 #include "markers_dialog.h"
33 #include "smpte_metadata_dialog.h"
34 #include "static_text.h"
35 #include "wx_util.h"
36 #include "lib/audio_content.h"
37 #include "lib/audio_processor.h"
38 #include "lib/config.h"
39 #include "lib/dcp_content.h"
40 #include "lib/dcp_content_type.h"
41 #include "lib/ffmpeg_content.h"
42 #include "lib/film.h"
43 #include "lib/ratio.h"
44 #include "lib/text_content.h"
45 #include "lib/util.h"
46 #include "lib/video_content.h"
47 #include <dcp/locale_convert.h>
48 #include <dcp/warnings.h>
49 LIBDCP_DISABLE_WARNINGS
50 #include <wx/gbsizer.h>
51 #include <wx/notebook.h>
52 #include <wx/spinctrl.h>
53 #include <wx/wx.h>
54 LIBDCP_ENABLE_WARNINGS
55 #include <boost/lexical_cast.hpp>
56
57
58 using std::list;
59 using std::make_pair;
60 using std::max;
61 using std::pair;
62 using std::shared_ptr;
63 using std::string;
64 using std::vector;
65 using std::weak_ptr;
66 using boost::lexical_cast;
67 #if BOOST_VERSION >= 106100
68 using namespace boost::placeholders;
69 #endif
70 using dcp::locale_convert;
71
72
73 DCPPanel::DCPPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
74         : _film (film)
75         , _viewer (viewer)
76         , _generally_sensitive (true)
77 {
78         _panel = new wxPanel (n);
79         _sizer = new wxBoxSizer (wxVERTICAL);
80         _panel->SetSizer (_sizer);
81
82         _grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
83         _sizer->Add (_grid, 0, wxEXPAND | wxALL, 8);
84
85         _name_label = create_label (_panel, _("Name"), true);
86         _name = new wxTextCtrl (_panel, wxID_ANY);
87         FocusManager::instance()->add(_name);
88
89         _use_isdcf_name = new CheckBox (_panel, _("Use ISDCF name"));
90         _copy_isdcf_name_button = new Button (_panel, _("Copy as name"));
91
92         /* wxST_ELLIPSIZE_MIDDLE works around a bug in GTK2 and/or wxWidgets, see
93            http://trac.wxwidgets.org/ticket/12539
94         */
95         _dcp_name = new StaticText (
96                 _panel, wxT (""), wxDefaultPosition, wxDefaultSize,
97                 wxALIGN_CENTRE_HORIZONTAL | wxST_NO_AUTORESIZE | wxST_ELLIPSIZE_MIDDLE
98                 );
99
100         _enable_audio_language = new CheckBox(_panel, _("Audio language"));
101         _audio_language = new wxStaticText (_panel, wxID_ANY, wxT(""));
102         _edit_audio_language = new Button (_panel, _("Edit..."));
103
104         _dcp_content_type_label = create_label (_panel, _("Content Type"), true);
105         _dcp_content_type = new Choice(_panel);
106
107         _encrypted = new CheckBox (_panel, _("Encrypted"));
108
109         wxClientDC dc (_panel);
110         auto size = dc.GetTextExtent (wxT ("GGGGGGGG..."));
111         size.SetHeight (-1);
112
113         _reels_label = create_label (_panel, _("Reels"), true);
114         _reel_type = new Choice(_panel);
115
116         _reel_length_label = create_label (_panel, _("Reel length"), true);
117         _reel_length = new SpinCtrl (_panel, DCPOMATIC_SPIN_CTRL_WIDTH);
118         _reel_length_gb_label = create_label (_panel, _("GB"), false);
119
120         _standard_label = create_label (_panel, _("Standard"), true);
121         _standard = new Choice(_panel);
122
123         _markers = new Button (_panel, _("Markers..."));
124         _metadata = new Button (_panel, _("Metadata..."));
125
126         _notebook = new wxNotebook (_panel, wxID_ANY);
127         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
128
129         _notebook->AddPage (make_video_panel (), _("Video"), false);
130         _notebook->AddPage (make_audio_panel (), _("Audio"), false);
131
132         _name->Bind                  (wxEVT_TEXT,     boost::bind(&DCPPanel::name_changed, this));
133         _use_isdcf_name->bind(&DCPPanel::use_isdcf_name_toggled, this);
134         _copy_isdcf_name_button->Bind(wxEVT_BUTTON,   boost::bind(&DCPPanel::copy_isdcf_name_button_clicked, this));
135         _dcp_content_type->Bind      (wxEVT_CHOICE,   boost::bind(&DCPPanel::dcp_content_type_changed, this));
136         _encrypted->bind(&DCPPanel::encrypted_toggled, this);
137         _reel_type->Bind             (wxEVT_CHOICE,   boost::bind(&DCPPanel::reel_type_changed, this));
138         _reel_length->Bind           (wxEVT_SPINCTRL, boost::bind(&DCPPanel::reel_length_changed, this));
139         _standard->Bind              (wxEVT_CHOICE,   boost::bind(&DCPPanel::standard_changed, this));
140         _markers->Bind               (wxEVT_BUTTON,   boost::bind(&DCPPanel::markers_clicked, this));
141         _metadata->Bind              (wxEVT_BUTTON,   boost::bind(&DCPPanel::metadata_clicked, this));
142         _enable_audio_language->bind(&DCPPanel::enable_audio_language_toggled, this);
143         _edit_audio_language->Bind   (wxEVT_BUTTON,   boost::bind(&DCPPanel::edit_audio_language_clicked, this));
144
145         for (auto i: DCPContentType::all()) {
146                 _dcp_content_type->add(i->pretty_name());
147         }
148
149         _reel_type->add(_("Single reel"));
150         _reel_type->add(_("Split by video content"));
151         /// TRANSLATORS: translate the word "Custom" here; do not include the "Reel|" prefix
152         _reel_type->add(S_("Reel|Custom"));
153         _reel_type->SetToolTip(_("How the DCP should be split into parts internally.  If in doubt, choose 'Single reel'"));
154
155         _reel_length->SetRange (1, 64);
156
157         add_standards();
158         _standard->SetToolTip(_("Which standard the DCP should use.  Interop is older and SMPTE is the modern standard.  If in doubt, choose 'SMPTE'"));
159
160         Config::instance()->Changed.connect (boost::bind(&DCPPanel::config_changed, this, _1));
161
162         add_to_grid ();
163 }
164
165
166 void
167 DCPPanel::add_standards()
168 {
169         _standard->add(_("SMPTE"), N_("smpte"));
170         if (Config::instance()->allow_smpte_bv20() || (_film && _film->limit_to_smpte_bv20())) {
171                 _standard->add(_("SMPTE (Bv2.0 only)"), N_("smpte-bv20"));
172         }
173         _standard->add(_("Interop"), N_("interop"));
174 }
175
176
177 void
178 DCPPanel::set_standard()
179 {
180         DCPOMATIC_ASSERT(_film);
181         DCPOMATIC_ASSERT(!_film->limit_to_smpte_bv20() || _standard->GetCount() == 3);
182
183         if (_film->interop()) {
184                 checked_set(_standard, "interop");
185         } else {
186                 checked_set(_standard, _film->limit_to_smpte_bv20() ? "smpte-bv20" : "smpte");
187         }
188 }
189
190
191 void
192 DCPPanel::standard_changed ()
193 {
194         if (!_film || !_standard->get()) {
195                 return;
196         }
197
198         auto const data = _standard->get_data();
199         if (!data) {
200                 return;
201         }
202
203         if (*data == N_("interop")) {
204                 _film->set_interop(true);
205                 _film->set_limit_to_smpte_bv20(false);
206         } else if (*data == N_("smpte")) {
207                 _film->set_interop(false);
208                 _film->set_limit_to_smpte_bv20(false);
209         } else if (*data == N_("smpte-bv20")) {
210                 _film->set_interop(false);
211                 _film->set_limit_to_smpte_bv20(true);
212         }
213 }
214
215
216 void
217 DCPPanel::add_to_grid ()
218 {
219         int r = 0;
220
221         auto name_sizer = new wxBoxSizer (wxHORIZONTAL);
222         name_sizer->Add (_name_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
223         name_sizer->Add (_name, 1, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
224         _grid->Add (name_sizer, wxGBPosition(r, 0), wxGBSpan(1, 2), wxRIGHT | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
225         ++r;
226
227         int flags = wxALIGN_CENTER_VERTICAL;
228 #ifdef __WXOSX__
229         flags |= wxALIGN_RIGHT;
230 #endif
231
232         _grid->Add (_use_isdcf_name, wxGBPosition(r, 0), wxDefaultSpan, flags);
233         {
234                 auto s = new wxBoxSizer (wxHORIZONTAL);
235                 s->Add (_copy_isdcf_name_button, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
236                 _grid->Add (s, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxBOTTOM, DCPOMATIC_CHECKBOX_BOTTOM_PAD);
237         }
238         ++r;
239
240         _grid->Add (_dcp_name, wxGBPosition(r, 0), wxGBSpan(1, 2), wxALIGN_CENTER_VERTICAL | wxEXPAND);
241         ++r;
242
243         {
244                 auto s = new wxBoxSizer (wxHORIZONTAL);
245                 s->Add (_enable_audio_language, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
246                 s->Add (_audio_language, 1, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_CHECKBOX_BOTTOM_PAD);
247                 s->Add (_edit_audio_language, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_CHECKBOX_BOTTOM_PAD);
248                 _grid->Add (s, wxGBPosition(r, 0), wxGBSpan(1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL);
249         }
250         ++r;
251
252         add_label_to_sizer (_grid, _dcp_content_type_label, true, wxGBPosition(r, 0));
253         _grid->Add (_dcp_content_type, wxGBPosition(r, 1));
254         ++r;
255
256         _grid->Add (_encrypted, wxGBPosition(r, 0), wxGBSpan(1, 2));
257         ++r;
258
259         add_label_to_sizer (_grid, _reels_label, true, wxGBPosition(r, 0));
260         _grid->Add (_reel_type, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
261         ++r;
262
263         add_label_to_sizer (_grid, _reel_length_label, true, wxGBPosition(r, 0));
264         {
265                 auto s = new wxBoxSizer (wxHORIZONTAL);
266                 s->Add (_reel_length);
267                 add_label_to_sizer (s, _reel_length_gb_label, false, 0, wxLEFT | wxALIGN_CENTER_VERTICAL);
268                 _grid->Add (s, wxGBPosition(r, 1));
269         }
270         ++r;
271
272         add_label_to_sizer (_grid, _standard_label, true, wxGBPosition(r, 0));
273         _grid->Add (_standard, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
274         ++r;
275
276         auto extra = new wxBoxSizer (wxHORIZONTAL);
277         extra->Add (_markers, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
278         extra->Add (_metadata, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
279         _grid->Add (extra, wxGBPosition(r, 0), wxGBSpan(1, 2));
280         ++r;
281 }
282
283
284 void
285 DCPPanel::name_changed ()
286 {
287         if (!_film) {
288                 return;
289         }
290
291         _film->set_name (string(_name->GetValue().mb_str()));
292 }
293
294
295 void
296 DCPPanel::j2k_bandwidth_changed ()
297 {
298         if (!_film) {
299                 return;
300         }
301
302         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1000000);
303 }
304
305
306 void
307 DCPPanel::encrypted_toggled ()
308 {
309         if (!_film) {
310                 return;
311         }
312
313         _film->set_encrypted (_encrypted->GetValue());
314 }
315
316
317 /** Called when the frame rate choice widget has been changed */
318 void
319 DCPPanel::frame_rate_choice_changed ()
320 {
321         if (!_film || !_frame_rate_choice->get()) {
322                 return;
323         }
324
325         _film->set_video_frame_rate (
326                 boost::lexical_cast<int>(
327                         wx_to_std(_frame_rate_choice->GetString(*_frame_rate_choice->get()))
328                         ),
329                 true
330                 );
331 }
332
333
334 /** Called when the frame rate spin widget has been changed */
335 void
336 DCPPanel::frame_rate_spin_changed ()
337 {
338         if (!_film) {
339                 return;
340         }
341
342         _film->set_video_frame_rate (_frame_rate_spin->GetValue());
343 }
344
345
346 void
347 DCPPanel::audio_channels_changed ()
348 {
349         if (!_film) {
350                 return;
351         }
352
353         _film->set_audio_channels(locale_convert<int>(string_client_data(_audio_channels->GetClientObject(*_audio_channels->get()))));
354 }
355
356
357 void
358 DCPPanel::resolution_changed ()
359 {
360         if (!_film || !_resolution->get()) {
361                 return;
362         }
363
364         _film->set_resolution(*_resolution->get() == 0 ? Resolution::TWO_K : Resolution::FOUR_K);
365 }
366
367
368 void
369 DCPPanel::markers_clicked ()
370 {
371         _markers_dialog.reset(_panel, _film, _viewer);
372         _markers_dialog->Show();
373 }
374
375
376 void
377 DCPPanel::metadata_clicked ()
378 {
379         if (_film->interop()) {
380                 _interop_metadata_dialog.reset(_panel, _film);
381                 _interop_metadata_dialog->setup ();
382                 _interop_metadata_dialog->Show ();
383         } else {
384                 _smpte_metadata_dialog.reset(_panel, _film);
385                 _smpte_metadata_dialog->setup ();
386                 _smpte_metadata_dialog->Show ();
387         }
388 }
389
390
391 void
392 DCPPanel::film_changed (Film::Property p)
393 {
394         switch (p) {
395         case Film::Property::NONE:
396                 break;
397         case Film::Property::CONTAINER:
398                 setup_container ();
399                 break;
400         case Film::Property::NAME:
401                 checked_set (_name, _film->name());
402                 setup_dcp_name ();
403                 break;
404         case Film::Property::DCP_CONTENT_TYPE:
405         {
406                 auto index = DCPContentType::as_index(_film->dcp_content_type());
407                 DCPOMATIC_ASSERT (index);
408                 checked_set (_dcp_content_type, *index);
409                 setup_dcp_name ();
410                 break;
411         }
412         case Film::Property::ENCRYPTED:
413                 checked_set (_encrypted, _film->encrypted ());
414                 break;
415         case Film::Property::RESOLUTION:
416                 checked_set (_resolution, _film->resolution() == Resolution::TWO_K ? 0 : 1);
417                 setup_container ();
418                 setup_dcp_name ();
419                 break;
420         case Film::Property::J2K_BANDWIDTH:
421                 checked_set (_j2k_bandwidth, _film->j2k_bandwidth() / 1000000);
422                 break;
423         case Film::Property::USE_ISDCF_NAME:
424         {
425                 checked_set (_use_isdcf_name, _film->use_isdcf_name());
426                 if (_film->use_isdcf_name()) {
427                         /* We are going back to using an ISDCF name.  Remove anything after a _ in the current name,
428                            in case the user has clicked 'Copy as name' then re-ticked 'Use ISDCF name' (#1513).
429                         */
430                         string const name = _film->name ();
431                         string::size_type const u = name.find("_");
432                         if (u != string::npos) {
433                                 _film->set_name (name.substr(0, u));
434                         }
435                 }
436                 setup_dcp_name ();
437                 break;
438         }
439         case Film::Property::VIDEO_FRAME_RATE:
440         {
441                 bool done = false;
442                 for (unsigned int i = 0; i < _frame_rate_choice->GetCount(); ++i) {
443                         if (wx_to_std(_frame_rate_choice->GetString(i)) == boost::lexical_cast<string>(_film->video_frame_rate())) {
444                                 checked_set (_frame_rate_choice, i);
445                                 done = true;
446                                 break;
447                         }
448                 }
449
450                 if (!done) {
451                         checked_set (_frame_rate_choice, -1);
452                 }
453
454                 checked_set (_frame_rate_spin, _film->video_frame_rate ());
455
456                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
457                 setup_dcp_name ();
458                 break;
459         }
460         case Film::Property::AUDIO_CHANNELS:
461                 if (_film->audio_channels() < minimum_allowed_audio_channels()) {
462                         _film->set_audio_channels (minimum_allowed_audio_channels());
463                 } else {
464                         checked_set (_audio_channels, locale_convert<string>(max(minimum_allowed_audio_channels(), _film->audio_channels())));
465                         setup_dcp_name ();
466                 }
467                 break;
468         case Film::Property::THREE_D:
469                 checked_set (_three_d, _film->three_d());
470                 setup_dcp_name ();
471                 break;
472         case Film::Property::REENCODE_J2K:
473                 checked_set (_reencode_j2k, _film->reencode_j2k());
474                 break;
475         case Film::Property::INTEROP:
476                 set_standard();
477                 setup_dcp_name ();
478                 _markers->Enable (!_film->interop());
479                 break;
480         case Film::Property::LIMIT_TO_SMPTE_BV20:
481                 set_standard();
482                 break;
483         case Film::Property::AUDIO_PROCESSOR:
484                 if (_film->audio_processor()) {
485                         checked_set (_audio_processor, _film->audio_processor()->id());
486                 } else {
487                         checked_set (_audio_processor, 0);
488                 }
489                 setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels());
490                 film_changed (Film::Property::AUDIO_CHANNELS);
491                 break;
492         case Film::Property::REEL_TYPE:
493                 checked_set (_reel_type, static_cast<int>(_film->reel_type()));
494                 _reel_length->Enable (_film->reel_type() == ReelType::BY_LENGTH);
495                 break;
496         case Film::Property::REEL_LENGTH:
497                 checked_set (_reel_length, _film->reel_length() / 1000000000LL);
498                 break;
499         case Film::Property::CONTENT:
500                 setup_dcp_name ();
501                 setup_sensitivity ();
502                 break;
503         case Film::Property::AUDIO_LANGUAGE:
504         {
505                 auto al = _film->audio_language();
506                 checked_set (_enable_audio_language, static_cast<bool>(al));
507                 checked_set (_audio_language, al ? std_to_wx(al->to_string()) : wxT(""));
508                 setup_dcp_name ();
509                 setup_sensitivity ();
510                 break;
511         }
512         case Film::Property::AUDIO_FRAME_RATE:
513                 if (_audio_sample_rate) {
514                         checked_set (_audio_sample_rate, _film->audio_frame_rate() == 48000 ? 0 : 1);
515                 }
516                 break;
517         case Film::Property::CONTENT_VERSIONS:
518         case Film::Property::VERSION_NUMBER:
519         case Film::Property::RELEASE_TERRITORY:
520         case Film::Property::RATINGS:
521         case Film::Property::FACILITY:
522         case Film::Property::STUDIO:
523         case Film::Property::TEMP_VERSION:
524         case Film::Property::PRE_RELEASE:
525         case Film::Property::RED_BAND:
526         case Film::Property::TWO_D_VERSION_OF_THREE_D:
527         case Film::Property::CHAIN:
528         case Film::Property::LUMINANCE:
529                 setup_dcp_name ();
530                 break;
531         default:
532                 break;
533         }
534 }
535
536
537 void
538 DCPPanel::film_content_changed (int property)
539 {
540         if (property == AudioContentProperty::STREAMS ||
541             property == TextContentProperty::USE ||
542             property == TextContentProperty::BURN ||
543             property == TextContentProperty::LANGUAGE ||
544             property == TextContentProperty::LANGUAGE_IS_ADDITIONAL ||
545             property == VideoContentProperty::CUSTOM_RATIO ||
546             property == VideoContentProperty::CUSTOM_SIZE ||
547             property == VideoContentProperty::BURNT_SUBTITLE_LANGUAGE ||
548             property == VideoContentProperty::CROP ||
549             property == DCPContentProperty::REFERENCE_VIDEO ||
550             property == DCPContentProperty::REFERENCE_AUDIO ||
551             property == DCPContentProperty::REFERENCE_TEXT) {
552                 setup_dcp_name ();
553                 setup_sensitivity ();
554         }
555 }
556
557
558 void
559 DCPPanel::setup_container ()
560 {
561         int n = 0;
562         auto ratios = Ratio::containers ();
563         auto i = ratios.begin ();
564         while (i != ratios.end() && *i != _film->container()) {
565                 ++i;
566                 ++n;
567         }
568
569         if (i == ratios.end()) {
570                 checked_set (_container, -1);
571                 checked_set (_container_size, wxT(""));
572         } else {
573                 checked_set (_container, n);
574                 auto const size = fit_ratio_within (_film->container()->ratio(), _film->full_frame ());
575                 checked_set (_container_size, wxString::Format("%dx%d", size.width, size.height));
576         }
577
578         setup_dcp_name ();
579 }
580
581
582 /** Called when the container widget has been changed */
583 void
584 DCPPanel::container_changed ()
585 {
586         if (!_film) {
587                 return;
588         }
589
590         auto const n = _container->get();
591         if (n) {
592                 auto ratios = Ratio::containers ();
593                 DCPOMATIC_ASSERT(*n < int(ratios.size()));
594                 _film->set_container(ratios[*n]);
595         }
596 }
597
598
599 /** Called when the DCP content type widget has been changed */
600 void
601 DCPPanel::dcp_content_type_changed ()
602 {
603         if (!_film) {
604                 return;
605         }
606
607         auto n = _dcp_content_type->get();
608         if (n) {
609                 _film->set_dcp_content_type(DCPContentType::from_index(*n));
610         }
611 }
612
613
614 void
615 DCPPanel::set_film (shared_ptr<Film> film)
616 {
617         /* We are changing film, so destroy any dialogs for the old one */
618         _audio_dialog.reset();
619         _markers_dialog.reset();
620         _interop_metadata_dialog.reset();
621         _smpte_metadata_dialog.reset();
622
623         _film = film;
624
625         if (!_film) {
626                 /* Really should all the film_changed below but this might be enough */
627                 checked_set (_dcp_name, wxT(""));
628                 set_general_sensitivity (false);
629                 return;
630         }
631
632         _standard->Clear();
633         add_standards();
634
635         film_changed (Film::Property::NAME);
636         film_changed (Film::Property::USE_ISDCF_NAME);
637         film_changed (Film::Property::CONTENT);
638         film_changed (Film::Property::DCP_CONTENT_TYPE);
639         film_changed (Film::Property::CONTAINER);
640         film_changed (Film::Property::RESOLUTION);
641         film_changed (Film::Property::ENCRYPTED);
642         film_changed (Film::Property::J2K_BANDWIDTH);
643         film_changed (Film::Property::VIDEO_FRAME_RATE);
644         film_changed (Film::Property::AUDIO_CHANNELS);
645         film_changed (Film::Property::SEQUENCE);
646         film_changed (Film::Property::THREE_D);
647         film_changed (Film::Property::INTEROP);
648         film_changed (Film::Property::AUDIO_PROCESSOR);
649         film_changed (Film::Property::REEL_TYPE);
650         film_changed (Film::Property::REEL_LENGTH);
651         film_changed (Film::Property::REENCODE_J2K);
652         film_changed (Film::Property::AUDIO_LANGUAGE);
653         film_changed (Film::Property::AUDIO_FRAME_RATE);
654         film_changed (Film::Property::LIMIT_TO_SMPTE_BV20);
655
656         set_general_sensitivity(static_cast<bool>(_film));
657 }
658
659
660 void
661 DCPPanel::set_general_sensitivity (bool s)
662 {
663         _generally_sensitive = s;
664         setup_sensitivity ();
665 }
666
667
668 void
669 DCPPanel::setup_sensitivity ()
670 {
671         _name->Enable                   (_generally_sensitive);
672         _use_isdcf_name->Enable         (_generally_sensitive);
673         _dcp_content_type->Enable       (_generally_sensitive);
674         _copy_isdcf_name_button->Enable (_generally_sensitive);
675         _enable_audio_language->Enable  (_generally_sensitive);
676         _audio_language->Enable         (_enable_audio_language->GetValue());
677         _edit_audio_language->Enable    (_enable_audio_language->GetValue());
678         _encrypted->Enable              (_generally_sensitive);
679         _reel_type->Enable              (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->references_dcp_audio());
680         _reel_length->Enable            (_generally_sensitive && _film && _film->reel_type() == ReelType::BY_LENGTH);
681         _markers->Enable                (_generally_sensitive && _film && !_film->interop());
682         _metadata->Enable               (_generally_sensitive);
683         _frame_rate_choice->Enable      (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->contains_atmos_content());
684         _frame_rate_spin->Enable        (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->contains_atmos_content());
685         _audio_channels->Enable         (_generally_sensitive && _film && !_film->references_dcp_audio() && !_film->contains_atmos_content());
686         _audio_processor->Enable        (_generally_sensitive && _film && !_film->references_dcp_audio());
687         _j2k_bandwidth->Enable          (_generally_sensitive && _film && !_film->references_dcp_video());
688         _container->Enable              (_generally_sensitive && _film && !_film->references_dcp_video());
689         _best_frame_rate->Enable (
690                 _generally_sensitive &&
691                 _film &&
692                 _film->best_video_frame_rate () != _film->video_frame_rate() &&
693                 !_film->references_dcp_video() &&
694                 !_film->contains_atmos_content()
695                 );
696         _resolution->Enable             (_generally_sensitive && _film && !_film->references_dcp_video());
697         _three_d->Enable                (_generally_sensitive && _film && !_film->references_dcp_video());
698
699         _standard->Enable (
700                 _generally_sensitive &&
701                 _film &&
702                 !_film->references_dcp_video() &&
703                 !_film->references_dcp_audio() &&
704                 !_film->contains_atmos_content()
705                 );
706
707         _reencode_j2k->Enable           (_generally_sensitive && _film);
708         _show_audio->Enable             (_generally_sensitive && _film);
709 }
710
711
712 void
713 DCPPanel::use_isdcf_name_toggled ()
714 {
715         if (!_film) {
716                 return;
717         }
718
719         _film->set_use_isdcf_name (_use_isdcf_name->GetValue());
720 }
721
722 void
723 DCPPanel::setup_dcp_name ()
724 {
725         _dcp_name->SetLabel (std_to_wx(_film->dcp_name(true)));
726         _dcp_name->SetToolTip (std_to_wx(_film->dcp_name(true)));
727 }
728
729
730 void
731 DCPPanel::best_frame_rate_clicked ()
732 {
733         if (!_film) {
734                 return;
735         }
736
737         _film->set_video_frame_rate (_film->best_video_frame_rate());
738 }
739
740
741 void
742 DCPPanel::three_d_changed ()
743 {
744         if (!_film) {
745                 return;
746         }
747
748         _film->set_three_d (_three_d->GetValue());
749 }
750
751
752 void
753 DCPPanel::reencode_j2k_changed ()
754 {
755         if (!_film) {
756                 return;
757         }
758
759         _film->set_reencode_j2k (_reencode_j2k->GetValue());
760 }
761
762
763 void
764 DCPPanel::config_changed (Config::Property p)
765 {
766         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
767         setup_frame_rate_widget ();
768
769         if (p == Config::SHOW_EXPERIMENTAL_AUDIO_PROCESSORS) {
770                 _audio_processor->Clear ();
771                 add_audio_processors ();
772                 if (_film) {
773                         film_changed (Film::Property::AUDIO_PROCESSOR);
774                 }
775         } else if (p == Config::ALLOW_SMPTE_BV20) {
776                 _standard->Clear();
777                 add_standards();
778                 if (_film) {
779                         film_changed(Film::Property::INTEROP);
780                         film_changed(Film::Property::LIMIT_TO_SMPTE_BV20);
781                 }
782         }
783 }
784
785
786 void
787 DCPPanel::setup_frame_rate_widget ()
788 {
789         if (Config::instance()->allow_any_dcp_frame_rate()) {
790                 _frame_rate_choice->Hide ();
791                 _frame_rate_spin->Show ();
792         } else {
793                 _frame_rate_choice->Show ();
794                 _frame_rate_spin->Hide ();
795         }
796         _frame_rate_sizer->Layout();
797 }
798
799
800 wxPanel *
801 DCPPanel::make_video_panel ()
802 {
803         auto panel = new wxPanel (_notebook);
804         auto sizer = new wxBoxSizer (wxVERTICAL);
805         _video_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
806         sizer->Add (_video_grid, 0, wxALL, 8);
807         panel->SetSizer (sizer);
808
809         _container_label = create_label (panel, _("Container"), true);
810         _container = new Choice(panel);
811         _container_size = new StaticText (panel, wxT (""));
812
813         _resolution_label = create_label (panel, _("Resolution"), true);
814         _resolution = new Choice(panel);
815
816         _frame_rate_label = create_label (panel, _("Frame Rate"), true);
817         _frame_rate_choice = new Choice(panel);
818         _frame_rate_spin = new SpinCtrl (panel, DCPOMATIC_SPIN_CTRL_WIDTH);
819         _best_frame_rate = new Button (panel, _("Use best"));
820
821         _three_d = new CheckBox (panel, _("3D"));
822
823         _j2k_bandwidth_label = create_label (panel, _("JPEG2000 bandwidth\nfor newly-encoded data"), true);
824         _j2k_bandwidth = new SpinCtrl (panel, DCPOMATIC_SPIN_CTRL_WIDTH);
825         _mbits_label = create_label (panel, _("Mbit/s"), false);
826
827         _reencode_j2k = new CheckBox (panel, _("Re-encode JPEG2000 data from input"));
828
829         _container->Bind         (wxEVT_CHOICE,   boost::bind(&DCPPanel::container_changed, this));
830         _frame_rate_choice->Bind (wxEVT_CHOICE,   boost::bind(&DCPPanel::frame_rate_choice_changed, this));
831         _frame_rate_spin->Bind   (wxEVT_SPINCTRL, boost::bind(&DCPPanel::frame_rate_spin_changed, this));
832         _best_frame_rate->Bind   (wxEVT_BUTTON,   boost::bind(&DCPPanel::best_frame_rate_clicked, this));
833         _j2k_bandwidth->Bind     (wxEVT_SPINCTRL, boost::bind(&DCPPanel::j2k_bandwidth_changed, this));
834         /* Also listen to wxEVT_TEXT so that typing numbers directly in is always noticed */
835         _j2k_bandwidth->Bind     (wxEVT_TEXT,     boost::bind(&DCPPanel::j2k_bandwidth_changed, this));
836         _resolution->Bind        (wxEVT_CHOICE,   boost::bind(&DCPPanel::resolution_changed, this));
837         _three_d->bind(&DCPPanel::three_d_changed, this);
838         _reencode_j2k->bind(&DCPPanel::reencode_j2k_changed, this);
839
840         for (auto i: Ratio::containers()) {
841                 _container->add(i->container_nickname());
842         }
843
844         for (auto i: Config::instance()->allowed_dcp_frame_rates()) {
845                 _frame_rate_choice->add(boost::lexical_cast<string>(i));
846         }
847
848         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
849         _frame_rate_spin->SetRange (1, 480);
850
851         _resolution->add(_("2K"));
852         _resolution->add(_("4K"));
853
854         add_video_panel_to_grid ();
855         setup_frame_rate_widget();
856
857         return panel;
858 }
859
860
861 void
862 DCPPanel::add_video_panel_to_grid ()
863 {
864         int r = 0;
865
866         add_label_to_sizer (_video_grid, _container_label, true, wxGBPosition (r, 0));
867         {
868                 auto s = new wxBoxSizer (wxHORIZONTAL);
869                 s->Add (_container, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
870                 s->Add (_container_size, 1, wxLEFT | wxALIGN_CENTER_VERTICAL);
871                 _video_grid->Add (s, wxGBPosition(r, 1));
872                 ++r;
873         }
874
875         add_label_to_sizer (_video_grid, _resolution_label, true, wxGBPosition (r, 0));
876         _video_grid->Add (_resolution, wxGBPosition (r, 1));
877         ++r;
878
879         add_label_to_sizer (_video_grid, _frame_rate_label, true, wxGBPosition (r, 0));
880         {
881                 _frame_rate_sizer = new wxBoxSizer (wxHORIZONTAL);
882                 _frame_rate_sizer->Add (_frame_rate_choice, 1, wxALIGN_CENTER_VERTICAL);
883                 _frame_rate_sizer->Add (_frame_rate_spin, 1, wxALIGN_CENTER_VERTICAL);
884                 _frame_rate_sizer->Add (_best_frame_rate, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
885                 _video_grid->Add (_frame_rate_sizer, wxGBPosition (r, 1));
886                 ++r;
887         }
888
889         _video_grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
890         ++r;
891
892         add_label_to_sizer (_video_grid, _j2k_bandwidth_label, true, wxGBPosition (r, 0));
893         auto s = new wxBoxSizer (wxHORIZONTAL);
894         s->Add (_j2k_bandwidth, 0, wxALIGN_CENTER_VERTICAL);
895         add_label_to_sizer (s, _mbits_label, false, 0, wxLEFT | wxALIGN_CENTER_VERTICAL);
896         _video_grid->Add (s, wxGBPosition(r, 1), wxDefaultSpan);
897         ++r;
898         _video_grid->Add (_reencode_j2k, wxGBPosition(r, 0), wxGBSpan(1, 2));
899 }
900
901
902 int
903 DCPPanel::minimum_allowed_audio_channels () const
904 {
905         int min = 2;
906         if (_film && _film->audio_processor ()) {
907                 min = _film->audio_processor()->out_channels ();
908         }
909
910         if (min % 2 == 1) {
911                 ++min;
912         }
913
914         return min;
915 }
916
917
918 wxPanel *
919 DCPPanel::make_audio_panel ()
920 {
921         auto panel = new wxPanel (_notebook);
922         _audio_panel_sizer = new wxBoxSizer (wxVERTICAL);
923         _audio_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
924         _audio_panel_sizer->Add (_audio_grid, 0, wxALL, 8);
925         panel->SetSizer (_audio_panel_sizer);
926
927         _channels_label = create_label (panel, _("Channels"), true);
928         _audio_channels = new Choice(panel);
929         setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels ());
930
931         if (Config::instance()->allow_96khz_audio()) {
932                 _audio_sample_rate_label = create_label (panel, _("Sample rate"), true);
933                 _audio_sample_rate = new wxChoice (panel, wxID_ANY);
934         }
935
936         _processor_label = create_label (panel, _("Processor"), true);
937         _audio_processor = new Choice(panel);
938         add_audio_processors ();
939
940         _show_audio = new Button (panel, _("Show graph of audio levels..."));
941
942         _audio_channels->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_channels_changed, this));
943         if (_audio_sample_rate) {
944                 _audio_sample_rate->Bind (wxEVT_CHOICE, boost::bind(&DCPPanel::audio_sample_rate_changed, this));
945         }
946         _audio_processor->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_processor_changed, this));
947         _show_audio->Bind (wxEVT_BUTTON, boost::bind (&DCPPanel::show_audio_clicked, this));
948
949         if (_audio_sample_rate) {
950                 _audio_sample_rate->Append (_("48kHz"));
951                 _audio_sample_rate->Append (_("96kHz"));
952         }
953
954         add_audio_panel_to_grid ();
955
956         return panel;
957 }
958
959
960 void
961 DCPPanel::add_audio_panel_to_grid ()
962 {
963         int r = 0;
964
965         add_label_to_sizer (_audio_grid, _channels_label, true, wxGBPosition (r, 0));
966         _audio_grid->Add (_audio_channels, wxGBPosition (r, 1));
967         ++r;
968
969         if (_audio_sample_rate_label && _audio_sample_rate) {
970                 add_label_to_sizer (_audio_grid, _audio_sample_rate_label, true, wxGBPosition(r, 0));
971                 _audio_grid->Add (_audio_sample_rate, wxGBPosition(r, 1));
972                 ++r;
973         }
974
975         add_label_to_sizer (_audio_grid, _processor_label, true, wxGBPosition (r, 0));
976         _audio_grid->Add (_audio_processor, wxGBPosition (r, 1));
977         ++r;
978
979         _audio_grid->Add (_show_audio, wxGBPosition (r, 0), wxGBSpan (1, 2));
980         ++r;
981 }
982
983
984 void
985 DCPPanel::copy_isdcf_name_button_clicked ()
986 {
987         _film->set_name (_film->isdcf_name (true));
988         _film->set_use_isdcf_name (false);
989 }
990
991
992 void
993 DCPPanel::audio_processor_changed ()
994 {
995         if (!_film || !_audio_processor->get()) {
996                 return;
997         }
998
999         auto const s = string_client_data(_audio_processor->GetClientObject(*_audio_processor->get()));
1000         _film->set_audio_processor (AudioProcessor::from_id (s));
1001 }
1002
1003
1004 void
1005 DCPPanel::show_audio_clicked ()
1006 {
1007         if (!_film) {
1008                 return;
1009         }
1010
1011         _audio_dialog.reset(_panel, _film, _viewer);
1012         _audio_dialog->Show();
1013 }
1014
1015
1016 void
1017 DCPPanel::reel_type_changed ()
1018 {
1019         if (!_film || !_reel_type->get()) {
1020                 return;
1021         }
1022
1023         _film->set_reel_type(static_cast<ReelType>(*_reel_type->get()));
1024 }
1025
1026
1027 void
1028 DCPPanel::reel_length_changed ()
1029 {
1030         if (!_film) {
1031                 return;
1032         }
1033
1034         _film->set_reel_length (_reel_length->GetValue() * 1000000000LL);
1035 }
1036
1037
1038 void
1039 DCPPanel::add_audio_processors ()
1040 {
1041         _audio_processor->add(_("None"), new wxStringClientData(N_("none")));
1042         for (auto ap: AudioProcessor::visible()) {
1043                 _audio_processor->add(std_to_wx(ap->name()), new wxStringClientData(std_to_wx(ap->id())));
1044         }
1045         _audio_panel_sizer->Layout();
1046 }
1047
1048
1049 void
1050 DCPPanel::enable_audio_language_toggled ()
1051 {
1052         setup_sensitivity ();
1053         if (_enable_audio_language->GetValue()) {
1054                 auto al = wx_to_std (_audio_language->GetLabel());
1055                 _film->set_audio_language (al.empty() ? dcp::LanguageTag("en-US") : dcp::LanguageTag(al));
1056         } else {
1057                 _film->set_audio_language (boost::none);
1058         }
1059 }
1060
1061
1062 void
1063 DCPPanel::edit_audio_language_clicked ()
1064 {
1065        DCPOMATIC_ASSERT (_film->audio_language());
1066        auto d = make_wx<LanguageTagDialog>(_panel, *_film->audio_language());
1067        if (d->ShowModal() == wxID_OK) {
1068                _film->set_audio_language(d->get());
1069        }
1070 }
1071
1072
1073 void
1074 DCPPanel::audio_sample_rate_changed ()
1075 {
1076         if (_audio_sample_rate) {
1077                 _film->set_audio_frame_rate (_audio_sample_rate->GetSelection() == 0 ? 48000 : 96000);
1078         }
1079 }
1080