dd952e22a9817dd81c5adbe24bc655301d441381
[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 <boost/thread.hpp>
29 #include <boost/filesystem.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include "lib/format.h"
32 #include "lib/film.h"
33 #include "lib/transcode_job.h"
34 #include "lib/exceptions.h"
35 #include "lib/ab_transcode_job.h"
36 #include "lib/job_manager.h"
37 #include "lib/filter.h"
38 #include "lib/config.h"
39 #include "lib/ffmpeg_decoder.h"
40 #include "lib/log.h"
41 #include "filter_dialog.h"
42 #include "wx_util.h"
43 #include "film_editor.h"
44 #include "gain_calculator_dialog.h"
45 #include "sound_processor.h"
46 #include "dci_metadata_dialog.h"
47 #include "scaler.h"
48 #include "audio_dialog.h"
49
50 using std::string;
51 using std::cout;
52 using std::stringstream;
53 using std::pair;
54 using std::fixed;
55 using std::setprecision;
56 using std::list;
57 using std::vector;
58 using boost::shared_ptr;
59 using boost::dynamic_pointer_cast;
60
61 /** @param f Film to edit */
62 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
63         : wxPanel (parent)
64         , _film (f)
65         , _generally_sensitive (true)
66         , _audio_dialog (0)
67 {
68         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
69         SetSizer (s);
70         _notebook = new wxNotebook (this, wxID_ANY);
71         s->Add (_notebook, 1);
72
73         make_film_panel ();
74         _notebook->AddPage (_film_panel, _("Film"), true);
75         make_video_panel ();
76         _notebook->AddPage (_video_panel, _("Video"), false);
77         make_audio_panel ();
78         _notebook->AddPage (_audio_panel, _("Audio"), false);
79         make_subtitle_panel ();
80         _notebook->AddPage (_subtitle_panel, _("Subtitles"), false);
81
82         set_film (_film);
83         connect_to_widgets ();
84
85         JobManager::instance()->ActiveJobsChanged.connect (
86                 bind (&FilmEditor::active_jobs_changed, this, _1)
87                 );
88         
89         setup_visibility ();
90         setup_formats ();
91 }
92
93 void
94 FilmEditor::make_film_panel ()
95 {
96         _film_panel = new wxPanel (_notebook);
97         _film_sizer = new wxBoxSizer (wxVERTICAL);
98         _film_panel->SetSizer (_film_sizer);
99
100         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
101         _film_sizer->Add (grid, 0, wxALL, 8);
102
103         int r = 0;
104         
105         add_label_to_grid_bag_sizer (grid, _film_panel, _("Name"), wxGBPosition (r, 0));
106         _name = new wxTextCtrl (_film_panel, wxID_ANY);
107         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
108         ++r;
109         
110         add_label_to_grid_bag_sizer (grid, _film_panel, _("DCP Name"), wxGBPosition (r, 0));
111         _dcp_name = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
112         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
113         ++r;
114
115         _use_dci_name = new wxCheckBox (_film_panel, wxID_ANY, _("Use DCI name"));
116         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
117         _edit_dci_button = new wxButton (_film_panel, wxID_ANY, _("Details..."));
118         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
119         ++r;
120
121         add_label_to_grid_bag_sizer (grid, _film_panel, _("Content"), wxGBPosition (r, 0));
122         _content = new wxFilePickerCtrl (_film_panel, wxID_ANY, wxT (""), _("Select Content File"), wxT("*.*"));
123         grid->Add (_content, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
124         ++r;
125
126         _trust_content_header = new wxCheckBox (_film_panel, wxID_ANY, _("Trust content's header"));
127         video_control (_trust_content_header);
128         grid->Add (_trust_content_header, wxGBPosition (r, 0), wxGBSpan(1, 2));
129         ++r;
130
131         add_label_to_grid_bag_sizer (grid, _film_panel, _("Content Type"), wxGBPosition (r, 0));
132         _dcp_content_type = new wxChoice (_film_panel, wxID_ANY);
133         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
134         ++r;
135
136         video_control (add_label_to_grid_bag_sizer (grid, _film_panel, _("Original Frame Rate"), wxGBPosition (r, 0)));
137         _source_frame_rate = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
138         grid->Add (video_control (_source_frame_rate), wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
139         ++r;
140
141         {
142                 add_label_to_grid_bag_sizer (grid, _film_panel, _("DCP Frame Rate"), wxGBPosition (r, 0));
143                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
144                 _dcp_frame_rate = new wxChoice (_film_panel, wxID_ANY);
145                 s->Add (_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
146                 _best_dcp_frame_rate = new wxButton (_film_panel, wxID_ANY, _("Use best"));
147                 s->Add (_best_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 6);
148                 grid->Add (s, wxGBPosition (r, 1));
149         }
150         ++r;
151
152         _frame_rate_description = new wxStaticText (_film_panel, wxID_ANY, wxT ("\n \n "), wxDefaultPosition, wxDefaultSize);
153         grid->Add (video_control (_frame_rate_description), wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
154         wxFont font = _frame_rate_description->GetFont();
155         font.SetStyle(wxFONTSTYLE_ITALIC);
156         font.SetPointSize(font.GetPointSize() - 1);
157         _frame_rate_description->SetFont(font);
158         ++r;
159         
160         video_control (add_label_to_grid_bag_sizer (grid, _film_panel, _("Length"), wxGBPosition (r, 0)));
161         _length = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
162         grid->Add (video_control (_length), wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
163         ++r;
164
165
166         {
167                 video_control (add_label_to_grid_bag_sizer (grid, _film_panel, _("Trim frames"), wxGBPosition (r, 0)));
168                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
169                 video_control (add_label_to_sizer (s, _film_panel, _("Start")));
170                 _trim_start = new wxSpinCtrl (_film_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
171                 s->Add (video_control (_trim_start));
172                 video_control (add_label_to_sizer (s, _film_panel, _("End")));
173                 _trim_end = new wxSpinCtrl (_film_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
174                 s->Add (video_control (_trim_end));
175
176                 grid->Add (s, wxGBPosition (r, 1));
177         }
178         ++r;
179
180         video_control (add_label_to_grid_bag_sizer (grid, _film_panel, _("Trim method"), wxGBPosition (r, 0)));
181         _trim_type = new wxChoice (_film_panel, wxID_ANY);
182         grid->Add (video_control (_trim_type), wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
183         ++r;
184
185         _dcp_ab = new wxCheckBox (_film_panel, wxID_ANY, _("A/B"));
186         video_control (_dcp_ab);
187         grid->Add (_dcp_ab, wxGBPosition (r, 0));
188         ++r;
189
190         /* STILL-only stuff */
191         {
192                 still_control (add_label_to_grid_bag_sizer (grid, _film_panel, _("Duration"), wxGBPosition (r, 0)));
193                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
194                 _still_duration = new wxSpinCtrl (_film_panel);
195                 still_control (_still_duration);
196                 s->Add (_still_duration, 1, wxEXPAND);
197                 /// TRANSLATORS: `s' here is an abbreviation for seconds, the unit of time
198                 still_control (add_label_to_sizer (s, _film_panel, _("s")));
199                 grid->Add (s, wxGBPosition (r, 1));
200         }
201         ++r;
202
203         vector<DCPContentType const *> const ct = DCPContentType::all ();
204         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
205                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
206         }
207
208         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
209         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
210                 _dcp_frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
211         }
212
213         _trim_type->Append (_("encode all frames and play the subset"));
214         _trim_type->Append (_("encode only the subset"));
215 }
216
217 void
218 FilmEditor::connect_to_widgets ()
219 {
220         _name->Connect                 (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED,         wxCommandEventHandler (FilmEditor::name_changed), 0, this);
221         _use_dci_name->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
222         _edit_dci_button->Connect      (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
223         _format->Connect               (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::format_changed), 0, this);
224         _content->Connect              (wxID_ANY, wxEVT_COMMAND_FILEPICKER_CHANGED,   wxCommandEventHandler (FilmEditor::content_changed), 0, this);
225         _trust_content_header->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::trust_content_header_changed), 0, this);
226         _left_crop->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
227         _right_crop->Connect           (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
228         _top_crop->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
229         _bottom_crop->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
230         _filters_button->Connect       (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
231         _scaler->Connect               (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
232         _dcp_content_type->Connect     (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
233         _dcp_frame_rate->Connect       (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_frame_rate_changed), 0, this);
234         _best_dcp_frame_rate->Connect  (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::best_dcp_frame_rate_clicked), 0, this);
235         _dcp_ab->Connect               (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::dcp_ab_toggled), 0, this);
236         _still_duration->Connect       (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::still_duration_changed), 0, this);
237         _trim_start->Connect           (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::trim_start_changed), 0, this);
238         _trim_end->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::trim_end_changed), 0, this);
239         _trim_type->Connect            (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::trim_type_changed), 0, this);
240         _with_subtitles->Connect       (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::with_subtitles_toggled), 0, this);
241         _subtitle_offset->Connect      (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_offset_changed), 0, this);
242         _subtitle_scale->Connect       (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_scale_changed), 0, this);
243         _colour_lut->Connect           (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::colour_lut_changed), 0, this);
244         _j2k_bandwidth->Connect        (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
245         _subtitle_stream->Connect      (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::subtitle_stream_changed), 0, this);
246         _audio_stream->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::audio_stream_changed), 0, this);
247         _audio_gain->Connect           (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
248         _audio_gain_calculate_button->Connect (
249                 wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::audio_gain_calculate_button_clicked), 0, this
250                 );
251         _show_audio->Connect           (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::show_audio_clicked), 0, this);
252         _audio_delay->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
253         _use_content_audio->Connect    (wxID_ANY, wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler (FilmEditor::use_audio_changed), 0, this);
254         _use_external_audio->Connect   (wxID_ANY, wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler (FilmEditor::use_audio_changed), 0, this);
255         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
256                 _external_audio[i]->Connect (
257                         wxID_ANY, wxEVT_COMMAND_FILEPICKER_CHANGED, wxCommandEventHandler (FilmEditor::external_audio_changed), 0, this
258                         );
259         }
260 }
261
262 void
263 FilmEditor::make_video_panel ()
264 {
265         _video_panel = new wxPanel (_notebook);
266         _video_sizer = new wxBoxSizer (wxVERTICAL);
267         _video_panel->SetSizer (_video_sizer);
268         
269         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
270         _video_sizer->Add (grid, 0, wxALL, 8);
271
272         int r = 0;
273         add_label_to_grid_bag_sizer (grid, _video_panel, _("Format"), wxGBPosition (r, 0));
274         _format = new wxChoice (_video_panel, wxID_ANY);
275         grid->Add (_format, wxGBPosition (r, 1));
276         ++r;
277
278         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), wxGBPosition (r, 0));
279         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
280         grid->Add (_left_crop, wxGBPosition (r, 1));
281         ++r;
282
283         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), wxGBPosition (r, 0));
284         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
285         grid->Add (_right_crop, wxGBPosition (r, 1));
286         ++r;
287         
288         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), wxGBPosition (r, 0));
289         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
290         grid->Add (_top_crop, wxGBPosition (r, 1));
291         ++r;
292         
293         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), wxGBPosition (r, 0));
294         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
295         grid->Add (_bottom_crop, wxGBPosition (r, 1));
296         ++r;
297
298         _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
299         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
300         wxFont font = _scaling_description->GetFont();
301         font.SetStyle(wxFONTSTYLE_ITALIC);
302         font.SetPointSize(font.GetPointSize() - 1);
303         _scaling_description->SetFont(font);
304         ++r;
305
306         /* VIDEO-only stuff */
307         {
308                 video_control (add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), wxGBPosition (r, 0)));
309                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
310                 _filters = new wxStaticText (_video_panel, wxID_ANY, _("None"));
311                 video_control (_filters);
312                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
313                 _filters_button = new wxButton (_video_panel, wxID_ANY, _("Edit..."));
314                 video_control (_filters_button);
315                 s->Add (_filters_button, 0);
316                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
317         }
318         ++r;
319
320         video_control (add_label_to_grid_bag_sizer (grid, _video_panel, _("Scaler"), wxGBPosition (r, 0)));
321         _scaler = new wxChoice (_video_panel, wxID_ANY);
322         grid->Add (video_control (_scaler), wxGBPosition (r, 1));
323         ++r;
324
325         vector<Scaler const *> const sc = Scaler::all ();
326         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
327                 _scaler->Append (std_to_wx ((*i)->name()));
328         }
329
330         add_label_to_grid_bag_sizer (grid, _video_panel, _("Colour look-up table"), wxGBPosition (r, 0));
331         _colour_lut = new wxChoice (_video_panel, wxID_ANY);
332         for (int i = 0; i < 2; ++i) {
333                 _colour_lut->Append (std_to_wx (colour_lut_index_to_name (i)));
334         }
335         _colour_lut->SetSelection (0);
336         grid->Add (_colour_lut, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
337         ++r;
338
339         {
340                 add_label_to_grid_bag_sizer (grid, _video_panel, _("JPEG2000 bandwidth"), wxGBPosition (r, 0));
341                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
342                 _j2k_bandwidth = new wxSpinCtrl (_video_panel, wxID_ANY);
343                 s->Add (_j2k_bandwidth, 1);
344                 add_label_to_sizer (s, _video_panel, _("MBps"));
345                 grid->Add (s, wxGBPosition (r, 1));
346         }
347         ++r;
348
349         _left_crop->SetRange (0, 1024);
350         _top_crop->SetRange (0, 1024);
351         _right_crop->SetRange (0, 1024);
352         _bottom_crop->SetRange (0, 1024);
353         _still_duration->SetRange (1, 60 * 60);
354         _trim_start->SetRange (0, 24 * 60 * 60);
355         _trim_end->SetRange (0, 24 * 60 * 60);
356         _j2k_bandwidth->SetRange (50, 250);
357 }
358
359 void
360 FilmEditor::make_audio_panel ()
361 {
362         _audio_panel = new wxPanel (_notebook);
363         _audio_sizer = new wxBoxSizer (wxVERTICAL);
364         _audio_panel->SetSizer (_audio_sizer);
365         
366         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
367         _audio_sizer->Add (grid, 0, wxALL, 8);
368
369         _show_audio = new wxButton (_audio_panel, wxID_ANY, _("Show Audio..."));
370         grid->Add (_show_audio, 1);
371         grid->AddSpacer (0);
372
373         {
374                 video_control (add_label_to_sizer (grid, _audio_panel, _("Audio Gain")));
375                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
376                 _audio_gain = new wxSpinCtrl (_audio_panel);
377                 s->Add (video_control (_audio_gain), 1);
378                 video_control (add_label_to_sizer (s, _audio_panel, _("dB")));
379                 _audio_gain_calculate_button = new wxButton (_audio_panel, wxID_ANY, _("Calculate..."));
380                 video_control (_audio_gain_calculate_button);
381                 s->Add (_audio_gain_calculate_button, 1, wxEXPAND);
382                 grid->Add (s);
383         }
384
385         {
386                 video_control (add_label_to_sizer (grid, _audio_panel, _("Audio Delay")));
387                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
388                 _audio_delay = new wxSpinCtrl (_audio_panel);
389                 s->Add (video_control (_audio_delay), 1);
390                 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
391                 video_control (add_label_to_sizer (s, _audio_panel, _("ms")));
392                 grid->Add (s);
393         }
394
395         {
396                 _use_content_audio = new wxRadioButton (_audio_panel, wxID_ANY, _("Use content's audio"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
397                 grid->Add (video_control (_use_content_audio));
398                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
399                 _audio_stream = new wxChoice (_audio_panel, wxID_ANY);
400                 s->Add (video_control (_audio_stream), 1);
401                 _audio = new wxStaticText (_audio_panel, wxID_ANY, wxT (""));
402                 s->Add (video_control (_audio), 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
403                 grid->Add (s, 1, wxEXPAND);
404         }
405
406         _use_external_audio = new wxRadioButton (_audio_panel, wxID_ANY, _("Use external audio"));
407         grid->Add (_use_external_audio);
408         grid->AddSpacer (0);
409
410         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
411                 add_label_to_sizer (grid, _audio_panel, std_to_wx (audio_channel_name (i)));
412                 _external_audio[i] = new wxFilePickerCtrl (_audio_panel, wxID_ANY, wxT (""), _("Select Audio File"), wxT ("*.wav"));
413                 grid->Add (_external_audio[i], 1, wxEXPAND);
414         }
415
416         _audio_gain->SetRange (-60, 60);
417         _audio_delay->SetRange (-1000, 1000);
418 }
419
420 void
421 FilmEditor::make_subtitle_panel ()
422 {
423         _subtitle_panel = new wxPanel (_notebook);
424         _subtitle_sizer = new wxBoxSizer (wxVERTICAL);
425         _subtitle_panel->SetSizer (_subtitle_sizer);
426         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
427         _subtitle_sizer->Add (grid, 0, wxALL, 8);
428
429         _with_subtitles = new wxCheckBox (_subtitle_panel, wxID_ANY, _("With Subtitles"));
430         video_control (_with_subtitles);
431         grid->Add (_with_subtitles, 1);
432         
433         _subtitle_stream = new wxChoice (_subtitle_panel, wxID_ANY);
434         grid->Add (video_control (_subtitle_stream));
435
436         {
437                 video_control (add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset")));
438                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
439                 _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
440                 s->Add (_subtitle_offset);
441                 video_control (add_label_to_sizer (s, _subtitle_panel, _("pixels")));
442                 grid->Add (s);
443         }
444
445         {
446                 video_control (add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale")));
447                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
448                 _subtitle_scale = new wxSpinCtrl (_subtitle_panel);
449                 s->Add (video_control (_subtitle_scale));
450                 video_control (add_label_to_sizer (s, _subtitle_panel, _("%")));
451                 grid->Add (s);
452         }
453
454         _subtitle_offset->SetRange (-1024, 1024);
455         _subtitle_scale->SetRange (1, 1000);
456 }
457
458 /** Called when the left crop widget has been changed */
459 void
460 FilmEditor::left_crop_changed (wxCommandEvent &)
461 {
462         if (!_film) {
463                 return;
464         }
465
466         _film->set_left_crop (_left_crop->GetValue ());
467 }
468
469 /** Called when the right crop widget has been changed */
470 void
471 FilmEditor::right_crop_changed (wxCommandEvent &)
472 {
473         if (!_film) {
474                 return;
475         }
476
477         _film->set_right_crop (_right_crop->GetValue ());
478 }
479
480 /** Called when the top crop widget has been changed */
481 void
482 FilmEditor::top_crop_changed (wxCommandEvent &)
483 {
484         if (!_film) {
485                 return;
486         }
487
488         _film->set_top_crop (_top_crop->GetValue ());
489 }
490
491 /** Called when the bottom crop value has been changed */
492 void
493 FilmEditor::bottom_crop_changed (wxCommandEvent &)
494 {
495         if (!_film) {
496                 return;
497         }
498
499         _film->set_bottom_crop (_bottom_crop->GetValue ());
500 }
501
502 /** Called when the content filename has been changed */
503 void
504 FilmEditor::content_changed (wxCommandEvent &)
505 {
506         if (!_film) {
507                 return;
508         }
509
510         try {
511                 _film->set_content (wx_to_std (_content->GetPath ()));
512         } catch (std::exception& e) {
513                 _content->SetPath (std_to_wx (_film->directory ()));
514                 error_dialog (this, wxString::Format (_("Could not set content: %s"), std_to_wx (e.what()).data()));
515         }
516 }
517
518 void
519 FilmEditor::trust_content_header_changed (wxCommandEvent &)
520 {
521         if (!_film) {
522                 return;
523         }
524
525         _film->set_trust_content_header (_trust_content_header->GetValue ());
526 }
527
528 /** Called when the DCP A/B switch has been toggled */
529 void
530 FilmEditor::dcp_ab_toggled (wxCommandEvent &)
531 {
532         if (!_film) {
533                 return;
534         }
535         
536         _film->set_dcp_ab (_dcp_ab->GetValue ());
537 }
538
539 /** Called when the name widget has been changed */
540 void
541 FilmEditor::name_changed (wxCommandEvent &)
542 {
543         if (!_film) {
544                 return;
545         }
546
547         _film->set_name (string (_name->GetValue().mb_str()));
548 }
549
550 void
551 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
552 {
553         if (!_film) {
554                 return;
555         }
556
557         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
558 }
559
560 void
561 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
562 {
563         if (!_film) {
564                 return;
565         }
566
567         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
568 }
569
570 void
571 FilmEditor::colour_lut_changed (wxCommandEvent &)
572 {
573         if (!_film) {
574                 return;
575         }
576         
577         _film->set_colour_lut (_colour_lut->GetSelection ());
578 }
579
580 void
581 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
582 {
583         if (!_film) {
584                 return;
585         }
586         
587         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
588 }
589
590 void
591 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
592 {
593         if (!_film) {
594                 return;
595         }
596
597         _film->set_dcp_frame_rate (
598                 boost::lexical_cast<int> (
599                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
600                         )
601                 );
602 }
603
604
605 /** Called when the metadata stored in the Film object has changed;
606  *  so that we can update the GUI.
607  *  @param p Property of the Film that has changed.
608  */
609 void
610 FilmEditor::film_changed (Film::Property p)
611 {
612         ensure_ui_thread ();
613         
614         if (!_film) {
615                 return;
616         }
617
618         stringstream s;
619                 
620         switch (p) {
621         case Film::NONE:
622                 break;
623         case Film::CONTENT:
624                 checked_set (_content, _film->content ());
625                 setup_visibility ();
626                 setup_formats ();
627                 setup_subtitle_control_sensitivity ();
628                 setup_streams ();
629                 setup_show_audio_sensitivity ();
630                 setup_frame_rate_description ();
631                 break;
632         case Film::TRUST_CONTENT_HEADER:
633                 checked_set (_trust_content_header, _film->trust_content_header ());
634                 break;
635         case Film::SUBTITLE_STREAMS:
636                 setup_subtitle_control_sensitivity ();
637                 setup_streams ();
638                 break;
639         case Film::CONTENT_AUDIO_STREAMS:
640                 setup_streams ();
641                 setup_show_audio_sensitivity ();
642                 setup_frame_rate_description ();
643                 break;
644         case Film::FORMAT:
645         {
646                 int n = 0;
647                 vector<Format const *>::iterator i = _formats.begin ();
648                 while (i != _formats.end() && *i != _film->format ()) {
649                         ++i;
650                         ++n;
651                 }
652                 if (i == _formats.end()) {
653                         checked_set (_format, -1);
654                 } else {
655                         checked_set (_format, n);
656                 }
657                 setup_dcp_name ();
658                 setup_scaling_description ();
659                 break;
660         }
661         case Film::CROP:
662                 checked_set (_left_crop, _film->crop().left);
663                 checked_set (_right_crop, _film->crop().right);
664                 checked_set (_top_crop, _film->crop().top);
665                 checked_set (_bottom_crop, _film->crop().bottom);
666                 setup_scaling_description ();
667                 break;
668         case Film::FILTERS:
669         {
670                 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
671                 if (p.first.empty () && p.second.empty ()) {
672                         _filters->SetLabel (_("None"));
673                 } else {
674                         string const b = p.first + " " + p.second;
675                         _filters->SetLabel (std_to_wx (b));
676                 }
677                 _film_sizer->Layout ();
678                 break;
679         }
680         case Film::NAME:
681                 checked_set (_name, _film->name());
682                 setup_dcp_name ();
683                 break;
684         case Film::SOURCE_FRAME_RATE:
685                 s << fixed << setprecision(2) << _film->source_frame_rate();
686                 _source_frame_rate->SetLabel (std_to_wx (s.str ()));
687                 setup_frame_rate_description ();
688                 break;
689         case Film::SIZE:
690                 setup_scaling_description ();
691                 break;
692         case Film::LENGTH:
693                 if (_film->source_frame_rate() > 0 && _film->length()) {
694                         s << _film->length().get() << " "
695                           << wx_to_std (_("frames")) << "; " << seconds_to_hms (_film->length().get() / _film->source_frame_rate());
696                 } else if (_film->length()) {
697                         s << _film->length().get() << " "
698                           << wx_to_std (_("frames"));
699                 } 
700                 _length->SetLabel (std_to_wx (s.str ()));
701                 if (_film->length()) {
702                         _trim_start->SetRange (0, _film->length().get());
703                         _trim_end->SetRange (0, _film->length().get());
704                 }
705                 break;
706         case Film::DCP_CONTENT_TYPE:
707                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
708                 setup_dcp_name ();
709                 break;
710         case Film::DCP_AB:
711                 checked_set (_dcp_ab, _film->dcp_ab ());
712                 break;
713         case Film::SCALER:
714                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
715                 break;
716         case Film::TRIM_START:
717                 checked_set (_trim_start, _film->trim_start());
718                 break;
719         case Film::TRIM_END:
720                 checked_set (_trim_end, _film->trim_end());
721                 break;
722         case Film::TRIM_TYPE:
723                 checked_set (_trim_type, _film->trim_type() == Film::CPL ? 0 : 1);
724                 break;
725         case Film::AUDIO_GAIN:
726                 checked_set (_audio_gain, _film->audio_gain ());
727                 break;
728         case Film::AUDIO_DELAY:
729                 checked_set (_audio_delay, _film->audio_delay ());
730                 break;
731         case Film::STILL_DURATION:
732                 checked_set (_still_duration, _film->still_duration ());
733                 break;
734         case Film::WITH_SUBTITLES:
735                 checked_set (_with_subtitles, _film->with_subtitles ());
736                 setup_subtitle_control_sensitivity ();
737                 setup_dcp_name ();
738                 break;
739         case Film::SUBTITLE_OFFSET:
740                 checked_set (_subtitle_offset, _film->subtitle_offset ());
741                 break;
742         case Film::SUBTITLE_SCALE:
743                 checked_set (_subtitle_scale, _film->subtitle_scale() * 100);
744                 break;
745         case Film::COLOUR_LUT:
746                 checked_set (_colour_lut, _film->colour_lut ());
747                 break;
748         case Film::J2K_BANDWIDTH:
749                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
750                 break;
751         case Film::USE_DCI_NAME:
752                 checked_set (_use_dci_name, _film->use_dci_name ());
753                 setup_dcp_name ();
754                 break;
755         case Film::DCI_METADATA:
756                 setup_dcp_name ();
757                 break;
758         case Film::CONTENT_AUDIO_STREAM:
759                 if (_film->content_audio_stream()) {
760                         checked_set (_audio_stream, _film->content_audio_stream()->to_string());
761                 }
762                 setup_dcp_name ();
763                 setup_audio_details ();
764                 setup_audio_control_sensitivity ();
765                 setup_show_audio_sensitivity ();
766                 setup_frame_rate_description ();
767                 break;
768         case Film::USE_CONTENT_AUDIO:
769                 _film->log()->log (String::compose ("Film::USE_CONTENT_AUDIO changed; setting GUI using %1", _film->use_content_audio ()));
770                 checked_set (_use_content_audio, _film->use_content_audio());
771                 checked_set (_use_external_audio, !_film->use_content_audio());
772                 setup_dcp_name ();
773                 setup_audio_details ();
774                 setup_audio_control_sensitivity ();
775                 setup_show_audio_sensitivity ();
776                 setup_frame_rate_description ();
777                 break;
778         case Film::SUBTITLE_STREAM:
779                 if (_film->subtitle_stream()) {
780                         checked_set (_subtitle_stream, _film->subtitle_stream()->to_string());
781                 }
782                 break;
783         case Film::EXTERNAL_AUDIO:
784         {
785                 vector<string> a = _film->external_audio ();
786                 for (size_t i = 0; i < a.size() && i < MAX_AUDIO_CHANNELS; ++i) {
787                         checked_set (_external_audio[i], a[i]);
788                 }
789                 setup_audio_details ();
790                 setup_show_audio_sensitivity ();
791                 setup_frame_rate_description ();
792                 break;
793         }
794         case Film::DCP_FRAME_RATE:
795                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
796                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_frame_rate())) {
797                                 if (_dcp_frame_rate->GetSelection() != int(i)) {
798                                         _dcp_frame_rate->SetSelection (i);
799                                         break;
800                                 }
801                         }
802                 }
803
804                 if (_film->source_frame_rate()) {
805                         _best_dcp_frame_rate->Enable (best_dcp_frame_rate (_film->source_frame_rate ()) != _film->dcp_frame_rate ());
806                 } else {
807                         _best_dcp_frame_rate->Disable ();
808                 }
809
810                 setup_frame_rate_description ();
811         }
812 }
813
814 void
815 FilmEditor::setup_frame_rate_description ()
816 {
817         wxString d;
818         int lines = 0;
819         
820         if (_film->source_frame_rate()) {
821                 d << std_to_wx (FrameRateConversion (_film->source_frame_rate(), _film->dcp_frame_rate()).description);
822                 ++lines;
823 #ifdef HAVE_SWRESAMPLE
824                 if (_film->audio_stream() && _film->audio_stream()->sample_rate() != _film->target_audio_sample_rate ()) {
825                         d << wxString::Format (
826                                 _("Audio will be resampled from %dHz to %dHz\n"),
827                                 _film->audio_stream()->sample_rate(),
828                                 _film->target_audio_sample_rate()
829                                 );
830                         ++lines;
831                 }
832 #endif          
833         }
834
835         for (int i = lines; i < 2; ++i) {
836                 d << wxT ("\n ");
837         }
838
839         _frame_rate_description->SetLabel (d);
840 }
841
842 /** Called when the format widget has been changed */
843 void
844 FilmEditor::format_changed (wxCommandEvent &)
845 {
846         if (!_film) {
847                 return;
848         }
849
850         int const n = _format->GetSelection ();
851         if (n >= 0) {
852                 assert (n < int (_formats.size()));
853                 _film->set_format (_formats[n]);
854         }
855 }
856
857 /** Called when the DCP content type widget has been changed */
858 void
859 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
860 {
861         if (!_film) {
862                 return;
863         }
864
865         int const n = _dcp_content_type->GetSelection ();
866         if (n != wxNOT_FOUND) {
867                 _film->set_dcp_content_type (DCPContentType::from_index (n));
868         }
869 }
870
871 /** Sets the Film that we are editing */
872 void
873 FilmEditor::set_film (shared_ptr<Film> f)
874 {
875         _film = f;
876
877         set_things_sensitive (_film != 0);
878
879         if (_film) {
880                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
881         }
882
883         if (_film) {
884                 FileChanged (_film->directory ());
885         } else {
886                 FileChanged ("");
887         }
888
889         if (_audio_dialog) {
890                 _audio_dialog->set_film (_film);
891         }
892         
893         film_changed (Film::NAME);
894         film_changed (Film::USE_DCI_NAME);
895         film_changed (Film::CONTENT);
896         film_changed (Film::TRUST_CONTENT_HEADER);
897         film_changed (Film::DCP_CONTENT_TYPE);
898         film_changed (Film::FORMAT);
899         film_changed (Film::CROP);
900         film_changed (Film::FILTERS);
901         film_changed (Film::SCALER);
902         film_changed (Film::TRIM_START);
903         film_changed (Film::TRIM_END);
904         film_changed (Film::TRIM_TYPE);
905         film_changed (Film::DCP_AB);
906         film_changed (Film::CONTENT_AUDIO_STREAM);
907         film_changed (Film::EXTERNAL_AUDIO);
908         film_changed (Film::USE_CONTENT_AUDIO);
909         film_changed (Film::AUDIO_GAIN);
910         film_changed (Film::AUDIO_DELAY);
911         film_changed (Film::STILL_DURATION);
912         film_changed (Film::WITH_SUBTITLES);
913         film_changed (Film::SUBTITLE_OFFSET);
914         film_changed (Film::SUBTITLE_SCALE);
915         film_changed (Film::COLOUR_LUT);
916         film_changed (Film::J2K_BANDWIDTH);
917         film_changed (Film::DCI_METADATA);
918         film_changed (Film::SIZE);
919         film_changed (Film::LENGTH);
920         film_changed (Film::CONTENT_AUDIO_STREAMS);
921         film_changed (Film::SUBTITLE_STREAMS);
922         film_changed (Film::SOURCE_FRAME_RATE);
923         film_changed (Film::DCP_FRAME_RATE);
924 }
925
926 /** Updates the sensitivity of lots of widgets to a given value.
927  *  @param s true to make sensitive, false to make insensitive.
928  */
929 void
930 FilmEditor::set_things_sensitive (bool s)
931 {
932         _generally_sensitive = s;
933         
934         _name->Enable (s);
935         _use_dci_name->Enable (s);
936         _edit_dci_button->Enable (s);
937         _format->Enable (s);
938         _content->Enable (s);
939         _trust_content_header->Enable (s);
940         _left_crop->Enable (s);
941         _right_crop->Enable (s);
942         _top_crop->Enable (s);
943         _bottom_crop->Enable (s);
944         _filters_button->Enable (s);
945         _scaler->Enable (s);
946         _audio_stream->Enable (s);
947         _dcp_content_type->Enable (s);
948         _dcp_frame_rate->Enable (s);
949         _trim_start->Enable (s);
950         _trim_end->Enable (s);
951         _trim_type->Enable (s);
952         _dcp_ab->Enable (s);
953         _colour_lut->Enable (s);
954         _j2k_bandwidth->Enable (s);
955         _audio_gain->Enable (s);
956         _audio_gain_calculate_button->Enable (s);
957         _show_audio->Enable (s);
958         _audio_delay->Enable (s);
959         _still_duration->Enable (s);
960
961         setup_subtitle_control_sensitivity ();
962         setup_audio_control_sensitivity ();
963         setup_show_audio_sensitivity ();
964 }
965
966 /** Called when the `Edit filters' button has been clicked */
967 void
968 FilmEditor::edit_filters_clicked (wxCommandEvent &)
969 {
970         FilterDialog* d = new FilterDialog (this, _film->filters());
971         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
972         d->ShowModal ();
973         d->Destroy ();
974 }
975
976 /** Called when the scaler widget has been changed */
977 void
978 FilmEditor::scaler_changed (wxCommandEvent &)
979 {
980         if (!_film) {
981                 return;
982         }
983
984         int const n = _scaler->GetSelection ();
985         if (n >= 0) {
986                 _film->set_scaler (Scaler::from_index (n));
987         }
988 }
989
990 void
991 FilmEditor::audio_gain_changed (wxCommandEvent &)
992 {
993         if (!_film) {
994                 return;
995         }
996
997         _film->set_audio_gain (_audio_gain->GetValue ());
998 }
999
1000 void
1001 FilmEditor::audio_delay_changed (wxCommandEvent &)
1002 {
1003         if (!_film) {
1004                 return;
1005         }
1006
1007         _film->set_audio_delay (_audio_delay->GetValue ());
1008 }
1009
1010 wxControl *
1011 FilmEditor::video_control (wxControl* c)
1012 {
1013         _video_controls.push_back (c);
1014         return c;
1015 }
1016
1017 wxControl *
1018 FilmEditor::still_control (wxControl* c)
1019 {
1020         _still_controls.push_back (c);
1021         return c;
1022 }
1023
1024 void
1025 FilmEditor::setup_visibility ()
1026 {
1027         ContentType c = VIDEO;
1028
1029         if (_film) {
1030                 c = _film->content_type ();
1031         }
1032
1033         for (list<wxControl*>::iterator i = _video_controls.begin(); i != _video_controls.end(); ++i) {
1034                 (*i)->Show (c == VIDEO);
1035         }
1036
1037         for (list<wxControl*>::iterator i = _still_controls.begin(); i != _still_controls.end(); ++i) {
1038                 (*i)->Show (c == STILL);
1039         }
1040
1041         setup_notebook_size ();
1042 }
1043
1044 void
1045 FilmEditor::setup_notebook_size ()
1046 {
1047         _notebook->InvalidateBestSize ();
1048         
1049         _film_sizer->Layout ();
1050         _film_sizer->SetSizeHints (_film_panel);
1051         _video_sizer->Layout ();
1052         _video_sizer->SetSizeHints (_video_panel);
1053         _audio_sizer->Layout ();
1054         _audio_sizer->SetSizeHints (_audio_panel);
1055         _subtitle_sizer->Layout ();
1056         _subtitle_sizer->SetSizeHints (_subtitle_panel);
1057
1058         _notebook->Fit ();
1059         Fit ();
1060 }
1061
1062 void
1063 FilmEditor::still_duration_changed (wxCommandEvent &)
1064 {
1065         if (!_film) {
1066                 return;
1067         }
1068
1069         _film->set_still_duration (_still_duration->GetValue ());
1070 }
1071
1072 void
1073 FilmEditor::trim_start_changed (wxCommandEvent &)
1074 {
1075         if (!_film) {
1076                 return;
1077         }
1078
1079         _film->set_trim_start (_trim_start->GetValue ());
1080 }
1081
1082 void
1083 FilmEditor::trim_end_changed (wxCommandEvent &)
1084 {
1085         if (!_film) {
1086                 return;
1087         }
1088
1089         _film->set_trim_end (_trim_end->GetValue ());
1090 }
1091
1092 void
1093 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
1094 {
1095         GainCalculatorDialog* d = new GainCalculatorDialog (this);
1096         d->ShowModal ();
1097
1098         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
1099                 d->Destroy ();
1100                 return;
1101         }
1102         
1103         _audio_gain->SetValue (
1104                 Config::instance()->sound_processor()->db_for_fader_change (
1105                         d->wanted_fader (),
1106                         d->actual_fader ()
1107                         )
1108                 );
1109
1110         /* This appears to be necessary, as the change is not signalled,
1111            I think.
1112         */
1113         wxCommandEvent dummy;
1114         audio_gain_changed (dummy);
1115         
1116         d->Destroy ();
1117 }
1118
1119 void
1120 FilmEditor::setup_formats ()
1121 {
1122         ContentType c = VIDEO;
1123
1124         if (_film) {
1125                 c = _film->content_type ();
1126         }
1127         
1128         _formats.clear ();
1129
1130         vector<Format const *> fmt = Format::all ();
1131         for (vector<Format const *>::iterator i = fmt.begin(); i != fmt.end(); ++i) {
1132                 if (c == VIDEO || (c == STILL && dynamic_cast<VariableFormat const *> (*i))) {
1133                         _formats.push_back (*i);
1134                 }
1135         }
1136
1137         _format->Clear ();
1138         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
1139                 _format->Append (std_to_wx ((*i)->name ()));
1140         }
1141
1142         _film_sizer->Layout ();
1143 }
1144
1145 void
1146 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
1147 {
1148         if (!_film) {
1149                 return;
1150         }
1151
1152         _film->set_with_subtitles (_with_subtitles->GetValue ());
1153 }
1154
1155 void
1156 FilmEditor::setup_subtitle_control_sensitivity ()
1157 {
1158         bool h = false;
1159         if (_generally_sensitive && _film) {
1160                 h = !_film->subtitle_streams().empty();
1161         }
1162         
1163         _with_subtitles->Enable (h);
1164
1165         bool j = false;
1166         if (_film) {
1167                 j = _film->with_subtitles ();
1168         }
1169         
1170         _subtitle_stream->Enable (j);
1171         _subtitle_offset->Enable (j);
1172         _subtitle_scale->Enable (j);
1173 }
1174
1175 void
1176 FilmEditor::setup_audio_control_sensitivity ()
1177 {
1178         _use_content_audio->Enable (_generally_sensitive && _film && !_film->content_audio_streams().empty());
1179         _use_external_audio->Enable (_generally_sensitive);
1180         
1181         bool const source = _generally_sensitive && _use_content_audio->GetValue();
1182         bool const external = _generally_sensitive && _use_external_audio->GetValue();
1183
1184         _audio_stream->Enable (source);
1185         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
1186                 _external_audio[i]->Enable (external);
1187         }
1188 }
1189
1190 void
1191 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
1192 {
1193         if (!_film) {
1194                 return;
1195         }
1196
1197         _film->set_use_dci_name (_use_dci_name->GetValue ());
1198 }
1199
1200 void
1201 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
1202 {
1203         if (!_film) {
1204                 return;
1205         }
1206
1207         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
1208         d->ShowModal ();
1209         _film->set_dci_metadata (d->dci_metadata ());
1210         d->Destroy ();
1211 }
1212
1213 void
1214 FilmEditor::setup_streams ()
1215 {
1216         _audio_stream->Clear ();
1217         vector<shared_ptr<AudioStream> > a = _film->content_audio_streams ();
1218         for (vector<shared_ptr<AudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
1219                 shared_ptr<FFmpegAudioStream> ffa = dynamic_pointer_cast<FFmpegAudioStream> (*i);
1220                 assert (ffa);
1221                 _audio_stream->Append (std_to_wx (ffa->name()), new wxStringClientData (std_to_wx (ffa->to_string ())));
1222         }
1223         
1224         if (_film->use_content_audio() && _film->audio_stream()) {
1225                 checked_set (_audio_stream, _film->audio_stream()->to_string());
1226         }
1227
1228         _subtitle_stream->Clear ();
1229         vector<shared_ptr<SubtitleStream> > s = _film->subtitle_streams ();
1230         for (vector<shared_ptr<SubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
1231                 _subtitle_stream->Append (std_to_wx ((*i)->name()), new wxStringClientData (std_to_wx ((*i)->to_string ())));
1232         }
1233         if (_film->subtitle_stream()) {
1234                 checked_set (_subtitle_stream, _film->subtitle_stream()->to_string());
1235         } else {
1236                 _subtitle_stream->SetSelection (wxNOT_FOUND);
1237         }
1238 }
1239
1240 void
1241 FilmEditor::audio_stream_changed (wxCommandEvent &)
1242 {
1243         if (!_film) {
1244                 return;
1245         }
1246
1247         _film->set_content_audio_stream (
1248                 audio_stream_factory (
1249                         string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ())),
1250                         Film::state_version
1251                         )
1252                 );
1253 }
1254
1255 void
1256 FilmEditor::subtitle_stream_changed (wxCommandEvent &)
1257 {
1258         if (!_film) {
1259                 return;
1260         }
1261
1262         _film->set_subtitle_stream (
1263                 subtitle_stream_factory (
1264                         string_client_data (_subtitle_stream->GetClientObject (_subtitle_stream->GetSelection ())),
1265                         Film::state_version
1266                         )
1267                 );
1268 }
1269
1270 void
1271 FilmEditor::setup_audio_details ()
1272 {
1273         if (!_film->content_audio_stream()) {
1274                 _audio->SetLabel (wxT (""));
1275         } else {
1276                 wxString s;
1277                 if (_film->audio_stream()->channels() == 1) {
1278                         s << _("1 channel");
1279                 } else {
1280                         s << _film->audio_stream()->channels () << wxT (" ") << _("channels");
1281                 }
1282                 s << wxT (", ") << _film->audio_stream()->sample_rate() << _("Hz");
1283                 _audio->SetLabel (s);
1284         }
1285
1286         setup_notebook_size ();
1287 }
1288
1289 void
1290 FilmEditor::active_jobs_changed (bool a)
1291 {
1292         set_things_sensitive (!a);
1293 }
1294
1295 void
1296 FilmEditor::use_audio_changed (wxCommandEvent &)
1297 {
1298         _film->set_use_content_audio (_use_content_audio->GetValue());
1299 }
1300
1301 void
1302 FilmEditor::external_audio_changed (wxCommandEvent &)
1303 {
1304         vector<string> a;
1305         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
1306                 a.push_back (wx_to_std (_external_audio[i]->GetPath()));
1307         }
1308
1309         _film->set_external_audio (a);
1310 }
1311
1312 void
1313 FilmEditor::setup_dcp_name ()
1314 {
1315         string s = _film->dcp_name (true);
1316         if (s.length() > 28) {
1317                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
1318                 _dcp_name->SetToolTip (std_to_wx (s));
1319         } else {
1320                 _dcp_name->SetLabel (std_to_wx (s));
1321         }
1322 }
1323
1324 void
1325 FilmEditor::show_audio_clicked (wxCommandEvent &)
1326 {
1327         if (_audio_dialog) {
1328                 _audio_dialog->Destroy ();
1329                 _audio_dialog = 0;
1330         }
1331         
1332         _audio_dialog = new AudioDialog (this);
1333         _audio_dialog->Show ();
1334         _audio_dialog->set_film (_film);
1335 }
1336
1337 void
1338 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
1339 {
1340         if (!_film) {
1341                 return;
1342         }
1343         
1344         _film->set_dcp_frame_rate (best_dcp_frame_rate (_film->source_frame_rate ()));
1345 }
1346
1347 void
1348 FilmEditor::setup_show_audio_sensitivity ()
1349 {
1350         _show_audio->Enable (_film && _film->has_audio ());
1351 }
1352
1353 void
1354 FilmEditor::setup_scaling_description ()
1355 {
1356         wxString d;
1357
1358         int lines = 0;
1359
1360         if (_film->size().width && _film->size().height) {
1361                 d << wxString::Format (
1362                         _("Original video is %dx%d (%.2f:1)\n"),
1363                         _film->size().width, _film->size().height,
1364                         float (_film->size().width) / _film->size().height
1365                         );
1366                 ++lines;
1367         }
1368
1369         Crop const crop = _film->crop ();
1370         if (crop.left || crop.right || crop.top || crop.bottom) {
1371                 libdcp::Size const cropped = _film->cropped_size (_film->size ());
1372                 d << wxString::Format (
1373                         _("Cropped to %dx%d (%.2f:1)\n"),
1374                         cropped.width, cropped.height,
1375                         float (cropped.width) / cropped.height
1376                         );
1377                 ++lines;
1378         }
1379
1380         Format const * format = _film->format ();
1381         if (format) {
1382                 int const padding = format->dcp_padding (_film);
1383                 libdcp::Size scaled = format->dcp_size ();
1384                 scaled.width -= padding * 2;
1385                 d << wxString::Format (
1386                         _("Scaled to %dx%d (%.2f:1)\n"),
1387                         scaled.width, scaled.height,
1388                         float (scaled.width) / scaled.height
1389                         );
1390                 ++lines;
1391
1392                 if (padding) {
1393                         d << wxString::Format (
1394                                 _("Padded with black to %dx%d (%.2f:1)\n"),
1395                                 format->dcp_size().width, format->dcp_size().height,
1396                                 float (format->dcp_size().width) / format->dcp_size().height
1397                                 );
1398                         ++lines;
1399                 }
1400         }
1401
1402         for (int i = lines; i < 4; ++i) {
1403                 d << wxT ("\n ");
1404         }
1405
1406         _scaling_description->SetLabel (d);
1407 }
1408
1409 void
1410 FilmEditor::trim_type_changed (wxCommandEvent &)
1411 {
1412         _film->set_trim_type (_trim_type->GetSelection () == 0 ? Film::CPL : Film::ENCODE);
1413 }