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