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