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