Various fixes to make audio analysis sort-of work.
[dcpomatic.git] / src / wx / film_editor.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/film_editor.cc
21  *  @brief A wx widget to edit a film's metadata, and perform various functions.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include <wx/wx.h>
27 #include <wx/notebook.h>
28 #include <wx/listctrl.h>
29 #include <boost/thread.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include "lib/film.h"
33 #include "lib/transcode_job.h"
34 #include "lib/exceptions.h"
35 #include "lib/job_manager.h"
36 #include "lib/filter.h"
37 #include "lib/ratio.h"
38 #include "lib/config.h"
39 #include "lib/imagemagick_content.h"
40 #include "lib/sndfile_content.h"
41 #include "lib/dcp_content_type.h"
42 #include "filter_dialog.h"
43 #include "wx_util.h"
44 #include "film_editor.h"
45 #include "gain_calculator_dialog.h"
46 #include "sound_processor.h"
47 #include "dci_metadata_dialog.h"
48 #include "scaler.h"
49 #include "audio_dialog.h"
50 #include "imagemagick_content_dialog.h"
51 #include "timeline_dialog.h"
52 #include "audio_mapping_view.h"
53 #include "timecode.h"
54
55 using std::string;
56 using std::cout;
57 using std::stringstream;
58 using std::pair;
59 using std::fixed;
60 using std::setprecision;
61 using std::list;
62 using std::vector;
63 using std::max;
64 using boost::shared_ptr;
65 using boost::weak_ptr;
66 using boost::dynamic_pointer_cast;
67 using boost::lexical_cast;
68
69 /** @param f Film to edit */
70 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
71         : wxPanel (parent)
72         , _generally_sensitive (true)
73         , _audio_dialog (0)
74         , _timeline_dialog (0)
75 {
76         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
77
78         _main_notebook = new wxNotebook (this, wxID_ANY);
79         s->Add (_main_notebook, 1);
80
81         make_content_panel ();
82         _main_notebook->AddPage (_content_panel, _("Content"), true);
83         make_dcp_panel ();
84         _main_notebook->AddPage (_dcp_panel, _("DCP"), false);
85         
86         setup_ratios ();
87
88         set_film (f);
89         connect_to_widgets ();
90
91         JobManager::instance()->ActiveJobsChanged.connect (
92                 bind (&FilmEditor::active_jobs_changed, this, _1)
93                 );
94         
95         SetSizerAndFit (s);
96 }
97
98 void
99 FilmEditor::make_dcp_panel ()
100 {
101         _dcp_panel = new wxPanel (_main_notebook);
102         _dcp_sizer = new wxBoxSizer (wxVERTICAL);
103         _dcp_panel->SetSizer (_dcp_sizer);
104
105         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
106         _dcp_sizer->Add (grid, 0, wxEXPAND | wxALL, 8);
107
108         int r = 0;
109         
110         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Name"), wxGBPosition (r, 0));
111         _name = new wxTextCtrl (_dcp_panel, wxID_ANY);
112         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
113         ++r;
114         
115         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Name"), wxGBPosition (r, 0));
116         _dcp_name = new wxStaticText (_dcp_panel, wxID_ANY, wxT (""));
117         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
118         ++r;
119
120         _use_dci_name = new wxCheckBox (_dcp_panel, wxID_ANY, _("Use DCI name"));
121         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
122         _edit_dci_button = new wxButton (_dcp_panel, wxID_ANY, _("Details..."));
123         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
124         ++r;
125
126         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Container"), wxGBPosition (r, 0));
127         _container = new wxChoice (_dcp_panel, wxID_ANY);
128         grid->Add (_container, wxGBPosition (r, 1));
129         ++r;
130
131         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Content Type"), wxGBPosition (r, 0));
132         _dcp_content_type = new wxChoice (_dcp_panel, wxID_ANY);
133         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
134         ++r;
135
136         {
137                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Frame Rate"), wxGBPosition (r, 0));
138                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
139                 _dcp_frame_rate = new wxChoice (_dcp_panel, wxID_ANY);
140                 s->Add (_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
141                 _best_dcp_frame_rate = new wxButton (_dcp_panel, wxID_ANY, _("Use best"));
142                 s->Add (_best_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND);
143                 grid->Add (s, wxGBPosition (r, 1));
144         }
145         ++r;
146
147         {
148                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("JPEG2000 bandwidth"), wxGBPosition (r, 0));
149                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
150                 _j2k_bandwidth = new wxSpinCtrl (_dcp_panel, wxID_ANY);
151                 s->Add (_j2k_bandwidth, 1);
152                 add_label_to_sizer (s, _dcp_panel, _("MBps"));
153                 grid->Add (s, wxGBPosition (r, 1));
154         }
155         ++r;
156
157         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Scaler"), wxGBPosition (r, 0));
158         _scaler = new wxChoice (_dcp_panel, wxID_ANY);
159         grid->Add (_scaler, wxGBPosition (r, 1));
160         ++r;
161
162         vector<Scaler const *> const sc = Scaler::all ();
163         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
164                 _scaler->Append (std_to_wx ((*i)->name()));
165         }
166
167         vector<Ratio const *> const ratio = Ratio::all ();
168         for (vector<Ratio const *>::const_iterator i = ratio.begin(); i != ratio.end(); ++i) {
169                 _container->Append (std_to_wx ((*i)->nickname ()));
170         }
171
172         vector<DCPContentType const *> const ct = DCPContentType::all ();
173         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
174                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
175         }
176
177         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
178         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
179                 _dcp_frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
180         }
181
182         _j2k_bandwidth->SetRange (50, 250);
183 }
184
185 void
186 FilmEditor::connect_to_widgets ()
187 {
188         _name->Connect                   (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED,         wxCommandEventHandler (FilmEditor::name_changed), 0, this);
189         _use_dci_name->Connect           (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
190         _edit_dci_button->Connect        (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
191         _container->Connect              (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::container_changed), 0, this);
192         _ratio->Connect                  (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::ratio_changed), 0, this);
193         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED,   wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
194         _content->Connect                (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler    (FilmEditor::content_selection_changed), 0, this);
195         _content_add->Connect            (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_add_clicked), 0, this);
196         _content_remove->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_remove_clicked), 0, this);
197         _content_timeline->Connect       (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_timeline_clicked), 0, this);
198         _loop_content->Connect           (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::loop_content_toggled), 0, this);
199         _loop_count->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::loop_count_changed), 0, this);
200         _left_crop->Connect              (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
201         _right_crop->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
202         _top_crop->Connect               (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
203         _bottom_crop->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
204         _filters_button->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
205         _scaler->Connect                 (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
206         _dcp_content_type->Connect       (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
207         _dcp_frame_rate->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_frame_rate_changed), 0, this);
208         _best_dcp_frame_rate->Connect    (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::best_dcp_frame_rate_clicked), 0, this);
209         _with_subtitles->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::with_subtitles_toggled), 0, this);
210         _subtitle_offset->Connect        (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_offset_changed), 0, this);
211         _subtitle_scale->Connect         (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_scale_changed), 0, this);
212         _colour_lut->Connect             (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::colour_lut_changed), 0, this);
213         _j2k_bandwidth->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
214         _audio_gain->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
215         _audio_gain_calculate_button->Connect (
216                 wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::audio_gain_calculate_button_clicked), 0, this
217                 );
218         _show_audio->Connect             (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::show_audio_clicked), 0, this);
219         _audio_delay->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
220         _audio_stream->Connect           (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::audio_stream_changed), 0, this);
221         _subtitle_stream->Connect        (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::subtitle_stream_changed), 0, this);
222         _audio_mapping->Changed.connect  (boost::bind (&FilmEditor::audio_mapping_changed, this, _1));
223         _start->Changed.connect          (boost::bind (&FilmEditor::start_changed, this));
224         _length->Changed.connect         (boost::bind (&FilmEditor::length_changed, this));
225 }
226
227 void
228 FilmEditor::make_video_panel ()
229 {
230         _video_panel = new wxPanel (_content_notebook);
231         wxBoxSizer* video_sizer = new wxBoxSizer (wxVERTICAL);
232         _video_panel->SetSizer (video_sizer);
233         
234         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
235         video_sizer->Add (grid, 0, wxALL, 8);
236
237         int r = 0;
238         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), wxGBPosition (r, 0));
239         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
240         grid->Add (_left_crop, wxGBPosition (r, 1));
241         ++r;
242
243         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), wxGBPosition (r, 0));
244         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
245         grid->Add (_right_crop, wxGBPosition (r, 1));
246         ++r;
247         
248         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), wxGBPosition (r, 0));
249         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
250         grid->Add (_top_crop, wxGBPosition (r, 1));
251         ++r;
252         
253         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), wxGBPosition (r, 0));
254         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
255         grid->Add (_bottom_crop, wxGBPosition (r, 1));
256         ++r;
257
258         add_label_to_grid_bag_sizer (grid, _video_panel, _("Scale to"), wxGBPosition (r, 0));
259         _ratio = new wxChoice (_video_panel, wxID_ANY);
260         grid->Add (_ratio, wxGBPosition (r, 1));
261         ++r;
262
263         _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
264         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
265         wxFont font = _scaling_description->GetFont();
266         font.SetStyle(wxFONTSTYLE_ITALIC);
267         font.SetPointSize(font.GetPointSize() - 1);
268         _scaling_description->SetFont(font);
269         ++r;
270
271         /* VIDEO-only stuff */
272         {
273                 add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), wxGBPosition (r, 0));
274                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
275                 _filters = new wxStaticText (_video_panel, wxID_ANY, _("None"));
276                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
277                 _filters_button = new wxButton (_video_panel, wxID_ANY, _("Edit..."));
278                 s->Add (_filters_button, 0);
279                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
280         }
281         ++r;
282
283         add_label_to_grid_bag_sizer (grid, _video_panel, _("Colour look-up table"), wxGBPosition (r, 0));
284         _colour_lut = new wxChoice (_video_panel, wxID_ANY);
285         for (int i = 0; i < 2; ++i) {
286                 _colour_lut->Append (std_to_wx (colour_lut_index_to_name (i)));
287         }
288         _colour_lut->SetSelection (0);
289         grid->Add (_colour_lut, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
290         ++r;
291
292         _left_crop->SetRange (0, 1024);
293         _top_crop->SetRange (0, 1024);
294         _right_crop->SetRange (0, 1024);
295         _bottom_crop->SetRange (0, 1024);
296 }
297
298 void
299 FilmEditor::make_content_panel ()
300 {
301         _content_panel = new wxPanel (_main_notebook);
302         _content_sizer = new wxBoxSizer (wxVERTICAL);
303         _content_panel->SetSizer (_content_sizer);
304
305         {
306                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
307                 
308                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
309                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
310
311                 _content->InsertColumn (0, wxT(""));
312                 _content->SetColumnWidth (0, 512);
313
314                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
315                 _content_add = new wxButton (_content_panel, wxID_ANY, _("Add..."));
316                 b->Add (_content_add, 1, wxEXPAND | wxLEFT | wxRIGHT);
317                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
318                 b->Add (_content_remove, 1, wxEXPAND | wxLEFT | wxRIGHT);
319                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
320                 b->Add (_content_timeline, 1, wxEXPAND | wxLEFT | wxRIGHT);
321
322                 s->Add (b, 0, wxALL, 4);
323
324                 _content_sizer->Add (s, 0.75, wxEXPAND | wxALL, 6);
325         }
326
327         wxBoxSizer* h = new wxBoxSizer (wxHORIZONTAL);
328         _loop_content = new wxCheckBox (_content_panel, wxID_ANY, _("Loop everything"));
329         h->Add (_loop_content, 0, wxALL, 6);
330         _loop_count = new wxSpinCtrl (_content_panel, wxID_ANY);
331         h->Add (_loop_count, 0, wxALL, 6);
332         add_label_to_sizer (h, _content_panel, _("times"));
333         _content_sizer->Add (h, 0, wxALL, 6);
334
335         _content_notebook = new wxNotebook (_content_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_LEFT);
336         _content_sizer->Add (_content_notebook, 1, wxEXPAND | wxTOP, 6);
337
338         make_video_panel ();
339         _content_notebook->AddPage (_video_panel, _("Video"), false);
340         make_audio_panel ();
341         _content_notebook->AddPage (_audio_panel, _("Audio"), false);
342         make_subtitle_panel ();
343         _content_notebook->AddPage (_subtitle_panel, _("Subtitles"), false);
344         make_timing_panel ();
345         _content_notebook->AddPage (_timing_panel, _("Timing"), false);
346
347         _loop_count->SetRange (2, 1024);
348 }
349
350 void
351 FilmEditor::make_audio_panel ()
352 {
353         _audio_panel = new wxPanel (_content_notebook);
354         wxBoxSizer* audio_sizer = new wxBoxSizer (wxVERTICAL);
355         _audio_panel->SetSizer (audio_sizer);
356         
357         wxFlexGridSizer* grid = new wxFlexGridSizer (3, 4, 4);
358         audio_sizer->Add (grid, 0, wxALL, 8);
359
360         _show_audio = new wxButton (_audio_panel, wxID_ANY, _("Show Audio..."));
361         grid->Add (_show_audio, 1);
362         grid->AddSpacer (0);
363         grid->AddSpacer (0);
364
365         add_label_to_sizer (grid, _audio_panel, _("Audio Gain"));
366         {
367                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
368                 _audio_gain = new wxSpinCtrl (_audio_panel);
369                 s->Add (_audio_gain, 1);
370                 add_label_to_sizer (s, _audio_panel, _("dB"));
371                 grid->Add (s, 1);
372         }
373         
374         _audio_gain_calculate_button = new wxButton (_audio_panel, wxID_ANY, _("Calculate..."));
375         grid->Add (_audio_gain_calculate_button);
376
377         add_label_to_sizer (grid, _audio_panel, _("Audio Delay"));
378         {
379                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
380                 _audio_delay = new wxSpinCtrl (_audio_panel);
381                 s->Add (_audio_delay, 1);
382                 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
383                 add_label_to_sizer (s, _audio_panel, _("ms"));
384                 grid->Add (s);
385         }
386
387         grid->AddSpacer (0);
388
389         add_label_to_sizer (grid, _audio_panel, _("Audio Stream"));
390         _audio_stream = new wxChoice (_audio_panel, wxID_ANY);
391         grid->Add (_audio_stream, 1);
392         _audio_description = new wxStaticText (_audio_panel, wxID_ANY, wxT (""));
393         grid->AddSpacer (0);
394         
395         grid->Add (_audio_description, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
396         grid->AddSpacer (0);
397         grid->AddSpacer (0);
398         
399         _audio_mapping = new AudioMappingView (_audio_panel);
400         audio_sizer->Add (_audio_mapping, 1, wxEXPAND | wxALL, 6);
401
402         _audio_gain->SetRange (-60, 60);
403         _audio_delay->SetRange (-1000, 1000);
404 }
405
406 void
407 FilmEditor::make_subtitle_panel ()
408 {
409         _subtitle_panel = new wxPanel (_content_notebook);
410         wxBoxSizer* subtitle_sizer = new wxBoxSizer (wxVERTICAL);
411         _subtitle_panel->SetSizer (subtitle_sizer);
412         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
413         subtitle_sizer->Add (grid, 0, wxALL, 8);
414
415         _with_subtitles = new wxCheckBox (_subtitle_panel, wxID_ANY, _("With Subtitles"));
416         grid->Add (_with_subtitles, 1);
417         grid->AddSpacer (0);
418         
419         {
420                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"));
421                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
422                 _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
423                 s->Add (_subtitle_offset);
424                 add_label_to_sizer (s, _subtitle_panel, _("pixels"));
425                 grid->Add (s);
426         }
427
428         {
429                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale"));
430                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
431                 _subtitle_scale = new wxSpinCtrl (_subtitle_panel);
432                 s->Add (_subtitle_scale);
433                 add_label_to_sizer (s, _subtitle_panel, _("%"));
434                 grid->Add (s);
435         }
436
437         add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Stream"));
438         _subtitle_stream = new wxChoice (_subtitle_panel, wxID_ANY);
439         grid->Add (_subtitle_stream, 1, wxEXPAND | wxALL, 6);
440         grid->AddSpacer (0);
441         
442         _subtitle_offset->SetRange (-1024, 1024);
443         _subtitle_scale->SetRange (1, 1000);
444 }
445
446 void
447 FilmEditor::make_timing_panel ()
448 {
449         _timing_panel = new wxPanel (_content_notebook);
450         wxBoxSizer* timing_sizer = new wxBoxSizer (wxVERTICAL);
451         _timing_panel->SetSizer (timing_sizer);
452         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
453         timing_sizer->Add (grid, 0, wxALL, 8);
454
455         add_label_to_sizer (grid, _timing_panel, _("Start time"));
456         _start = new Timecode (_timing_panel);
457         grid->Add (_start);
458         add_label_to_sizer (grid, _timing_panel, _("Length"));
459         _length = new Timecode (_timing_panel);
460         grid->Add (_length);
461 }
462
463
464 /** Called when the left crop widget has been changed */
465 void
466 FilmEditor::left_crop_changed (wxCommandEvent &)
467 {
468         shared_ptr<VideoContent> c = selected_video_content ();
469         if (!c) {
470                 return;
471         }
472
473         c->set_left_crop (_left_crop->GetValue ());
474 }
475
476 /** Called when the right crop widget has been changed */
477 void
478 FilmEditor::right_crop_changed (wxCommandEvent &)
479 {
480         shared_ptr<VideoContent> c = selected_video_content ();
481         if (!c) {
482                 return;
483         }
484
485         c->set_right_crop (_right_crop->GetValue ());
486 }
487
488 /** Called when the top crop widget has been changed */
489 void
490 FilmEditor::top_crop_changed (wxCommandEvent &)
491 {
492         shared_ptr<VideoContent> c = selected_video_content ();
493         if (!c) {
494                 return;
495         }
496
497         c->set_top_crop (_top_crop->GetValue ());
498 }
499
500 /** Called when the bottom crop value has been changed */
501 void
502 FilmEditor::bottom_crop_changed (wxCommandEvent &)
503 {
504         shared_ptr<VideoContent> c = selected_video_content ();
505         if (!c) {
506                 return;
507         }
508
509         c->set_bottom_crop (_bottom_crop->GetValue ());
510 }
511
512 /** Called when the name widget has been changed */
513 void
514 FilmEditor::name_changed (wxCommandEvent &)
515 {
516         if (!_film) {
517                 return;
518         }
519
520         _film->set_name (string (_name->GetValue().mb_str()));
521 }
522
523 void
524 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
525 {
526         if (!_film) {
527                 return;
528         }
529
530         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
531 }
532
533 void
534 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
535 {
536         if (!_film) {
537                 return;
538         }
539
540         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
541 }
542
543 void
544 FilmEditor::colour_lut_changed (wxCommandEvent &)
545 {
546         if (!_film) {
547                 return;
548         }
549         
550         _film->set_colour_lut (_colour_lut->GetSelection ());
551 }
552
553 void
554 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
555 {
556         if (!_film) {
557                 return;
558         }
559         
560         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
561 }
562
563 void
564 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
565 {
566         if (!_film) {
567                 return;
568         }
569
570         _film->set_dcp_video_frame_rate (
571                 boost::lexical_cast<int> (
572                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
573                         )
574                 );
575 }
576
577
578 /** Called when the metadata stored in the Film object has changed;
579  *  so that we can update the GUI.
580  *  @param p Property of the Film that has changed.
581  */
582 void
583 FilmEditor::film_changed (Film::Property p)
584 {
585         ensure_ui_thread ();
586         
587         if (!_film) {
588                 return;
589         }
590
591         stringstream s;
592                 
593         switch (p) {
594         case Film::NONE:
595                 break;
596         case Film::CONTENT:
597                 setup_content ();
598                 setup_subtitle_control_sensitivity ();
599                 setup_show_audio_sensitivity ();
600                 break;
601         case Film::LOOP:
602                 checked_set (_loop_content, _film->loop() > 1);
603                 checked_set (_loop_count, _film->loop());
604                 setup_loop_sensitivity ();
605                 break;
606         case Film::CONTAINER:
607                 setup_container ();
608                 break;
609         case Film::NAME:
610                 checked_set (_name, _film->name());
611                 setup_dcp_name ();
612                 break;
613         case Film::DCP_CONTENT_TYPE:
614                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
615                 setup_dcp_name ();
616                 break;
617         case Film::SCALER:
618                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
619                 break;
620         case Film::WITH_SUBTITLES:
621                 checked_set (_with_subtitles, _film->with_subtitles ());
622                 setup_subtitle_control_sensitivity ();
623                 setup_dcp_name ();
624                 break;
625         case Film::SUBTITLE_OFFSET:
626                 checked_set (_subtitle_offset, _film->subtitle_offset ());
627                 break;
628         case Film::SUBTITLE_SCALE:
629                 checked_set (_subtitle_scale, _film->subtitle_scale() * 100);
630                 break;
631         case Film::COLOUR_LUT:
632                 checked_set (_colour_lut, _film->colour_lut ());
633                 break;
634         case Film::J2K_BANDWIDTH:
635                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
636                 break;
637         case Film::USE_DCI_NAME:
638                 checked_set (_use_dci_name, _film->use_dci_name ());
639                 setup_dcp_name ();
640                 break;
641         case Film::DCI_METADATA:
642                 setup_dcp_name ();
643                 break;
644         case Film::DCP_VIDEO_FRAME_RATE:
645         {
646                 bool done = false;
647                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
648                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_video_frame_rate())) {
649                                 checked_set (_dcp_frame_rate, i);
650                                 done = true;
651                                 break;
652                         }
653                 }
654
655                 if (!done) {
656                         checked_set (_dcp_frame_rate, -1);
657                 }
658
659                 _best_dcp_frame_rate->Enable (_film->best_dcp_video_frame_rate () != _film->dcp_video_frame_rate ());
660                 break;
661         }
662         }
663 }
664
665 void
666 FilmEditor::film_content_changed (weak_ptr<Content> weak_content, int property)
667 {
668         if (!_film) {
669                 /* We call this method ourselves (as well as using it as a signal handler)
670                    so _film can be 0.
671                 */
672                 return;
673         }
674
675         shared_ptr<Content> content = weak_content.lock ();
676         shared_ptr<VideoContent> video_content;
677         shared_ptr<AudioContent> audio_content;
678         shared_ptr<FFmpegContent> ffmpeg_content;
679         if (content) {
680                 video_content = dynamic_pointer_cast<VideoContent> (content);
681                 audio_content = dynamic_pointer_cast<AudioContent> (content);
682                 ffmpeg_content = dynamic_pointer_cast<FFmpegContent> (content);
683         }
684
685         /* We can't use case {} here */
686         
687         if (property == ContentProperty::START) {
688                 if (content) {
689                         _start->set (content->start (), _film->dcp_video_frame_rate ());
690                 } else {
691                         _start->set (0, 24);
692                 }
693         } else if (property == ContentProperty::LENGTH) {
694                 if (content) {
695                         _length->set (content->length (), _film->dcp_video_frame_rate ());
696                 } else {
697                         _length->set (0, 24);
698                 }
699         } else if (property == VideoContentProperty::VIDEO_CROP) {
700                 checked_set (_left_crop,   video_content ? video_content->crop().left :   0);
701                 checked_set (_right_crop,  video_content ? video_content->crop().right :  0);
702                 checked_set (_top_crop,    video_content ? video_content->crop().top :    0);
703                 checked_set (_bottom_crop, video_content ? video_content->crop().bottom : 0);
704                 setup_scaling_description ();
705         } else if (property == VideoContentProperty::VIDEO_RATIO) {
706                 if (video_content) {
707                         int n = 0;
708                         vector<Ratio const *> ratios = Ratio::all ();
709                         vector<Ratio const *>::iterator i = ratios.begin ();
710                         while (i != ratios.end() && *i != video_content->ratio()) {
711                                 ++i;
712                                 ++n;
713                         }
714
715                         if (i == ratios.end()) {
716                                 checked_set (_ratio, -1);
717                         } else {
718                                 checked_set (_ratio, n);
719                         }
720                 } else {
721                         checked_set (_ratio, -1);
722                 }
723         } else if (property == AudioContentProperty::AUDIO_GAIN) {
724                 checked_set (_audio_gain, audio_content ? audio_content->audio_gain() : 0);
725         } else if (property == AudioContentProperty::AUDIO_DELAY) {
726                 checked_set (_audio_delay, audio_content ? audio_content->audio_delay() : 0);
727         } else if (property == AudioContentProperty::AUDIO_MAPPING) {
728                 _audio_mapping->set (audio_content ? audio_content->audio_mapping () : AudioMapping ());
729         } else if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
730                 _subtitle_stream->Clear ();
731                 if (ffmpeg_content) {
732                         vector<shared_ptr<FFmpegSubtitleStream> > s = ffmpeg_content->subtitle_streams ();
733                         if (s.empty ()) {
734                                 _subtitle_stream->Enable (false);
735                         }
736                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
737                                 _subtitle_stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
738                         }
739                         
740                         if (ffmpeg_content->subtitle_stream()) {
741                                 checked_set (_subtitle_stream, lexical_cast<string> (ffmpeg_content->subtitle_stream()->id));
742                         } else {
743                                 _subtitle_stream->SetSelection (wxNOT_FOUND);
744                         }
745                 }
746                 setup_subtitle_control_sensitivity ();
747         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
748                 _audio_stream->Clear ();
749                 if (ffmpeg_content) {
750                         vector<shared_ptr<FFmpegAudioStream> > a = ffmpeg_content->audio_streams ();
751                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
752                                 _audio_stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
753                         }
754                         
755                         if (ffmpeg_content->audio_stream()) {
756                                 checked_set (_audio_stream, lexical_cast<string> (ffmpeg_content->audio_stream()->id));
757                         }
758                 }
759                 setup_show_audio_sensitivity ();
760         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
761                 setup_dcp_name ();
762                 setup_show_audio_sensitivity ();
763         } else if (property == FFmpegContentProperty::FILTERS) {
764                 if (ffmpeg_content) {
765                         pair<string, string> p = Filter::ffmpeg_strings (ffmpeg_content->filters ());
766                         if (p.first.empty () && p.second.empty ()) {
767                                 _filters->SetLabel (_("None"));
768                         } else {
769                                 string const b = p.first + " " + p.second;
770                                 _filters->SetLabel (std_to_wx (b));
771                         }
772                         _dcp_sizer->Layout ();
773                 }
774         }
775 }
776
777 void
778 FilmEditor::setup_container ()
779 {
780         int n = 0;
781         vector<Ratio const *> ratios = Ratio::all ();
782         vector<Ratio const *>::iterator i = ratios.begin ();
783         while (i != ratios.end() && *i != _film->container ()) {
784                 ++i;
785                 ++n;
786         }
787         
788         if (i == ratios.end()) {
789                 checked_set (_container, -1);
790         } else {
791                 checked_set (_container, n);
792         }
793         
794         setup_dcp_name ();
795         setup_scaling_description ();
796 }       
797
798 /** Called when the container widget has been changed */
799 void
800 FilmEditor::container_changed (wxCommandEvent &)
801 {
802         if (!_film) {
803                 return;
804         }
805
806         int const n = _container->GetSelection ();
807         if (n >= 0) {
808                 vector<Ratio const *> ratios = Ratio::all ();
809                 assert (n < int (ratios.size()));
810                 _film->set_container (ratios[n]);
811         }
812 }
813
814 /** Called when the DCP content type widget has been changed */
815 void
816 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
817 {
818         if (!_film) {
819                 return;
820         }
821
822         int const n = _dcp_content_type->GetSelection ();
823         if (n != wxNOT_FOUND) {
824                 _film->set_dcp_content_type (DCPContentType::from_index (n));
825         }
826 }
827
828 /** Sets the Film that we are editing */
829 void
830 FilmEditor::set_film (shared_ptr<Film> f)
831 {
832         set_things_sensitive (f != 0);
833
834         if (_film == f) {
835                 return;
836         }
837         
838         _film = f;
839
840         if (_film) {
841                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
842                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
843         }
844
845         if (_film) {
846                 FileChanged (_film->directory ());
847         } else {
848                 FileChanged ("");
849         }
850
851         film_changed (Film::NAME);
852         film_changed (Film::USE_DCI_NAME);
853         film_changed (Film::CONTENT);
854         film_changed (Film::LOOP);
855         film_changed (Film::DCP_CONTENT_TYPE);
856         film_changed (Film::CONTAINER);
857         film_changed (Film::SCALER);
858         film_changed (Film::WITH_SUBTITLES);
859         film_changed (Film::SUBTITLE_OFFSET);
860         film_changed (Film::SUBTITLE_SCALE);
861         film_changed (Film::COLOUR_LUT);
862         film_changed (Film::J2K_BANDWIDTH);
863         film_changed (Film::DCI_METADATA);
864         film_changed (Film::DCP_VIDEO_FRAME_RATE);
865
866         wxListEvent ev;
867         content_selection_changed (ev);
868 }
869
870 /** Updates the sensitivity of lots of widgets to a given value.
871  *  @param s true to make sensitive, false to make insensitive.
872  */
873 void
874 FilmEditor::set_things_sensitive (bool s)
875 {
876         _generally_sensitive = s;
877         
878         _name->Enable (s);
879         _use_dci_name->Enable (s);
880         _edit_dci_button->Enable (s);
881         _ratio->Enable (s);
882         _content->Enable (s);
883         _left_crop->Enable (s);
884         _right_crop->Enable (s);
885         _top_crop->Enable (s);
886         _bottom_crop->Enable (s);
887         _filters_button->Enable (s);
888         _scaler->Enable (s);
889         _dcp_content_type->Enable (s);
890         _best_dcp_frame_rate->Enable (s);
891         _dcp_frame_rate->Enable (s);
892         _colour_lut->Enable (s);
893         _j2k_bandwidth->Enable (s);
894         _audio_gain->Enable (s);
895         _audio_gain_calculate_button->Enable (s);
896         _show_audio->Enable (s);
897         _audio_delay->Enable (s);
898         _container->Enable (s);
899         _loop_content->Enable (s);
900         _loop_count->Enable (s);
901
902         setup_subtitle_control_sensitivity ();
903         setup_show_audio_sensitivity ();
904         setup_content_sensitivity ();
905 }
906
907 /** Called when the `Edit filters' button has been clicked */
908 void
909 FilmEditor::edit_filters_clicked (wxCommandEvent &)
910 {
911         shared_ptr<Content> c = selected_content ();
912         if (!c) {
913                 return;
914         }
915
916         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
917         if (!fc) {
918                 return;
919         }
920         
921         FilterDialog* d = new FilterDialog (this, fc->filters());
922         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, fc, _1));
923         d->ShowModal ();
924         d->Destroy ();
925 }
926
927 /** Called when the scaler widget has been changed */
928 void
929 FilmEditor::scaler_changed (wxCommandEvent &)
930 {
931         if (!_film) {
932                 return;
933         }
934
935         int const n = _scaler->GetSelection ();
936         if (n >= 0) {
937                 _film->set_scaler (Scaler::from_index (n));
938         }
939 }
940
941 void
942 FilmEditor::audio_gain_changed (wxCommandEvent &)
943 {
944         shared_ptr<AudioContent> ac = selected_audio_content ();
945         if (!ac) {
946                 return;
947         }
948
949         ac->set_audio_gain (_audio_gain->GetValue ());
950 }
951
952 void
953 FilmEditor::audio_delay_changed (wxCommandEvent &)
954 {
955         shared_ptr<AudioContent> ac = selected_audio_content ();
956         if (!ac) {
957                 return;
958         }
959
960         ac->set_audio_delay (_audio_delay->GetValue ());
961 }
962
963 void
964 FilmEditor::setup_main_notebook_size ()
965 {
966         _main_notebook->InvalidateBestSize ();
967
968         _content_sizer->Layout ();
969         _content_sizer->SetSizeHints (_content_panel);
970         _dcp_sizer->Layout ();
971         _dcp_sizer->SetSizeHints (_dcp_panel);
972
973         _main_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_ratios ()
1006 {
1007         _ratios = Ratio::all ();
1008
1009         _ratio->Clear ();
1010         for (vector<Ratio const *>::iterator i = _ratios.begin(); i != _ratios.end(); ++i) {
1011                 _ratio->Append (std_to_wx ((*i)->nickname ()));
1012         }
1013
1014         _dcp_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         shared_ptr<Content> c = selected_content ();
1096         if (!c) {
1097                 return;
1098         }
1099
1100         shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c);
1101         if (!ac) {
1102                 return;
1103         }
1104         
1105         _audio_dialog = new AudioDialog (this);
1106         _audio_dialog->Show ();
1107         _audio_dialog->set_content (ac);
1108 }
1109
1110 void
1111 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
1112 {
1113         if (!_film) {
1114                 return;
1115         }
1116         
1117         _film->set_dcp_video_frame_rate (_film->best_dcp_video_frame_rate ());
1118 }
1119
1120 void
1121 FilmEditor::setup_show_audio_sensitivity ()
1122 {
1123         _show_audio->Enable (_film);
1124 }
1125
1126 void
1127 FilmEditor::setup_content ()
1128 {
1129         string selected_summary;
1130         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1131         if (s != -1) {
1132                 selected_summary = wx_to_std (_content->GetItemText (s));
1133         }
1134         
1135         _content->DeleteAllItems ();
1136
1137         Playlist::ContentList content = _film->content ();
1138         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
1139                 int const t = _content->GetItemCount ();
1140                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
1141                 if ((*i)->summary() == selected_summary) {
1142                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1143                 }
1144         }
1145
1146         if (selected_summary.empty () && !content.empty ()) {
1147                 /* Select the item of content if none was selected before */
1148                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1149         }
1150 }
1151
1152 void
1153 FilmEditor::content_add_clicked (wxCommandEvent &)
1154 {
1155         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
1156         int const r = d->ShowModal ();
1157         d->Destroy ();
1158
1159         if (r != wxID_OK) {
1160                 return;
1161         }
1162
1163         wxArrayString paths;
1164         d->GetPaths (paths);
1165
1166         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
1167                 boost::filesystem::path p (wx_to_std (paths[i]));
1168
1169                 shared_ptr<Content> c;
1170
1171                 if (ImageMagickContent::valid_file (p)) {
1172                         c.reset (new ImageMagickContent (_film, p));
1173                 } else if (SndfileContent::valid_file (p)) {
1174                         c.reset (new SndfileContent (_film, p));
1175                 } else {
1176                         c.reset (new FFmpegContent (_film, p));
1177                 }
1178
1179                 _film->examine_and_add_content (c);
1180         }
1181 }
1182
1183 void
1184 FilmEditor::content_remove_clicked (wxCommandEvent &)
1185 {
1186         shared_ptr<Content> c = selected_content ();
1187         if (c) {
1188                 _film->remove_content (c);
1189         }
1190 }
1191
1192 void
1193 FilmEditor::content_selection_changed (wxListEvent &)
1194 {
1195         setup_content_sensitivity ();
1196         shared_ptr<Content> s = selected_content ();
1197         
1198         if (_audio_dialog && s && dynamic_pointer_cast<AudioContent> (s)) {
1199                 _audio_dialog->set_content (dynamic_pointer_cast<AudioContent> (s));
1200         }
1201         
1202         film_content_changed (s, ContentProperty::START);
1203         film_content_changed (s, ContentProperty::LENGTH);
1204         film_content_changed (s, VideoContentProperty::VIDEO_CROP);
1205         film_content_changed (s, VideoContentProperty::VIDEO_RATIO);
1206         film_content_changed (s, AudioContentProperty::AUDIO_GAIN);
1207         film_content_changed (s, AudioContentProperty::AUDIO_DELAY);
1208         film_content_changed (s, AudioContentProperty::AUDIO_MAPPING);
1209         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAM);
1210         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAMS);
1211         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAM);
1212 }
1213
1214 void
1215 FilmEditor::setup_content_sensitivity ()
1216 {
1217         _content_add->Enable (_generally_sensitive);
1218
1219         shared_ptr<Content> selection = selected_content ();
1220
1221         _content_remove->Enable (selection && _generally_sensitive);
1222         _content_timeline->Enable (_generally_sensitive);
1223
1224         _video_panel->Enable    (selection && dynamic_pointer_cast<VideoContent>  (selection) && _generally_sensitive);
1225         _audio_panel->Enable    (selection && dynamic_pointer_cast<AudioContent>  (selection) && _generally_sensitive);
1226         _subtitle_panel->Enable (selection && dynamic_pointer_cast<FFmpegContent> (selection) && _generally_sensitive);
1227         _timing_panel->Enable   (selection && _generally_sensitive);
1228 }
1229
1230 shared_ptr<Content>
1231 FilmEditor::selected_content ()
1232 {
1233         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1234         if (s == -1) {
1235                 return shared_ptr<Content> ();
1236         }
1237
1238         Playlist::ContentList c = _film->content ();
1239         if (s < 0 || size_t (s) >= c.size ()) {
1240                 return shared_ptr<Content> ();
1241         }
1242         
1243         return c[s];
1244 }
1245
1246 shared_ptr<VideoContent>
1247 FilmEditor::selected_video_content ()
1248 {
1249         shared_ptr<Content> c = selected_content ();
1250         if (!c) {
1251                 return shared_ptr<VideoContent> ();
1252         }
1253
1254         return dynamic_pointer_cast<VideoContent> (c);
1255 }
1256
1257 shared_ptr<AudioContent>
1258 FilmEditor::selected_audio_content ()
1259 {
1260         shared_ptr<Content> c = selected_content ();
1261         if (!c) {
1262                 return shared_ptr<AudioContent> ();
1263         }
1264
1265         return dynamic_pointer_cast<AudioContent> (c);
1266 }
1267
1268 void
1269 FilmEditor::setup_scaling_description ()
1270 {
1271         wxString d;
1272
1273 #if 0   
1274 XXX
1275         int lines = 0;
1276
1277         if (_film->video_size().width && _film->video_size().height) {
1278                 d << wxString::Format (
1279                         _("Original video is %dx%d (%.2f:1)\n"),
1280                         _film->video_size().width, _film->video_size().height,
1281                         float (_film->video_size().width) / _film->video_size().height
1282                         );
1283                 ++lines;
1284         }
1285
1286         Crop const crop = _film->crop ();
1287         if (crop.left || crop.right || crop.top || crop.bottom) {
1288                 libdcp::Size const cropped = _film->cropped_size (_film->video_size ());
1289                 d << wxString::Format (
1290                         _("Cropped to %dx%d (%.2f:1)\n"),
1291                         cropped.width, cropped.height,
1292                         float (cropped.width) / cropped.height
1293                         );
1294                 ++lines;
1295         }
1296
1297         Format const * format = _film->format ();
1298         if (format) {
1299                 int const padding = format->dcp_padding (_film);
1300                 libdcp::Size scaled = format->dcp_size ();
1301                 scaled.width -= padding * 2;
1302                 d << wxString::Format (
1303                         _("Scaled to %dx%d (%.2f:1)\n"),
1304                         scaled.width, scaled.height,
1305                         float (scaled.width) / scaled.height
1306                         );
1307                 ++lines;
1308
1309                 if (padding) {
1310                         d << wxString::Format (
1311                                 _("Padded with black to %dx%d (%.2f:1)\n"),
1312                                 format->dcp_size().width, format->dcp_size().height,
1313                                 float (format->dcp_size().width) / format->dcp_size().height
1314                                 );
1315                         ++lines;
1316                 }
1317         }
1318
1319         for (int i = lines; i < 4; ++i) {
1320                 d << wxT ("\n ");
1321         }
1322
1323 #endif  
1324         _scaling_description->SetLabel (d);
1325 }
1326
1327 void
1328 FilmEditor::loop_content_toggled (wxCommandEvent &)
1329 {
1330         if (_loop_content->GetValue ()) {
1331                 _film->set_loop (_loop_count->GetValue ());
1332         } else {
1333                 _film->set_loop (1);
1334         }
1335                 
1336         setup_loop_sensitivity ();
1337 }
1338
1339 void
1340 FilmEditor::loop_count_changed (wxCommandEvent &)
1341 {
1342         _film->set_loop (_loop_count->GetValue ());
1343 }
1344
1345 void
1346 FilmEditor::setup_loop_sensitivity ()
1347 {
1348         _loop_count->Enable (_loop_content->GetValue ());
1349 }
1350
1351 void
1352 FilmEditor::content_timeline_clicked (wxCommandEvent &)
1353 {
1354         if (_timeline_dialog) {
1355                 _timeline_dialog->Destroy ();
1356                 _timeline_dialog = 0;
1357         }
1358         
1359         _timeline_dialog = new TimelineDialog (this, _film);
1360         _timeline_dialog->Show ();
1361 }
1362
1363 void
1364 FilmEditor::audio_stream_changed (wxCommandEvent &)
1365 {
1366         shared_ptr<Content> c = selected_content ();
1367         if (!c) {
1368                 return;
1369         }
1370         
1371         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
1372         if (!fc) {
1373                 return;
1374         }
1375         
1376         vector<shared_ptr<FFmpegAudioStream> > a = fc->audio_streams ();
1377         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
1378         string const s = string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ()));
1379         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
1380                 ++i;
1381         }
1382
1383         if (i != a.end ()) {
1384                 fc->set_audio_stream (*i);
1385         }
1386
1387         if (!fc->audio_stream ()) {
1388                 _audio_description->SetLabel (wxT (""));
1389         } else {
1390                 wxString s;
1391                 if (fc->audio_channels() == 1) {
1392                         s << _("1 channel");
1393                 } else {
1394                         s << fc->audio_channels() << wxT (" ") << _("channels");
1395                 }
1396                 s << wxT (", ") << fc->content_audio_frame_rate() << _("Hz");
1397                 _audio_description->SetLabel (s);
1398         }
1399 }
1400
1401
1402
1403 void
1404 FilmEditor::subtitle_stream_changed (wxCommandEvent &)
1405 {
1406         shared_ptr<Content> c = selected_content ();
1407         if (!c) {
1408                 return;
1409         }
1410         
1411         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
1412         if (!fc) {
1413                 return;
1414         }
1415         
1416         vector<shared_ptr<FFmpegSubtitleStream> > a = fc->subtitle_streams ();
1417         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
1418         string const s = string_client_data (_subtitle_stream->GetClientObject (_subtitle_stream->GetSelection ()));
1419         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
1420                 ++i;
1421         }
1422
1423         if (i != a.end ()) {
1424                 fc->set_subtitle_stream (*i);
1425         }
1426 }
1427
1428 void
1429 FilmEditor::audio_mapping_changed (AudioMapping m)
1430 {
1431         shared_ptr<Content> c = selected_content ();
1432         if (!c) {
1433                 return;
1434         }
1435         
1436         shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c);
1437         if (!ac) {
1438                 return;
1439         }
1440
1441         ac->set_audio_mapping (m);
1442 }
1443
1444 void
1445 FilmEditor::start_changed ()
1446 {
1447         shared_ptr<Content> c = selected_content ();
1448         if (!c) {
1449                 return;
1450         }
1451
1452         c->set_start (_start->get (_film->dcp_video_frame_rate ()));
1453 }
1454
1455 void
1456 FilmEditor::length_changed ()
1457 {
1458         shared_ptr<Content> c = selected_content ();
1459         if (!c) {
1460                 return;
1461         }
1462
1463         shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (c);
1464         if (ic) {
1465                 ic->set_video_length (_length->get(_film->dcp_video_frame_rate()) * ic->video_frame_rate() / TIME_HZ);
1466         }
1467 }
1468
1469 void
1470 FilmEditor::set_selection (weak_ptr<Content> wc)
1471 {
1472         Playlist::ContentList content = _film->content ();
1473         for (size_t i = 0; i < content.size(); ++i) {
1474                 if (content[i] == wc.lock ()) {
1475                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1476                 } else {
1477                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
1478                 }
1479         }
1480 }
1481
1482 void
1483 FilmEditor::ratio_changed (wxCommandEvent &)
1484 {
1485         if (!_film) {
1486                 return;
1487         }
1488
1489         shared_ptr<Content> c = selected_content ();
1490         if (!c) {
1491                 return;
1492         }
1493
1494         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
1495         if (!vc) {
1496                 return;
1497         }
1498         
1499         int const n = _ratio->GetSelection ();
1500         if (n >= 0) {
1501                 vector<Ratio const *> ratios = Ratio::all ();
1502                 assert (n < int (ratios.size()));
1503                 vc->set_ratio (ratios[n]);
1504         }
1505 }