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