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