Add burn subtitles option to Film.
[dcpomatic.git] / src / wx / film_editor.cc
1 /*
2     Copyright (C) 2012-2014 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 /** @file src/film_editor.cc
21  *  @brief A wx widget to edit a film's metadata, and perform various functions.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include <wx/wx.h>
27 #include <wx/notebook.h>
28 #include <wx/listctrl.h>
29 #include <boost/thread.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include "lib/film.h"
33 #include "lib/transcode_job.h"
34 #include "lib/exceptions.h"
35 #include "lib/job_manager.h"
36 #include "lib/filter.h"
37 #include "lib/ratio.h"
38 #include "lib/config.h"
39 #include "lib/image_content.h"
40 #include "lib/ffmpeg_content.h"
41 #include "lib/sndfile_content.h"
42 #include "lib/dcp_content_type.h"
43 #include "lib/sound_processor.h"
44 #include "lib/scaler.h"
45 #include "lib/playlist.h"
46 #include "lib/content.h"
47 #include "lib/content_factory.h"
48 #include "timecode.h"
49 #include "wx_util.h"
50 #include "film_editor.h"
51 #include "isdcf_metadata_dialog.h"
52 #include "timeline_dialog.h"
53 #include "timing_panel.h"
54 #include "subtitle_panel.h"
55 #include "audio_panel.h"
56 #include "video_panel.h"
57
58 using std::string;
59 using std::cout;
60 using std::stringstream;
61 using std::pair;
62 using std::fixed;
63 using std::setprecision;
64 using std::list;
65 using std::vector;
66 using std::max;
67 using boost::shared_ptr;
68 using boost::weak_ptr;
69 using boost::dynamic_pointer_cast;
70 using boost::lexical_cast;
71
72 /** @param f Film to edit */
73 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
74         : wxPanel (parent)
75         , _menu (this)
76         , _generally_sensitive (true)
77         , _timeline_dialog (0)
78 {
79         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
80
81         _main_notebook = new wxNotebook (this, wxID_ANY);
82         s->Add (_main_notebook, 1);
83
84         make_content_panel ();
85         _main_notebook->AddPage (_content_panel, _("Content"), true);
86         make_dcp_panel ();
87         _main_notebook->AddPage (_dcp_panel, _("DCP"), false);
88         
89         set_film (f);
90         connect_to_widgets ();
91
92         JobManager::instance()->ActiveJobsChanged.connect (
93                 bind (&FilmEditor::active_jobs_changed, this, _1)
94                 );
95
96         Config::instance()->Changed.connect (boost::bind (&FilmEditor::config_changed, this));
97         
98         SetSizerAndFit (s);
99 }
100
101 void
102 FilmEditor::make_dcp_panel ()
103 {
104         _dcp_panel = new wxPanel (_main_notebook);
105         _dcp_sizer = new wxBoxSizer (wxVERTICAL);
106         _dcp_panel->SetSizer (_dcp_sizer);
107
108         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
109         _dcp_sizer->Add (grid, 0, wxEXPAND | wxALL, 8);
110
111         int r = 0;
112         
113         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Name"), true, wxGBPosition (r, 0));
114         _name = new wxTextCtrl (_dcp_panel, wxID_ANY);
115         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxLEFT | wxRIGHT);
116         ++r;
117         
118         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Name"), true, wxGBPosition (r, 0));
119         _dcp_name = new wxStaticText (_dcp_panel, wxID_ANY, wxT (""));
120         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
121         ++r;
122
123         int flags = wxALIGN_CENTER_VERTICAL;
124 #ifdef __WXOSX__
125         flags |= wxALIGN_RIGHT;
126 #endif  
127
128         _use_isdcf_name = new wxCheckBox (_dcp_panel, wxID_ANY, _("Use ISDCF name"));
129         grid->Add (_use_isdcf_name, wxGBPosition (r, 0), wxDefaultSpan, flags);
130         _edit_isdcf_button = new wxButton (_dcp_panel, wxID_ANY, _("Details..."));
131         grid->Add (_edit_isdcf_button, wxGBPosition (r, 1), wxDefaultSpan);
132         ++r;
133
134         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Container"), true, wxGBPosition (r, 0));
135         _container = new wxChoice (_dcp_panel, wxID_ANY);
136         grid->Add (_container, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
137         ++r;
138
139         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Content Type"), true, wxGBPosition (r, 0));
140         _dcp_content_type = new wxChoice (_dcp_panel, wxID_ANY);
141         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
142         ++r;
143
144         {
145                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Frame Rate"), true, wxGBPosition (r, 0));
146                 _frame_rate_sizer = new wxBoxSizer (wxHORIZONTAL);
147                 _frame_rate_choice = new wxChoice (_dcp_panel, wxID_ANY);
148                 _frame_rate_sizer->Add (_frame_rate_choice, 1, wxALIGN_CENTER_VERTICAL);
149                 _frame_rate_spin = new wxSpinCtrl (_dcp_panel, wxID_ANY);
150                 _frame_rate_sizer->Add (_frame_rate_spin, 1, wxALIGN_CENTER_VERTICAL);
151                 setup_frame_rate_widget ();
152                 _best_frame_rate = new wxButton (_dcp_panel, wxID_ANY, _("Use best"));
153                 _frame_rate_sizer->Add (_best_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND);
154                 grid->Add (_frame_rate_sizer, wxGBPosition (r, 1));
155         }
156         ++r;
157
158         _burn_subtitles = new wxCheckBox (_dcp_panel, wxID_ANY, _("Burn subtitles into image"));
159         grid->Add (_burn_subtitles, wxGBPosition (r, 0), wxGBSpan (1, 2));
160         ++r;
161
162         _signed = new wxCheckBox (_dcp_panel, wxID_ANY, _("Signed"));
163         grid->Add (_signed, wxGBPosition (r, 0), wxGBSpan (1, 2));
164         ++r;
165         
166         _encrypted = new wxCheckBox (_dcp_panel, wxID_ANY, _("Encrypted"));
167         grid->Add (_encrypted, wxGBPosition (r, 0), wxGBSpan (1, 2));
168         ++r;
169
170         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Audio channels"), true, wxGBPosition (r, 0));
171         _audio_channels = new wxSpinCtrl (_dcp_panel, wxID_ANY);
172         grid->Add (_audio_channels, wxGBPosition (r, 1));
173         ++r;
174
175         _three_d = new wxCheckBox (_dcp_panel, wxID_ANY, _("3D"));
176         grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
177         ++r;
178
179         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Resolution"), true, wxGBPosition (r, 0));
180         _resolution = new wxChoice (_dcp_panel, wxID_ANY);
181         grid->Add (_resolution, wxGBPosition (r, 1));
182         ++r;
183
184         {
185                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("JPEG2000 bandwidth"), true, wxGBPosition (r, 0));
186                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
187                 _j2k_bandwidth = new wxSpinCtrl (_dcp_panel, wxID_ANY);
188                 s->Add (_j2k_bandwidth, 1);
189                 add_label_to_sizer (s, _dcp_panel, _("Mbit/s"), false);
190                 grid->Add (s, wxGBPosition (r, 1));
191         }
192         ++r;
193
194         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Standard"), true, wxGBPosition (r, 0));
195         _standard = new wxChoice (_dcp_panel, wxID_ANY);
196         grid->Add (_standard, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
197         ++r;
198
199         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Scaler"), true, wxGBPosition (r, 0));
200         _scaler = new wxChoice (_dcp_panel, wxID_ANY);
201         grid->Add (_scaler, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
202         ++r;
203
204         vector<Scaler const *> const sc = Scaler::all ();
205         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
206                 _scaler->Append (std_to_wx ((*i)->name()));
207         }
208
209         vector<Ratio const *> const ratio = Ratio::all ();
210         for (vector<Ratio const *>::const_iterator i = ratio.begin(); i != ratio.end(); ++i) {
211                 _container->Append (std_to_wx ((*i)->nickname ()));
212         }
213
214         vector<DCPContentType const *> const ct = DCPContentType::all ();
215         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
216                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
217         }
218
219         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
220         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
221                 _frame_rate_choice->Append (std_to_wx (boost::lexical_cast<string> (*i)));
222         }
223
224         _audio_channels->SetRange (0, MAX_DCP_AUDIO_CHANNELS);
225         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
226         _frame_rate_spin->SetRange (1, 480);
227
228         _resolution->Append (_("2K"));
229         _resolution->Append (_("4K"));
230
231         _standard->Append (_("SMPTE"));
232         _standard->Append (_("Interop"));
233 }
234
235 void
236 FilmEditor::connect_to_widgets ()
237 {
238         _name->Bind             (wxEVT_COMMAND_TEXT_UPDATED,          boost::bind (&FilmEditor::name_changed, this));
239         _use_isdcf_name->Bind   (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::use_isdcf_name_toggled, this));
240         _edit_isdcf_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::edit_isdcf_button_clicked, this));
241         _container->Bind        (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::container_changed, this));
242         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_SELECTED,    boost::bind (&FilmEditor::content_selection_changed, this));
243         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_DESELECTED,  boost::bind (&FilmEditor::content_selection_changed, this));
244         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, boost::bind (&FilmEditor::content_right_click, this, _1));
245         _content_add_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_add_file_clicked, this));
246         _content_add_folder->Bind (wxEVT_COMMAND_BUTTON_CLICKED,      boost::bind (&FilmEditor::content_add_folder_clicked, this));
247         _content_remove->Bind   (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_remove_clicked, this));
248         _content_earlier->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_earlier_clicked, this));
249         _content_later->Bind    (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_later_clicked, this));
250         _content_timeline->Bind (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_timeline_clicked, this));
251         _scaler->Bind           (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::scaler_changed, this));
252         _dcp_content_type->Bind (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::dcp_content_type_changed, this));
253         _frame_rate_choice->Bind(wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::frame_rate_choice_changed, this));
254         _frame_rate_spin->Bind  (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::frame_rate_spin_changed, this));
255         _best_frame_rate->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::best_frame_rate_clicked, this));
256         _burn_subtitles->Bind   (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::burn_subtitles_toggled, this));
257         _signed->Bind           (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::signed_toggled, this));
258         _encrypted->Bind        (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::encrypted_toggled, this));
259         _audio_channels->Bind   (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::audio_channels_changed, this));
260         _j2k_bandwidth->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::j2k_bandwidth_changed, this));
261         _resolution->Bind       (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::resolution_changed, this));
262         _sequence_video->Bind   (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::sequence_video_changed, this));
263         _three_d->Bind          (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::three_d_changed, this));
264         _standard->Bind         (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::standard_changed, this));
265 }
266
267 void
268 FilmEditor::make_content_panel ()
269 {
270         _content_panel = new wxPanel (_main_notebook);
271         _content_sizer = new wxBoxSizer (wxVERTICAL);
272         _content_panel->SetSizer (_content_sizer);
273
274         {
275                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
276                 
277                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
278                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
279
280                 _content->InsertColumn (0, wxT(""));
281                 _content->SetColumnWidth (0, 512);
282
283                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
284                 _content_add_file = new wxButton (_content_panel, wxID_ANY, _("Add file(s)..."));
285                 b->Add (_content_add_file, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
286                 _content_add_folder = new wxButton (_content_panel, wxID_ANY, _("Add folder..."));
287                 b->Add (_content_add_folder, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
288                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
289                 b->Add (_content_remove, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
290                 _content_earlier = new wxButton (_content_panel, wxID_ANY, _("Up"));
291                 b->Add (_content_earlier, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
292                 _content_later = new wxButton (_content_panel, wxID_ANY, _("Down"));
293                 b->Add (_content_later, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
294                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
295                 b->Add (_content_timeline, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
296
297                 s->Add (b, 0, wxALL, 4);
298
299                 _content_sizer->Add (s, 0, wxEXPAND | wxALL, 6);
300         }
301
302         _sequence_video = new wxCheckBox (_content_panel, wxID_ANY, _("Keep video in sequence"));
303         _content_sizer->Add (_sequence_video);
304
305         _content_notebook = new wxNotebook (_content_panel, wxID_ANY);
306         _content_sizer->Add (_content_notebook, 1, wxEXPAND | wxTOP, 6);
307
308         _video_panel = new VideoPanel (this);
309         _panels.push_back (_video_panel);
310         _audio_panel = new AudioPanel (this);
311         _panels.push_back (_audio_panel);
312         _subtitle_panel = new SubtitlePanel (this);
313         _panels.push_back (_subtitle_panel);
314         _timing_panel = new TimingPanel (this);
315         _panels.push_back (_timing_panel);
316 }
317
318 /** Called when the name widget has been changed */
319 void
320 FilmEditor::name_changed ()
321 {
322         if (!_film) {
323                 return;
324         }
325
326         _film->set_name (string (_name->GetValue().mb_str()));
327 }
328
329 void
330 FilmEditor::j2k_bandwidth_changed ()
331 {
332         if (!_film) {
333                 return;
334         }
335         
336         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1000000);
337 }
338
339 void
340 FilmEditor::signed_toggled ()
341 {
342         if (!_film) {
343                 return;
344         }
345
346         _film->set_signed (_signed->GetValue ());
347 }
348
349 void
350 FilmEditor::burn_subtitles_toggled ()
351 {
352         if (!_film) {
353                 return;
354         }
355
356         _film->set_burn_subtitles (_burn_subtitles->GetValue ());
357 }
358
359 void
360 FilmEditor::encrypted_toggled ()
361 {
362         if (!_film) {
363                 return;
364         }
365
366         _film->set_encrypted (_encrypted->GetValue ());
367 }
368                                
369 /** Called when the frame rate choice widget has been changed */
370 void
371 FilmEditor::frame_rate_choice_changed ()
372 {
373         if (!_film) {
374                 return;
375         }
376
377         _film->set_video_frame_rate (
378                 boost::lexical_cast<int> (
379                         wx_to_std (_frame_rate_choice->GetString (_frame_rate_choice->GetSelection ()))
380                         )
381                 );
382 }
383
384 /** Called when the frame rate spin widget has been changed */
385 void
386 FilmEditor::frame_rate_spin_changed ()
387 {
388         if (!_film) {
389                 return;
390         }
391
392         _film->set_video_frame_rate (_frame_rate_spin->GetValue ());
393 }
394
395 void
396 FilmEditor::audio_channels_changed ()
397 {
398         if (!_film) {
399                 return;
400         }
401
402         _film->set_audio_channels (_audio_channels->GetValue ());
403 }
404
405 void
406 FilmEditor::resolution_changed ()
407 {
408         if (!_film) {
409                 return;
410         }
411
412         _film->set_resolution (_resolution->GetSelection() == 0 ? RESOLUTION_2K : RESOLUTION_4K);
413 }
414
415 void
416 FilmEditor::standard_changed ()
417 {
418         if (!_film) {
419                 return;
420         }
421
422         _film->set_interop (_standard->GetSelection() == 1);
423 }
424
425 /** Called when the metadata stored in the Film object has changed;
426  *  so that we can update the GUI.
427  *  @param p Property of the Film that has changed.
428  */
429 void
430 FilmEditor::film_changed (Film::Property p)
431 {
432         ensure_ui_thread ();
433         
434         if (!_film) {
435                 return;
436         }
437
438         stringstream s;
439
440         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
441                 (*i)->film_changed (p);
442         }
443                 
444         switch (p) {
445         case Film::NONE:
446                 break;
447         case Film::CONTENT:
448                 setup_content ();
449                 break;
450         case Film::CONTAINER:
451                 setup_container ();
452                 break;
453         case Film::NAME:
454                 checked_set (_name, _film->name());
455                 setup_dcp_name ();
456                 break;
457         case Film::DCP_CONTENT_TYPE:
458                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
459                 setup_dcp_name ();
460                 break;
461         case Film::SCALER:
462                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
463                 break;
464         case Film::BURN_SUBTITLES:
465                 checked_set (_burn_subtitles, _film->burn_subtitles ());
466                 break;
467         case Film::SIGNED:
468                 checked_set (_signed, _film->is_signed ());
469                 break;
470         case Film::ENCRYPTED:
471                 checked_set (_encrypted, _film->encrypted ());
472                 if (_film->encrypted ()) {
473                         _film->set_signed (true);
474                         _signed->Enable (false);
475                 } else {
476                         _signed->Enable (_generally_sensitive);
477                 }
478                 break;
479         case Film::RESOLUTION:
480                 checked_set (_resolution, _film->resolution() == RESOLUTION_2K ? 0 : 1);
481                 setup_dcp_name ();
482                 break;
483         case Film::J2K_BANDWIDTH:
484                 checked_set (_j2k_bandwidth, _film->j2k_bandwidth() / 1000000);
485                 break;
486         case Film::USE_ISDCF_NAME:
487                 checked_set (_use_isdcf_name, _film->use_isdcf_name ());
488                 setup_dcp_name ();
489                 break;
490         case Film::ISDCF_METADATA:
491                 setup_dcp_name ();
492                 break;
493         case Film::VIDEO_FRAME_RATE:
494         {
495                 bool done = false;
496                 for (unsigned int i = 0; i < _frame_rate_choice->GetCount(); ++i) {
497                         if (wx_to_std (_frame_rate_choice->GetString(i)) == boost::lexical_cast<string> (_film->video_frame_rate())) {
498                                 checked_set (_frame_rate_choice, i);
499                                 done = true;
500                                 break;
501                         }
502                 }
503
504                 if (!done) {
505                         checked_set (_frame_rate_choice, -1);
506                 }
507
508                 _frame_rate_spin->SetValue (_film->video_frame_rate ());
509
510                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
511                 break;
512         }
513         case Film::AUDIO_CHANNELS:
514                 checked_set (_audio_channels, _film->audio_channels ());
515                 setup_dcp_name ();
516                 break;
517         case Film::SEQUENCE_VIDEO:
518                 checked_set (_sequence_video, _film->sequence_video ());
519                 break;
520         case Film::THREE_D:
521                 checked_set (_three_d, _film->three_d ());
522                 setup_dcp_name ();
523                 break;
524         case Film::INTEROP:
525                 checked_set (_standard, _film->interop() ? 1 : 0);
526                 break;
527         }
528 }
529
530 void
531 FilmEditor::film_content_changed (int property)
532 {
533         ensure_ui_thread ();
534         
535         if (!_film) {
536                 /* We call this method ourselves (as well as using it as a signal handler)
537                    so _film can be 0.
538                 */
539                 return;
540         }
541
542         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
543                 (*i)->film_content_changed (property);
544         }
545
546         if (property == FFmpegContentProperty::AUDIO_STREAM || property == SubtitleContentProperty::SUBTITLE_USE) {
547                 setup_dcp_name ();
548         } else if (property == ContentProperty::PATH) {
549                 setup_content ();
550         } else if (property == ContentProperty::POSITION) {
551                 setup_content ();
552         }
553 }
554
555 void
556 FilmEditor::setup_container ()
557 {
558         int n = 0;
559         vector<Ratio const *> ratios = Ratio::all ();
560         vector<Ratio const *>::iterator i = ratios.begin ();
561         while (i != ratios.end() && *i != _film->container ()) {
562                 ++i;
563                 ++n;
564         }
565         
566         if (i == ratios.end()) {
567                 checked_set (_container, -1);
568         } else {
569                 checked_set (_container, n);
570         }
571         
572         setup_dcp_name ();
573 }       
574
575 /** Called when the container widget has been changed */
576 void
577 FilmEditor::container_changed ()
578 {
579         if (!_film) {
580                 return;
581         }
582
583         int const n = _container->GetSelection ();
584         if (n >= 0) {
585                 vector<Ratio const *> ratios = Ratio::all ();
586                 assert (n < int (ratios.size()));
587                 _film->set_container (ratios[n]);
588         }
589 }
590
591 /** Called when the DCP content type widget has been changed */
592 void
593 FilmEditor::dcp_content_type_changed ()
594 {
595         if (!_film) {
596                 return;
597         }
598
599         int const n = _dcp_content_type->GetSelection ();
600         if (n != wxNOT_FOUND) {
601                 _film->set_dcp_content_type (DCPContentType::from_index (n));
602         }
603 }
604
605 /** Sets the Film that we are editing */
606 void
607 FilmEditor::set_film (shared_ptr<Film> f)
608 {
609         set_general_sensitivity (f != 0);
610
611         if (_film == f) {
612                 return;
613         }
614         
615         _film = f;
616
617         if (_film) {
618                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
619                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _2));
620         }
621
622         if (_film) {
623                 FileChanged (_film->directory ());
624         } else {
625                 FileChanged ("");
626         }
627
628         film_changed (Film::NAME);
629         film_changed (Film::USE_ISDCF_NAME);
630         film_changed (Film::CONTENT);
631         film_changed (Film::DCP_CONTENT_TYPE);
632         film_changed (Film::CONTAINER);
633         film_changed (Film::RESOLUTION);
634         film_changed (Film::SCALER);
635         film_changed (Film::SIGNED);
636         film_changed (Film::BURN_SUBTITLES);
637         film_changed (Film::ENCRYPTED);
638         film_changed (Film::J2K_BANDWIDTH);
639         film_changed (Film::ISDCF_METADATA);
640         film_changed (Film::VIDEO_FRAME_RATE);
641         film_changed (Film::AUDIO_CHANNELS);
642         film_changed (Film::SEQUENCE_VIDEO);
643         film_changed (Film::THREE_D);
644         film_changed (Film::INTEROP);
645
646         if (!_film->content().empty ()) {
647                 set_selection (_film->content().front ());
648         }
649
650         content_selection_changed ();
651 }
652
653 void
654 FilmEditor::set_general_sensitivity (bool s)
655 {
656         _generally_sensitive = s;
657
658         /* Stuff in the Content / DCP tabs */
659         _name->Enable (s);
660         _use_isdcf_name->Enable (s);
661         _edit_isdcf_button->Enable (s);
662         _content->Enable (s);
663         _content_add_file->Enable (s);
664         _content_add_folder->Enable (s);
665         _content_remove->Enable (s);
666         _content_earlier->Enable (s);
667         _content_later->Enable (s);
668         _content_timeline->Enable (s);
669         _dcp_content_type->Enable (s);
670
671         bool si = s;
672         if (_film && _film->encrypted ()) {
673                 si = false;
674         }
675         _burn_subtitles->Enable (s);
676         _signed->Enable (si);
677         
678         _encrypted->Enable (s);
679         _frame_rate_choice->Enable (s);
680         _frame_rate_spin->Enable (s);
681         _audio_channels->Enable (s);
682         _j2k_bandwidth->Enable (s);
683         _container->Enable (s);
684         _best_frame_rate->Enable (s && _film && _film->best_video_frame_rate () != _film->video_frame_rate ());
685         _sequence_video->Enable (s);
686         _resolution->Enable (s);
687         _scaler->Enable (s);
688         _three_d->Enable (s);
689         _standard->Enable (s);
690
691         /* Set the panels in the content notebook */
692         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
693                 (*i)->Enable (s);
694         }
695 }
696
697 /** Called when the scaler widget has been changed */
698 void
699 FilmEditor::scaler_changed ()
700 {
701         if (!_film) {
702                 return;
703         }
704
705         int const n = _scaler->GetSelection ();
706         if (n >= 0) {
707                 _film->set_scaler (Scaler::from_index (n));
708         }
709 }
710
711 void
712 FilmEditor::use_isdcf_name_toggled ()
713 {
714         if (!_film) {
715                 return;
716         }
717
718         _film->set_use_isdcf_name (_use_isdcf_name->GetValue ());
719 }
720
721 void
722 FilmEditor::edit_isdcf_button_clicked ()
723 {
724         if (!_film) {
725                 return;
726         }
727
728         ISDCFMetadataDialog* d = new ISDCFMetadataDialog (this, _film->isdcf_metadata ());
729         d->ShowModal ();
730         _film->set_isdcf_metadata (d->isdcf_metadata ());
731         d->Destroy ();
732 }
733
734 void
735 FilmEditor::active_jobs_changed (bool a)
736 {
737         set_general_sensitivity (!a);
738 }
739
740 void
741 FilmEditor::setup_dcp_name ()
742 {
743         string s = _film->dcp_name (true);
744         if (s.length() > 28) {
745                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
746                 _dcp_name->SetToolTip (std_to_wx (s));
747         } else {
748                 _dcp_name->SetLabel (std_to_wx (s));
749         }
750 }
751
752 void
753 FilmEditor::best_frame_rate_clicked ()
754 {
755         if (!_film) {
756                 return;
757         }
758         
759         _film->set_video_frame_rate (_film->best_video_frame_rate ());
760 }
761
762 void
763 FilmEditor::setup_content ()
764 {
765         string selected_summary;
766         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
767         if (s != -1) {
768                 selected_summary = wx_to_std (_content->GetItemText (s));
769         }
770         
771         _content->DeleteAllItems ();
772
773         ContentList content = _film->content ();
774         sort (content.begin(), content.end(), ContentSorter ());
775         
776         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
777                 int const t = _content->GetItemCount ();
778                 bool const valid = (*i)->paths_valid ();
779
780                 string s = (*i)->summary ();
781                 if (!valid) {
782                         s = _("MISSING: ") + s;
783                 }
784
785                 _content->InsertItem (t, std_to_wx (s));
786
787                 if ((*i)->summary() == selected_summary) {
788                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
789                 }
790
791                 if (!valid) {
792                         _content->SetItemTextColour (t, *wxRED);
793                 }
794         }
795
796         if (selected_summary.empty () && !content.empty ()) {
797                 /* Select the item of content if none was selected before */
798                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
799         }
800 }
801
802 void
803 FilmEditor::content_add_file_clicked ()
804 {
805         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
806            non-Latin filenames or paths.
807         */
808         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE | wxFD_CHANGE_DIR);
809         int const r = d->ShowModal ();
810
811         if (r != wxID_OK) {
812                 d->Destroy ();
813                 return;
814         }
815
816         wxArrayString paths;
817         d->GetPaths (paths);
818
819         /* XXX: check for lots of files here and do something */
820
821         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
822                 _film->examine_and_add_content (content_factory (_film, wx_to_std (paths[i])));
823         }
824
825         d->Destroy ();
826 }
827
828 void
829 FilmEditor::content_add_folder_clicked ()
830 {
831         wxDirDialog* d = new wxDirDialog (this, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
832         int const r = d->ShowModal ();
833         d->Destroy ();
834         
835         if (r != wxID_OK) {
836                 return;
837         }
838
839         shared_ptr<ImageContent> ic;
840         
841         try {
842                 ic.reset (new ImageContent (_film, boost::filesystem::path (wx_to_std (d->GetPath ()))));
843         } catch (FileError& e) {
844                 error_dialog (this, std_to_wx (e.what ()));
845                 return;
846         }
847
848         _film->examine_and_add_content (ic);
849 }
850
851 void
852 FilmEditor::content_remove_clicked ()
853 {
854         ContentList c = selected_content ();
855         if (c.size() == 1) {
856                 _film->remove_content (c.front ());
857         }
858
859         content_selection_changed ();
860 }
861
862 void
863 FilmEditor::content_selection_changed ()
864 {
865         setup_content_sensitivity ();
866
867         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
868                 (*i)->content_selection_changed ();
869         }
870 }
871
872 /** Set up broad sensitivity based on the type of content that is selected */
873 void
874 FilmEditor::setup_content_sensitivity ()
875 {
876         _content_add_file->Enable (_generally_sensitive);
877         _content_add_folder->Enable (_generally_sensitive);
878
879         ContentList selection = selected_content ();
880         VideoContentList video_selection = selected_video_content ();
881         AudioContentList audio_selection = selected_audio_content ();
882
883         _content_remove->Enable   (selection.size() == 1 && _generally_sensitive);
884         _content_earlier->Enable  (selection.size() == 1 && _generally_sensitive);
885         _content_later->Enable    (selection.size() == 1 && _generally_sensitive);
886         _content_timeline->Enable (!_film->content().empty() && _generally_sensitive);
887
888         _video_panel->Enable    (video_selection.size() > 0 && _generally_sensitive);
889         _audio_panel->Enable    (audio_selection.size() > 0 && _generally_sensitive);
890         _subtitle_panel->Enable (selection.size() == 1 && dynamic_pointer_cast<SubtitleContent> (selection.front()) && _generally_sensitive);
891         _timing_panel->Enable   (selection.size() == 1 && _generally_sensitive);
892 }
893
894 ContentList
895 FilmEditor::selected_content ()
896 {
897         ContentList sel;
898         long int s = -1;
899         while (true) {
900                 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
901                 if (s == -1) {
902                         break;
903                 }
904
905                 if (s < int (_film->content().size ())) {
906                         sel.push_back (_film->content()[s]);
907                 }
908         }
909
910         return sel;
911 }
912
913 VideoContentList
914 FilmEditor::selected_video_content ()
915 {
916         ContentList c = selected_content ();
917         VideoContentList vc;
918         
919         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
920                 shared_ptr<VideoContent> t = dynamic_pointer_cast<VideoContent> (*i);
921                 if (t) {
922                         vc.push_back (t);
923                 }
924         }
925
926         return vc;
927 }
928
929 AudioContentList
930 FilmEditor::selected_audio_content ()
931 {
932         ContentList c = selected_content ();
933         AudioContentList ac;
934         
935         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
936                 shared_ptr<AudioContent> t = dynamic_pointer_cast<AudioContent> (*i);
937                 if (t) {
938                         ac.push_back (t);
939                 }
940         }
941
942         return ac;
943 }
944
945 SubtitleContentList
946 FilmEditor::selected_subtitle_content ()
947 {
948         ContentList c = selected_content ();
949         SubtitleContentList sc;
950         
951         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
952                 shared_ptr<SubtitleContent> t = dynamic_pointer_cast<SubtitleContent> (*i);
953                 if (t) {
954                         sc.push_back (t);
955                 }
956         }
957
958         return sc;
959 }
960
961 FFmpegContentList
962 FilmEditor::selected_ffmpeg_content ()
963 {
964         ContentList c = selected_content ();
965         FFmpegContentList sc;
966         
967         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
968                 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (*i);
969                 if (t) {
970                         sc.push_back (t);
971                 }
972         }
973
974         return sc;
975 }
976
977 void
978 FilmEditor::content_timeline_clicked ()
979 {
980         if (_timeline_dialog) {
981                 _timeline_dialog->Destroy ();
982                 _timeline_dialog = 0;
983         }
984         
985         _timeline_dialog = new TimelineDialog (this, _film);
986         _timeline_dialog->Show ();
987 }
988
989 void
990 FilmEditor::set_selection (weak_ptr<Content> wc)
991 {
992         ContentList content = _film->content ();
993         for (size_t i = 0; i < content.size(); ++i) {
994                 if (content[i] == wc.lock ()) {
995                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
996                 } else {
997                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
998                 }
999         }
1000 }
1001
1002 void
1003 FilmEditor::sequence_video_changed ()
1004 {
1005         if (!_film) {
1006                 return;
1007         }
1008         
1009         _film->set_sequence_video (_sequence_video->GetValue ());
1010 }
1011
1012 void
1013 FilmEditor::content_right_click (wxListEvent& ev)
1014 {
1015         _menu.popup (_film, selected_content (), ev.GetPoint ());
1016 }
1017
1018 void
1019 FilmEditor::three_d_changed ()
1020 {
1021         if (!_film) {
1022                 return;
1023         }
1024
1025         _film->set_three_d (_three_d->GetValue ());
1026 }
1027
1028 void
1029 FilmEditor::content_earlier_clicked ()
1030 {
1031         ContentList sel = selected_content ();
1032         if (sel.size() == 1) {
1033                 _film->move_content_earlier (sel.front ());
1034                 content_selection_changed ();
1035         }
1036 }
1037
1038 void
1039 FilmEditor::content_later_clicked ()
1040 {
1041         ContentList sel = selected_content ();
1042         if (sel.size() == 1) {
1043                 _film->move_content_later (sel.front ());
1044                 content_selection_changed ();
1045         }
1046 }
1047
1048 void
1049 FilmEditor::config_changed ()
1050 {
1051         _j2k_bandwidth->SetRange (1, Config::instance()->maximum_j2k_bandwidth() / 1000000);
1052         setup_frame_rate_widget ();
1053 }
1054
1055 void
1056 FilmEditor::setup_frame_rate_widget ()
1057 {
1058         if (Config::instance()->allow_any_dcp_frame_rate ()) {
1059                 _frame_rate_choice->Hide ();
1060                 _frame_rate_spin->Show ();
1061         } else {
1062                 _frame_rate_choice->Show ();
1063                 _frame_rate_spin->Hide ();
1064         }
1065
1066         _frame_rate_sizer->Layout ();
1067 }