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