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