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