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