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