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