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