94aea9b1a4512bfdcf6e079b3ae76ae6a4687585
[dcpomatic.git] / src / wx / film_editor.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/film_editor.cc
21  *  @brief A wx widget to edit a film's metadata, and perform various functions.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include <wx/wx.h>
27 #include <wx/notebook.h>
28 #include <wx/listctrl.h>
29 #include <boost/thread.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include "lib/film.h"
33 #include "lib/transcode_job.h"
34 #include "lib/exceptions.h"
35 #include "lib/job_manager.h"
36 #include "lib/filter.h"
37 #include "lib/ratio.h"
38 #include "lib/config.h"
39 #include "lib/still_image_content.h"
40 #include "lib/sndfile_content.h"
41 #include "lib/dcp_content_type.h"
42 #include "lib/sound_processor.h"
43 #include "lib/scaler.h"
44 #include "timecode.h"
45 #include "filter_dialog.h"
46 #include "wx_util.h"
47 #include "film_editor.h"
48 #include "dci_metadata_dialog.h"
49 #include "timeline_dialog.h"
50 #include "timing_panel.h"
51 #include "subtitle_panel.h"
52 #include "audio_panel.h"
53
54 using std::string;
55 using std::cout;
56 using std::stringstream;
57 using std::pair;
58 using std::fixed;
59 using std::setprecision;
60 using std::list;
61 using std::vector;
62 using std::max;
63 using boost::shared_ptr;
64 using boost::weak_ptr;
65 using boost::dynamic_pointer_cast;
66 using boost::lexical_cast;
67
68 /** @param f Film to edit */
69 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
70         : wxPanel (parent)
71         , _menu (f, this)
72         , _generally_sensitive (true)
73         , _timeline_dialog (0)
74 {
75         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
76
77         _main_notebook = new wxNotebook (this, wxID_ANY);
78         s->Add (_main_notebook, 1);
79
80         make_content_panel ();
81         _main_notebook->AddPage (_content_panel, _("Content"), true);
82         make_dcp_panel ();
83         _main_notebook->AddPage (_dcp_panel, _("DCP"), false);
84         
85         setup_ratios ();
86
87         set_film (f);
88         connect_to_widgets ();
89
90         JobManager::instance()->ActiveJobsChanged.connect (
91                 bind (&FilmEditor::active_jobs_changed, this, _1)
92                 );
93         
94         SetSizerAndFit (s);
95 }
96
97 void
98 FilmEditor::make_dcp_panel ()
99 {
100         _dcp_panel = new wxPanel (_main_notebook);
101         _dcp_sizer = new wxBoxSizer (wxVERTICAL);
102         _dcp_panel->SetSizer (_dcp_sizer);
103
104         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
105         _dcp_sizer->Add (grid, 0, wxEXPAND | wxALL, 8);
106
107         int r = 0;
108         
109         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Name"), true, wxGBPosition (r, 0));
110         _name = new wxTextCtrl (_dcp_panel, wxID_ANY);
111         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxLEFT | wxRIGHT);
112         ++r;
113         
114         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Name"), true, wxGBPosition (r, 0));
115         _dcp_name = new wxStaticText (_dcp_panel, wxID_ANY, wxT (""));
116         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
117         ++r;
118
119         int flags = wxALIGN_CENTER_VERTICAL;
120 #ifdef __WXOSX__
121         flags |= wxALIGN_RIGHT;
122 #endif  
123
124         _use_dci_name = new wxCheckBox (_dcp_panel, wxID_ANY, _("Use DCI name"));
125         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, flags);
126         _edit_dci_button = new wxButton (_dcp_panel, wxID_ANY, _("Details..."));
127         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
128         ++r;
129
130         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Container"), true, wxGBPosition (r, 0));
131         _container = new wxChoice (_dcp_panel, wxID_ANY);
132         grid->Add (_container, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
133         ++r;
134
135         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Content Type"), true, wxGBPosition (r, 0));
136         _dcp_content_type = new wxChoice (_dcp_panel, wxID_ANY);
137         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
138         ++r;
139
140         {
141                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Frame Rate"), true, wxGBPosition (r, 0));
142                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
143                 _dcp_frame_rate = new wxChoice (_dcp_panel, wxID_ANY);
144                 s->Add (_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
145                 _best_dcp_frame_rate = new wxButton (_dcp_panel, wxID_ANY, _("Use best"));
146                 s->Add (_best_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND);
147                 grid->Add (s, wxGBPosition (r, 1));
148         }
149         ++r;
150
151         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Audio channels"), true, wxGBPosition (r, 0));
152         _dcp_audio_channels = new wxSpinCtrl (_dcp_panel, wxID_ANY);
153         grid->Add (_dcp_audio_channels, wxGBPosition (r, 1));
154         ++r;
155
156         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Resolution"), true, wxGBPosition (r, 0));
157         _dcp_resolution = new wxChoice (_dcp_panel, wxID_ANY);
158         grid->Add (_dcp_resolution, wxGBPosition (r, 1));
159         ++r;
160
161         {
162                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("JPEG2000 bandwidth"), true, wxGBPosition (r, 0));
163                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
164                 _j2k_bandwidth = new wxSpinCtrl (_dcp_panel, wxID_ANY);
165                 s->Add (_j2k_bandwidth, 1);
166                 add_label_to_sizer (s, _dcp_panel, _("MBps"), false);
167                 grid->Add (s, wxGBPosition (r, 1));
168         }
169         ++r;
170
171         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Scaler"), true, wxGBPosition (r, 0));
172         _scaler = new wxChoice (_dcp_panel, wxID_ANY);
173         grid->Add (_scaler, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
174         ++r;
175
176         vector<Scaler const *> const sc = Scaler::all ();
177         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
178                 _scaler->Append (std_to_wx ((*i)->name()));
179         }
180
181         vector<Ratio const *> const ratio = Ratio::all ();
182         for (vector<Ratio const *>::const_iterator i = ratio.begin(); i != ratio.end(); ++i) {
183                 _container->Append (std_to_wx ((*i)->nickname ()));
184         }
185
186         vector<DCPContentType const *> const ct = DCPContentType::all ();
187         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
188                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
189         }
190
191         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
192         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
193                 _dcp_frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
194         }
195
196         _dcp_audio_channels->SetRange (0, MAX_AUDIO_CHANNELS);
197         _j2k_bandwidth->SetRange (50, 250);
198
199         _dcp_resolution->Append (_("2K"));
200         _dcp_resolution->Append (_("4K"));
201 }
202
203 void
204 FilmEditor::connect_to_widgets ()
205 {
206         _name->Connect                   (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED,         wxCommandEventHandler (FilmEditor::name_changed), 0, this);
207         _use_dci_name->Connect           (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
208         _edit_dci_button->Connect        (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
209         _container->Connect              (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::container_changed), 0, this);
210         _ratio->Connect                  (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::ratio_changed), 0, this);
211         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED,   wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
212         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
213         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK,wxListEventHandler    (FilmEditor::content_right_click), 0, this);
214         _content_add->Connect            (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_add_clicked), 0, this);
215         _content_remove->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_remove_clicked), 0, this);
216         _content_timeline->Connect       (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_timeline_clicked), 0, this);
217         _left_crop->Connect              (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
218         _right_crop->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
219         _top_crop->Connect               (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
220         _bottom_crop->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
221         _filters_button->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
222         _scaler->Connect                 (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
223         _dcp_content_type->Connect       (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
224         _dcp_frame_rate->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_frame_rate_changed), 0, this);
225         _best_dcp_frame_rate->Connect    (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::best_dcp_frame_rate_clicked), 0, this);
226         _dcp_audio_channels->Connect     (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::dcp_audio_channels_changed), 0, this);
227         _j2k_bandwidth->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
228         _dcp_resolution->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_resolution_changed), 0, this);
229         _sequence_video->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::sequence_video_changed), 0, this);
230 }
231
232 void
233 FilmEditor::make_video_panel ()
234 {
235         _video_panel = new wxPanel (_content_notebook);
236         wxBoxSizer* video_sizer = new wxBoxSizer (wxVERTICAL);
237         _video_panel->SetSizer (video_sizer);
238         
239         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
240         video_sizer->Add (grid, 0, wxALL, 8);
241
242         int r = 0;
243         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), true, wxGBPosition (r, 0));
244         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
245         grid->Add (_left_crop, wxGBPosition (r, 1));
246         ++r;
247
248         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), true, wxGBPosition (r, 0));
249         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
250         grid->Add (_right_crop, wxGBPosition (r, 1));
251         ++r;
252         
253         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), true, wxGBPosition (r, 0));
254         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
255         grid->Add (_top_crop, wxGBPosition (r, 1));
256         ++r;
257         
258         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), true, wxGBPosition (r, 0));
259         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
260         grid->Add (_bottom_crop, wxGBPosition (r, 1));
261         ++r;
262
263         add_label_to_grid_bag_sizer (grid, _video_panel, _("Scale to"), true, wxGBPosition (r, 0));
264         _ratio = new wxChoice (_video_panel, wxID_ANY);
265         grid->Add (_ratio, wxGBPosition (r, 1));
266         ++r;
267
268         _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
269         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
270         wxFont font = _scaling_description->GetFont();
271         font.SetStyle(wxFONTSTYLE_ITALIC);
272         font.SetPointSize(font.GetPointSize() - 1);
273         _scaling_description->SetFont(font);
274         ++r;
275
276         /* VIDEO-only stuff */
277         {
278                 add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), true, wxGBPosition (r, 0));
279                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
280                 _filters = new wxStaticText (_video_panel, wxID_ANY, _("None"));
281                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
282                 _filters_button = new wxButton (_video_panel, wxID_ANY, _("Edit..."));
283                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
284                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
285         }
286         ++r;
287
288         _left_crop->SetRange (0, 1024);
289         _top_crop->SetRange (0, 1024);
290         _right_crop->SetRange (0, 1024);
291         _bottom_crop->SetRange (0, 1024);
292 }
293
294 void
295 FilmEditor::make_content_panel ()
296 {
297         _content_panel = new wxPanel (_main_notebook);
298         _content_sizer = new wxBoxSizer (wxVERTICAL);
299         _content_panel->SetSizer (_content_sizer);
300
301         {
302                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
303                 
304                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
305                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
306
307                 _content->InsertColumn (0, wxT(""));
308                 _content->SetColumnWidth (0, 512);
309
310                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
311                 _content_add = new wxButton (_content_panel, wxID_ANY, _("Add..."));
312                 b->Add (_content_add, 1, wxEXPAND | wxLEFT | wxRIGHT);
313                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
314                 b->Add (_content_remove, 1, wxEXPAND | wxLEFT | wxRIGHT);
315                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
316                 b->Add (_content_timeline, 1, wxEXPAND | wxLEFT | wxRIGHT);
317
318                 s->Add (b, 0, wxALL, 4);
319
320                 _content_sizer->Add (s, 0.75, wxEXPAND | wxALL, 6);
321         }
322
323         _sequence_video = new wxCheckBox (_content_panel, wxID_ANY, _("Keep video in sequence"));
324         _content_sizer->Add (_sequence_video);
325
326         _content_notebook = new wxNotebook (_content_panel, wxID_ANY);
327         _content_sizer->Add (_content_notebook, 1, wxEXPAND | wxTOP, 6);
328
329         make_video_panel ();
330         _content_notebook->AddPage (_video_panel, _("Video"), false);
331         _audio_panel = new AudioPanel (this);
332         _subtitle_panel = new SubtitlePanel (this);
333         _timing_panel = new TimingPanel (this);
334 }
335
336 /** Called when the left crop widget has been changed */
337 void
338 FilmEditor::left_crop_changed (wxCommandEvent &)
339 {
340         shared_ptr<VideoContent> c = selected_video_content ();
341         if (!c) {
342                 return;
343         }
344
345         c->set_left_crop (_left_crop->GetValue ());
346 }
347
348 /** Called when the right crop widget has been changed */
349 void
350 FilmEditor::right_crop_changed (wxCommandEvent &)
351 {
352         shared_ptr<VideoContent> c = selected_video_content ();
353         if (!c) {
354                 return;
355         }
356
357         c->set_right_crop (_right_crop->GetValue ());
358 }
359
360 /** Called when the top crop widget has been changed */
361 void
362 FilmEditor::top_crop_changed (wxCommandEvent &)
363 {
364         shared_ptr<VideoContent> c = selected_video_content ();
365         if (!c) {
366                 return;
367         }
368
369         c->set_top_crop (_top_crop->GetValue ());
370 }
371
372 /** Called when the bottom crop value has been changed */
373 void
374 FilmEditor::bottom_crop_changed (wxCommandEvent &)
375 {
376         shared_ptr<VideoContent> c = selected_video_content ();
377         if (!c) {
378                 return;
379         }
380
381         c->set_bottom_crop (_bottom_crop->GetValue ());
382 }
383
384 /** Called when the name widget has been changed */
385 void
386 FilmEditor::name_changed (wxCommandEvent &)
387 {
388         if (!_film) {
389                 return;
390         }
391
392         _film->set_name (string (_name->GetValue().mb_str()));
393 }
394
395 void
396 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
397 {
398         if (!_film) {
399                 return;
400         }
401         
402         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
403 }
404
405 void
406 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
407 {
408         if (!_film) {
409                 return;
410         }
411
412         _film->set_dcp_video_frame_rate (
413                 boost::lexical_cast<int> (
414                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
415                         )
416                 );
417 }
418
419 void
420 FilmEditor::dcp_audio_channels_changed (wxCommandEvent &)
421 {
422         if (!_film) {
423                 return;
424         }
425
426         _film->set_dcp_audio_channels (_dcp_audio_channels->GetValue ());
427 }
428
429 void
430 FilmEditor::dcp_resolution_changed (wxCommandEvent &)
431 {
432         if (!_film) {
433                 return;
434         }
435
436         _film->set_resolution (_dcp_resolution->GetSelection() == 0 ? RESOLUTION_2K : RESOLUTION_4K);
437 }
438
439
440 /** Called when the metadata stored in the Film object has changed;
441  *  so that we can update the GUI.
442  *  @param p Property of the Film that has changed.
443  */
444 void
445 FilmEditor::film_changed (Film::Property p)
446 {
447         ensure_ui_thread ();
448         
449         if (!_film) {
450                 return;
451         }
452
453         stringstream s;
454
455         _audio_panel->film_changed (p);
456         _subtitle_panel->film_changed (p);
457         _timing_panel->film_changed (p);
458                 
459         switch (p) {
460         case Film::NONE:
461                 break;
462         case Film::CONTENT:
463                 setup_content ();
464                 break;
465         case Film::CONTAINER:
466                 setup_container ();
467                 break;
468         case Film::NAME:
469                 checked_set (_name, _film->name());
470                 setup_dcp_name ();
471                 break;
472         case Film::WITH_SUBTITLES:
473                 setup_dcp_name ();
474                 break;
475         case Film::DCP_CONTENT_TYPE:
476                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
477                 setup_dcp_name ();
478                 break;
479         case Film::SCALER:
480                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
481                 break;
482         case Film::RESOLUTION:
483                 checked_set (_dcp_resolution, _film->resolution() == RESOLUTION_2K ? 0 : 1);
484                 setup_dcp_name ();
485                 break;
486         case Film::J2K_BANDWIDTH:
487                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
488                 break;
489         case Film::USE_DCI_NAME:
490                 checked_set (_use_dci_name, _film->use_dci_name ());
491                 setup_dcp_name ();
492                 break;
493         case Film::DCI_METADATA:
494                 setup_dcp_name ();
495                 break;
496         case Film::DCP_VIDEO_FRAME_RATE:
497         {
498                 bool done = false;
499                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
500                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_video_frame_rate())) {
501                                 checked_set (_dcp_frame_rate, i);
502                                 done = true;
503                                 break;
504                         }
505                 }
506
507                 if (!done) {
508                         checked_set (_dcp_frame_rate, -1);
509                 }
510
511                 _best_dcp_frame_rate->Enable (_film->best_dcp_video_frame_rate () != _film->dcp_video_frame_rate ());
512                 break;
513         }
514         case Film::DCP_AUDIO_CHANNELS:
515                 _dcp_audio_channels->SetValue (_film->dcp_audio_channels ());
516                 setup_dcp_name ();
517                 break;
518         case Film::SEQUENCE_VIDEO:
519                 checked_set (_sequence_video, _film->sequence_video ());
520                 break;
521         }
522 }
523
524 void
525 FilmEditor::film_content_changed (weak_ptr<Content> weak_content, int property)
526 {
527         ensure_ui_thread ();
528         
529         if (!_film) {
530                 /* We call this method ourselves (as well as using it as a signal handler)
531                    so _film can be 0.
532                 */
533                 return;
534         }
535
536         shared_ptr<Content> content = weak_content.lock ();
537         if (content != selected_content ()) {
538                 return;
539         }
540         
541         shared_ptr<VideoContent> video_content;
542         shared_ptr<AudioContent> audio_content;
543         shared_ptr<SubtitleContent> subtitle_content;
544         shared_ptr<FFmpegContent> ffmpeg_content;
545         if (content) {
546                 video_content = dynamic_pointer_cast<VideoContent> (content);
547                 audio_content = dynamic_pointer_cast<AudioContent> (content);
548                 subtitle_content = dynamic_pointer_cast<SubtitleContent> (content);
549                 ffmpeg_content = dynamic_pointer_cast<FFmpegContent> (content);
550         }
551
552         _audio_panel->film_content_changed    (content, audio_content, subtitle_content, ffmpeg_content, property);
553         _subtitle_panel->film_content_changed (content, audio_content, subtitle_content, ffmpeg_content, property);
554         _timing_panel->film_content_changed   (content, audio_content, subtitle_content, ffmpeg_content, property);
555
556         /* We can't use case {} here */
557         
558         if (property == VideoContentProperty::VIDEO_CROP) {
559                 checked_set (_left_crop,   video_content ? video_content->crop().left   : 0);
560                 checked_set (_right_crop,  video_content ? video_content->crop().right  : 0);
561                 checked_set (_top_crop,    video_content ? video_content->crop().top    : 0);
562                 checked_set (_bottom_crop, video_content ? video_content->crop().bottom : 0);
563                 setup_scaling_description ();
564         } else if (property == VideoContentProperty::VIDEO_RATIO) {
565                 if (video_content) {
566                         int n = 0;
567                         vector<Ratio const *> ratios = Ratio::all ();
568                         vector<Ratio const *>::iterator i = ratios.begin ();
569                         while (i != ratios.end() && *i != video_content->ratio()) {
570                                 ++i;
571                                 ++n;
572                         }
573
574                         if (i == ratios.end()) {
575                                 checked_set (_ratio, -1);
576                         } else {
577                                 checked_set (_ratio, n);
578                         }
579                 } else {
580                         checked_set (_ratio, -1);
581                 }
582                 setup_scaling_description ();
583         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
584                 setup_dcp_name ();
585         } else if (property == FFmpegContentProperty::FILTERS) {
586                 if (ffmpeg_content) {
587                         pair<string, string> p = Filter::ffmpeg_strings (ffmpeg_content->filters ());
588                         if (p.first.empty () && p.second.empty ()) {
589                                 _filters->SetLabel (_("None"));
590                         } else {
591                                 string const b = p.first + " " + p.second;
592                                 _filters->SetLabel (std_to_wx (b));
593                         }
594                         _dcp_sizer->Layout ();
595                 }
596         }
597 }
598
599 void
600 FilmEditor::setup_container ()
601 {
602         int n = 0;
603         vector<Ratio const *> ratios = Ratio::all ();
604         vector<Ratio const *>::iterator i = ratios.begin ();
605         while (i != ratios.end() && *i != _film->container ()) {
606                 ++i;
607                 ++n;
608         }
609         
610         if (i == ratios.end()) {
611                 checked_set (_container, -1);
612         } else {
613                 checked_set (_container, n);
614         }
615         
616         setup_dcp_name ();
617         setup_scaling_description ();
618 }       
619
620 /** Called when the container widget has been changed */
621 void
622 FilmEditor::container_changed (wxCommandEvent &)
623 {
624         if (!_film) {
625                 return;
626         }
627
628         int const n = _container->GetSelection ();
629         if (n >= 0) {
630                 vector<Ratio const *> ratios = Ratio::all ();
631                 assert (n < int (ratios.size()));
632                 _film->set_container (ratios[n]);
633         }
634 }
635
636 /** Called when the DCP content type widget has been changed */
637 void
638 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
639 {
640         if (!_film) {
641                 return;
642         }
643
644         int const n = _dcp_content_type->GetSelection ();
645         if (n != wxNOT_FOUND) {
646                 _film->set_dcp_content_type (DCPContentType::from_index (n));
647         }
648 }
649
650 /** Sets the Film that we are editing */
651 void
652 FilmEditor::set_film (shared_ptr<Film> f)
653 {
654         set_things_sensitive (f != 0);
655
656         if (_film == f) {
657                 return;
658         }
659         
660         _film = f;
661
662         if (_film) {
663                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
664                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
665         }
666
667         if (_film) {
668                 FileChanged (_film->directory ());
669         } else {
670                 FileChanged ("");
671         }
672
673         film_changed (Film::NAME);
674         film_changed (Film::USE_DCI_NAME);
675         film_changed (Film::CONTENT);
676         film_changed (Film::DCP_CONTENT_TYPE);
677         film_changed (Film::CONTAINER);
678         film_changed (Film::RESOLUTION);
679         film_changed (Film::SCALER);
680         film_changed (Film::WITH_SUBTITLES);
681         film_changed (Film::J2K_BANDWIDTH);
682         film_changed (Film::DCI_METADATA);
683         film_changed (Film::DCP_VIDEO_FRAME_RATE);
684         film_changed (Film::DCP_AUDIO_CHANNELS);
685         film_changed (Film::SEQUENCE_VIDEO);
686
687         if (!_film->content().empty ()) {
688                 set_selection (_film->content().front ());
689         }
690
691         wxListEvent ev;
692         content_selection_changed (ev);
693 }
694
695 /** Updates the sensitivity of lots of widgets to a given value.
696  *  @param s true to make sensitive, false to make insensitive.
697  */
698 void
699 FilmEditor::set_things_sensitive (bool s)
700 {
701         _generally_sensitive = s;
702         
703         _name->Enable (s);
704         _use_dci_name->Enable (s);
705         _edit_dci_button->Enable (s);
706         _content->Enable (s);
707         _dcp_content_type->Enable (s);
708         _dcp_frame_rate->Enable (s);
709         _dcp_audio_channels->Enable (s);
710         _j2k_bandwidth->Enable (s);
711         _container->Enable (s);
712
713         _subtitle_panel->setup_control_sensitivity ();
714         _audio_panel->setup_sensitivity ();
715         setup_content_sensitivity ();
716         _best_dcp_frame_rate->Enable (s && _film && _film->best_dcp_video_frame_rate () != _film->dcp_video_frame_rate ());
717 }
718
719 /** Called when the `Edit filters' button has been clicked */
720 void
721 FilmEditor::edit_filters_clicked (wxCommandEvent &)
722 {
723         shared_ptr<Content> c = selected_content ();
724         if (!c) {
725                 return;
726         }
727
728         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
729         if (!fc) {
730                 return;
731         }
732         
733         FilterDialog* d = new FilterDialog (this, fc->filters());
734         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, fc, _1));
735         d->ShowModal ();
736         d->Destroy ();
737 }
738
739 /** Called when the scaler widget has been changed */
740 void
741 FilmEditor::scaler_changed (wxCommandEvent &)
742 {
743         if (!_film) {
744                 return;
745         }
746
747         int const n = _scaler->GetSelection ();
748         if (n >= 0) {
749                 _film->set_scaler (Scaler::from_index (n));
750         }
751 }
752
753 void
754 FilmEditor::setup_ratios ()
755 {
756         _ratios = Ratio::all ();
757
758         _ratio->Clear ();
759         for (vector<Ratio const *>::iterator i = _ratios.begin(); i != _ratios.end(); ++i) {
760                 _ratio->Append (std_to_wx ((*i)->nickname ()));
761         }
762
763         _dcp_sizer->Layout ();
764 }
765
766 void
767 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
768 {
769         if (!_film) {
770                 return;
771         }
772
773         _film->set_use_dci_name (_use_dci_name->GetValue ());
774 }
775
776 void
777 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
778 {
779         if (!_film) {
780                 return;
781         }
782
783         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
784         d->ShowModal ();
785         _film->set_dci_metadata (d->dci_metadata ());
786         d->Destroy ();
787 }
788
789 void
790 FilmEditor::active_jobs_changed (bool a)
791 {
792         set_things_sensitive (!a);
793 }
794
795 void
796 FilmEditor::setup_dcp_name ()
797 {
798         string s = _film->dcp_name (true);
799         if (s.length() > 28) {
800                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
801                 _dcp_name->SetToolTip (std_to_wx (s));
802         } else {
803                 _dcp_name->SetLabel (std_to_wx (s));
804         }
805 }
806
807 void
808 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
809 {
810         if (!_film) {
811                 return;
812         }
813         
814         _film->set_dcp_video_frame_rate (_film->best_dcp_video_frame_rate ());
815 }
816
817 void
818 FilmEditor::setup_content ()
819 {
820         string selected_summary;
821         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
822         if (s != -1) {
823                 selected_summary = wx_to_std (_content->GetItemText (s));
824         }
825         
826         _content->DeleteAllItems ();
827
828         ContentList content = _film->content ();
829         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
830                 int const t = _content->GetItemCount ();
831                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
832                 if ((*i)->summary() == selected_summary) {
833                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
834                 }
835         }
836
837         if (selected_summary.empty () && !content.empty ()) {
838                 /* Select the item of content if none was selected before */
839                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
840         }
841 }
842
843 void
844 FilmEditor::content_add_clicked (wxCommandEvent &)
845 {
846         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
847         int const r = d->ShowModal ();
848         d->Destroy ();
849
850         if (r != wxID_OK) {
851                 return;
852         }
853
854         wxArrayString paths;
855         d->GetPaths (paths);
856
857         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
858                 boost::filesystem::path p (wx_to_std (paths[i]));
859
860                 shared_ptr<Content> c;
861
862                 if (StillImageContent::valid_file (p)) {
863                         c.reset (new StillImageContent (_film, p));
864                 } else if (SndfileContent::valid_file (p)) {
865                         c.reset (new SndfileContent (_film, p));
866                 } else {
867                         c.reset (new FFmpegContent (_film, p));
868                 }
869
870                 _film->examine_and_add_content (c);
871         }
872 }
873
874 void
875 FilmEditor::content_remove_clicked (wxCommandEvent &)
876 {
877         shared_ptr<Content> c = selected_content ();
878         if (c) {
879                 _film->remove_content (c);
880         }
881
882         wxListEvent ev;
883         content_selection_changed (ev);
884 }
885
886 void
887 FilmEditor::content_selection_changed (wxListEvent &)
888 {
889         setup_content_sensitivity ();
890         shared_ptr<Content> s = selected_content ();
891
892         _audio_panel->content_selection_changed ();
893         
894         film_content_changed (s, ContentProperty::START);
895         film_content_changed (s, ContentProperty::LENGTH);
896         film_content_changed (s, VideoContentProperty::VIDEO_CROP);
897         film_content_changed (s, VideoContentProperty::VIDEO_RATIO);
898         film_content_changed (s, AudioContentProperty::AUDIO_GAIN);
899         film_content_changed (s, AudioContentProperty::AUDIO_DELAY);
900         film_content_changed (s, AudioContentProperty::AUDIO_MAPPING);
901         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAM);
902         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAMS);
903         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAM);
904         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAMS);
905         film_content_changed (s, FFmpegContentProperty::FILTERS);
906         film_content_changed (s, SubtitleContentProperty::SUBTITLE_OFFSET);
907         film_content_changed (s, SubtitleContentProperty::SUBTITLE_SCALE);
908 }
909
910 void
911 FilmEditor::setup_content_sensitivity ()
912 {
913         _content_add->Enable (_generally_sensitive);
914
915         shared_ptr<Content> selection = selected_content ();
916
917         _content_remove->Enable (selection && _generally_sensitive);
918         _content_timeline->Enable (_generally_sensitive);
919
920         _video_panel->Enable    (selection && dynamic_pointer_cast<VideoContent>  (selection) && _generally_sensitive);
921         _audio_panel->Enable    (selection && dynamic_pointer_cast<AudioContent>  (selection) && _generally_sensitive);
922         _subtitle_panel->Enable (selection && dynamic_pointer_cast<FFmpegContent> (selection) && _generally_sensitive);
923         _timing_panel->Enable   (selection && _generally_sensitive);
924 }
925
926 shared_ptr<Content>
927 FilmEditor::selected_content ()
928 {
929         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
930         if (s == -1) {
931                 return shared_ptr<Content> ();
932         }
933
934         ContentList c = _film->content ();
935         if (s < 0 || size_t (s) >= c.size ()) {
936                 return shared_ptr<Content> ();
937         }
938         
939         return c[s];
940 }
941
942 shared_ptr<VideoContent>
943 FilmEditor::selected_video_content ()
944 {
945         shared_ptr<Content> c = selected_content ();
946         if (!c) {
947                 return shared_ptr<VideoContent> ();
948         }
949
950         return dynamic_pointer_cast<VideoContent> (c);
951 }
952
953 shared_ptr<AudioContent>
954 FilmEditor::selected_audio_content ()
955 {
956         shared_ptr<Content> c = selected_content ();
957         if (!c) {
958                 return shared_ptr<AudioContent> ();
959         }
960
961         return dynamic_pointer_cast<AudioContent> (c);
962 }
963
964 shared_ptr<SubtitleContent>
965 FilmEditor::selected_subtitle_content ()
966 {
967         shared_ptr<Content> c = selected_content ();
968         if (!c) {
969                 return shared_ptr<SubtitleContent> ();
970         }
971
972         return dynamic_pointer_cast<SubtitleContent> (c);
973 }
974
975 void
976 FilmEditor::setup_scaling_description ()
977 {
978         shared_ptr<VideoContent> vc = selected_video_content ();
979         if (!vc) {
980                 _scaling_description->SetLabel ("");
981                 return;
982         }
983
984         wxString d;
985
986         int lines = 0;
987
988         if (vc->video_size().width && vc->video_size().height) {
989                 d << wxString::Format (
990                         _("Original video is %dx%d (%.2f:1)\n"),
991                         vc->video_size().width, vc->video_size().height,
992                         float (vc->video_size().width) / vc->video_size().height
993                         );
994                 ++lines;
995         }
996
997         Crop const crop = vc->crop ();
998         if ((crop.left || crop.right || crop.top || crop.bottom) && vc->video_size() != libdcp::Size (0, 0)) {
999                 libdcp::Size cropped = vc->video_size ();
1000                 cropped.width -= crop.left + crop.right;
1001                 cropped.height -= crop.top + crop.bottom;
1002                 d << wxString::Format (
1003                         _("Cropped to %dx%d (%.2f:1)\n"),
1004                         cropped.width, cropped.height,
1005                         float (cropped.width) / cropped.height
1006                         );
1007                 ++lines;
1008         }
1009
1010         Ratio const * ratio = vc->ratio ();
1011         if (ratio) {
1012                 libdcp::Size container_size = _film->container()->size (_film->full_frame ());
1013                 
1014                 libdcp::Size const scaled = ratio->size (container_size);
1015                 d << wxString::Format (
1016                         _("Scaled to %dx%d (%.2f:1)\n"),
1017                         scaled.width, scaled.height,
1018                         float (scaled.width) / scaled.height
1019                         );
1020                 ++lines;
1021
1022                 if (scaled != container_size) {
1023                         d << wxString::Format (
1024                                 _("Padded with black to %dx%d (%.2f:1)\n"),
1025                                 container_size.width, container_size.height,
1026                                 float (container_size.width) / container_size.height
1027                                 );
1028                         ++lines;
1029                 }
1030         }
1031
1032         for (int i = lines; i < 4; ++i) {
1033                 d << wxT ("\n ");
1034         }
1035
1036         _scaling_description->SetLabel (d);
1037 }
1038
1039 void
1040 FilmEditor::content_timeline_clicked (wxCommandEvent &)
1041 {
1042         if (_timeline_dialog) {
1043                 _timeline_dialog->Destroy ();
1044                 _timeline_dialog = 0;
1045         }
1046         
1047         _timeline_dialog = new TimelineDialog (this, _film);
1048         _timeline_dialog->Show ();
1049 }
1050
1051 void
1052 FilmEditor::set_selection (weak_ptr<Content> wc)
1053 {
1054         ContentList content = _film->content ();
1055         for (size_t i = 0; i < content.size(); ++i) {
1056                 if (content[i] == wc.lock ()) {
1057                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1058                 } else {
1059                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
1060                 }
1061         }
1062 }
1063
1064 void
1065 FilmEditor::ratio_changed (wxCommandEvent &)
1066 {
1067         if (!_film) {
1068                 return;
1069         }
1070
1071         shared_ptr<Content> c = selected_content ();
1072         if (!c) {
1073                 return;
1074         }
1075
1076         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
1077         if (!vc) {
1078                 return;
1079         }
1080         
1081         int const n = _ratio->GetSelection ();
1082         if (n >= 0) {
1083                 vector<Ratio const *> ratios = Ratio::all ();
1084                 assert (n < int (ratios.size()));
1085                 vc->set_ratio (ratios[n]);
1086         }
1087 }
1088
1089 void
1090 FilmEditor::sequence_video_changed (wxCommandEvent &)
1091 {
1092         _film->set_sequence_video (_sequence_video->GetValue ());
1093 }
1094
1095 void
1096 FilmEditor::content_right_click (wxListEvent& ev)
1097 {
1098         ContentList cl;
1099         cl.push_back (selected_content ());
1100         _menu.popup (cl, ev.GetPoint ());
1101 }