b938ed87dd83322b096bd47a809641bf10bc41ff
[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                 setup_dcp_name ();
459                 _edit_isdcf_button->Enable (_film->use_isdcf_name ());
460                 break;
461         }
462         case Film::ISDCF_METADATA:
463                 setup_dcp_name ();
464                 break;
465         case Film::VIDEO_FRAME_RATE:
466         {
467                 bool done = false;
468                 for (unsigned int i = 0; i < _frame_rate_choice->GetCount(); ++i) {
469                         if (wx_to_std (_frame_rate_choice->GetString(i)) == boost::lexical_cast<string> (_film->video_frame_rate())) {
470                                 checked_set (_frame_rate_choice, i);
471                                 done = true;
472                                 break;
473                         }
474                 }
475
476                 if (!done) {
477                         checked_set (_frame_rate_choice, -1);
478                 }
479
480                 checked_set (_frame_rate_spin, _film->video_frame_rate ());
481
482                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
483                 setup_dcp_name ();
484                 break;
485         }
486         case Film::AUDIO_CHANNELS:
487                 if (_film->audio_channels () < minimum_allowed_audio_channels ()) {
488                         _film->set_audio_channels (minimum_allowed_audio_channels ());
489                 } else {
490                         checked_set (_audio_channels, locale_convert<string> (max (minimum_allowed_audio_channels(), _film->audio_channels ())));
491                         setup_dcp_name ();
492                 }
493                 break;
494         case Film::THREE_D:
495                 checked_set (_three_d, _film->three_d ());
496                 setup_dcp_name ();
497                 break;
498         case Film::REENCODE_J2K:
499                 checked_set (_reencode_j2k, _film->reencode_j2k());
500                 break;
501         case Film::INTEROP:
502                 checked_set (_standard, _film->interop() ? 1 : 0);
503                 setup_dcp_name ();
504                 _markers->Enable (!_film->interop());
505                 break;
506         case Film::AUDIO_PROCESSOR:
507                 if (_film->audio_processor ()) {
508                         checked_set (_audio_processor, _film->audio_processor()->id());
509                 } else {
510                         checked_set (_audio_processor, 0);
511                 }
512                 setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels ());
513                 film_changed (Film::AUDIO_CHANNELS);
514                 break;
515         case Film::REEL_TYPE:
516                 checked_set (_reel_type, _film->reel_type ());
517                 _reel_length->Enable (_film->reel_type() == REELTYPE_BY_LENGTH);
518                 break;
519         case Film::REEL_LENGTH:
520                 checked_set (_reel_length, _film->reel_length() / 1000000000LL);
521                 break;
522         case Film::UPLOAD_AFTER_MAKE_DCP:
523                 checked_set (_upload_after_make_dcp, _film->upload_after_make_dcp ());
524                 break;
525         case Film::CONTENT:
526                 setup_dcp_name ();
527                 break;
528         default:
529                 break;
530         }
531 }
532
533 void
534 DCPPanel::film_content_changed (int property)
535 {
536         if (property == AudioContentProperty::STREAMS ||
537             property == TextContentProperty::USE ||
538             property == TextContentProperty::BURN ||
539             property == VideoContentProperty::SCALE ||
540             property == DCPContentProperty::REFERENCE_VIDEO ||
541             property == DCPContentProperty::REFERENCE_AUDIO ||
542             property == DCPContentProperty::REFERENCE_TEXT) {
543                 setup_dcp_name ();
544                 setup_sensitivity ();
545         }
546 }
547
548
549 void
550 DCPPanel::setup_container ()
551 {
552         int n = 0;
553         vector<Ratio const *> ratios = Ratio::containers ();
554         vector<Ratio const *>::iterator i = ratios.begin ();
555         while (i != ratios.end() && *i != _film->container ()) {
556                 ++i;
557                 ++n;
558         }
559
560         if (i == ratios.end()) {
561                 checked_set (_container, -1);
562                 checked_set (_container_size, wxT (""));
563         } else {
564                 checked_set (_container, n);
565                 dcp::Size const size = fit_ratio_within (_film->container()->ratio(), _film->full_frame ());
566                 checked_set (_container_size, wxString::Format ("%dx%d", size.width, size.height));
567         }
568
569         setup_dcp_name ();
570 }
571
572 /** Called when the container widget has been changed */
573 void
574 DCPPanel::container_changed ()
575 {
576         if (!_film) {
577                 return;
578         }
579
580         int const n = _container->GetSelection ();
581         if (n >= 0) {
582                 vector<Ratio const *> ratios = Ratio::containers ();
583                 DCPOMATIC_ASSERT (n < int (ratios.size()));
584                 _film->set_container (ratios[n]);
585         }
586 }
587
588 /** Called when the DCP content type widget has been changed */
589 void
590 DCPPanel::dcp_content_type_changed ()
591 {
592         if (!_film) {
593                 return;
594         }
595
596         int const n = _dcp_content_type->GetSelection ();
597         if (n != wxNOT_FOUND) {
598                 _film->set_dcp_content_type (DCPContentType::from_index (n));
599         }
600 }
601
602 void
603 DCPPanel::set_film (shared_ptr<Film> film)
604 {
605         /* We are changing film, so destroy any dialogs for the old one */
606         if (_audio_dialog) {
607                 _audio_dialog->Destroy ();
608                 _audio_dialog = 0;
609         }
610         if (_markers_dialog) {
611                 _markers_dialog->Destroy ();
612                 _markers_dialog = 0;
613         }
614         if (_metadata_dialog) {
615                 _metadata_dialog->Destroy ();
616                 _metadata_dialog = 0;
617         }
618
619         _film = film;
620
621         if (!_film) {
622                 /* Really should all the film_changed below but this might be enough */
623                 checked_set (_dcp_name, wxT(""));
624                 set_general_sensitivity (false);
625                 return;
626         }
627
628         film_changed (Film::NAME);
629         film_changed (Film::USE_ISDCF_NAME);
630         film_changed (Film::CONTENT);
631         film_changed (Film::DCP_CONTENT_TYPE);
632         film_changed (Film::CONTAINER);
633         film_changed (Film::RESOLUTION);
634         film_changed (Film::SIGNED);
635         film_changed (Film::ENCRYPTED);
636         film_changed (Film::KEY);
637         film_changed (Film::J2K_BANDWIDTH);
638         film_changed (Film::ISDCF_METADATA);
639         film_changed (Film::VIDEO_FRAME_RATE);
640         film_changed (Film::AUDIO_CHANNELS);
641         film_changed (Film::SEQUENCE);
642         film_changed (Film::THREE_D);
643         film_changed (Film::INTEROP);
644         film_changed (Film::AUDIO_PROCESSOR);
645         film_changed (Film::REEL_TYPE);
646         film_changed (Film::REEL_LENGTH);
647         film_changed (Film::UPLOAD_AFTER_MAKE_DCP);
648         film_changed (Film::REENCODE_J2K);
649
650         set_general_sensitivity(static_cast<bool>(_film));
651 }
652
653 void
654 DCPPanel::set_general_sensitivity (bool s)
655 {
656         _generally_sensitive = s;
657         setup_sensitivity ();
658 }
659
660 void
661 DCPPanel::setup_sensitivity ()
662 {
663         _name->Enable                   (_generally_sensitive);
664         _use_isdcf_name->Enable         (_generally_sensitive);
665         _edit_isdcf_button->Enable      (_generally_sensitive);
666         _dcp_content_type->Enable       (_generally_sensitive);
667         _copy_isdcf_name_button->Enable (_generally_sensitive);
668
669         bool si = _generally_sensitive;
670         if (_film && _film->encrypted ()) {
671                 si = false;
672         }
673         _signed->Enable (si);
674
675         _encrypted->Enable              (_generally_sensitive);
676         _key->Enable                    (_generally_sensitive && _film && _film->encrypted ());
677         _edit_key->Enable               (_generally_sensitive && _film && _film->encrypted ());
678         _reel_type->Enable              (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->references_dcp_audio());
679         _reel_length->Enable            (_generally_sensitive && _film && _film->reel_type() == REELTYPE_BY_LENGTH);
680         _upload_after_make_dcp->Enable  (_generally_sensitive);
681         _markers->Enable                (_generally_sensitive && _film && !_film->interop());
682         _metadata->Enable               (_generally_sensitive);
683         _frame_rate_choice->Enable      (_generally_sensitive && _film && !_film->references_dcp_video());
684         _frame_rate_spin->Enable        (_generally_sensitive && _film && !_film->references_dcp_video());
685         _audio_channels->Enable         (_generally_sensitive && _film && !_film->references_dcp_audio());
686         _audio_processor->Enable        (_generally_sensitive && _film && !_film->references_dcp_audio());
687         _j2k_bandwidth->Enable          (_generally_sensitive && _film && !_film->references_dcp_video());
688         _container->Enable              (_generally_sensitive && _film && !_film->references_dcp_video());
689         _best_frame_rate->Enable        (_generally_sensitive && _film && _film->best_video_frame_rate () != _film->video_frame_rate ());
690         _resolution->Enable             (_generally_sensitive && _film && !_film->references_dcp_video());
691         _three_d->Enable                (_generally_sensitive && _film && !_film->references_dcp_video());
692         _standard->Enable               (_generally_sensitive && _film && !_film->references_dcp_video() && !_film->references_dcp_audio());
693         _reencode_j2k->Enable           (_generally_sensitive && _film);
694         _show_audio->Enable             (_generally_sensitive && _film);
695 }
696
697 void
698 DCPPanel::use_isdcf_name_toggled ()
699 {
700         if (!_film) {
701                 return;
702         }
703
704         _film->set_use_isdcf_name (_use_isdcf_name->GetValue ());
705 }
706
707 void
708 DCPPanel::edit_isdcf_button_clicked ()
709 {
710         if (!_film) {
711                 return;
712         }
713
714         ISDCFMetadataDialog* d = new ISDCFMetadataDialog (_panel, _film->isdcf_metadata (), _film->three_d ());
715         d->ShowModal ();
716         _film->set_isdcf_metadata (d->isdcf_metadata ());
717         d->Destroy ();
718 }
719
720 void
721 DCPPanel::setup_dcp_name ()
722 {
723         _dcp_name->SetLabel (std_to_wx (_film->dcp_name (true)));
724         _dcp_name->SetToolTip (std_to_wx (_film->dcp_name (true)));
725 }
726
727 void
728 DCPPanel::best_frame_rate_clicked ()
729 {
730         if (!_film) {
731                 return;
732         }
733
734         _film->set_video_frame_rate (_film->best_video_frame_rate ());
735 }
736
737 void
738 DCPPanel::three_d_changed ()
739 {
740         if (!_film) {
741                 return;
742         }
743
744         _film->set_three_d (_three_d->GetValue ());
745 }
746
747 void
748 DCPPanel::reencode_j2k_changed ()
749 {
750         if (!_film) {
751                 return;
752         }
753
754         _film->set_reencode_j2k (_reencode_j2k->GetValue());
755 }
756
757 void
758 DCPPanel::config_changed (Config::Property p)
759 {
760         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
761         setup_frame_rate_widget ();
762
763         if (p == Config::INTERFACE_COMPLEXITY) {
764                 _grid->Clear ();
765                 add_to_grid ();
766                 _sizer->Layout ();
767                 _grid->Layout ();
768
769                 _video_grid->Clear ();
770                 add_video_panel_to_grid ();
771                 _video_grid->Layout ();
772
773                 _audio_grid->Clear ();
774                 add_audio_panel_to_grid ();
775                 _audio_grid->Layout ();
776         }
777 }
778
779 void
780 DCPPanel::setup_frame_rate_widget ()
781 {
782         if (Config::instance()->allow_any_dcp_frame_rate ()) {
783                 _frame_rate_choice->Hide ();
784                 _frame_rate_spin->Show ();
785         } else {
786                 _frame_rate_choice->Show ();
787                 _frame_rate_spin->Hide ();
788         }
789
790         _frame_rate_sizer->Layout ();
791 }
792
793 wxPanel *
794 DCPPanel::make_video_panel ()
795 {
796         wxPanel* panel = new wxPanel (_notebook);
797         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
798         _video_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
799         sizer->Add (_video_grid, 0, wxALL, 8);
800         panel->SetSizer (sizer);
801
802         _container_label = create_label (panel, _("Container"), true);
803         _container = new wxChoice (panel, wxID_ANY);
804         _container_size = new StaticText (panel, wxT (""));
805
806         _resolution_label = create_label (panel, _("Resolution"), true);
807         _resolution = new wxChoice (panel, wxID_ANY);
808
809         _frame_rate_label = create_label (panel, _("Frame Rate"), true);
810         _frame_rate_choice = new wxChoice (panel, wxID_ANY);
811         _frame_rate_sizer = new wxBoxSizer (wxHORIZONTAL);
812         _frame_rate_sizer->Add (_frame_rate_choice, 1, wxALIGN_CENTER_VERTICAL);
813         _frame_rate_spin = new wxSpinCtrl (panel, wxID_ANY);
814         _frame_rate_sizer->Add (_frame_rate_spin, 1, wxALIGN_CENTER_VERTICAL);
815         setup_frame_rate_widget ();
816         _best_frame_rate = new Button (panel, _("Use best"));
817         _frame_rate_sizer->Add (_best_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
818
819         _three_d = new CheckBox (panel, _("3D"));
820
821         _j2k_bandwidth_label = create_label (panel, _("JPEG2000 bandwidth\nfor newly-encoded data"), true);
822         _j2k_bandwidth = new wxSpinCtrl (panel, wxID_ANY);
823         _mbits_label = create_label (panel, _("Mbit/s"), false);
824
825         _reencode_j2k = new CheckBox (panel, _("Re-encode JPEG2000 data from input"));
826
827         _container->Bind         (wxEVT_CHOICE,   boost::bind(&DCPPanel::container_changed, this));
828         _frame_rate_choice->Bind (wxEVT_CHOICE,   boost::bind(&DCPPanel::frame_rate_choice_changed, this));
829         _frame_rate_spin->Bind   (wxEVT_SPINCTRL, boost::bind(&DCPPanel::frame_rate_spin_changed, this));
830         _best_frame_rate->Bind   (wxEVT_BUTTON,   boost::bind(&DCPPanel::best_frame_rate_clicked, this));
831         _j2k_bandwidth->Bind     (wxEVT_SPINCTRL, boost::bind(&DCPPanel::j2k_bandwidth_changed, this));
832         /* Also listen to wxEVT_TEXT so that typing numbers directly in is always noticed */
833         _j2k_bandwidth->Bind     (wxEVT_TEXT,     boost::bind(&DCPPanel::j2k_bandwidth_changed, this));
834         _resolution->Bind        (wxEVT_CHOICE,   boost::bind(&DCPPanel::resolution_changed, this));
835         _three_d->Bind           (wxEVT_CHECKBOX, boost::bind(&DCPPanel::three_d_changed, this));
836         _reencode_j2k->Bind      (wxEVT_CHECKBOX, boost::bind(&DCPPanel::reencode_j2k_changed, this));
837
838         BOOST_FOREACH (Ratio const * i, Ratio::containers()) {
839                 _container->Append (std_to_wx(i->container_nickname()));
840         }
841
842         BOOST_FOREACH (int i, Config::instance()->allowed_dcp_frame_rates()) {
843                 _frame_rate_choice->Append (std_to_wx (boost::lexical_cast<string> (i)));
844         }
845
846         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
847         _frame_rate_spin->SetRange (1, 480);
848
849         _resolution->Append (_("2K"));
850         _resolution->Append (_("4K"));
851
852         add_video_panel_to_grid ();
853
854         return panel;
855 }
856
857 void
858 DCPPanel::add_video_panel_to_grid ()
859 {
860         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
861
862         int r = 0;
863
864         add_label_to_sizer (_video_grid, _container_label, true, wxGBPosition (r, 0));
865         {
866                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
867                 s->Add (_container, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
868                 s->Add (_container_size, 1, wxLEFT | wxALIGN_CENTER_VERTICAL);
869                 _video_grid->Add (s, wxGBPosition(r, 1));
870                 ++r;
871         }
872
873         add_label_to_sizer (_video_grid, _resolution_label, true, wxGBPosition (r, 0));
874         _video_grid->Add (_resolution, wxGBPosition (r, 1));
875         ++r;
876
877         add_label_to_sizer (_video_grid, _frame_rate_label, true, wxGBPosition (r, 0));
878         {
879                 _frame_rate_sizer = new wxBoxSizer (wxHORIZONTAL);
880                 _frame_rate_sizer->Add (_frame_rate_choice, 1, wxALIGN_CENTER_VERTICAL);
881                 _frame_rate_sizer->Add (_frame_rate_spin, 1, wxALIGN_CENTER_VERTICAL);
882                 _frame_rate_sizer->Add (_best_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
883                 _video_grid->Add (_frame_rate_sizer, wxGBPosition (r, 1));
884                 ++r;
885         }
886
887         _video_grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
888         ++r;
889
890         _j2k_bandwidth_label->Show (full);
891         _j2k_bandwidth->Show (full);
892         _mbits_label->Show (full);
893
894         if (full) {
895                 add_label_to_sizer (_video_grid, _j2k_bandwidth_label, true, wxGBPosition (r, 0));
896                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
897                 s->Add (_j2k_bandwidth, 1);
898                 add_label_to_sizer (s, _mbits_label, false);
899                 _video_grid->Add (s, wxGBPosition (r, 1));
900                 ++r;
901                 _video_grid->Add (_reencode_j2k, wxGBPosition(r, 0), wxGBSpan(1, 2));
902         }
903 }
904
905 int
906 DCPPanel::minimum_allowed_audio_channels () const
907 {
908         int min = 2;
909         if (_film && _film->audio_processor ()) {
910                 min = _film->audio_processor()->out_channels ();
911         }
912
913         if (min % 2 == 1) {
914                 ++min;
915         }
916
917         return min;
918 }
919
920 wxPanel *
921 DCPPanel::make_audio_panel ()
922 {
923         wxPanel* panel = new wxPanel (_notebook);
924         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
925         _audio_grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
926         sizer->Add (_audio_grid, 0, wxALL, 8);
927         panel->SetSizer (sizer);
928
929         _channels_label = create_label (panel, _("Channels"), true);
930         _audio_channels = new wxChoice (panel, wxID_ANY);
931         setup_audio_channels_choice (_audio_channels, minimum_allowed_audio_channels ());
932
933         _processor_label = create_label (panel, _("Processor"), true);
934         _audio_processor = new wxChoice (panel, wxID_ANY);
935         _audio_processor->Append (_("None"), new wxStringClientData (N_("none")));
936         BOOST_FOREACH (AudioProcessor const * ap, AudioProcessor::all ()) {
937                 _audio_processor->Append (std_to_wx (ap->name ()), new wxStringClientData (std_to_wx (ap->id ())));
938         }
939
940         _show_audio = new Button (panel, _("Show audio..."));
941
942         _audio_channels->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_channels_changed, this));
943         _audio_processor->Bind (wxEVT_CHOICE, boost::bind (&DCPPanel::audio_processor_changed, this));
944         _show_audio->Bind (wxEVT_BUTTON, boost::bind (&DCPPanel::show_audio_clicked, this));
945
946         add_audio_panel_to_grid ();
947
948         return panel;
949 }
950
951 void
952 DCPPanel::add_audio_panel_to_grid ()
953 {
954         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
955
956         int r = 0;
957
958         _channels_label->Show (full);
959         _audio_channels->Show (full);
960
961         if (full) {
962                 add_label_to_sizer (_audio_grid, _channels_label, true, wxGBPosition (r, 0));
963                 _audio_grid->Add (_audio_channels, wxGBPosition (r, 1));
964                 ++r;
965         }
966
967         _processor_label->Show (full);
968         _audio_processor->Show (full);
969
970         if (full) {
971                 add_label_to_sizer (_audio_grid, _processor_label, true, wxGBPosition (r, 0));
972                 _audio_grid->Add (_audio_processor, wxGBPosition (r, 1));
973                 ++r;
974         }
975
976         _audio_grid->Add (_show_audio, wxGBPosition (r, 0), wxGBSpan (1, 2));
977         ++r;
978 }
979
980 void
981 DCPPanel::copy_isdcf_name_button_clicked ()
982 {
983         _film->set_name (_film->isdcf_name (true));
984         _film->set_use_isdcf_name (false);
985 }
986
987 void
988 DCPPanel::audio_processor_changed ()
989 {
990         if (!_film) {
991                 return;
992         }
993
994         string const s = string_client_data (_audio_processor->GetClientObject (_audio_processor->GetSelection ()));
995         _film->set_audio_processor (AudioProcessor::from_id (s));
996 }
997
998 void
999 DCPPanel::show_audio_clicked ()
1000 {
1001         if (!_film) {
1002                 return;
1003         }
1004
1005         if (_audio_dialog) {
1006                 _audio_dialog->Destroy ();
1007                 _audio_dialog = 0;
1008         }
1009
1010         AudioDialog* d = new AudioDialog (_panel, _film);
1011         d->Show ();
1012 }
1013
1014 void
1015 DCPPanel::reel_type_changed ()
1016 {
1017         if (!_film) {
1018                 return;
1019         }
1020
1021         _film->set_reel_type (static_cast<ReelType> (_reel_type->GetSelection ()));
1022 }
1023
1024 void
1025 DCPPanel::reel_length_changed ()
1026 {
1027         if (!_film) {
1028                 return;
1029         }
1030
1031         _film->set_reel_length (_reel_length->GetValue() * 1000000000LL);
1032 }