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