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