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