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