Some basics of AudioMapping.
[dcpomatic.git] / src / wx / film_editor.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 /** @file src/film_editor.cc
23  *  @brief A wx widget to edit a film's metadata, and perform various functions.
24  */
25
26 #include <iostream>
27 #include <iomanip>
28 #include <wx/wx.h>
29 #include <wx/notebook.h>
30 #include <wx/listctrl.h>
31 #include <boost/thread.hpp>
32 #include <boost/filesystem.hpp>
33 #include <boost/lexical_cast.hpp>
34 #include "lib/format.h"
35 #include "lib/film.h"
36 #include "lib/transcode_job.h"
37 #include "lib/exceptions.h"
38 #include "lib/ab_transcode_job.h"
39 #include "lib/job_manager.h"
40 #include "lib/filter.h"
41 #include "lib/config.h"
42 #include "lib/ffmpeg_decoder.h"
43 #include "lib/imagemagick_content.h"
44 #include "lib/sndfile_content.h"
45 #include "lib/dcp_content_type.h"
46 #include "filter_dialog.h"
47 #include "wx_util.h"
48 #include "film_editor.h"
49 #include "gain_calculator_dialog.h"
50 #include "sound_processor.h"
51 #include "dci_metadata_dialog.h"
52 #include "scaler.h"
53 #include "audio_dialog.h"
54 #include "imagemagick_content_dialog.h"
55 #include "ffmpeg_content_dialog.h"
56 #include "timeline_dialog.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         , _generally_sensitive (true)
76         , _audio_dialog (0)
77         , _timeline_dialog (0)
78 {
79         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
80         _notebook = new wxNotebook (this, wxID_ANY);
81         s->Add (_notebook, 1);
82
83         make_film_panel ();
84         _notebook->AddPage (_film_panel, _("Film"), true);
85         make_content_panel ();
86         _notebook->AddPage (_content_panel, _("Content"), false);
87         make_video_panel ();
88         _notebook->AddPage (_video_panel, _("Video"), false);
89         make_audio_panel ();
90         _notebook->AddPage (_audio_panel, _("Audio"), false);
91         make_subtitle_panel ();
92         _notebook->AddPage (_subtitle_panel, _("Subtitles"), false);
93
94         setup_formats ();
95
96         set_film (f);
97         connect_to_widgets ();
98
99         JobManager::instance()->ActiveJobsChanged.connect (
100                 bind (&FilmEditor::active_jobs_changed, this, _1)
101                 );
102         
103         SetSizerAndFit (s);
104 }
105
106 void
107 FilmEditor::make_film_panel ()
108 {
109         _film_panel = new wxPanel (_notebook);
110         _film_sizer = new wxBoxSizer (wxVERTICAL);
111         _film_panel->SetSizer (_film_sizer);
112
113         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
114         _film_sizer->Add (grid, 0, wxALL, 8);
115
116         int r = 0;
117         
118         add_label_to_grid_bag_sizer (grid, _film_panel, _("Name"), wxGBPosition (r, 0));
119         _name = new wxTextCtrl (_film_panel, wxID_ANY);
120         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
121         ++r;
122         
123         add_label_to_grid_bag_sizer (grid, _film_panel, _("DCP Name"), wxGBPosition (r, 0));
124         _dcp_name = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
125         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
126         ++r;
127
128         _use_dci_name = new wxCheckBox (_film_panel, wxID_ANY, _("Use DCI name"));
129         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
130         _edit_dci_button = new wxButton (_film_panel, wxID_ANY, _("Details..."));
131         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
132         ++r;
133
134         _trust_content_headers = new wxCheckBox (_film_panel, wxID_ANY, _("Trust content headers"));
135         grid->Add (_trust_content_headers, wxGBPosition (r, 0), wxGBSpan(1, 2));
136         ++r;
137
138         add_label_to_grid_bag_sizer (grid, _film_panel, _("Content Type"), wxGBPosition (r, 0));
139         _dcp_content_type = new wxChoice (_film_panel, wxID_ANY);
140         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
141         ++r;
142
143         {
144                 add_label_to_grid_bag_sizer (grid, _film_panel, _("DCP Frame Rate"), wxGBPosition (r, 0));
145                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
146                 _dcp_frame_rate = new wxChoice (_film_panel, wxID_ANY);
147                 s->Add (_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
148                 _best_dcp_frame_rate = new wxButton (_film_panel, wxID_ANY, _("Use best"));
149                 s->Add (_best_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 6);
150                 grid->Add (s, wxGBPosition (r, 1));
151         }
152         ++r;
153
154         _frame_rate_description = new wxStaticText (_film_panel, wxID_ANY, wxT ("\n \n "), wxDefaultPosition, wxDefaultSize);
155         grid->Add (_frame_rate_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
156         wxFont font = _frame_rate_description->GetFont();
157         font.SetStyle(wxFONTSTYLE_ITALIC);
158         font.SetPointSize(font.GetPointSize() - 1);
159         _frame_rate_description->SetFont(font);
160         ++r;
161         
162         add_label_to_grid_bag_sizer (grid, _film_panel, _("Length"), wxGBPosition (r, 0));
163         _length = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
164         grid->Add (_length, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
165         ++r;
166
167         add_label_to_grid_bag_sizer (grid, _film_panel, _("Trim method"), wxGBPosition (r, 0));
168         _trim_type = new wxChoice (_film_panel, wxID_ANY);
169         grid->Add (_trim_type, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
170         ++r;
171
172         _ab = new wxCheckBox (_film_panel, wxID_ANY, _("A/B"));
173         grid->Add (_ab, wxGBPosition (r, 0));
174         ++r;
175
176         vector<DCPContentType const *> const ct = DCPContentType::all ();
177         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
178                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
179         }
180
181         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
182         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
183                 _dcp_frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
184         }
185
186         _trim_type->Append (_("encode all frames and play the subset"));
187         _trim_type->Append (_("encode only the subset"));
188 }
189
190 void
191 FilmEditor::connect_to_widgets ()
192 {
193         _name->Connect                   (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED,         wxCommandEventHandler (FilmEditor::name_changed), 0, this);
194         _use_dci_name->Connect           (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
195         _edit_dci_button->Connect        (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
196         _format->Connect                 (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::format_changed), 0, this);
197         _trust_content_headers->Connect  (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::trust_content_headers_changed), 0, this);
198         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED,   wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
199         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
200         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_ACTIVATED,  wxListEventHandler    (FilmEditor::content_activated), 0, this);
201         _content_add->Connect            (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_add_clicked), 0, this);
202         _content_remove->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_remove_clicked), 0, this);
203         _content_properties->Connect     (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_properties_clicked), 0, this);
204         _content_timeline->Connect       (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_timeline_clicked), 0, this);
205         _loop_content->Connect           (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::loop_content_toggled), 0, this);
206         _loop_count->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::loop_count_changed), 0, this);
207         _left_crop->Connect              (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
208         _right_crop->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
209         _top_crop->Connect               (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
210         _bottom_crop->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
211         _filters_button->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
212         _scaler->Connect                 (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
213         _dcp_content_type->Connect       (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
214         _dcp_frame_rate->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_frame_rate_changed), 0, this);
215         _best_dcp_frame_rate->Connect    (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::best_dcp_frame_rate_clicked), 0, this);
216         _ab->Connect                     (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::ab_toggled), 0, this);
217         _trim_type->Connect              (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::trim_type_changed), 0, this);
218         _with_subtitles->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::with_subtitles_toggled), 0, this);
219         _subtitle_offset->Connect        (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_offset_changed), 0, this);
220         _subtitle_scale->Connect         (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_scale_changed), 0, this);
221         _colour_lut->Connect             (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::colour_lut_changed), 0, this);
222         _j2k_bandwidth->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
223         _audio_gain->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
224         _audio_gain_calculate_button->Connect (
225                 wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::audio_gain_calculate_button_clicked), 0, this
226                 );
227         _show_audio->Connect             (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::show_audio_clicked), 0, this);
228         _audio_delay->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
229 }
230
231 void
232 FilmEditor::make_video_panel ()
233 {
234         _video_panel = new wxPanel (_notebook);
235         _video_sizer = new wxBoxSizer (wxVERTICAL);
236         _video_panel->SetSizer (_video_sizer);
237         
238         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
239         _video_sizer->Add (grid, 0, wxALL, 8);
240
241         int r = 0;
242         add_label_to_grid_bag_sizer (grid, _video_panel, _("Format"), wxGBPosition (r, 0));
243         _format = new wxChoice (_video_panel, wxID_ANY);
244         grid->Add (_format, wxGBPosition (r, 1));
245         ++r;
246
247         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), wxGBPosition (r, 0));
248         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
249         grid->Add (_left_crop, wxGBPosition (r, 1));
250         ++r;
251
252         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), wxGBPosition (r, 0));
253         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
254         grid->Add (_right_crop, wxGBPosition (r, 1));
255         ++r;
256         
257         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), wxGBPosition (r, 0));
258         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
259         grid->Add (_top_crop, wxGBPosition (r, 1));
260         ++r;
261         
262         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), wxGBPosition (r, 0));
263         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
264         grid->Add (_bottom_crop, wxGBPosition (r, 1));
265         ++r;
266
267         _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
268         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
269         wxFont font = _scaling_description->GetFont();
270         font.SetStyle(wxFONTSTYLE_ITALIC);
271         font.SetPointSize(font.GetPointSize() - 1);
272         _scaling_description->SetFont(font);
273         ++r;
274
275         /* VIDEO-only stuff */
276         {
277                 add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), wxGBPosition (r, 0));
278                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
279                 _filters = new wxStaticText (_video_panel, wxID_ANY, _("None"));
280                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
281                 _filters_button = new wxButton (_video_panel, wxID_ANY, _("Edit..."));
282                 s->Add (_filters_button, 0);
283                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
284         }
285         ++r;
286
287         add_label_to_grid_bag_sizer (grid, _video_panel, _("Scaler"), wxGBPosition (r, 0));
288         _scaler = new wxChoice (_video_panel, wxID_ANY);
289         grid->Add (_scaler, wxGBPosition (r, 1));
290         ++r;
291
292         vector<Scaler const *> const sc = Scaler::all ();
293         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
294                 _scaler->Append (std_to_wx ((*i)->name()));
295         }
296
297         add_label_to_grid_bag_sizer (grid, _video_panel, _("Colour look-up table"), wxGBPosition (r, 0));
298         _colour_lut = new wxChoice (_video_panel, wxID_ANY);
299         for (int i = 0; i < 2; ++i) {
300                 _colour_lut->Append (std_to_wx (colour_lut_index_to_name (i)));
301         }
302         _colour_lut->SetSelection (0);
303         grid->Add (_colour_lut, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
304         ++r;
305
306         {
307                 add_label_to_grid_bag_sizer (grid, _video_panel, _("JPEG2000 bandwidth"), wxGBPosition (r, 0));
308                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
309                 _j2k_bandwidth = new wxSpinCtrl (_video_panel, wxID_ANY);
310                 s->Add (_j2k_bandwidth, 1);
311                 add_label_to_sizer (s, _video_panel, _("MBps"));
312                 grid->Add (s, wxGBPosition (r, 1));
313         }
314         ++r;
315
316         _left_crop->SetRange (0, 1024);
317         _top_crop->SetRange (0, 1024);
318         _right_crop->SetRange (0, 1024);
319         _bottom_crop->SetRange (0, 1024);
320         _j2k_bandwidth->SetRange (50, 250);
321 }
322
323 void
324 FilmEditor::make_content_panel ()
325 {
326         _content_panel = new wxPanel (_notebook);
327         _content_sizer = new wxBoxSizer (wxVERTICAL);
328         _content_panel->SetSizer (_content_sizer);
329         
330         {
331                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
332                 
333                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
334                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
335
336                 _content->InsertColumn (0, wxT(""));
337                 _content->SetColumnWidth (0, 512);
338
339                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
340                 _content_add = new wxButton (_content_panel, wxID_ANY, _("Add..."));
341                 b->Add (_content_add, 1, wxEXPAND | wxLEFT | wxRIGHT);
342                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
343                 b->Add (_content_remove, 1, wxEXPAND | wxLEFT | wxRIGHT);
344                 _content_properties = new wxButton (_content_panel, wxID_ANY, _("Properties..."));
345                 b->Add (_content_properties);
346                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
347                 b->Add (_content_timeline, 1, wxEXPAND | wxLEFT | wxRIGHT);
348
349                 s->Add (b, 0, wxALL, 4);
350
351                 _content_sizer->Add (s, 0.75, wxEXPAND | wxALL, 6);
352         }
353
354         _content_information = new wxTextCtrl (_content_panel, wxID_ANY, wxT ("\n \n "), wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxTE_MULTILINE);
355         _content_sizer->Add (_content_information, 1, wxEXPAND | wxALL, 6);
356
357         wxBoxSizer* h = new wxBoxSizer (wxHORIZONTAL);
358         _loop_content = new wxCheckBox (_content_panel, wxID_ANY, _("Loop everything"));
359         h->Add (_loop_content, 0, wxALL, 6);
360         _loop_count = new wxSpinCtrl (_content_panel, wxID_ANY);
361         h->Add (_loop_count, 0, wxALL, 6);
362         add_label_to_sizer (h, _content_panel, _("times"));
363         _content_sizer->Add (h, 0, wxALL, 6);
364
365         _loop_count->SetRange (2, 1024);
366 }
367
368 void
369 FilmEditor::make_audio_panel ()
370 {
371         _audio_panel = new wxPanel (_notebook);
372         _audio_sizer = new wxBoxSizer (wxVERTICAL);
373         _audio_panel->SetSizer (_audio_sizer);
374         
375         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
376         _audio_sizer->Add (grid, 0, wxALL, 8);
377
378         _show_audio = new wxButton (_audio_panel, wxID_ANY, _("Show Audio..."));
379         grid->Add (_show_audio, 1);
380         grid->AddSpacer (0);
381
382         {
383                 add_label_to_sizer (grid, _audio_panel, _("Audio Gain"));
384                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
385                 _audio_gain = new wxSpinCtrl (_audio_panel);
386                 s->Add (_audio_gain, 1);
387                 add_label_to_sizer (s, _audio_panel, _("dB"));
388                 _audio_gain_calculate_button = new wxButton (_audio_panel, wxID_ANY, _("Calculate..."));
389                 s->Add (_audio_gain_calculate_button, 1, wxEXPAND);
390                 grid->Add (s);
391         }
392
393         {
394                 add_label_to_sizer (grid, _audio_panel, _("Audio Delay"));
395                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
396                 _audio_delay = new wxSpinCtrl (_audio_panel);
397                 s->Add (_audio_delay, 1);
398                 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
399                 add_label_to_sizer (s, _audio_panel, _("ms"));
400                 grid->Add (s);
401         }
402
403         _audio_gain->SetRange (-60, 60);
404         _audio_delay->SetRange (-1000, 1000);
405 }
406
407 void
408 FilmEditor::make_subtitle_panel ()
409 {
410         _subtitle_panel = new wxPanel (_notebook);
411         _subtitle_sizer = new wxBoxSizer (wxVERTICAL);
412         _subtitle_panel->SetSizer (_subtitle_sizer);
413         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
414         _subtitle_sizer->Add (grid, 0, wxALL, 8);
415
416         _with_subtitles = new wxCheckBox (_subtitle_panel, wxID_ANY, _("With Subtitles"));
417         grid->Add (_with_subtitles, 1);
418         grid->AddSpacer (0);
419         
420         {
421                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"));
422                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
423                 _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
424                 s->Add (_subtitle_offset);
425                 add_label_to_sizer (s, _subtitle_panel, _("pixels"));
426                 grid->Add (s);
427         }
428
429         {
430                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale"));
431                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
432                 _subtitle_scale = new wxSpinCtrl (_subtitle_panel);
433                 s->Add (_subtitle_scale);
434                 add_label_to_sizer (s, _subtitle_panel, _("%"));
435                 grid->Add (s);
436         }
437
438         _subtitle_offset->SetRange (-1024, 1024);
439         _subtitle_scale->SetRange (1, 1000);
440 }
441
442 /** Called when the left crop widget has been changed */
443 void
444 FilmEditor::left_crop_changed (wxCommandEvent &)
445 {
446         if (!_film) {
447                 return;
448         }
449
450         _film->set_left_crop (_left_crop->GetValue ());
451 }
452
453 /** Called when the right crop widget has been changed */
454 void
455 FilmEditor::right_crop_changed (wxCommandEvent &)
456 {
457         if (!_film) {
458                 return;
459         }
460
461         _film->set_right_crop (_right_crop->GetValue ());
462 }
463
464 /** Called when the top crop widget has been changed */
465 void
466 FilmEditor::top_crop_changed (wxCommandEvent &)
467 {
468         if (!_film) {
469                 return;
470         }
471
472         _film->set_top_crop (_top_crop->GetValue ());
473 }
474
475 /** Called when the bottom crop value has been changed */
476 void
477 FilmEditor::bottom_crop_changed (wxCommandEvent &)
478 {
479         if (!_film) {
480                 return;
481         }
482
483         _film->set_bottom_crop (_bottom_crop->GetValue ());
484 }
485
486 void
487 FilmEditor::trust_content_headers_changed (wxCommandEvent &)
488 {
489         if (!_film) {
490                 return;
491         }
492
493         _film->set_trust_content_headers (_trust_content_headers->GetValue ());
494 }
495
496 /** Called when the DCP A/B switch has been toggled */
497 void
498 FilmEditor::ab_toggled (wxCommandEvent &)
499 {
500         if (!_film) {
501                 return;
502         }
503         
504         _film->set_ab (_ab->GetValue ());
505 }
506
507 /** Called when the name widget has been changed */
508 void
509 FilmEditor::name_changed (wxCommandEvent &)
510 {
511         if (!_film) {
512                 return;
513         }
514
515         _film->set_name (string (_name->GetValue().mb_str()));
516 }
517
518 void
519 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
520 {
521         if (!_film) {
522                 return;
523         }
524
525         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
526 }
527
528 void
529 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
530 {
531         if (!_film) {
532                 return;
533         }
534
535         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
536 }
537
538 void
539 FilmEditor::colour_lut_changed (wxCommandEvent &)
540 {
541         if (!_film) {
542                 return;
543         }
544         
545         _film->set_colour_lut (_colour_lut->GetSelection ());
546 }
547
548 void
549 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
550 {
551         if (!_film) {
552                 return;
553         }
554         
555         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
556 }
557
558 void
559 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
560 {
561         if (!_film) {
562                 return;
563         }
564
565         _film->set_dcp_video_frame_rate (
566                 boost::lexical_cast<int> (
567                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
568                         )
569                 );
570 }
571
572
573 /** Called when the metadata stored in the Film object has changed;
574  *  so that we can update the GUI.
575  *  @param p Property of the Film that has changed.
576  */
577 void
578 FilmEditor::film_changed (Film::Property p)
579 {
580         ensure_ui_thread ();
581         
582         if (!_film) {
583                 return;
584         }
585
586         stringstream s;
587                 
588         switch (p) {
589         case Film::NONE:
590                 break;
591         case Film::CONTENT:
592                 setup_content ();
593                 setup_formats ();
594                 setup_format ();
595                 setup_subtitle_control_sensitivity ();
596                 setup_show_audio_sensitivity ();
597                 setup_length ();
598                 break;
599         case Film::LOOP:
600                 checked_set (_loop_content, _film->loop() > 1);
601                 checked_set (_loop_count, _film->loop());
602                 setup_loop_sensitivity ();
603                 break;
604         case Film::TRUST_CONTENT_HEADERS:
605                 checked_set (_trust_content_headers, _film->trust_content_headers ());
606                 break;
607         case Film::FORMAT:
608                 setup_format ();
609                 break;
610         case Film::CROP:
611                 checked_set (_left_crop, _film->crop().left);
612                 checked_set (_right_crop, _film->crop().right);
613                 checked_set (_top_crop, _film->crop().top);
614                 checked_set (_bottom_crop, _film->crop().bottom);
615                 setup_scaling_description ();
616                 break;
617         case Film::FILTERS:
618         {
619                 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
620                 if (p.first.empty () && p.second.empty ()) {
621                         _filters->SetLabel (_("None"));
622                 } else {
623                         string const b = p.first + " " + p.second;
624                         _filters->SetLabel (std_to_wx (b));
625                 }
626                 _film_sizer->Layout ();
627                 break;
628         }
629         case Film::NAME:
630                 checked_set (_name, _film->name());
631                 setup_dcp_name ();
632                 break;
633         case Film::DCP_CONTENT_TYPE:
634                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
635                 setup_dcp_name ();
636                 break;
637         case Film::AB:
638                 checked_set (_ab, _film->ab ());
639                 break;
640         case Film::SCALER:
641                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
642                 break;
643         case Film::TRIM_TYPE:
644                 checked_set (_trim_type, _film->trim_type() == Film::CPL ? 0 : 1);
645                 break;
646         case Film::AUDIO_GAIN:
647                 checked_set (_audio_gain, _film->audio_gain ());
648                 break;
649         case Film::AUDIO_DELAY:
650                 checked_set (_audio_delay, _film->audio_delay ());
651                 break;
652         case Film::WITH_SUBTITLES:
653                 checked_set (_with_subtitles, _film->with_subtitles ());
654                 setup_subtitle_control_sensitivity ();
655                 setup_dcp_name ();
656                 break;
657         case Film::SUBTITLE_OFFSET:
658                 checked_set (_subtitle_offset, _film->subtitle_offset ());
659                 break;
660         case Film::SUBTITLE_SCALE:
661                 checked_set (_subtitle_scale, _film->subtitle_scale() * 100);
662                 break;
663         case Film::COLOUR_LUT:
664                 checked_set (_colour_lut, _film->colour_lut ());
665                 break;
666         case Film::J2K_BANDWIDTH:
667                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
668                 break;
669         case Film::USE_DCI_NAME:
670                 checked_set (_use_dci_name, _film->use_dci_name ());
671                 setup_dcp_name ();
672                 break;
673         case Film::DCI_METADATA:
674                 setup_dcp_name ();
675                 break;
676         case Film::DCP_VIDEO_FRAME_RATE:
677         {
678                 bool done = false;
679                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
680                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_video_frame_rate())) {
681                                 checked_set (_dcp_frame_rate, i);
682                                 done = true;
683                                 break;
684                         }
685                 }
686
687                 if (!done) {
688                         checked_set (_dcp_frame_rate, -1);
689                 }
690
691                 _best_dcp_frame_rate->Enable (_film->best_dcp_video_frame_rate () != _film->dcp_video_frame_rate ());
692                 setup_frame_rate_description ();
693                 break;
694         }
695         }
696 }
697
698 void
699 FilmEditor::film_content_changed (weak_ptr<Content> content, int property)
700 {
701         if (!_film) {
702                 /* We call this method ourselves (as well as using it as a signal handler)
703                    so _film can be 0.
704                 */
705                 return;
706         }
707                 
708         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
709                 setup_subtitle_control_sensitivity ();
710         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
711                 setup_show_audio_sensitivity ();
712         } else if (property == VideoContentProperty::VIDEO_LENGTH || property == AudioContentProperty::AUDIO_LENGTH) {
713                 setup_length ();
714                 boost::shared_ptr<Content> c = content.lock ();
715                 if (c && c == selected_content()) {
716                         setup_content_information ();
717                 }
718         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
719                 setup_dcp_name ();
720                 setup_show_audio_sensitivity ();
721         }
722 }
723
724 void
725 FilmEditor::setup_format ()
726 {
727         int n = 0;
728         vector<Format const *>::iterator i = _formats.begin ();
729         while (i != _formats.end() && *i != _film->format ()) {
730                 ++i;
731                 ++n;
732         }
733         
734         if (i == _formats.end()) {
735                 checked_set (_format, -1);
736         } else {
737                 checked_set (_format, n);
738         }
739         
740         setup_dcp_name ();
741         setup_scaling_description ();
742 }       
743
744 void
745 FilmEditor::setup_length ()
746 {
747         stringstream s;
748         Time const length = _film->length ();
749         
750         s << time_to_hms (length);
751         _length->SetLabel (std_to_wx (s.str ()));
752 }       
753
754 void
755 FilmEditor::setup_frame_rate_description ()
756 {
757         wxString d;
758         int lines = 0;
759
760 #if 0
761         XXX
762                 
763         if (_film->video_frame_rate()) {
764                 d << std_to_wx (FrameRateConversion (_film->video_frame_rate(), _film->dcp_frame_rate()).description);
765                 ++lines;
766                 if (_film->audio_frame_rate() && _film->audio_frame_rate() != _film->target_audio_sample_rate ()) {
767                         d << wxString::Format (
768                                 _("Audio will be resampled from %dHz to %dHz\n"),
769                                 _film->audio_frame_rate(),
770                                 _film->target_audio_sample_rate()
771                                 );
772                         ++lines;
773                 }
774         }
775 #endif  
776
777         for (int i = lines; i < 2; ++i) {
778                 d << wxT ("\n ");
779         }
780
781         _frame_rate_description->SetLabel (d);
782 }
783
784 /** Called when the format widget has been changed */
785 void
786 FilmEditor::format_changed (wxCommandEvent &)
787 {
788         if (!_film) {
789                 return;
790         }
791
792         int const n = _format->GetSelection ();
793         if (n >= 0) {
794                 assert (n < int (_formats.size()));
795                 _film->set_format (_formats[n]);
796         }
797 }
798
799 /** Called when the DCP content type widget has been changed */
800 void
801 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
802 {
803         if (!_film) {
804                 return;
805         }
806
807         int const n = _dcp_content_type->GetSelection ();
808         if (n != wxNOT_FOUND) {
809                 _film->set_dcp_content_type (DCPContentType::from_index (n));
810         }
811 }
812
813 /** Sets the Film that we are editing */
814 void
815 FilmEditor::set_film (shared_ptr<Film> f)
816 {
817         set_things_sensitive (f != 0);
818
819         if (_film == f) {
820                 return;
821         }
822         
823         _film = f;
824
825         if (_film) {
826                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
827                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
828         }
829
830         if (_film) {
831                 FileChanged (_film->directory ());
832         } else {
833                 FileChanged ("");
834         }
835
836         if (_audio_dialog) {
837                 _audio_dialog->set_film (_film);
838         }
839         
840         film_changed (Film::NAME);
841         film_changed (Film::USE_DCI_NAME);
842         film_changed (Film::CONTENT);
843         film_changed (Film::LOOP);
844         film_changed (Film::TRUST_CONTENT_HEADERS);
845         film_changed (Film::DCP_CONTENT_TYPE);
846         film_changed (Film::FORMAT);
847         film_changed (Film::CROP);
848         film_changed (Film::FILTERS);
849         film_changed (Film::SCALER);
850         film_changed (Film::TRIM_START);
851         film_changed (Film::TRIM_END);
852         film_changed (Film::AB);
853         film_changed (Film::TRIM_TYPE);
854         film_changed (Film::AUDIO_GAIN);
855         film_changed (Film::AUDIO_DELAY);
856         film_changed (Film::WITH_SUBTITLES);
857         film_changed (Film::SUBTITLE_OFFSET);
858         film_changed (Film::SUBTITLE_SCALE);
859         film_changed (Film::COLOUR_LUT);
860         film_changed (Film::J2K_BANDWIDTH);
861         film_changed (Film::DCI_METADATA);
862         film_changed (Film::DCP_VIDEO_FRAME_RATE);
863
864         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::SUBTITLE_STREAMS);
865         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::SUBTITLE_STREAM);
866         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::AUDIO_STREAMS);
867         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::AUDIO_STREAM);
868
869         setup_content_information ();
870 }
871
872 /** Updates the sensitivity of lots of widgets to a given value.
873  *  @param s true to make sensitive, false to make insensitive.
874  */
875 void
876 FilmEditor::set_things_sensitive (bool s)
877 {
878         _generally_sensitive = s;
879         
880         _name->Enable (s);
881         _use_dci_name->Enable (s);
882         _edit_dci_button->Enable (s);
883         _format->Enable (s);
884         _content->Enable (s);
885         _trust_content_headers->Enable (s);
886         _content->Enable (s);
887         _left_crop->Enable (s);
888         _right_crop->Enable (s);
889         _top_crop->Enable (s);
890         _bottom_crop->Enable (s);
891         _filters_button->Enable (s);
892         _scaler->Enable (s);
893         _dcp_content_type->Enable (s);
894         _best_dcp_frame_rate->Enable (s);
895         _dcp_frame_rate->Enable (s);
896         _ab->Enable (s);
897         _trim_type->Enable (s);
898         _colour_lut->Enable (s);
899         _j2k_bandwidth->Enable (s);
900         _audio_gain->Enable (s);
901         _audio_gain_calculate_button->Enable (s);
902         _show_audio->Enable (s);
903         _audio_delay->Enable (s);
904
905         setup_subtitle_control_sensitivity ();
906         setup_show_audio_sensitivity ();
907         setup_content_button_sensitivity ();
908 }
909
910 /** Called when the `Edit filters' button has been clicked */
911 void
912 FilmEditor::edit_filters_clicked (wxCommandEvent &)
913 {
914         FilterDialog* d = new FilterDialog (this, _film->filters());
915         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
916         d->ShowModal ();
917         d->Destroy ();
918 }
919
920 /** Called when the scaler widget has been changed */
921 void
922 FilmEditor::scaler_changed (wxCommandEvent &)
923 {
924         if (!_film) {
925                 return;
926         }
927
928         int const n = _scaler->GetSelection ();
929         if (n >= 0) {
930                 _film->set_scaler (Scaler::from_index (n));
931         }
932 }
933
934 void
935 FilmEditor::audio_gain_changed (wxCommandEvent &)
936 {
937         if (!_film) {
938                 return;
939         }
940
941         _film->set_audio_gain (_audio_gain->GetValue ());
942 }
943
944 void
945 FilmEditor::audio_delay_changed (wxCommandEvent &)
946 {
947         if (!_film) {
948                 return;
949         }
950
951         _film->set_audio_delay (_audio_delay->GetValue ());
952 }
953
954 void
955 FilmEditor::setup_notebook_size ()
956 {
957         _notebook->InvalidateBestSize ();
958         
959         _film_sizer->Layout ();
960         _film_sizer->SetSizeHints (_film_panel);
961         _video_sizer->Layout ();
962         _video_sizer->SetSizeHints (_video_panel);
963         _audio_sizer->Layout ();
964         _audio_sizer->SetSizeHints (_audio_panel);
965         _subtitle_sizer->Layout ();
966         _subtitle_sizer->SetSizeHints (_subtitle_panel);
967
968         _notebook->Fit ();
969         Fit ();
970 }
971
972 void
973 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
974 {
975         GainCalculatorDialog* d = new GainCalculatorDialog (this);
976         d->ShowModal ();
977
978         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
979                 d->Destroy ();
980                 return;
981         }
982         
983         _audio_gain->SetValue (
984                 Config::instance()->sound_processor()->db_for_fader_change (
985                         d->wanted_fader (),
986                         d->actual_fader ()
987                         )
988                 );
989
990         /* This appears to be necessary, as the change is not signalled,
991            I think.
992         */
993         wxCommandEvent dummy;
994         audio_gain_changed (dummy);
995         
996         d->Destroy ();
997 }
998
999 void
1000 FilmEditor::setup_formats ()
1001 {
1002         _formats = Format::all ();
1003
1004         _format->Clear ();
1005         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
1006                 _format->Append (std_to_wx ((*i)->name ()));
1007         }
1008
1009         _film_sizer->Layout ();
1010 }
1011
1012 void
1013 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
1014 {
1015         if (!_film) {
1016                 return;
1017         }
1018
1019         _film->set_with_subtitles (_with_subtitles->GetValue ());
1020 }
1021
1022 void
1023 FilmEditor::setup_subtitle_control_sensitivity ()
1024 {
1025         bool h = false;
1026         if (_generally_sensitive && _film) {
1027                 h = _film->has_subtitles ();
1028         }
1029         
1030         _with_subtitles->Enable (h);
1031
1032         bool j = false;
1033         if (_film) {
1034                 j = _film->with_subtitles ();
1035         }
1036         
1037         _subtitle_offset->Enable (j);
1038         _subtitle_scale->Enable (j);
1039 }
1040
1041 void
1042 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
1043 {
1044         if (!_film) {
1045                 return;
1046         }
1047
1048         _film->set_use_dci_name (_use_dci_name->GetValue ());
1049 }
1050
1051 void
1052 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
1053 {
1054         if (!_film) {
1055                 return;
1056         }
1057
1058         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
1059         d->ShowModal ();
1060         _film->set_dci_metadata (d->dci_metadata ());
1061         d->Destroy ();
1062 }
1063
1064 void
1065 FilmEditor::active_jobs_changed (bool a)
1066 {
1067         set_things_sensitive (!a);
1068 }
1069
1070 void
1071 FilmEditor::setup_dcp_name ()
1072 {
1073         string s = _film->dcp_name (true);
1074         if (s.length() > 28) {
1075                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
1076                 _dcp_name->SetToolTip (std_to_wx (s));
1077         } else {
1078                 _dcp_name->SetLabel (std_to_wx (s));
1079         }
1080 }
1081
1082 void
1083 FilmEditor::show_audio_clicked (wxCommandEvent &)
1084 {
1085         if (_audio_dialog) {
1086                 _audio_dialog->Destroy ();
1087                 _audio_dialog = 0;
1088         }
1089         
1090         _audio_dialog = new AudioDialog (this);
1091         _audio_dialog->Show ();
1092         _audio_dialog->set_film (_film);
1093 }
1094
1095 void
1096 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
1097 {
1098         if (!_film) {
1099                 return;
1100         }
1101         
1102         _film->set_dcp_video_frame_rate (_film->best_dcp_video_frame_rate ());
1103 }
1104
1105 void
1106 FilmEditor::setup_show_audio_sensitivity ()
1107 {
1108         _show_audio->Enable (_film);
1109 }
1110
1111 void
1112 FilmEditor::setup_content ()
1113 {
1114         string selected_summary;
1115         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1116         if (s != -1) {
1117                 selected_summary = wx_to_std (_content->GetItemText (s));
1118         }
1119         
1120         _content->DeleteAllItems ();
1121
1122         Playlist::RegionList regions = _film->regions ();
1123         for (Playlist::RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
1124                 int const t = _content->GetItemCount ();
1125                 _content->InsertItem (t, std_to_wx (i->content->summary ()));
1126                 if (i->content->summary() == selected_summary) {
1127                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1128                 }
1129         }
1130
1131         if (selected_summary.empty () && !regions.empty ()) {
1132                 /* Select the item of content if non was selected before */
1133                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1134         }
1135
1136         setup_content_information ();
1137 }
1138
1139 void
1140 FilmEditor::content_add_clicked (wxCommandEvent &)
1141 {
1142         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
1143         int const r = d->ShowModal ();
1144         d->Destroy ();
1145
1146         if (r != wxID_OK) {
1147                 return;
1148         }
1149
1150         wxArrayString paths;
1151         d->GetPaths (paths);
1152
1153         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
1154                 boost::filesystem::path p (wx_to_std (paths[i]));
1155
1156                 if (ImageMagickContent::valid_file (p)) {
1157                         _film->add_content (shared_ptr<ImageMagickContent> (new ImageMagickContent (p)));
1158                 } else if (SndfileContent::valid_file (p)) {
1159                         _film->add_content (shared_ptr<SndfileContent> (new SndfileContent (p)));
1160                 } else {
1161                         _film->add_content (shared_ptr<FFmpegContent> (new FFmpegContent (p)));
1162                 }
1163         }
1164 }
1165
1166 void
1167 FilmEditor::content_remove_clicked (wxCommandEvent &)
1168 {
1169         shared_ptr<Content> c = selected_content ();
1170         if (c) {
1171                 _film->remove_content (c);
1172         }
1173 }
1174
1175 void
1176 FilmEditor::content_activated (wxListEvent& ev)
1177 {
1178         Playlist::RegionList r = _film->regions ();
1179         assert (ev.GetIndex() >= 0 && size_t (ev.GetIndex()) < r.size ());
1180
1181         content_properties (r[ev.GetIndex()].content);
1182 }
1183
1184 void
1185 FilmEditor::content_properties_clicked (wxCommandEvent &)
1186 {
1187         shared_ptr<Content> c = selected_content ();
1188         if (!c) {
1189                 return;
1190         }
1191
1192         content_properties (c);
1193 }
1194
1195 void
1196 FilmEditor::content_properties (shared_ptr<Content> content)
1197 {
1198         shared_ptr<ImageMagickContent> im = dynamic_pointer_cast<ImageMagickContent> (content);
1199         if (im) {
1200                 ImageMagickContentDialog* d = new ImageMagickContentDialog (this, im);
1201                 d->ShowModal ();
1202                 d->Destroy ();
1203         }
1204
1205         shared_ptr<FFmpegContent> ff = dynamic_pointer_cast<FFmpegContent> (content);
1206         if (ff) {
1207                 FFmpegContentDialog* d = new FFmpegContentDialog (this, ff);
1208                 d->ShowModal ();
1209                 d->Destroy ();
1210         }
1211 }
1212
1213 void
1214 FilmEditor::content_selection_changed (wxListEvent &)
1215 {
1216         setup_content_button_sensitivity ();
1217         setup_content_information ();
1218 }
1219
1220 void
1221 FilmEditor::setup_content_information ()
1222 {
1223         shared_ptr<Content> c = selected_content ();
1224         if (!c) {
1225                 _content_information->SetValue (wxT (""));
1226                 return;
1227         }
1228
1229         _content_information->SetValue (std_to_wx (c->information ()));
1230 }
1231
1232 void
1233 FilmEditor::setup_content_button_sensitivity ()
1234 {
1235         _content_add->Enable (_generally_sensitive);
1236
1237         shared_ptr<Content> selection = selected_content ();
1238
1239         _content_properties->Enable (
1240                 selection && _generally_sensitive &&
1241                 (dynamic_pointer_cast<ImageMagickContent> (selection) || dynamic_pointer_cast<FFmpegContent> (selection))
1242                 );
1243         
1244         _content_remove->Enable (selection && _generally_sensitive);
1245         _content_timeline->Enable (_generally_sensitive);
1246 }
1247
1248 shared_ptr<Content>
1249 FilmEditor::selected_content ()
1250 {
1251         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1252         if (s == -1) {
1253                 return shared_ptr<Content> ();
1254         }
1255
1256         Playlist::RegionList r = _film->regions ();
1257         if (s < 0 || size_t (s) >= r.size ()) {
1258                 return shared_ptr<Content> ();
1259         }
1260         
1261         return r[s].content;
1262 }
1263
1264 void
1265 FilmEditor::setup_scaling_description ()
1266 {
1267         wxString d;
1268
1269 #if 0   
1270 XXX
1271         int lines = 0;
1272
1273         if (_film->video_size().width && _film->video_size().height) {
1274                 d << wxString::Format (
1275                         _("Original video is %dx%d (%.2f:1)\n"),
1276                         _film->video_size().width, _film->video_size().height,
1277                         float (_film->video_size().width) / _film->video_size().height
1278                         );
1279                 ++lines;
1280         }
1281
1282         Crop const crop = _film->crop ();
1283         if (crop.left || crop.right || crop.top || crop.bottom) {
1284                 libdcp::Size const cropped = _film->cropped_size (_film->video_size ());
1285                 d << wxString::Format (
1286                         _("Cropped to %dx%d (%.2f:1)\n"),
1287                         cropped.width, cropped.height,
1288                         float (cropped.width) / cropped.height
1289                         );
1290                 ++lines;
1291         }
1292
1293         Format const * format = _film->format ();
1294         if (format) {
1295                 int const padding = format->dcp_padding (_film);
1296                 libdcp::Size scaled = format->dcp_size ();
1297                 scaled.width -= padding * 2;
1298                 d << wxString::Format (
1299                         _("Scaled to %dx%d (%.2f:1)\n"),
1300                         scaled.width, scaled.height,
1301                         float (scaled.width) / scaled.height
1302                         );
1303                 ++lines;
1304
1305                 if (padding) {
1306                         d << wxString::Format (
1307                                 _("Padded with black to %dx%d (%.2f:1)\n"),
1308                                 format->dcp_size().width, format->dcp_size().height,
1309                                 float (format->dcp_size().width) / format->dcp_size().height
1310                                 );
1311                         ++lines;
1312                 }
1313         }
1314
1315         for (int i = lines; i < 4; ++i) {
1316                 d << wxT ("\n ");
1317         }
1318
1319 #endif  
1320         _scaling_description->SetLabel (d);
1321 }
1322
1323 void
1324 FilmEditor::trim_type_changed (wxCommandEvent &)
1325 {
1326         _film->set_trim_type (_trim_type->GetSelection () == 0 ? Film::CPL : Film::ENCODE);
1327 }
1328
1329 void
1330 FilmEditor::loop_content_toggled (wxCommandEvent &)
1331 {
1332         if (_loop_content->GetValue ()) {
1333                 _film->set_loop (_loop_count->GetValue ());
1334         } else {
1335                 _film->set_loop (1);
1336         }
1337                 
1338         setup_loop_sensitivity ();
1339 }
1340
1341 void
1342 FilmEditor::loop_count_changed (wxCommandEvent &)
1343 {
1344         _film->set_loop (_loop_count->GetValue ());
1345 }
1346
1347 void
1348 FilmEditor::setup_loop_sensitivity ()
1349 {
1350         _loop_count->Enable (_loop_content->GetValue ());
1351 }
1352
1353 void
1354 FilmEditor::content_timeline_clicked (wxCommandEvent &)
1355 {
1356         if (_timeline_dialog) {
1357                 _timeline_dialog->Destroy ();
1358                 _timeline_dialog = 0;
1359         }
1360         
1361         _timeline_dialog = new TimelineDialog (this, _film);
1362         _timeline_dialog->Show ();
1363 }