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