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