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