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