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