Remove some dcp prefixes; better logging of content on DCP creation.
[dcpomatic.git] / src / wx / film_editor.cc
1 /*
2     Copyright (C) 2012-2013 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/still_image_content.h"
40 #include "lib/sndfile_content.h"
41 #include "lib/dcp_content_type.h"
42 #include "lib/sound_processor.h"
43 #include "lib/scaler.h"
44 #include "timecode.h"
45 #include "wx_util.h"
46 #include "film_editor.h"
47 #include "dci_metadata_dialog.h"
48 #include "timeline_dialog.h"
49 #include "timing_panel.h"
50 #include "subtitle_panel.h"
51 #include "audio_panel.h"
52 #include "video_panel.h"
53
54 using std::string;
55 using std::cout;
56 using std::stringstream;
57 using std::pair;
58 using std::fixed;
59 using std::setprecision;
60 using std::list;
61 using std::vector;
62 using std::max;
63 using boost::shared_ptr;
64 using boost::weak_ptr;
65 using boost::dynamic_pointer_cast;
66 using boost::lexical_cast;
67
68 /** @param f Film to edit */
69 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
70         : wxPanel (parent)
71         , _menu (f, this)
72         , _generally_sensitive (true)
73         , _timeline_dialog (0)
74 {
75         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
76
77         _main_notebook = new wxNotebook (this, wxID_ANY);
78         s->Add (_main_notebook, 1);
79
80         make_content_panel ();
81         _main_notebook->AddPage (_content_panel, _("Content"), true);
82         make_dcp_panel ();
83         _main_notebook->AddPage (_dcp_panel, _("DCP"), false);
84         
85         set_film (f);
86         connect_to_widgets ();
87
88         JobManager::instance()->ActiveJobsChanged.connect (
89                 bind (&FilmEditor::active_jobs_changed, this, _1)
90                 );
91         
92         SetSizerAndFit (s);
93 }
94
95 void
96 FilmEditor::make_dcp_panel ()
97 {
98         _dcp_panel = new wxPanel (_main_notebook);
99         _dcp_sizer = new wxBoxSizer (wxVERTICAL);
100         _dcp_panel->SetSizer (_dcp_sizer);
101
102         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
103         _dcp_sizer->Add (grid, 0, wxEXPAND | wxALL, 8);
104
105         int r = 0;
106         
107         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Name"), true, wxGBPosition (r, 0));
108         _name = new wxTextCtrl (_dcp_panel, wxID_ANY);
109         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxLEFT | wxRIGHT);
110         ++r;
111         
112         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Name"), true, wxGBPosition (r, 0));
113         _dcp_name = new wxStaticText (_dcp_panel, wxID_ANY, wxT (""));
114         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
115         ++r;
116
117         int flags = wxALIGN_CENTER_VERTICAL;
118 #ifdef __WXOSX__
119         flags |= wxALIGN_RIGHT;
120 #endif  
121
122         _use_dci_name = new wxCheckBox (_dcp_panel, wxID_ANY, _("Use DCI name"));
123         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, flags);
124         _edit_dci_button = new wxButton (_dcp_panel, wxID_ANY, _("Details..."));
125         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
126         ++r;
127
128         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Container"), true, wxGBPosition (r, 0));
129         _container = new wxChoice (_dcp_panel, wxID_ANY);
130         grid->Add (_container, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
131         ++r;
132
133         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Content Type"), true, wxGBPosition (r, 0));
134         _dcp_content_type = new wxChoice (_dcp_panel, wxID_ANY);
135         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
136         ++r;
137
138         {
139                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Frame Rate"), true, wxGBPosition (r, 0));
140                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
141                 _frame_rate = new wxChoice (_dcp_panel, wxID_ANY);
142                 s->Add (_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
143                 _best_frame_rate = new wxButton (_dcp_panel, wxID_ANY, _("Use best"));
144                 s->Add (_best_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND);
145                 grid->Add (s, wxGBPosition (r, 1));
146         }
147         ++r;
148
149         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Audio channels"), true, wxGBPosition (r, 0));
150         _audio_channels = new wxSpinCtrl (_dcp_panel, wxID_ANY);
151         grid->Add (_audio_channels, wxGBPosition (r, 1));
152         ++r;
153
154         _three_d = new wxCheckBox (_dcp_panel, wxID_ANY, _("3D"));
155         grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
156         ++r;
157
158         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Resolution"), true, wxGBPosition (r, 0));
159         _resolution = new wxChoice (_dcp_panel, wxID_ANY);
160         grid->Add (_resolution, wxGBPosition (r, 1));
161         ++r;
162
163         {
164                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("JPEG2000 bandwidth"), true, wxGBPosition (r, 0));
165                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
166                 _j2k_bandwidth = new wxSpinCtrl (_dcp_panel, wxID_ANY);
167                 s->Add (_j2k_bandwidth, 1);
168                 add_label_to_sizer (s, _dcp_panel, _("MBps"), false);
169                 grid->Add (s, wxGBPosition (r, 1));
170         }
171         ++r;
172
173         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Scaler"), true, wxGBPosition (r, 0));
174         _scaler = new wxChoice (_dcp_panel, wxID_ANY);
175         grid->Add (_scaler, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
176         ++r;
177
178         vector<Scaler const *> const sc = Scaler::all ();
179         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
180                 _scaler->Append (std_to_wx ((*i)->name()));
181         }
182
183         vector<Ratio const *> const ratio = Ratio::all ();
184         for (vector<Ratio const *>::const_iterator i = ratio.begin(); i != ratio.end(); ++i) {
185                 _container->Append (std_to_wx ((*i)->nickname ()));
186         }
187
188         vector<DCPContentType const *> const ct = DCPContentType::all ();
189         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
190                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
191         }
192
193         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
194         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
195                 _frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
196         }
197
198         _audio_channels->SetRange (0, MAX_AUDIO_CHANNELS);
199         _j2k_bandwidth->SetRange (50, 250);
200
201         _resolution->Append (_("2K"));
202         _resolution->Append (_("4K"));
203 }
204
205 void
206 FilmEditor::connect_to_widgets ()
207 {
208         _name->Connect                   (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED,         wxCommandEventHandler (FilmEditor::name_changed), 0, this);
209         _use_dci_name->Connect           (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
210         _edit_dci_button->Connect        (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
211         _container->Connect              (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::container_changed), 0, this);
212         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED,   wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
213         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
214         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK,wxListEventHandler    (FilmEditor::content_right_click), 0, this);
215         _content_add->Connect            (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_add_clicked), 0, this);
216         _content_remove->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_remove_clicked), 0, this);
217         _content_timeline->Connect       (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_timeline_clicked), 0, this);
218         _scaler->Connect                 (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
219         _dcp_content_type->Connect       (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
220         _frame_rate->Connect             (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::frame_rate_changed), 0, this);
221         _best_frame_rate->Connect        (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::best_frame_rate_clicked), 0, this);
222         _audio_channels->Connect         (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_channels_changed), 0, this);
223         _j2k_bandwidth->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
224         _resolution->Connect             (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::resolution_changed), 0, this);
225         _sequence_video->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::sequence_video_changed), 0, this);
226         _three_d->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&FilmEditor::three_d_changed, this));
227 }
228
229 void
230 FilmEditor::make_content_panel ()
231 {
232         _content_panel = new wxPanel (_main_notebook);
233         _content_sizer = new wxBoxSizer (wxVERTICAL);
234         _content_panel->SetSizer (_content_sizer);
235
236         {
237                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
238                 
239                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
240                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
241
242                 _content->InsertColumn (0, wxT(""));
243                 _content->SetColumnWidth (0, 512);
244
245                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
246                 _content_add = new wxButton (_content_panel, wxID_ANY, _("Add..."));
247                 b->Add (_content_add, 1, wxEXPAND | wxLEFT | wxRIGHT);
248                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
249                 b->Add (_content_remove, 1, wxEXPAND | wxLEFT | wxRIGHT);
250                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
251                 b->Add (_content_timeline, 1, wxEXPAND | wxLEFT | wxRIGHT);
252
253                 s->Add (b, 0, wxALL, 4);
254
255                 _content_sizer->Add (s, 0.75, wxEXPAND | wxALL, 6);
256         }
257
258         _sequence_video = new wxCheckBox (_content_panel, wxID_ANY, _("Keep video in sequence"));
259         _content_sizer->Add (_sequence_video);
260
261         _content_notebook = new wxNotebook (_content_panel, wxID_ANY);
262         _content_sizer->Add (_content_notebook, 1, wxEXPAND | wxTOP, 6);
263
264         _video_panel = new VideoPanel (this);
265         _panels.push_back (_video_panel);
266         _audio_panel = new AudioPanel (this);
267         _panels.push_back (_audio_panel);
268         _subtitle_panel = new SubtitlePanel (this);
269         _panels.push_back (_subtitle_panel);
270         _timing_panel = new TimingPanel (this);
271         _panels.push_back (_timing_panel);
272 }
273
274 /** Called when the name widget has been changed */
275 void
276 FilmEditor::name_changed (wxCommandEvent &)
277 {
278         if (!_film) {
279                 return;
280         }
281
282         _film->set_name (string (_name->GetValue().mb_str()));
283 }
284
285 void
286 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
287 {
288         if (!_film) {
289                 return;
290         }
291         
292         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
293 }
294
295 void
296 FilmEditor::frame_rate_changed (wxCommandEvent &)
297 {
298         if (!_film) {
299                 return;
300         }
301
302         _film->set_video_frame_rate (
303                 boost::lexical_cast<int> (
304                         wx_to_std (_frame_rate->GetString (_frame_rate->GetSelection ()))
305                         )
306                 );
307 }
308
309 void
310 FilmEditor::audio_channels_changed (wxCommandEvent &)
311 {
312         if (!_film) {
313                 return;
314         }
315
316         _film->set_audio_channels (_audio_channels->GetValue ());
317 }
318
319 void
320 FilmEditor::resolution_changed (wxCommandEvent &)
321 {
322         if (!_film) {
323                 return;
324         }
325
326         _film->set_resolution (_resolution->GetSelection() == 0 ? RESOLUTION_2K : RESOLUTION_4K);
327 }
328
329
330 /** Called when the metadata stored in the Film object has changed;
331  *  so that we can update the GUI.
332  *  @param p Property of the Film that has changed.
333  */
334 void
335 FilmEditor::film_changed (Film::Property p)
336 {
337         ensure_ui_thread ();
338         
339         if (!_film) {
340                 return;
341         }
342
343         stringstream s;
344
345         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
346                 (*i)->film_changed (p);
347         }
348                 
349         switch (p) {
350         case Film::NONE:
351                 break;
352         case Film::CONTENT:
353                 setup_content ();
354                 break;
355         case Film::CONTAINER:
356                 setup_container ();
357                 break;
358         case Film::NAME:
359                 checked_set (_name, _film->name());
360                 setup_dcp_name ();
361                 break;
362         case Film::WITH_SUBTITLES:
363                 setup_dcp_name ();
364                 break;
365         case Film::DCP_CONTENT_TYPE:
366                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
367                 setup_dcp_name ();
368                 break;
369         case Film::SCALER:
370                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
371                 break;
372         case Film::RESOLUTION:
373                 checked_set (_resolution, _film->resolution() == RESOLUTION_2K ? 0 : 1);
374                 setup_dcp_name ();
375                 break;
376         case Film::J2K_BANDWIDTH:
377                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
378                 break;
379         case Film::USE_DCI_NAME:
380                 checked_set (_use_dci_name, _film->use_dci_name ());
381                 setup_dcp_name ();
382                 break;
383         case Film::DCI_METADATA:
384                 setup_dcp_name ();
385                 break;
386         case Film::VIDEO_FRAME_RATE:
387         {
388                 bool done = false;
389                 for (unsigned int i = 0; i < _frame_rate->GetCount(); ++i) {
390                         if (wx_to_std (_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->video_frame_rate())) {
391                                 checked_set (_frame_rate, i);
392                                 done = true;
393                                 break;
394                         }
395                 }
396
397                 if (!done) {
398                         checked_set (_frame_rate, -1);
399                 }
400
401                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
402                 break;
403         }
404         case Film::AUDIO_CHANNELS:
405                 _audio_channels->SetValue (_film->audio_channels ());
406                 setup_dcp_name ();
407                 break;
408         case Film::SEQUENCE_VIDEO:
409                 checked_set (_sequence_video, _film->sequence_video ());
410                 break;
411         case Film::THREE_D:
412                 checked_set (_three_d, _film->three_d ());
413                 break;
414         }
415 }
416
417 void
418 FilmEditor::film_content_changed (weak_ptr<Content> weak_content, int property)
419 {
420         ensure_ui_thread ();
421         
422         if (!_film) {
423                 /* We call this method ourselves (as well as using it as a signal handler)
424                    so _film can be 0.
425                 */
426                 return;
427         }
428
429         shared_ptr<Content> content = weak_content.lock ();
430         if (!content || content != selected_content ()) {
431                 return;
432         }
433
434         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
435                 (*i)->film_content_changed (content, property);
436         }
437
438         if (property == FFmpegContentProperty::AUDIO_STREAM) {
439                 setup_dcp_name ();
440         }
441 }
442
443 void
444 FilmEditor::setup_container ()
445 {
446         int n = 0;
447         vector<Ratio const *> ratios = Ratio::all ();
448         vector<Ratio const *>::iterator i = ratios.begin ();
449         while (i != ratios.end() && *i != _film->container ()) {
450                 ++i;
451                 ++n;
452         }
453         
454         if (i == ratios.end()) {
455                 checked_set (_container, -1);
456         } else {
457                 checked_set (_container, n);
458         }
459         
460         setup_dcp_name ();
461 }       
462
463 /** Called when the container widget has been changed */
464 void
465 FilmEditor::container_changed (wxCommandEvent &)
466 {
467         if (!_film) {
468                 return;
469         }
470
471         int const n = _container->GetSelection ();
472         if (n >= 0) {
473                 vector<Ratio const *> ratios = Ratio::all ();
474                 assert (n < int (ratios.size()));
475                 _film->set_container (ratios[n]);
476         }
477 }
478
479 /** Called when the DCP content type widget has been changed */
480 void
481 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
482 {
483         if (!_film) {
484                 return;
485         }
486
487         int const n = _dcp_content_type->GetSelection ();
488         if (n != wxNOT_FOUND) {
489                 _film->set_dcp_content_type (DCPContentType::from_index (n));
490         }
491 }
492
493 /** Sets the Film that we are editing */
494 void
495 FilmEditor::set_film (shared_ptr<Film> f)
496 {
497         set_general_sensitivity (f != 0);
498
499         wxListEvent ev;
500         content_selection_changed (ev);
501
502         if (_film == f) {
503                 return;
504         }
505         
506         _film = f;
507
508         if (_film) {
509                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
510                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
511         }
512
513         if (_film) {
514                 FileChanged (_film->directory ());
515         } else {
516                 FileChanged ("");
517         }
518
519         film_changed (Film::NAME);
520         film_changed (Film::USE_DCI_NAME);
521         film_changed (Film::CONTENT);
522         film_changed (Film::DCP_CONTENT_TYPE);
523         film_changed (Film::CONTAINER);
524         film_changed (Film::RESOLUTION);
525         film_changed (Film::SCALER);
526         film_changed (Film::WITH_SUBTITLES);
527         film_changed (Film::J2K_BANDWIDTH);
528         film_changed (Film::DCI_METADATA);
529         film_changed (Film::VIDEO_FRAME_RATE);
530         film_changed (Film::AUDIO_CHANNELS);
531         film_changed (Film::SEQUENCE_VIDEO);
532         film_changed (Film::THREE_D);
533
534         if (!_film->content().empty ()) {
535                 set_selection (_film->content().front ());
536         }
537 }
538
539 void
540 FilmEditor::set_general_sensitivity (bool s)
541 {
542         _generally_sensitive = s;
543
544         /* Stuff in the Content / DCP tabs */
545         _name->Enable (s);
546         _use_dci_name->Enable (s);
547         _edit_dci_button->Enable (s);
548         _content->Enable (s);
549         _content_add->Enable (s);
550         _content_remove->Enable (s);
551         _content_timeline->Enable (s);
552         _dcp_content_type->Enable (s);
553         _frame_rate->Enable (s);
554         _audio_channels->Enable (s);
555         _j2k_bandwidth->Enable (s);
556         _container->Enable (s);
557         _best_frame_rate->Enable (s && _film && _film->best_video_frame_rate () != _film->video_frame_rate ());
558         _sequence_video->Enable (s);
559         _resolution->Enable (s);
560         _scaler->Enable (s);
561         _three_d->Enable (s);
562
563         /* Set the panels in the content notebook */
564         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
565                 (*i)->Enable (s);
566         }
567 }
568
569 /** Called when the scaler widget has been changed */
570 void
571 FilmEditor::scaler_changed (wxCommandEvent &)
572 {
573         if (!_film) {
574                 return;
575         }
576
577         int const n = _scaler->GetSelection ();
578         if (n >= 0) {
579                 _film->set_scaler (Scaler::from_index (n));
580         }
581 }
582
583 void
584 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
585 {
586         if (!_film) {
587                 return;
588         }
589
590         _film->set_use_dci_name (_use_dci_name->GetValue ());
591 }
592
593 void
594 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
595 {
596         if (!_film) {
597                 return;
598         }
599
600         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
601         d->ShowModal ();
602         _film->set_dci_metadata (d->dci_metadata ());
603         d->Destroy ();
604 }
605
606 void
607 FilmEditor::active_jobs_changed (bool a)
608 {
609         set_general_sensitivity (!a);
610 }
611
612 void
613 FilmEditor::setup_dcp_name ()
614 {
615         string s = _film->dcp_name (true);
616         if (s.length() > 28) {
617                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
618                 _dcp_name->SetToolTip (std_to_wx (s));
619         } else {
620                 _dcp_name->SetLabel (std_to_wx (s));
621         }
622 }
623
624 void
625 FilmEditor::best_frame_rate_clicked (wxCommandEvent &)
626 {
627         if (!_film) {
628                 return;
629         }
630         
631         _film->set_video_frame_rate (_film->best_video_frame_rate ());
632 }
633
634 void
635 FilmEditor::setup_content ()
636 {
637         string selected_summary;
638         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
639         if (s != -1) {
640                 selected_summary = wx_to_std (_content->GetItemText (s));
641         }
642         
643         _content->DeleteAllItems ();
644
645         ContentList content = _film->content ();
646         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
647                 int const t = _content->GetItemCount ();
648                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
649                 if ((*i)->summary() == selected_summary) {
650                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
651                 }
652         }
653
654         if (selected_summary.empty () && !content.empty ()) {
655                 /* Select the item of content if none was selected before */
656                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
657         }
658 }
659
660 void
661 FilmEditor::content_add_clicked (wxCommandEvent &)
662 {
663         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
664         int const r = d->ShowModal ();
665         d->Destroy ();
666
667         if (r != wxID_OK) {
668                 return;
669         }
670
671         wxArrayString paths;
672         d->GetPaths (paths);
673
674         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
675                 boost::filesystem::path p (wx_to_std (paths[i]));
676
677                 shared_ptr<Content> c;
678
679                 if (StillImageContent::valid_file (p)) {
680                         c.reset (new StillImageContent (_film, p));
681                 } else if (SndfileContent::valid_file (p)) {
682                         c.reset (new SndfileContent (_film, p));
683                 } else {
684                         c.reset (new FFmpegContent (_film, p));
685                 }
686
687                 _film->examine_and_add_content (c);
688         }
689 }
690
691 void
692 FilmEditor::content_remove_clicked (wxCommandEvent &)
693 {
694         shared_ptr<Content> c = selected_content ();
695         if (c) {
696                 _film->remove_content (c);
697         }
698
699         wxListEvent ev;
700         content_selection_changed (ev);
701 }
702
703 void
704 FilmEditor::content_selection_changed (wxListEvent &)
705 {
706         setup_content_sensitivity ();
707         shared_ptr<Content> s = selected_content ();
708
709         /* All other sensitivity in content panels should be triggered by
710            one of these.
711         */
712         film_content_changed (s, ContentProperty::START);
713         film_content_changed (s, ContentProperty::LENGTH);
714         film_content_changed (s, VideoContentProperty::VIDEO_CROP);
715         film_content_changed (s, VideoContentProperty::VIDEO_RATIO);
716         film_content_changed (s, VideoContentProperty::VIDEO_FRAME_TYPE);
717         film_content_changed (s, AudioContentProperty::AUDIO_GAIN);
718         film_content_changed (s, AudioContentProperty::AUDIO_DELAY);
719         film_content_changed (s, AudioContentProperty::AUDIO_MAPPING);
720         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAM);
721         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAMS);
722         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAM);
723         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAMS);
724         film_content_changed (s, FFmpegContentProperty::FILTERS);
725         film_content_changed (s, SubtitleContentProperty::SUBTITLE_OFFSET);
726         film_content_changed (s, SubtitleContentProperty::SUBTITLE_SCALE);
727 }
728
729 /** Set up broad sensitivity based on the type of content that is selected */
730 void
731 FilmEditor::setup_content_sensitivity ()
732 {
733         _content_add->Enable (_generally_sensitive);
734
735         shared_ptr<Content> selection = selected_content ();
736
737         _content_remove->Enable (selection && _generally_sensitive);
738         _content_timeline->Enable (_generally_sensitive);
739
740         _video_panel->Enable    (selection && dynamic_pointer_cast<VideoContent>  (selection) && _generally_sensitive);
741         _audio_panel->Enable    (selection && dynamic_pointer_cast<AudioContent>  (selection) && _generally_sensitive);
742         _subtitle_panel->Enable (selection && dynamic_pointer_cast<FFmpegContent> (selection) && _generally_sensitive);
743         _timing_panel->Enable   (selection && _generally_sensitive);
744 }
745
746 shared_ptr<Content>
747 FilmEditor::selected_content ()
748 {
749         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
750         if (s == -1) {
751                 return shared_ptr<Content> ();
752         }
753
754         ContentList c = _film->content ();
755         if (s < 0 || size_t (s) >= c.size ()) {
756                 return shared_ptr<Content> ();
757         }
758         
759         return c[s];
760 }
761
762 shared_ptr<VideoContent>
763 FilmEditor::selected_video_content ()
764 {
765         shared_ptr<Content> c = selected_content ();
766         if (!c) {
767                 return shared_ptr<VideoContent> ();
768         }
769
770         return dynamic_pointer_cast<VideoContent> (c);
771 }
772
773 shared_ptr<AudioContent>
774 FilmEditor::selected_audio_content ()
775 {
776         shared_ptr<Content> c = selected_content ();
777         if (!c) {
778                 return shared_ptr<AudioContent> ();
779         }
780
781         return dynamic_pointer_cast<AudioContent> (c);
782 }
783
784 shared_ptr<SubtitleContent>
785 FilmEditor::selected_subtitle_content ()
786 {
787         shared_ptr<Content> c = selected_content ();
788         if (!c) {
789                 return shared_ptr<SubtitleContent> ();
790         }
791
792         return dynamic_pointer_cast<SubtitleContent> (c);
793 }
794
795 void
796 FilmEditor::content_timeline_clicked (wxCommandEvent &)
797 {
798         if (_timeline_dialog) {
799                 _timeline_dialog->Destroy ();
800                 _timeline_dialog = 0;
801         }
802         
803         _timeline_dialog = new TimelineDialog (this, _film);
804         _timeline_dialog->Show ();
805 }
806
807 void
808 FilmEditor::set_selection (weak_ptr<Content> wc)
809 {
810         ContentList content = _film->content ();
811         for (size_t i = 0; i < content.size(); ++i) {
812                 if (content[i] == wc.lock ()) {
813                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
814                 } else {
815                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
816                 }
817         }
818 }
819
820 void
821 FilmEditor::sequence_video_changed (wxCommandEvent &)
822 {
823         if (!_film) {
824                 return;
825         }
826         
827         _film->set_sequence_video (_sequence_video->GetValue ());
828 }
829
830 void
831 FilmEditor::content_right_click (wxListEvent& ev)
832 {
833         ContentList cl;
834         cl.push_back (selected_content ());
835         _menu.popup (cl, ev.GetPoint ());
836 }
837
838 void
839 FilmEditor::three_d_changed ()
840 {
841         if (!_film) {
842                 return;
843         }
844
845         _film->set_three_d (_three_d->GetValue ());
846 }