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 \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         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), wxGBPosition (r, 0));
252         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
253         grid->Add (_left_crop, wxGBPosition (r, 1));
254         ++r;
255
256         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), wxGBPosition (r, 0));
257         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
258         grid->Add (_right_crop, wxGBPosition (r, 1));
259         ++r;
260         
261         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), wxGBPosition (r, 0));
262         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
263         grid->Add (_top_crop, wxGBPosition (r, 1));
264         ++r;
265         
266         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), wxGBPosition (r, 0));
267         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
268         grid->Add (_bottom_crop, wxGBPosition (r, 1));
269         ++r;
270
271         _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
272         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
273         wxFont font = _scaling_description->GetFont();
274         font.SetStyle(wxFONTSTYLE_ITALIC);
275         font.SetPointSize(font.GetPointSize() - 1);
276         _scaling_description->SetFont(font);
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         {
434                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"));
435                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
436                 _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
437                 s->Add (_subtitle_offset);
438                 add_label_to_sizer (s, _subtitle_panel, _("pixels"));
439                 grid->Add (s);
440         }
441
442         {
443                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale"));
444                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
445                 _subtitle_scale = new wxSpinCtrl (_subtitle_panel);
446                 s->Add (_subtitle_scale);
447                 add_label_to_sizer (s, _subtitle_panel, _("%"));
448                 grid->Add (s);
449         }
450
451         _subtitle_offset->SetRange (-1024, 1024);
452         _subtitle_scale->SetRange (1, 1000);
453 }
454
455 /** Called when the left crop widget has been changed */
456 void
457 FilmEditor::left_crop_changed (wxCommandEvent &)
458 {
459         if (!_film) {
460                 return;
461         }
462
463         _film->set_left_crop (_left_crop->GetValue ());
464 }
465
466 /** Called when the right crop widget has been changed */
467 void
468 FilmEditor::right_crop_changed (wxCommandEvent &)
469 {
470         if (!_film) {
471                 return;
472         }
473
474         _film->set_right_crop (_right_crop->GetValue ());
475 }
476
477 /** Called when the top crop widget has been changed */
478 void
479 FilmEditor::top_crop_changed (wxCommandEvent &)
480 {
481         if (!_film) {
482                 return;
483         }
484
485         _film->set_top_crop (_top_crop->GetValue ());
486 }
487
488 /** Called when the bottom crop value has been changed */
489 void
490 FilmEditor::bottom_crop_changed (wxCommandEvent &)
491 {
492         if (!_film) {
493                 return;
494         }
495
496         _film->set_bottom_crop (_bottom_crop->GetValue ());
497 }
498
499 void
500 FilmEditor::trust_content_headers_changed (wxCommandEvent &)
501 {
502         if (!_film) {
503                 return;
504         }
505
506         _film->set_trust_content_headers (_trust_content_headers->GetValue ());
507 }
508
509 /** Called when the DCP A/B switch has been toggled */
510 void
511 FilmEditor::ab_toggled (wxCommandEvent &)
512 {
513         if (!_film) {
514                 return;
515         }
516         
517         _film->set_ab (_ab->GetValue ());
518 }
519
520 /** Called when the name widget has been changed */
521 void
522 FilmEditor::name_changed (wxCommandEvent &)
523 {
524         if (!_film) {
525                 return;
526         }
527
528         _film->set_name (string (_name->GetValue().mb_str()));
529 }
530
531 void
532 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
533 {
534         if (!_film) {
535                 return;
536         }
537
538         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
539 }
540
541 void
542 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
543 {
544         if (!_film) {
545                 return;
546         }
547
548         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
549 }
550
551 void
552 FilmEditor::colour_lut_changed (wxCommandEvent &)
553 {
554         if (!_film) {
555                 return;
556         }
557         
558         _film->set_colour_lut (_colour_lut->GetSelection ());
559 }
560
561 void
562 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
563 {
564         if (!_film) {
565                 return;
566         }
567         
568         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
569 }
570
571 void
572 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
573 {
574         if (!_film) {
575                 return;
576         }
577
578         _film->set_dcp_frame_rate (
579                 boost::lexical_cast<int> (
580                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
581                         )
582                 );
583 }
584
585
586 /** Called when the metadata stored in the Film object has changed;
587  *  so that we can update the GUI.
588  *  @param p Property of the Film that has changed.
589  */
590 void
591 FilmEditor::film_changed (Film::Property p)
592 {
593         ensure_ui_thread ();
594         
595         if (!_film) {
596                 return;
597         }
598
599         stringstream s;
600                 
601         switch (p) {
602         case Film::NONE:
603                 break;
604         case Film::CONTENT:
605                 setup_content ();
606                 setup_formats ();
607                 setup_format ();
608                 setup_subtitle_control_sensitivity ();
609                 setup_streams ();
610                 setup_show_audio_sensitivity ();
611                 break;
612         case Film::TRUST_CONTENT_HEADERS:
613                 checked_set (_trust_content_headers, _film->trust_content_headers ());
614                 break;
615         case Film::FORMAT:
616                 setup_format ();
617                 break;
618         case Film::CROP:
619                 checked_set (_left_crop, _film->crop().left);
620                 checked_set (_right_crop, _film->crop().right);
621                 checked_set (_top_crop, _film->crop().top);
622                 checked_set (_bottom_crop, _film->crop().bottom);
623                 setup_scaling_description ();
624                 break;
625         case Film::FILTERS:
626         {
627                 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
628                 if (p.first.empty () && p.second.empty ()) {
629                         _filters->SetLabel (_("None"));
630                 } else {
631                         string const b = p.first + " " + p.second;
632                         _filters->SetLabel (std_to_wx (b));
633                 }
634                 _film_sizer->Layout ();
635                 break;
636         }
637         case Film::NAME:
638                 checked_set (_name, _film->name());
639                 setup_dcp_name ();
640                 break;
641         case Film::DCP_CONTENT_TYPE:
642                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
643                 setup_dcp_name ();
644                 break;
645         case Film::AB:
646                 checked_set (_ab, _film->ab ());
647                 break;
648         case Film::SCALER:
649                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
650                 break;
651         case Film::TRIM_START:
652                 checked_set (_trim_start, _film->trim_start());
653                 break;
654         case Film::TRIM_END:
655                 checked_set (_trim_end, _film->trim_end());
656                 break;
657         case Film::AUDIO_GAIN:
658                 checked_set (_audio_gain, _film->audio_gain ());
659                 break;
660         case Film::AUDIO_DELAY:
661                 checked_set (_audio_delay, _film->audio_delay ());
662                 break;
663         case Film::WITH_SUBTITLES:
664                 checked_set (_with_subtitles, _film->with_subtitles ());
665                 setup_subtitle_control_sensitivity ();
666                 setup_dcp_name ();
667                 break;
668         case Film::SUBTITLE_OFFSET:
669                 checked_set (_subtitle_offset, _film->subtitle_offset ());
670                 break;
671         case Film::SUBTITLE_SCALE:
672                 checked_set (_subtitle_scale, _film->subtitle_scale() * 100);
673                 break;
674         case Film::COLOUR_LUT:
675                 checked_set (_colour_lut, _film->colour_lut ());
676                 break;
677         case Film::J2K_BANDWIDTH:
678                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
679                 break;
680         case Film::USE_DCI_NAME:
681                 checked_set (_use_dci_name, _film->use_dci_name ());
682                 setup_dcp_name ();
683                 break;
684         case Film::DCI_METADATA:
685                 setup_dcp_name ();
686                 break;
687         case Film::DCP_FRAME_RATE:
688                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
689                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_frame_rate())) {
690                                 if (_dcp_frame_rate->GetSelection() != int(i)) {
691                                         _dcp_frame_rate->SetSelection (i);
692                                         break;
693                                 }
694                         }
695                 }
696
697                 if (_film->video_frame_rate()) {
698                         _best_dcp_frame_rate->Enable (best_dcp_frame_rate (_film->video_frame_rate ()) != _film->dcp_frame_rate ());
699                 } else {
700                         _best_dcp_frame_rate->Disable ();
701                 }
702                 setup_frame_rate_description ();
703                 break;
704         case Film::AUDIO_MAPPING:
705                 _audio_mapping->set_mapping (_film->audio_mapping ());
706                 break;
707         }
708 }
709
710 void
711 FilmEditor::film_content_changed (weak_ptr<Content> content, int property)
712 {
713         if (!_film) {
714                 /* We call this method ourselves (as well as using it as a signal handler)
715                    so _film can be 0.
716                 */
717                 return;
718         }
719                 
720         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
721                 setup_subtitle_control_sensitivity ();
722                 setup_streams ();
723         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
724                 setup_streams ();
725                 setup_show_audio_sensitivity ();
726         } else if (property == VideoContentProperty::VIDEO_LENGTH) {
727                 setup_length ();
728                 boost::shared_ptr<Content> c = content.lock ();
729                 if (c && c == selected_content()) {
730                         setup_content_information ();
731                 }
732         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
733                 if (_film->ffmpeg_audio_stream()) {
734                         checked_set (_ffmpeg_audio_stream, boost::lexical_cast<string> (_film->ffmpeg_audio_stream()->id));
735                 }
736                 setup_dcp_name ();
737                 setup_audio_details ();
738                 setup_show_audio_sensitivity ();
739         } else if (property == FFmpegContentProperty::SUBTITLE_STREAM) {
740                 if (_film->ffmpeg_subtitle_stream()) {
741                         checked_set (_ffmpeg_subtitle_stream, boost::lexical_cast<string> (_film->ffmpeg_subtitle_stream()->id));
742                 }
743         }
744 }
745
746 void
747 FilmEditor::setup_format ()
748 {
749         int n = 0;
750         vector<Format const *>::iterator i = _formats.begin ();
751         while (i != _formats.end() && *i != _film->format ()) {
752                 ++i;
753                 ++n;
754         }
755         
756         if (i == _formats.end()) {
757                 checked_set (_format, -1);
758         } else {
759                 checked_set (_format, n);
760         }
761         
762         setup_dcp_name ();
763         setup_scaling_description ();
764 }       
765
766 void
767 FilmEditor::setup_length ()
768 {
769         stringstream s;
770         if (_film->video_frame_rate() > 0 && _film->video_length()) {
771                 s << _film->video_length() << " "
772                   << wx_to_std (_("frames")) << "; " << seconds_to_hms (_film->video_length() / _film->video_frame_rate());
773         } else if (_film->video_length()) {
774                 s << _film->video_length() << " "
775                   << wx_to_std (_("frames"));
776         } 
777         _length->SetLabel (std_to_wx (s.str ()));
778         if (_film->video_length()) {
779                 _trim_start->SetRange (0, _film->video_length());
780                 _trim_end->SetRange (0, _film->video_length());
781         }
782 }       
783
784 void
785 FilmEditor::setup_frame_rate_description ()
786 {
787         wxString d;
788         int lines = 0;
789         
790         if (_film->video_frame_rate()) {
791                 d << std_to_wx (FrameRateConversion (_film->video_frame_rate(), _film->dcp_frame_rate()).description);
792                 ++lines;
793 #ifdef HAVE_SWRESAMPLE
794                 if (_film->audio_frame_rate() && _film->audio_frame_rate() != _film->target_audio_sample_rate ()) {
795                         d << wxString::Format (
796                                 _("Audio will be resampled from %dHz to %dHz\n"),
797                                 _film->audio_frame_rate(),
798                                 _film->target_audio_sample_rate()
799                                 );
800                         ++lines;
801                 }
802 #endif          
803         }
804
805         for (int i = lines; i < 2; ++i) {
806                 d << wxT ("\n ");
807         }
808
809         _frame_rate_description->SetLabel (d);
810 }
811
812 /** Called when the format widget has been changed */
813 void
814 FilmEditor::format_changed (wxCommandEvent &)
815 {
816         if (!_film) {
817                 return;
818         }
819
820         int const n = _format->GetSelection ();
821         if (n >= 0) {
822                 assert (n < int (_formats.size()));
823                 _film->set_format (_formats[n]);
824         }
825 }
826
827 /** Called when the DCP content type widget has been changed */
828 void
829 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
830 {
831         if (!_film) {
832                 return;
833         }
834
835         int const n = _dcp_content_type->GetSelection ();
836         if (n != wxNOT_FOUND) {
837                 _film->set_dcp_content_type (DCPContentType::from_index (n));
838         }
839 }
840
841 /** Sets the Film that we are editing */
842 void
843 FilmEditor::set_film (shared_ptr<Film> f)
844 {
845         set_things_sensitive (_film != 0);
846
847         if (_film == f) {
848                 return;
849         }
850         
851         _film = f;
852
853         if (_film) {
854                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
855                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
856         }
857
858         if (_film) {
859                 FileChanged (_film->directory ());
860         } else {
861                 FileChanged ("");
862         }
863
864         if (_audio_dialog) {
865                 _audio_dialog->set_film (_film);
866         }
867         
868         film_changed (Film::NAME);
869         film_changed (Film::USE_DCI_NAME);
870         film_changed (Film::CONTENT);
871         film_changed (Film::TRUST_CONTENT_HEADERS);
872         film_changed (Film::DCP_CONTENT_TYPE);
873         film_changed (Film::FORMAT);
874         film_changed (Film::CROP);
875         film_changed (Film::FILTERS);
876         film_changed (Film::SCALER);
877         film_changed (Film::TRIM_START);
878         film_changed (Film::TRIM_END);
879         film_changed (Film::AB);
880         film_changed (Film::AUDIO_GAIN);
881         film_changed (Film::AUDIO_DELAY);
882         film_changed (Film::WITH_SUBTITLES);
883         film_changed (Film::SUBTITLE_OFFSET);
884         film_changed (Film::SUBTITLE_SCALE);
885         film_changed (Film::COLOUR_LUT);
886         film_changed (Film::J2K_BANDWIDTH);
887         film_changed (Film::DCI_METADATA);
888         film_changed (Film::DCP_FRAME_RATE);
889         film_changed (Film::AUDIO_MAPPING);
890
891         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::SUBTITLE_STREAMS);
892         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::SUBTITLE_STREAM);
893         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::AUDIO_STREAMS);
894         film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::AUDIO_STREAM);
895 }
896
897 /** Updates the sensitivity of lots of widgets to a given value.
898  *  @param s true to make sensitive, false to make insensitive.
899  */
900 void
901 FilmEditor::set_things_sensitive (bool s)
902 {
903         _generally_sensitive = s;
904         
905         _name->Enable (s);
906         _use_dci_name->Enable (s);
907         _edit_dci_button->Enable (s);
908         _format->Enable (s);
909         _content->Enable (s);
910         _trust_content_headers->Enable (s);
911         _content->Enable (s);
912         _left_crop->Enable (s);
913         _right_crop->Enable (s);
914         _top_crop->Enable (s);
915         _bottom_crop->Enable (s);
916         _filters_button->Enable (s);
917         _scaler->Enable (s);
918         _ffmpeg_audio_stream->Enable (s);
919         _dcp_content_type->Enable (s);
920         _dcp_frame_rate->Enable (s);
921         _trim_start->Enable (s);
922         _trim_end->Enable (s);
923         _ab->Enable (s);
924         _colour_lut->Enable (s);
925         _j2k_bandwidth->Enable (s);
926         _audio_gain->Enable (s);
927         _audio_gain_calculate_button->Enable (s);
928         _show_audio->Enable (s);
929         _audio_delay->Enable (s);
930
931         setup_subtitle_control_sensitivity ();
932         setup_show_audio_sensitivity ();
933         setup_content_button_sensitivity ();
934 }
935
936 /** Called when the `Edit filters' button has been clicked */
937 void
938 FilmEditor::edit_filters_clicked (wxCommandEvent &)
939 {
940         FilterDialog* d = new FilterDialog (this, _film->filters());
941         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
942         d->ShowModal ();
943         d->Destroy ();
944 }
945
946 /** Called when the scaler widget has been changed */
947 void
948 FilmEditor::scaler_changed (wxCommandEvent &)
949 {
950         if (!_film) {
951                 return;
952         }
953
954         int const n = _scaler->GetSelection ();
955         if (n >= 0) {
956                 _film->set_scaler (Scaler::from_index (n));
957         }
958 }
959
960 void
961 FilmEditor::audio_gain_changed (wxCommandEvent &)
962 {
963         if (!_film) {
964                 return;
965         }
966
967         _film->set_audio_gain (_audio_gain->GetValue ());
968 }
969
970 void
971 FilmEditor::audio_delay_changed (wxCommandEvent &)
972 {
973         if (!_film) {
974                 return;
975         }
976
977         _film->set_audio_delay (_audio_delay->GetValue ());
978 }
979
980 void
981 FilmEditor::setup_notebook_size ()
982 {
983         _notebook->InvalidateBestSize ();
984         
985         _film_sizer->Layout ();
986         _film_sizer->SetSizeHints (_film_panel);
987         _video_sizer->Layout ();
988         _video_sizer->SetSizeHints (_video_panel);
989         _audio_sizer->Layout ();
990         _audio_sizer->SetSizeHints (_audio_panel);
991         _subtitle_sizer->Layout ();
992         _subtitle_sizer->SetSizeHints (_subtitle_panel);
993
994         _notebook->Fit ();
995         Fit ();
996 }
997
998 void
999 FilmEditor::trim_start_changed (wxCommandEvent &)
1000 {
1001         if (!_film) {
1002                 return;
1003         }
1004
1005         _film->set_trim_start (_trim_start->GetValue ());
1006 }
1007
1008 void
1009 FilmEditor::trim_end_changed (wxCommandEvent &)
1010 {
1011         if (!_film) {
1012                 return;
1013         }
1014
1015         _film->set_trim_end (_trim_end->GetValue ());
1016 }
1017
1018 void
1019 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
1020 {
1021         GainCalculatorDialog* d = new GainCalculatorDialog (this);
1022         d->ShowModal ();
1023
1024         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
1025                 d->Destroy ();
1026                 return;
1027         }
1028         
1029         _audio_gain->SetValue (
1030                 Config::instance()->sound_processor()->db_for_fader_change (
1031                         d->wanted_fader (),
1032                         d->actual_fader ()
1033                         )
1034                 );
1035
1036         /* This appears to be necessary, as the change is not signalled,
1037            I think.
1038         */
1039         wxCommandEvent dummy;
1040         audio_gain_changed (dummy);
1041         
1042         d->Destroy ();
1043 }
1044
1045 void
1046 FilmEditor::setup_formats ()
1047 {
1048         _formats = Format::all ();
1049
1050         _format->Clear ();
1051         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
1052                 _format->Append (std_to_wx ((*i)->name ()));
1053         }
1054
1055         _film_sizer->Layout ();
1056 }
1057
1058 void
1059 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
1060 {
1061         if (!_film) {
1062                 return;
1063         }
1064
1065         _film->set_with_subtitles (_with_subtitles->GetValue ());
1066 }
1067
1068 void
1069 FilmEditor::setup_subtitle_control_sensitivity ()
1070 {
1071         bool h = false;
1072         if (_generally_sensitive && _film) {
1073                 h = !_film->ffmpeg_subtitle_streams().empty();
1074         }
1075         
1076         _with_subtitles->Enable (h);
1077
1078         bool j = false;
1079         if (_film) {
1080                 j = _film->with_subtitles ();
1081         }
1082         
1083         _ffmpeg_subtitle_stream->Enable (j);
1084         _subtitle_offset->Enable (j);
1085         _subtitle_scale->Enable (j);
1086 }
1087
1088 void
1089 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
1090 {
1091         if (!_film) {
1092                 return;
1093         }
1094
1095         _film->set_use_dci_name (_use_dci_name->GetValue ());
1096 }
1097
1098 void
1099 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
1100 {
1101         if (!_film) {
1102                 return;
1103         }
1104
1105         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
1106         d->ShowModal ();
1107         _film->set_dci_metadata (d->dci_metadata ());
1108         d->Destroy ();
1109 }
1110
1111 void
1112 FilmEditor::setup_streams ()
1113 {
1114         if (!_film) {
1115                 return;
1116         }
1117         
1118         _ffmpeg_audio_stream->Clear ();
1119         vector<FFmpegAudioStream> a = _film->ffmpeg_audio_streams ();
1120         for (vector<FFmpegAudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
1121                 _ffmpeg_audio_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (boost::lexical_cast<string> (i->id))));
1122         }
1123         
1124         if (_film->ffmpeg_audio_stream()) {
1125                 checked_set (_ffmpeg_audio_stream, boost::lexical_cast<string> (_film->ffmpeg_audio_stream()->id));
1126         }
1127
1128         _ffmpeg_subtitle_stream->Clear ();
1129         vector<FFmpegSubtitleStream> s = _film->ffmpeg_subtitle_streams ();
1130         for (vector<FFmpegSubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
1131                 _ffmpeg_subtitle_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (boost::lexical_cast<string> (i->id))));
1132         }
1133         
1134         if (_film->ffmpeg_subtitle_stream()) {
1135                 checked_set (_ffmpeg_subtitle_stream, boost::lexical_cast<string> (_film->ffmpeg_subtitle_stream()->id));
1136         } else {
1137                 _ffmpeg_subtitle_stream->SetSelection (wxNOT_FOUND);
1138         }
1139 }
1140
1141 void
1142 FilmEditor::ffmpeg_audio_stream_changed (wxCommandEvent &)
1143 {
1144         if (!_film) {
1145                 return;
1146         }
1147
1148         vector<FFmpegAudioStream> a = _film->ffmpeg_audio_streams ();
1149         vector<FFmpegAudioStream>::iterator i = a.begin ();
1150         string const s = string_client_data (_ffmpeg_audio_stream->GetClientObject (_ffmpeg_audio_stream->GetSelection ()));
1151         while (i != a.end() && lexical_cast<string> (i->id) != s) {
1152                 ++i;
1153         }
1154
1155         if (i != a.end ()) {
1156                 _film->set_ffmpeg_audio_stream (*i);
1157         }
1158 }
1159
1160 void
1161 FilmEditor::ffmpeg_subtitle_stream_changed (wxCommandEvent &)
1162 {
1163         if (!_film) {
1164                 return;
1165         }
1166
1167         vector<FFmpegSubtitleStream> a = _film->ffmpeg_subtitle_streams ();
1168         vector<FFmpegSubtitleStream>::iterator i = a.begin ();
1169         string const s = string_client_data (_ffmpeg_subtitle_stream->GetClientObject (_ffmpeg_subtitle_stream->GetSelection ()));
1170         while (i != a.end() && lexical_cast<string> (i->id) != s) {
1171                 ++i;
1172         }
1173
1174         if (i != a.end ()) {
1175                 _film->set_ffmpeg_subtitle_stream (*i);
1176         }
1177 }
1178
1179 void
1180 FilmEditor::setup_audio_details ()
1181 {
1182         if (!_film->ffmpeg_audio_stream()) {
1183                 _audio->SetLabel (wxT (""));
1184         } else {
1185                 wxString s;
1186                 if (_film->audio_channels() == 1) {
1187                         s << _("1 channel");
1188                 } else {
1189                         s << _film->audio_channels() << wxT (" ") << _("channels");
1190                 }
1191                 s << wxT (", ") << _film->audio_frame_rate() << _("Hz");
1192                 _audio->SetLabel (s);
1193         }
1194
1195         setup_notebook_size ();
1196 }
1197
1198 void
1199 FilmEditor::active_jobs_changed (bool a)
1200 {
1201         set_things_sensitive (!a);
1202 }
1203
1204 void
1205 FilmEditor::setup_dcp_name ()
1206 {
1207         string s = _film->dcp_name (true);
1208         if (s.length() > 28) {
1209                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
1210                 _dcp_name->SetToolTip (std_to_wx (s));
1211         } else {
1212                 _dcp_name->SetLabel (std_to_wx (s));
1213         }
1214 }
1215
1216 void
1217 FilmEditor::show_audio_clicked (wxCommandEvent &)
1218 {
1219         if (_audio_dialog) {
1220                 _audio_dialog->Destroy ();
1221                 _audio_dialog = 0;
1222         }
1223         
1224         _audio_dialog = new AudioDialog (this);
1225         _audio_dialog->Show ();
1226         _audio_dialog->set_film (_film);
1227 }
1228
1229 void
1230 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
1231 {
1232         if (!_film) {
1233                 return;
1234         }
1235         
1236         _film->set_dcp_frame_rate (best_dcp_frame_rate (_film->video_frame_rate ()));
1237 }
1238
1239 void
1240 FilmEditor::setup_show_audio_sensitivity ()
1241 {
1242         _show_audio->Enable (_film && _film->has_audio ());
1243 }
1244
1245 void
1246 FilmEditor::setup_content ()
1247 {
1248         string selected_summary;
1249         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1250         if (s != -1) {
1251                 selected_summary = wx_to_std (_content->GetItemText (s));
1252         }
1253         
1254         _content->DeleteAllItems ();
1255
1256         ContentList content = _film->content ();
1257         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
1258                 int const t = _content->GetItemCount ();
1259                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
1260                 if ((*i)->summary() == selected_summary) {
1261                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1262                 }
1263         }
1264
1265         if (selected_summary.empty () && !content.empty ()) {
1266                 /* Select the first item of content if non was selected before */
1267                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1268         }
1269 }
1270
1271 void
1272 FilmEditor::content_add_clicked (wxCommandEvent &)
1273 {
1274         wxFileDialog* d = new wxFileDialog (this);
1275         int const r = d->ShowModal ();
1276         d->Destroy ();
1277
1278         if (r != wxID_OK) {
1279                 return;
1280         }
1281
1282         boost::filesystem::path p (wx_to_std (d->GetPath()));
1283
1284         if (ImageMagickContent::valid_file (p)) {
1285                 _film->add_content (shared_ptr<ImageMagickContent> (new ImageMagickContent (p)));
1286         } else if (SndfileContent::valid_file (p)) {
1287                 _film->add_content (shared_ptr<SndfileContent> (new SndfileContent (p)));
1288         } else {
1289                 _film->add_content (shared_ptr<FFmpegContent> (new FFmpegContent (p)));
1290         }
1291         
1292 }
1293
1294 void
1295 FilmEditor::content_remove_clicked (wxCommandEvent &)
1296 {
1297         shared_ptr<Content> c = selected_content ();
1298         if (c) {
1299                 _film->remove_content (c);
1300         }
1301 }
1302
1303 void
1304 FilmEditor::content_activated (wxListEvent& ev)
1305 {
1306         ContentList c = _film->content ();
1307         assert (ev.GetIndex() >= 0 && size_t (ev.GetIndex()) < c.size ());
1308
1309         edit_content (c[ev.GetIndex()]);
1310 }
1311
1312 void
1313 FilmEditor::content_edit_clicked (wxCommandEvent &)
1314 {
1315         shared_ptr<Content> c = selected_content ();
1316         if (!c) {
1317                 return;
1318         }
1319
1320         edit_content (c);
1321 }
1322
1323 void
1324 FilmEditor::edit_content (shared_ptr<Content> c)
1325 {
1326         shared_ptr<ImageMagickContent> im = dynamic_pointer_cast<ImageMagickContent> (c);
1327         if (im) {
1328                 ImageMagickContentDialog* d = new ImageMagickContentDialog (this, im);
1329                 d->ShowModal ();
1330                 d->Destroy ();
1331
1332                 im->set_video_length (d->video_length() * 24);
1333         }
1334 }
1335
1336 void
1337 FilmEditor::content_earlier_clicked (wxCommandEvent &)
1338 {
1339         shared_ptr<Content> c = selected_content ();
1340         if (c) {
1341                 _film->move_content_earlier (c);
1342         }
1343 }
1344
1345 void
1346 FilmEditor::content_later_clicked (wxCommandEvent &)
1347 {
1348         shared_ptr<Content> c = selected_content ();
1349         if (c) {
1350                 _film->move_content_later (c);
1351         }
1352 }
1353
1354 void
1355 FilmEditor::content_selection_changed (wxListEvent &)
1356 {
1357         setup_content_button_sensitivity ();
1358         setup_content_information ();
1359 }
1360
1361 void
1362 FilmEditor::setup_content_information ()
1363 {
1364         shared_ptr<Content> c = selected_content ();
1365         if (!c) {
1366                 _content_information->SetValue (wxT (""));
1367                 return;
1368         }
1369
1370         _content_information->SetValue (std_to_wx (c->information ()));
1371 }
1372
1373 void
1374 FilmEditor::setup_content_button_sensitivity ()
1375 {
1376         _content_add->Enable (_generally_sensitive);
1377
1378         shared_ptr<Content> selection = selected_content ();
1379
1380         _content_edit->Enable (selection && _generally_sensitive && dynamic_pointer_cast<ImageMagickContent> (selection));
1381         _content_remove->Enable (selection && _generally_sensitive);
1382         _content_earlier->Enable (selection && _generally_sensitive);
1383         _content_later->Enable (selection && _generally_sensitive);
1384 }
1385
1386 shared_ptr<Content>
1387 FilmEditor::selected_content ()
1388 {
1389         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1390         if (s == -1) {
1391                 return shared_ptr<Content> ();
1392         }
1393
1394         ContentList c = _film->content ();
1395         if (s < 0 || size_t (s) >= c.size ()) {
1396                 return shared_ptr<Content> ();
1397         }
1398         
1399         return c[s];
1400 }
1401
1402 void
1403 FilmEditor::setup_scaling_description ()
1404 {
1405         wxString d;
1406
1407         int lines = 0;
1408
1409         if (_film->video_size().width && _film->video_size().height) {
1410                 d << wxString::Format (
1411                         _("Original video is %dx%d (%.2f:1)\n"),
1412                         _film->video_size().width, _film->video_size().height,
1413                         float (_film->video_size().width) / _film->video_size().height
1414                         );
1415                 ++lines;
1416         }
1417
1418         Crop const crop = _film->crop ();
1419         if (crop.left || crop.right || crop.top || crop.bottom) {
1420                 libdcp::Size const cropped = _film->cropped_size (_film->video_size ());
1421                 d << wxString::Format (
1422                         _("Cropped to %dx%d (%.2f:1)\n"),
1423                         cropped.width, cropped.height,
1424                         float (cropped.width) / cropped.height
1425                         );
1426                 ++lines;
1427         }
1428
1429         Format const * format = _film->format ();
1430         if (format) {
1431                 int const padding = format->dcp_padding (_film);
1432                 libdcp::Size scaled = format->dcp_size ();
1433                 scaled.width -= padding * 2;
1434                 d << wxString::Format (
1435                         _("Scaled to %dx%d (%.2f:1)\n"),
1436                         scaled.width, scaled.height,
1437                         float (scaled.width) / scaled.height
1438                         );
1439                 ++lines;
1440
1441                 if (padding) {
1442                         d << wxString::Format (
1443                                 _("Padded with black to %dx%d (%.2f:1)\n"),
1444                                 format->dcp_size().width, format->dcp_size().height,
1445                                 float (format->dcp_size().width) / format->dcp_size().height
1446                                 );
1447                         ++lines;
1448                 }
1449         }
1450
1451         for (int i = lines; i < 4; ++i) {
1452                 d << wxT ("\n ");
1453         }
1454
1455         _scaling_description->SetLabel (d);
1456 }