Setup DCP name after changing 3D switch.
[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->Bind             (wxEVT_COMMAND_TEXT_UPDATED,          boost::bind (&FilmEditor::name_changed, this));
209         _use_dci_name->Bind     (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::use_dci_name_toggled, this));
210         _edit_dci_button->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::edit_dci_button_clicked, this));
211         _container->Bind        (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::container_changed, this));
212         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_SELECTED,    boost::bind (&FilmEditor::content_selection_changed, this));
213         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_DESELECTED,  boost::bind (&FilmEditor::content_selection_changed, this));
214         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, boost::bind (&FilmEditor::content_right_click, this, _1));
215         _content_add->Bind      (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_add_clicked, this));
216         _content_remove->Bind   (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_remove_clicked, this));
217         _content_timeline->Bind (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_timeline_clicked, this));
218         _scaler->Bind           (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::scaler_changed, this));
219         _dcp_content_type->Bind (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::dcp_content_type_changed, this));
220         _frame_rate->Bind       (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::frame_rate_changed, this));
221         _best_frame_rate->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::best_frame_rate_clicked, this));
222         _audio_channels->Bind   (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::audio_channels_changed, this));
223         _j2k_bandwidth->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::j2k_bandwidth_changed, this));
224         _resolution->Bind       (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::resolution_changed, this));
225         _sequence_video->Bind   (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::sequence_video_changed, 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 ()
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 ()
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 ()
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 ()
311 {
312         if (!_film) {
313                 return;
314         }
315
316         _film->set_audio_channels (_audio_channels->GetValue ());
317 }
318
319 void
320 FilmEditor::resolution_changed ()
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                 setup_dcp_name ();
414                 break;
415         }
416 }
417
418 void
419 FilmEditor::film_content_changed (weak_ptr<Content> weak_content, int property)
420 {
421         ensure_ui_thread ();
422         
423         if (!_film) {
424                 /* We call this method ourselves (as well as using it as a signal handler)
425                    so _film can be 0.
426                 */
427                 return;
428         }
429
430         shared_ptr<Content> content = weak_content.lock ();
431         if (!content || content != selected_content ()) {
432                 return;
433         }
434
435         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
436                 (*i)->film_content_changed (content, property);
437         }
438
439         if (property == FFmpegContentProperty::AUDIO_STREAM) {
440                 setup_dcp_name ();
441         }
442 }
443
444 void
445 FilmEditor::setup_container ()
446 {
447         int n = 0;
448         vector<Ratio const *> ratios = Ratio::all ();
449         vector<Ratio const *>::iterator i = ratios.begin ();
450         while (i != ratios.end() && *i != _film->container ()) {
451                 ++i;
452                 ++n;
453         }
454         
455         if (i == ratios.end()) {
456                 checked_set (_container, -1);
457         } else {
458                 checked_set (_container, n);
459         }
460         
461         setup_dcp_name ();
462 }       
463
464 /** Called when the container widget has been changed */
465 void
466 FilmEditor::container_changed ()
467 {
468         if (!_film) {
469                 return;
470         }
471
472         int const n = _container->GetSelection ();
473         if (n >= 0) {
474                 vector<Ratio const *> ratios = Ratio::all ();
475                 assert (n < int (ratios.size()));
476                 _film->set_container (ratios[n]);
477         }
478 }
479
480 /** Called when the DCP content type widget has been changed */
481 void
482 FilmEditor::dcp_content_type_changed ()
483 {
484         if (!_film) {
485                 return;
486         }
487
488         int const n = _dcp_content_type->GetSelection ();
489         if (n != wxNOT_FOUND) {
490                 _film->set_dcp_content_type (DCPContentType::from_index (n));
491         }
492 }
493
494 /** Sets the Film that we are editing */
495 void
496 FilmEditor::set_film (shared_ptr<Film> f)
497 {
498         set_general_sensitivity (f != 0);
499
500         content_selection_changed ();
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 ()
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 ()
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 ()
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 ()
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 ()
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 ()
693 {
694         shared_ptr<Content> c = selected_content ();
695         if (c) {
696                 _film->remove_content (c);
697         }
698
699         content_selection_changed ();
700 }
701
702 void
703 FilmEditor::content_selection_changed ()
704 {
705         setup_content_sensitivity ();
706         shared_ptr<Content> s = selected_content ();
707
708         /* All other sensitivity in content panels should be triggered by
709            one of these.
710         */
711         film_content_changed (s, ContentProperty::START);
712         film_content_changed (s, ContentProperty::LENGTH);
713         film_content_changed (s, VideoContentProperty::VIDEO_CROP);
714         film_content_changed (s, VideoContentProperty::VIDEO_RATIO);
715         film_content_changed (s, VideoContentProperty::VIDEO_FRAME_TYPE);
716         film_content_changed (s, AudioContentProperty::AUDIO_GAIN);
717         film_content_changed (s, AudioContentProperty::AUDIO_DELAY);
718         film_content_changed (s, AudioContentProperty::AUDIO_MAPPING);
719         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAM);
720         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAMS);
721         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAM);
722         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAMS);
723         film_content_changed (s, FFmpegContentProperty::FILTERS);
724         film_content_changed (s, SubtitleContentProperty::SUBTITLE_OFFSET);
725         film_content_changed (s, SubtitleContentProperty::SUBTITLE_SCALE);
726 }
727
728 /** Set up broad sensitivity based on the type of content that is selected */
729 void
730 FilmEditor::setup_content_sensitivity ()
731 {
732         _content_add->Enable (_generally_sensitive);
733
734         shared_ptr<Content> selection = selected_content ();
735
736         _content_remove->Enable (selection && _generally_sensitive);
737         _content_timeline->Enable (_generally_sensitive);
738
739         _video_panel->Enable    (selection && dynamic_pointer_cast<VideoContent>  (selection) && _generally_sensitive);
740         _audio_panel->Enable    (selection && dynamic_pointer_cast<AudioContent>  (selection) && _generally_sensitive);
741         _subtitle_panel->Enable (selection && dynamic_pointer_cast<FFmpegContent> (selection) && _generally_sensitive);
742         _timing_panel->Enable   (selection && _generally_sensitive);
743 }
744
745 shared_ptr<Content>
746 FilmEditor::selected_content ()
747 {
748         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
749         if (s == -1) {
750                 return shared_ptr<Content> ();
751         }
752
753         ContentList c = _film->content ();
754         if (s < 0 || size_t (s) >= c.size ()) {
755                 return shared_ptr<Content> ();
756         }
757         
758         return c[s];
759 }
760
761 shared_ptr<VideoContent>
762 FilmEditor::selected_video_content ()
763 {
764         shared_ptr<Content> c = selected_content ();
765         if (!c) {
766                 return shared_ptr<VideoContent> ();
767         }
768
769         return dynamic_pointer_cast<VideoContent> (c);
770 }
771
772 shared_ptr<AudioContent>
773 FilmEditor::selected_audio_content ()
774 {
775         shared_ptr<Content> c = selected_content ();
776         if (!c) {
777                 return shared_ptr<AudioContent> ();
778         }
779
780         return dynamic_pointer_cast<AudioContent> (c);
781 }
782
783 shared_ptr<SubtitleContent>
784 FilmEditor::selected_subtitle_content ()
785 {
786         shared_ptr<Content> c = selected_content ();
787         if (!c) {
788                 return shared_ptr<SubtitleContent> ();
789         }
790
791         return dynamic_pointer_cast<SubtitleContent> (c);
792 }
793
794 void
795 FilmEditor::content_timeline_clicked ()
796 {
797         if (_timeline_dialog) {
798                 _timeline_dialog->Destroy ();
799                 _timeline_dialog = 0;
800         }
801         
802         _timeline_dialog = new TimelineDialog (this, _film);
803         _timeline_dialog->Show ();
804 }
805
806 void
807 FilmEditor::set_selection (weak_ptr<Content> wc)
808 {
809         ContentList content = _film->content ();
810         for (size_t i = 0; i < content.size(); ++i) {
811                 if (content[i] == wc.lock ()) {
812                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
813                 } else {
814                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
815                 }
816         }
817 }
818
819 void
820 FilmEditor::sequence_video_changed ()
821 {
822         if (!_film) {
823                 return;
824         }
825         
826         _film->set_sequence_video (_sequence_video->GetValue ());
827 }
828
829 void
830 FilmEditor::content_right_click (wxListEvent& ev)
831 {
832         ContentList cl;
833         cl.push_back (selected_content ());
834         _menu.popup (cl, ev.GetPoint ());
835 }
836
837 void
838 FilmEditor::three_d_changed ()
839 {
840         if (!_film) {
841                 return;
842         }
843
844         _film->set_three_d (_three_d->GetValue ());
845 }