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