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