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