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