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