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