Remove unused image content dialog.
[dcpomatic.git] / src / wx / film_editor.cc
1 /*
2     Copyright (C) 2012-2013 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/film.h"
33 #include "lib/transcode_job.h"
34 #include "lib/exceptions.h"
35 #include "lib/job_manager.h"
36 #include "lib/filter.h"
37 #include "lib/ratio.h"
38 #include "lib/config.h"
39 #include "lib/still_image_content.h"
40 #include "lib/sndfile_content.h"
41 #include "lib/dcp_content_type.h"
42 #include "lib/sound_processor.h"
43 #include "lib/scaler.h"
44 #include "timecode.h"
45 #include "filter_dialog.h"
46 #include "wx_util.h"
47 #include "film_editor.h"
48 #include "gain_calculator_dialog.h"
49 #include "dci_metadata_dialog.h"
50 #include "audio_dialog.h"
51 #include "timeline_dialog.h"
52 #include "audio_mapping_view.h"
53
54 using std::string;
55 using std::cout;
56 using std::stringstream;
57 using std::pair;
58 using std::fixed;
59 using std::setprecision;
60 using std::list;
61 using std::vector;
62 using std::max;
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         , _menu (f, this)
72         , _generally_sensitive (true)
73         , _audio_dialog (0)
74         , _timeline_dialog (0)
75 {
76         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
77
78         _main_notebook = new wxNotebook (this, wxID_ANY);
79         s->Add (_main_notebook, 1);
80
81         make_content_panel ();
82         _main_notebook->AddPage (_content_panel, _("Content"), true);
83         make_dcp_panel ();
84         _main_notebook->AddPage (_dcp_panel, _("DCP"), false);
85         
86         setup_ratios ();
87
88         set_film (f);
89         connect_to_widgets ();
90
91         JobManager::instance()->ActiveJobsChanged.connect (
92                 bind (&FilmEditor::active_jobs_changed, this, _1)
93                 );
94         
95         SetSizerAndFit (s);
96 }
97
98 void
99 FilmEditor::make_dcp_panel ()
100 {
101         _dcp_panel = new wxPanel (_main_notebook);
102         _dcp_sizer = new wxBoxSizer (wxVERTICAL);
103         _dcp_panel->SetSizer (_dcp_sizer);
104
105         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
106         _dcp_sizer->Add (grid, 0, wxEXPAND | wxALL, 8);
107
108         int r = 0;
109         
110         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Name"), true, wxGBPosition (r, 0));
111         _name = new wxTextCtrl (_dcp_panel, wxID_ANY);
112         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxLEFT | wxRIGHT);
113         ++r;
114         
115         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Name"), true, wxGBPosition (r, 0));
116         _dcp_name = new wxStaticText (_dcp_panel, wxID_ANY, wxT (""));
117         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
118         ++r;
119
120         int flags = wxALIGN_CENTER_VERTICAL;
121 #ifdef __WXOSX__
122         flags |= wxALIGN_RIGHT;
123 #endif  
124
125         _use_dci_name = new wxCheckBox (_dcp_panel, wxID_ANY, _("Use DCI name"));
126         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, flags);
127         _edit_dci_button = new wxButton (_dcp_panel, wxID_ANY, _("Details..."));
128         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
129         ++r;
130
131         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Container"), true, wxGBPosition (r, 0));
132         _container = new wxChoice (_dcp_panel, wxID_ANY);
133         grid->Add (_container, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
134         ++r;
135
136         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Content Type"), true, wxGBPosition (r, 0));
137         _dcp_content_type = new wxChoice (_dcp_panel, wxID_ANY);
138         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
139         ++r;
140
141         {
142                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Frame Rate"), true, wxGBPosition (r, 0));
143                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
144                 _dcp_frame_rate = new wxChoice (_dcp_panel, wxID_ANY);
145                 s->Add (_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
146                 _best_dcp_frame_rate = new wxButton (_dcp_panel, wxID_ANY, _("Use best"));
147                 s->Add (_best_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND);
148                 grid->Add (s, wxGBPosition (r, 1));
149         }
150         ++r;
151
152         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Audio channels"), true, wxGBPosition (r, 0));
153         _dcp_audio_channels = new wxSpinCtrl (_dcp_panel, wxID_ANY);
154         grid->Add (_dcp_audio_channels, wxGBPosition (r, 1));
155         ++r;
156
157         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Resolution"), true, wxGBPosition (r, 0));
158         _dcp_resolution = new wxChoice (_dcp_panel, wxID_ANY);
159         grid->Add (_dcp_resolution, wxGBPosition (r, 1));
160         ++r;
161
162         {
163                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("JPEG2000 bandwidth"), true, wxGBPosition (r, 0));
164                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
165                 _j2k_bandwidth = new wxSpinCtrl (_dcp_panel, wxID_ANY);
166                 s->Add (_j2k_bandwidth, 1);
167                 add_label_to_sizer (s, _dcp_panel, _("MBps"), false);
168                 grid->Add (s, wxGBPosition (r, 1));
169         }
170         ++r;
171
172         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Scaler"), true, wxGBPosition (r, 0));
173         _scaler = new wxChoice (_dcp_panel, wxID_ANY);
174         grid->Add (_scaler, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
175         ++r;
176
177         vector<Scaler const *> const sc = Scaler::all ();
178         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
179                 _scaler->Append (std_to_wx ((*i)->name()));
180         }
181
182         vector<Ratio const *> const ratio = Ratio::all ();
183         for (vector<Ratio const *>::const_iterator i = ratio.begin(); i != ratio.end(); ++i) {
184                 _container->Append (std_to_wx ((*i)->nickname ()));
185         }
186
187         vector<DCPContentType const *> const ct = DCPContentType::all ();
188         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
189                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
190         }
191
192         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
193         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
194                 _dcp_frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
195         }
196
197         _dcp_audio_channels->SetRange (0, MAX_AUDIO_CHANNELS);
198         _j2k_bandwidth->SetRange (50, 250);
199
200         _dcp_resolution->Append (_("2K"));
201         _dcp_resolution->Append (_("4K"));
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         _container->Connect              (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::container_changed), 0, this);
211         _ratio->Connect                  (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::ratio_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_RIGHT_CLICK,wxListEventHandler    (FilmEditor::content_right_click), 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_timeline->Connect       (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::content_timeline_clicked), 0, this);
218         _left_crop->Connect              (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
219         _right_crop->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
220         _top_crop->Connect               (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
221         _bottom_crop->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
222         _filters_button->Connect         (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
223         _scaler->Connect                 (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
224         _dcp_content_type->Connect       (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
225         _dcp_frame_rate->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_frame_rate_changed), 0, this);
226         _best_dcp_frame_rate->Connect    (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::best_dcp_frame_rate_clicked), 0, this);
227         _dcp_audio_channels->Connect     (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::dcp_audio_channels_changed), 0, this);
228         _with_subtitles->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::with_subtitles_toggled), 0, this);
229         _subtitle_offset->Connect        (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_offset_changed), 0, this);
230         _subtitle_scale->Connect         (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::subtitle_scale_changed), 0, this);
231         _j2k_bandwidth->Connect          (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
232         _audio_gain->Connect             (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
233         _audio_gain_calculate_button->Connect (
234                 wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::audio_gain_calculate_button_clicked), 0, this
235                 );
236         _show_audio->Connect             (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,       wxCommandEventHandler (FilmEditor::show_audio_clicked), 0, this);
237         _audio_delay->Connect            (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED,     wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
238         _audio_stream->Connect           (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::audio_stream_changed), 0, this);
239         _subtitle_stream->Connect        (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::subtitle_stream_changed), 0, this);
240         _dcp_resolution->Connect         (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,      wxCommandEventHandler (FilmEditor::dcp_resolution_changed), 0, this);
241         _sequence_video->Connect         (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,     wxCommandEventHandler (FilmEditor::sequence_video_changed), 0, this);
242                 
243         _audio_mapping->Changed.connect  (boost::bind (&FilmEditor::audio_mapping_changed, this, _1));
244         _start->Changed.connect          (boost::bind (&FilmEditor::start_changed, this));
245         _length->Changed.connect         (boost::bind (&FilmEditor::length_changed, this));
246 }
247
248 void
249 FilmEditor::make_video_panel ()
250 {
251         _video_panel = new wxPanel (_content_notebook);
252         wxBoxSizer* video_sizer = new wxBoxSizer (wxVERTICAL);
253         _video_panel->SetSizer (video_sizer);
254         
255         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
256         video_sizer->Add (grid, 0, wxALL, 8);
257
258         int r = 0;
259         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), true, wxGBPosition (r, 0));
260         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
261         grid->Add (_left_crop, wxGBPosition (r, 1));
262         ++r;
263
264         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), true, wxGBPosition (r, 0));
265         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
266         grid->Add (_right_crop, wxGBPosition (r, 1));
267         ++r;
268         
269         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), true, wxGBPosition (r, 0));
270         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
271         grid->Add (_top_crop, wxGBPosition (r, 1));
272         ++r;
273         
274         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), true, wxGBPosition (r, 0));
275         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
276         grid->Add (_bottom_crop, wxGBPosition (r, 1));
277         ++r;
278
279         add_label_to_grid_bag_sizer (grid, _video_panel, _("Scale to"), true, wxGBPosition (r, 0));
280         _ratio = new wxChoice (_video_panel, wxID_ANY);
281         grid->Add (_ratio, wxGBPosition (r, 1));
282         ++r;
283
284         _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
285         grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
286         wxFont font = _scaling_description->GetFont();
287         font.SetStyle(wxFONTSTYLE_ITALIC);
288         font.SetPointSize(font.GetPointSize() - 1);
289         _scaling_description->SetFont(font);
290         ++r;
291
292         /* VIDEO-only stuff */
293         {
294                 add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), true, wxGBPosition (r, 0));
295                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
296                 _filters = new wxStaticText (_video_panel, wxID_ANY, _("None"));
297                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
298                 _filters_button = new wxButton (_video_panel, wxID_ANY, _("Edit..."));
299                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
300                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
301         }
302         ++r;
303
304         _left_crop->SetRange (0, 1024);
305         _top_crop->SetRange (0, 1024);
306         _right_crop->SetRange (0, 1024);
307         _bottom_crop->SetRange (0, 1024);
308 }
309
310 void
311 FilmEditor::make_content_panel ()
312 {
313         _content_panel = new wxPanel (_main_notebook);
314         _content_sizer = new wxBoxSizer (wxVERTICAL);
315         _content_panel->SetSizer (_content_sizer);
316
317         {
318                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
319                 
320                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
321                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
322
323                 _content->InsertColumn (0, wxT(""));
324                 _content->SetColumnWidth (0, 512);
325
326                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
327                 _content_add = new wxButton (_content_panel, wxID_ANY, _("Add..."));
328                 b->Add (_content_add, 1, wxEXPAND | wxLEFT | wxRIGHT);
329                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
330                 b->Add (_content_remove, 1, wxEXPAND | wxLEFT | wxRIGHT);
331                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
332                 b->Add (_content_timeline, 1, wxEXPAND | wxLEFT | wxRIGHT);
333
334                 s->Add (b, 0, wxALL, 4);
335
336                 _content_sizer->Add (s, 0.75, wxEXPAND | wxALL, 6);
337         }
338
339         _sequence_video = new wxCheckBox (_content_panel, wxID_ANY, _("Keep video in sequence"));
340         _content_sizer->Add (_sequence_video);
341
342         _content_notebook = new wxNotebook (_content_panel, wxID_ANY);
343         _content_sizer->Add (_content_notebook, 1, wxEXPAND | wxTOP, 6);
344
345         make_video_panel ();
346         _content_notebook->AddPage (_video_panel, _("Video"), false);
347         make_audio_panel ();
348         _content_notebook->AddPage (_audio_panel, _("Audio"), false);
349         make_subtitle_panel ();
350         _content_notebook->AddPage (_subtitle_panel, _("Subtitles"), false);
351         make_timing_panel ();
352         _content_notebook->AddPage (_timing_panel, _("Timing"), false);
353 }
354
355 void
356 FilmEditor::make_audio_panel ()
357 {
358         _audio_panel = new wxPanel (_content_notebook);
359         wxBoxSizer* audio_sizer = new wxBoxSizer (wxVERTICAL);
360         _audio_panel->SetSizer (audio_sizer);
361         
362         wxFlexGridSizer* grid = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
363         audio_sizer->Add (grid, 0, wxALL, 8);
364
365         _show_audio = new wxButton (_audio_panel, wxID_ANY, _("Show Audio..."));
366         grid->Add (_show_audio, 1);
367         grid->AddSpacer (0);
368         grid->AddSpacer (0);
369
370         add_label_to_sizer (grid, _audio_panel, _("Audio Gain"), true);
371         {
372                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
373                 _audio_gain = new wxSpinCtrl (_audio_panel);
374                 s->Add (_audio_gain, 1);
375                 add_label_to_sizer (s, _audio_panel, _("dB"), false);
376                 grid->Add (s, 1);
377         }
378         
379         _audio_gain_calculate_button = new wxButton (_audio_panel, wxID_ANY, _("Calculate..."));
380         grid->Add (_audio_gain_calculate_button);
381
382         add_label_to_sizer (grid, _audio_panel, _("Audio Delay"), false);
383         {
384                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
385                 _audio_delay = new wxSpinCtrl (_audio_panel);
386                 s->Add (_audio_delay, 1);
387                 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
388                 add_label_to_sizer (s, _audio_panel, _("ms"), false);
389                 grid->Add (s);
390         }
391
392         grid->AddSpacer (0);
393
394         add_label_to_sizer (grid, _audio_panel, _("Audio Stream"), true);
395         _audio_stream = new wxChoice (_audio_panel, wxID_ANY);
396         grid->Add (_audio_stream, 1, wxEXPAND);
397         _audio_description = new wxStaticText (_audio_panel, wxID_ANY, wxT (""));
398         grid->AddSpacer (0);
399         
400         grid->Add (_audio_description, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
401         grid->AddSpacer (0);
402         grid->AddSpacer (0);
403         
404         _audio_mapping = new AudioMappingView (_audio_panel);
405         audio_sizer->Add (_audio_mapping, 1, wxEXPAND | wxALL, 6);
406
407         _audio_gain->SetRange (-60, 60);
408         _audio_delay->SetRange (-1000, 1000);
409 }
410
411 void
412 FilmEditor::make_subtitle_panel ()
413 {
414         _subtitle_panel = new wxPanel (_content_notebook);
415         wxBoxSizer* subtitle_sizer = new wxBoxSizer (wxVERTICAL);
416         _subtitle_panel->SetSizer (subtitle_sizer);
417         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
418         subtitle_sizer->Add (grid, 0, wxALL, 8);
419
420         _with_subtitles = new wxCheckBox (_subtitle_panel, wxID_ANY, _("With Subtitles"));
421         grid->Add (_with_subtitles, 1);
422         grid->AddSpacer (0);
423         
424         {
425                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"), true);
426                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
427                 _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
428                 s->Add (_subtitle_offset);
429                 add_label_to_sizer (s, _subtitle_panel, _("%"), false);
430                 grid->Add (s);
431         }
432
433         {
434                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale"), true);
435                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
436                 _subtitle_scale = new wxSpinCtrl (_subtitle_panel);
437                 s->Add (_subtitle_scale);
438                 add_label_to_sizer (s, _subtitle_panel, _("%"), false);
439                 grid->Add (s);
440         }
441
442         add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Stream"), true);
443         _subtitle_stream = new wxChoice (_subtitle_panel, wxID_ANY);
444         grid->Add (_subtitle_stream, 1, wxEXPAND);
445         
446         _subtitle_offset->SetRange (-100, 100);
447         _subtitle_scale->SetRange (1, 1000);
448         _subtitle_scale->SetValue (100);
449 }
450
451 void
452 FilmEditor::make_timing_panel ()
453 {
454         _timing_panel = new wxPanel (_content_notebook);
455         wxBoxSizer* timing_sizer = new wxBoxSizer (wxVERTICAL);
456         _timing_panel->SetSizer (timing_sizer);
457         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
458         timing_sizer->Add (grid, 0, wxALL, 8);
459
460         add_label_to_sizer (grid, _timing_panel, _("Start time"), true);
461         _start = new Timecode (_timing_panel);
462         grid->Add (_start);
463         add_label_to_sizer (grid, _timing_panel, _("Length"), true);
464         _length = new Timecode (_timing_panel);
465         grid->Add (_length);
466 }
467
468
469 /** Called when the left crop widget has been changed */
470 void
471 FilmEditor::left_crop_changed (wxCommandEvent &)
472 {
473         shared_ptr<VideoContent> c = selected_video_content ();
474         if (!c) {
475                 return;
476         }
477
478         c->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         shared_ptr<VideoContent> c = selected_video_content ();
486         if (!c) {
487                 return;
488         }
489
490         c->set_right_crop (_right_crop->GetValue ());
491 }
492
493 /** Called when the top crop widget has been changed */
494 void
495 FilmEditor::top_crop_changed (wxCommandEvent &)
496 {
497         shared_ptr<VideoContent> c = selected_video_content ();
498         if (!c) {
499                 return;
500         }
501
502         c->set_top_crop (_top_crop->GetValue ());
503 }
504
505 /** Called when the bottom crop value has been changed */
506 void
507 FilmEditor::bottom_crop_changed (wxCommandEvent &)
508 {
509         shared_ptr<VideoContent> c = selected_video_content ();
510         if (!c) {
511                 return;
512         }
513
514         c->set_bottom_crop (_bottom_crop->GetValue ());
515 }
516
517 /** Called when the name widget has been changed */
518 void
519 FilmEditor::name_changed (wxCommandEvent &)
520 {
521         if (!_film) {
522                 return;
523         }
524
525         _film->set_name (string (_name->GetValue().mb_str()));
526 }
527
528 void
529 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
530 {
531         shared_ptr<SubtitleContent> c = selected_subtitle_content ();
532         if (!c) {
533                 return;
534         }
535
536         c->set_subtitle_offset (_subtitle_offset->GetValue() / 100.0);
537 }
538
539 void
540 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
541 {
542         shared_ptr<SubtitleContent> c = selected_subtitle_content ();
543         if (!c) {
544                 return;
545         }
546
547         c->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
548 }
549
550 void
551 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
552 {
553         if (!_film) {
554                 return;
555         }
556         
557         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
558 }
559
560 void
561 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
562 {
563         if (!_film) {
564                 return;
565         }
566
567         _film->set_dcp_video_frame_rate (
568                 boost::lexical_cast<int> (
569                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
570                         )
571                 );
572 }
573
574 void
575 FilmEditor::dcp_audio_channels_changed (wxCommandEvent &)
576 {
577         if (!_film) {
578                 return;
579         }
580
581         _film->set_dcp_audio_channels (_dcp_audio_channels->GetValue ());
582 }
583
584 void
585 FilmEditor::dcp_resolution_changed (wxCommandEvent &)
586 {
587         if (!_film) {
588                 return;
589         }
590
591         _film->set_resolution (_dcp_resolution->GetSelection() == 0 ? RESOLUTION_2K : RESOLUTION_4K);
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_subtitle_control_sensitivity ();
616                 setup_show_audio_sensitivity ();
617                 break;
618         case Film::CONTAINER:
619                 setup_container ();
620                 break;
621         case Film::NAME:
622                 checked_set (_name, _film->name());
623                 setup_dcp_name ();
624                 break;
625         case Film::DCP_CONTENT_TYPE:
626                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
627                 setup_dcp_name ();
628                 break;
629         case Film::SCALER:
630                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
631                 break;
632         case Film::WITH_SUBTITLES:
633                 checked_set (_with_subtitles, _film->with_subtitles ());
634                 setup_subtitle_control_sensitivity ();
635                 setup_dcp_name ();
636                 break;
637         case Film::RESOLUTION:
638                 checked_set (_dcp_resolution, _film->resolution() == RESOLUTION_2K ? 0 : 1);
639                 setup_dcp_name ();
640                 break;
641         case Film::J2K_BANDWIDTH:
642                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
643                 break;
644         case Film::USE_DCI_NAME:
645                 checked_set (_use_dci_name, _film->use_dci_name ());
646                 setup_dcp_name ();
647                 break;
648         case Film::DCI_METADATA:
649                 setup_dcp_name ();
650                 break;
651         case Film::DCP_VIDEO_FRAME_RATE:
652         {
653                 bool done = false;
654                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
655                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_video_frame_rate())) {
656                                 checked_set (_dcp_frame_rate, i);
657                                 done = true;
658                                 break;
659                         }
660                 }
661
662                 if (!done) {
663                         checked_set (_dcp_frame_rate, -1);
664                 }
665
666                 _best_dcp_frame_rate->Enable (_film->best_dcp_video_frame_rate () != _film->dcp_video_frame_rate ());
667                 break;
668         }
669         case Film::DCP_AUDIO_CHANNELS:
670                 _dcp_audio_channels->SetValue (_film->dcp_audio_channels ());
671                 _audio_mapping->set_channels (_film->dcp_audio_channels ());
672                 setup_dcp_name ();
673                 break;
674         case Film::SEQUENCE_VIDEO:
675                 checked_set (_sequence_video, _film->sequence_video ());
676                 break;
677         }
678 }
679
680 void
681 FilmEditor::film_content_changed (weak_ptr<Content> weak_content, int property)
682 {
683         ensure_ui_thread ();
684         
685         if (!_film) {
686                 /* We call this method ourselves (as well as using it as a signal handler)
687                    so _film can be 0.
688                 */
689                 return;
690         }
691
692         shared_ptr<Content> content = weak_content.lock ();
693         if (content != selected_content ()) {
694                 return;
695         }
696         
697         shared_ptr<VideoContent> video_content;
698         shared_ptr<AudioContent> audio_content;
699         shared_ptr<SubtitleContent> subtitle_content;
700         shared_ptr<FFmpegContent> ffmpeg_content;
701         if (content) {
702                 video_content = dynamic_pointer_cast<VideoContent> (content);
703                 audio_content = dynamic_pointer_cast<AudioContent> (content);
704                 subtitle_content = dynamic_pointer_cast<SubtitleContent> (content);
705                 ffmpeg_content = dynamic_pointer_cast<FFmpegContent> (content);
706         }
707
708         /* We can't use case {} here */
709         
710         if (property == ContentProperty::START) {
711                 if (content) {
712                         _start->set (content->start (), _film->dcp_video_frame_rate ());
713                 } else {
714                         _start->set (0, 24);
715                 }
716         } else if (property == ContentProperty::LENGTH) {
717                 if (content) {
718                         _length->set (content->length (), _film->dcp_video_frame_rate ());
719                 } else {
720                         _length->set (0, 24);
721                 }
722         } else if (property == VideoContentProperty::VIDEO_CROP) {
723                 checked_set (_left_crop,   video_content ? video_content->crop().left   : 0);
724                 checked_set (_right_crop,  video_content ? video_content->crop().right  : 0);
725                 checked_set (_top_crop,    video_content ? video_content->crop().top    : 0);
726                 checked_set (_bottom_crop, video_content ? video_content->crop().bottom : 0);
727                 setup_scaling_description ();
728         } else if (property == VideoContentProperty::VIDEO_RATIO) {
729                 if (video_content) {
730                         int n = 0;
731                         vector<Ratio const *> ratios = Ratio::all ();
732                         vector<Ratio const *>::iterator i = ratios.begin ();
733                         while (i != ratios.end() && *i != video_content->ratio()) {
734                                 ++i;
735                                 ++n;
736                         }
737
738                         if (i == ratios.end()) {
739                                 checked_set (_ratio, -1);
740                         } else {
741                                 checked_set (_ratio, n);
742                         }
743                 } else {
744                         checked_set (_ratio, -1);
745                 }
746                 setup_scaling_description ();
747         } else if (property == AudioContentProperty::AUDIO_GAIN) {
748                 checked_set (_audio_gain, audio_content ? audio_content->audio_gain() : 0);
749         } else if (property == AudioContentProperty::AUDIO_DELAY) {
750                 checked_set (_audio_delay, audio_content ? audio_content->audio_delay() : 0);
751         } else if (property == AudioContentProperty::AUDIO_MAPPING) {
752                 _audio_mapping->set (audio_content ? audio_content->audio_mapping () : AudioMapping ());
753         } else if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
754                 _subtitle_stream->Clear ();
755                 if (ffmpeg_content) {
756                         vector<shared_ptr<FFmpegSubtitleStream> > s = ffmpeg_content->subtitle_streams ();
757                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
758                                 _subtitle_stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
759                         }
760                         
761                         if (ffmpeg_content->subtitle_stream()) {
762                                 checked_set (_subtitle_stream, lexical_cast<string> (ffmpeg_content->subtitle_stream()->id));
763                         } else {
764                                 _subtitle_stream->SetSelection (wxNOT_FOUND);
765                         }
766                 }
767                 setup_subtitle_control_sensitivity ();
768         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
769                 _audio_stream->Clear ();
770                 if (ffmpeg_content) {
771                         vector<shared_ptr<FFmpegAudioStream> > a = ffmpeg_content->audio_streams ();
772                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
773                                 _audio_stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
774                         }
775                         
776                         if (ffmpeg_content->audio_stream()) {
777                                 checked_set (_audio_stream, lexical_cast<string> (ffmpeg_content->audio_stream()->id));
778                         }
779                 }
780                 setup_show_audio_sensitivity ();
781         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
782                 setup_dcp_name ();
783                 setup_show_audio_sensitivity ();
784         } else if (property == FFmpegContentProperty::FILTERS) {
785                 if (ffmpeg_content) {
786                         pair<string, string> p = Filter::ffmpeg_strings (ffmpeg_content->filters ());
787                         if (p.first.empty () && p.second.empty ()) {
788                                 _filters->SetLabel (_("None"));
789                         } else {
790                                 string const b = p.first + " " + p.second;
791                                 _filters->SetLabel (std_to_wx (b));
792                         }
793                         _dcp_sizer->Layout ();
794                 }
795         } else if (property == SubtitleContentProperty::SUBTITLE_OFFSET) {
796                 checked_set (_subtitle_offset, subtitle_content ? (subtitle_content->subtitle_offset() * 100) : 0);
797         } else if (property == SubtitleContentProperty::SUBTITLE_SCALE) {
798                 checked_set (_subtitle_scale, subtitle_content ? (subtitle_content->subtitle_scale() * 100) : 100);
799         }
800 }
801
802 void
803 FilmEditor::setup_container ()
804 {
805         int n = 0;
806         vector<Ratio const *> ratios = Ratio::all ();
807         vector<Ratio const *>::iterator i = ratios.begin ();
808         while (i != ratios.end() && *i != _film->container ()) {
809                 ++i;
810                 ++n;
811         }
812         
813         if (i == ratios.end()) {
814                 checked_set (_container, -1);
815         } else {
816                 checked_set (_container, n);
817         }
818         
819         setup_dcp_name ();
820         setup_scaling_description ();
821 }       
822
823 /** Called when the container widget has been changed */
824 void
825 FilmEditor::container_changed (wxCommandEvent &)
826 {
827         if (!_film) {
828                 return;
829         }
830
831         int const n = _container->GetSelection ();
832         if (n >= 0) {
833                 vector<Ratio const *> ratios = Ratio::all ();
834                 assert (n < int (ratios.size()));
835                 _film->set_container (ratios[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         film_changed (Film::NAME);
877         film_changed (Film::USE_DCI_NAME);
878         film_changed (Film::CONTENT);
879         film_changed (Film::DCP_CONTENT_TYPE);
880         film_changed (Film::CONTAINER);
881         film_changed (Film::RESOLUTION);
882         film_changed (Film::SCALER);
883         film_changed (Film::WITH_SUBTITLES);
884         film_changed (Film::J2K_BANDWIDTH);
885         film_changed (Film::DCI_METADATA);
886         film_changed (Film::DCP_VIDEO_FRAME_RATE);
887         film_changed (Film::DCP_AUDIO_CHANNELS);
888         film_changed (Film::SEQUENCE_VIDEO);
889
890         if (!_film->content().empty ()) {
891                 set_selection (_film->content().front ());
892         }
893
894         wxListEvent ev;
895         content_selection_changed (ev);
896 }
897
898 /** Updates the sensitivity of lots of widgets to a given value.
899  *  @param s true to make sensitive, false to make insensitive.
900  */
901 void
902 FilmEditor::set_things_sensitive (bool s)
903 {
904         _generally_sensitive = s;
905         
906         _name->Enable (s);
907         _use_dci_name->Enable (s);
908         _edit_dci_button->Enable (s);
909         _ratio->Enable (s);
910         _content->Enable (s);
911         _left_crop->Enable (s);
912         _right_crop->Enable (s);
913         _top_crop->Enable (s);
914         _bottom_crop->Enable (s);
915         _filters_button->Enable (s);
916         _scaler->Enable (s);
917         _dcp_content_type->Enable (s);
918         _dcp_frame_rate->Enable (s);
919         _dcp_audio_channels->Enable (s);
920         _j2k_bandwidth->Enable (s);
921         _audio_gain->Enable (s);
922         _audio_gain_calculate_button->Enable (s);
923         _show_audio->Enable (s);
924         _audio_delay->Enable (s);
925         _container->Enable (s);
926
927         setup_subtitle_control_sensitivity ();
928         setup_show_audio_sensitivity ();
929         setup_content_sensitivity ();
930         _best_dcp_frame_rate->Enable (s && _film && _film->best_dcp_video_frame_rate () != _film->dcp_video_frame_rate ());
931 }
932
933 /** Called when the `Edit filters' button has been clicked */
934 void
935 FilmEditor::edit_filters_clicked (wxCommandEvent &)
936 {
937         shared_ptr<Content> c = selected_content ();
938         if (!c) {
939                 return;
940         }
941
942         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
943         if (!fc) {
944                 return;
945         }
946         
947         FilterDialog* d = new FilterDialog (this, fc->filters());
948         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, fc, _1));
949         d->ShowModal ();
950         d->Destroy ();
951 }
952
953 /** Called when the scaler widget has been changed */
954 void
955 FilmEditor::scaler_changed (wxCommandEvent &)
956 {
957         if (!_film) {
958                 return;
959         }
960
961         int const n = _scaler->GetSelection ();
962         if (n >= 0) {
963                 _film->set_scaler (Scaler::from_index (n));
964         }
965 }
966
967 void
968 FilmEditor::audio_gain_changed (wxCommandEvent &)
969 {
970         shared_ptr<AudioContent> ac = selected_audio_content ();
971         if (!ac) {
972                 return;
973         }
974
975         ac->set_audio_gain (_audio_gain->GetValue ());
976 }
977
978 void
979 FilmEditor::audio_delay_changed (wxCommandEvent &)
980 {
981         shared_ptr<AudioContent> ac = selected_audio_content ();
982         if (!ac) {
983                 return;
984         }
985
986         ac->set_audio_delay (_audio_delay->GetValue ());
987 }
988
989 void
990 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
991 {
992         GainCalculatorDialog* d = new GainCalculatorDialog (this);
993         d->ShowModal ();
994
995         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
996                 d->Destroy ();
997                 return;
998         }
999         
1000         _audio_gain->SetValue (
1001                 Config::instance()->sound_processor()->db_for_fader_change (
1002                         d->wanted_fader (),
1003                         d->actual_fader ()
1004                         )
1005                 );
1006
1007         /* This appears to be necessary, as the change is not signalled,
1008            I think.
1009         */
1010         wxCommandEvent dummy;
1011         audio_gain_changed (dummy);
1012         
1013         d->Destroy ();
1014 }
1015
1016 void
1017 FilmEditor::setup_ratios ()
1018 {
1019         _ratios = Ratio::all ();
1020
1021         _ratio->Clear ();
1022         for (vector<Ratio const *>::iterator i = _ratios.begin(); i != _ratios.end(); ++i) {
1023                 _ratio->Append (std_to_wx ((*i)->nickname ()));
1024         }
1025
1026         _dcp_sizer->Layout ();
1027 }
1028
1029 void
1030 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
1031 {
1032         if (!_film) {
1033                 return;
1034         }
1035
1036         _film->set_with_subtitles (_with_subtitles->GetValue ());
1037 }
1038
1039 void
1040 FilmEditor::setup_subtitle_control_sensitivity ()
1041 {
1042         bool h = false;
1043         if (_generally_sensitive && _film) {
1044                 h = _film->has_subtitles ();
1045         }
1046         
1047         _with_subtitles->Enable (h);
1048
1049         bool j = false;
1050         if (_film) {
1051                 j = _film->with_subtitles ();
1052         }
1053         
1054         _subtitle_offset->Enable (j);
1055         _subtitle_scale->Enable (j);
1056         _subtitle_stream->Enable (j);
1057 }
1058
1059 void
1060 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
1061 {
1062         if (!_film) {
1063                 return;
1064         }
1065
1066         _film->set_use_dci_name (_use_dci_name->GetValue ());
1067 }
1068
1069 void
1070 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
1071 {
1072         if (!_film) {
1073                 return;
1074         }
1075
1076         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
1077         d->ShowModal ();
1078         _film->set_dci_metadata (d->dci_metadata ());
1079         d->Destroy ();
1080 }
1081
1082 void
1083 FilmEditor::active_jobs_changed (bool a)
1084 {
1085         set_things_sensitive (!a);
1086 }
1087
1088 void
1089 FilmEditor::setup_dcp_name ()
1090 {
1091         string s = _film->dcp_name (true);
1092         if (s.length() > 28) {
1093                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
1094                 _dcp_name->SetToolTip (std_to_wx (s));
1095         } else {
1096                 _dcp_name->SetLabel (std_to_wx (s));
1097         }
1098 }
1099
1100 void
1101 FilmEditor::show_audio_clicked (wxCommandEvent &)
1102 {
1103         if (_audio_dialog) {
1104                 _audio_dialog->Destroy ();
1105                 _audio_dialog = 0;
1106         }
1107
1108         shared_ptr<Content> c = selected_content ();
1109         if (!c) {
1110                 return;
1111         }
1112
1113         shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c);
1114         if (!ac) {
1115                 return;
1116         }
1117         
1118         _audio_dialog = new AudioDialog (this);
1119         _audio_dialog->Show ();
1120         _audio_dialog->set_content (ac);
1121 }
1122
1123 void
1124 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
1125 {
1126         if (!_film) {
1127                 return;
1128         }
1129         
1130         _film->set_dcp_video_frame_rate (_film->best_dcp_video_frame_rate ());
1131 }
1132
1133 void
1134 FilmEditor::setup_show_audio_sensitivity ()
1135 {
1136         _show_audio->Enable (_film);
1137 }
1138
1139 void
1140 FilmEditor::setup_content ()
1141 {
1142         string selected_summary;
1143         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1144         if (s != -1) {
1145                 selected_summary = wx_to_std (_content->GetItemText (s));
1146         }
1147         
1148         _content->DeleteAllItems ();
1149
1150         ContentList content = _film->content ();
1151         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
1152                 int const t = _content->GetItemCount ();
1153                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
1154                 if ((*i)->summary() == selected_summary) {
1155                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1156                 }
1157         }
1158
1159         if (selected_summary.empty () && !content.empty ()) {
1160                 /* Select the item of content if none was selected before */
1161                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1162         }
1163 }
1164
1165 void
1166 FilmEditor::content_add_clicked (wxCommandEvent &)
1167 {
1168         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
1169         int const r = d->ShowModal ();
1170         d->Destroy ();
1171
1172         if (r != wxID_OK) {
1173                 return;
1174         }
1175
1176         wxArrayString paths;
1177         d->GetPaths (paths);
1178
1179         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
1180                 boost::filesystem::path p (wx_to_std (paths[i]));
1181
1182                 shared_ptr<Content> c;
1183
1184                 if (StillImageContent::valid_file (p)) {
1185                         c.reset (new StillImageContent (_film, p));
1186                 } else if (SndfileContent::valid_file (p)) {
1187                         c.reset (new SndfileContent (_film, p));
1188                 } else {
1189                         c.reset (new FFmpegContent (_film, p));
1190                 }
1191
1192                 _film->examine_and_add_content (c);
1193         }
1194 }
1195
1196 void
1197 FilmEditor::content_remove_clicked (wxCommandEvent &)
1198 {
1199         shared_ptr<Content> c = selected_content ();
1200         if (c) {
1201                 _film->remove_content (c);
1202         }
1203
1204         wxListEvent ev;
1205         content_selection_changed (ev);
1206 }
1207
1208 void
1209 FilmEditor::content_selection_changed (wxListEvent &)
1210 {
1211         setup_content_sensitivity ();
1212         shared_ptr<Content> s = selected_content ();
1213         
1214         if (_audio_dialog && s && dynamic_pointer_cast<AudioContent> (s)) {
1215                 _audio_dialog->set_content (dynamic_pointer_cast<AudioContent> (s));
1216         }
1217
1218         film_content_changed (s, ContentProperty::START);
1219         film_content_changed (s, ContentProperty::LENGTH);
1220         film_content_changed (s, VideoContentProperty::VIDEO_CROP);
1221         film_content_changed (s, VideoContentProperty::VIDEO_RATIO);
1222         film_content_changed (s, AudioContentProperty::AUDIO_GAIN);
1223         film_content_changed (s, AudioContentProperty::AUDIO_DELAY);
1224         film_content_changed (s, AudioContentProperty::AUDIO_MAPPING);
1225         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAM);
1226         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAMS);
1227         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAM);
1228         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAMS);
1229         film_content_changed (s, FFmpegContentProperty::FILTERS);
1230         film_content_changed (s, SubtitleContentProperty::SUBTITLE_OFFSET);
1231         film_content_changed (s, SubtitleContentProperty::SUBTITLE_SCALE);
1232 }
1233
1234 void
1235 FilmEditor::setup_content_sensitivity ()
1236 {
1237         _content_add->Enable (_generally_sensitive);
1238
1239         shared_ptr<Content> selection = selected_content ();
1240
1241         _content_remove->Enable (selection && _generally_sensitive);
1242         _content_timeline->Enable (_generally_sensitive);
1243
1244         _video_panel->Enable    (selection && dynamic_pointer_cast<VideoContent>  (selection) && _generally_sensitive);
1245         _audio_panel->Enable    (selection && dynamic_pointer_cast<AudioContent>  (selection) && _generally_sensitive);
1246         _subtitle_panel->Enable (selection && dynamic_pointer_cast<FFmpegContent> (selection) && _generally_sensitive);
1247         _timing_panel->Enable   (selection && _generally_sensitive);
1248 }
1249
1250 shared_ptr<Content>
1251 FilmEditor::selected_content ()
1252 {
1253         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1254         if (s == -1) {
1255                 return shared_ptr<Content> ();
1256         }
1257
1258         ContentList c = _film->content ();
1259         if (s < 0 || size_t (s) >= c.size ()) {
1260                 return shared_ptr<Content> ();
1261         }
1262         
1263         return c[s];
1264 }
1265
1266 shared_ptr<VideoContent>
1267 FilmEditor::selected_video_content ()
1268 {
1269         shared_ptr<Content> c = selected_content ();
1270         if (!c) {
1271                 return shared_ptr<VideoContent> ();
1272         }
1273
1274         return dynamic_pointer_cast<VideoContent> (c);
1275 }
1276
1277 shared_ptr<AudioContent>
1278 FilmEditor::selected_audio_content ()
1279 {
1280         shared_ptr<Content> c = selected_content ();
1281         if (!c) {
1282                 return shared_ptr<AudioContent> ();
1283         }
1284
1285         return dynamic_pointer_cast<AudioContent> (c);
1286 }
1287
1288 shared_ptr<SubtitleContent>
1289 FilmEditor::selected_subtitle_content ()
1290 {
1291         shared_ptr<Content> c = selected_content ();
1292         if (!c) {
1293                 return shared_ptr<SubtitleContent> ();
1294         }
1295
1296         return dynamic_pointer_cast<SubtitleContent> (c);
1297 }
1298
1299 void
1300 FilmEditor::setup_scaling_description ()
1301 {
1302         shared_ptr<VideoContent> vc = selected_video_content ();
1303         if (!vc) {
1304                 _scaling_description->SetLabel ("");
1305                 return;
1306         }
1307
1308         wxString d;
1309
1310         int lines = 0;
1311
1312         if (vc->video_size().width && vc->video_size().height) {
1313                 d << wxString::Format (
1314                         _("Original video is %dx%d (%.2f:1)\n"),
1315                         vc->video_size().width, vc->video_size().height,
1316                         float (vc->video_size().width) / vc->video_size().height
1317                         );
1318                 ++lines;
1319         }
1320
1321         Crop const crop = vc->crop ();
1322         if ((crop.left || crop.right || crop.top || crop.bottom) && vc->video_size() != libdcp::Size (0, 0)) {
1323                 libdcp::Size cropped = vc->video_size ();
1324                 cropped.width -= crop.left + crop.right;
1325                 cropped.height -= crop.top + crop.bottom;
1326                 d << wxString::Format (
1327                         _("Cropped to %dx%d (%.2f:1)\n"),
1328                         cropped.width, cropped.height,
1329                         float (cropped.width) / cropped.height
1330                         );
1331                 ++lines;
1332         }
1333
1334         Ratio const * ratio = vc->ratio ();
1335         if (ratio) {
1336                 libdcp::Size container_size = _film->container()->size (_film->full_frame ());
1337                 
1338                 libdcp::Size const scaled = ratio->size (container_size);
1339                 d << wxString::Format (
1340                         _("Scaled to %dx%d (%.2f:1)\n"),
1341                         scaled.width, scaled.height,
1342                         float (scaled.width) / scaled.height
1343                         );
1344                 ++lines;
1345
1346                 if (scaled != container_size) {
1347                         d << wxString::Format (
1348                                 _("Padded with black to %dx%d (%.2f:1)\n"),
1349                                 container_size.width, container_size.height,
1350                                 float (container_size.width) / container_size.height
1351                                 );
1352                         ++lines;
1353                 }
1354         }
1355
1356         for (int i = lines; i < 4; ++i) {
1357                 d << wxT ("\n ");
1358         }
1359
1360         _scaling_description->SetLabel (d);
1361 }
1362
1363 void
1364 FilmEditor::content_timeline_clicked (wxCommandEvent &)
1365 {
1366         if (_timeline_dialog) {
1367                 _timeline_dialog->Destroy ();
1368                 _timeline_dialog = 0;
1369         }
1370         
1371         _timeline_dialog = new TimelineDialog (this, _film);
1372         _timeline_dialog->Show ();
1373 }
1374
1375 void
1376 FilmEditor::audio_stream_changed (wxCommandEvent &)
1377 {
1378         shared_ptr<Content> c = selected_content ();
1379         if (!c) {
1380                 return;
1381         }
1382         
1383         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
1384         if (!fc) {
1385                 return;
1386         }
1387         
1388         vector<shared_ptr<FFmpegAudioStream> > a = fc->audio_streams ();
1389         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
1390         string const s = string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ()));
1391         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
1392                 ++i;
1393         }
1394
1395         if (i != a.end ()) {
1396                 fc->set_audio_stream (*i);
1397         }
1398
1399         if (!fc->audio_stream ()) {
1400                 _audio_description->SetLabel (wxT (""));
1401         } else {
1402                 wxString s;
1403                 if (fc->audio_channels() == 1) {
1404                         s << _("1 channel");
1405                 } else {
1406                         s << fc->audio_channels() << wxT (" ") << _("channels");
1407                 }
1408                 s << wxT (", ") << fc->content_audio_frame_rate() << _("Hz");
1409                 _audio_description->SetLabel (s);
1410         }
1411 }
1412
1413
1414
1415 void
1416 FilmEditor::subtitle_stream_changed (wxCommandEvent &)
1417 {
1418         shared_ptr<Content> c = selected_content ();
1419         if (!c) {
1420                 return;
1421         }
1422         
1423         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
1424         if (!fc) {
1425                 return;
1426         }
1427         
1428         vector<shared_ptr<FFmpegSubtitleStream> > a = fc->subtitle_streams ();
1429         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
1430         string const s = string_client_data (_subtitle_stream->GetClientObject (_subtitle_stream->GetSelection ()));
1431         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
1432                 ++i;
1433         }
1434
1435         if (i != a.end ()) {
1436                 fc->set_subtitle_stream (*i);
1437         }
1438 }
1439
1440 void
1441 FilmEditor::audio_mapping_changed (AudioMapping m)
1442 {
1443         shared_ptr<Content> c = selected_content ();
1444         if (!c) {
1445                 return;
1446         }
1447         
1448         shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c);
1449         if (!ac) {
1450                 return;
1451         }
1452
1453         ac->set_audio_mapping (m);
1454 }
1455
1456 void
1457 FilmEditor::start_changed ()
1458 {
1459         shared_ptr<Content> c = selected_content ();
1460         if (!c) {
1461                 return;
1462         }
1463
1464         c->set_start (_start->get (_film->dcp_video_frame_rate ()));
1465 }
1466
1467 void
1468 FilmEditor::length_changed ()
1469 {
1470         shared_ptr<Content> c = selected_content ();
1471         if (!c) {
1472                 return;
1473         }
1474
1475         shared_ptr<StillImageContent> ic = dynamic_pointer_cast<StillImageContent> (c);
1476         if (ic) {
1477                 ic->set_video_length (_length->get(_film->dcp_video_frame_rate()) * ic->video_frame_rate() / TIME_HZ);
1478         }
1479 }
1480
1481 void
1482 FilmEditor::set_selection (weak_ptr<Content> wc)
1483 {
1484         ContentList content = _film->content ();
1485         for (size_t i = 0; i < content.size(); ++i) {
1486                 if (content[i] == wc.lock ()) {
1487                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1488                 } else {
1489                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
1490                 }
1491         }
1492 }
1493
1494 void
1495 FilmEditor::ratio_changed (wxCommandEvent &)
1496 {
1497         if (!_film) {
1498                 return;
1499         }
1500
1501         shared_ptr<Content> c = selected_content ();
1502         if (!c) {
1503                 return;
1504         }
1505
1506         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
1507         if (!vc) {
1508                 return;
1509         }
1510         
1511         int const n = _ratio->GetSelection ();
1512         if (n >= 0) {
1513                 vector<Ratio const *> ratios = Ratio::all ();
1514                 assert (n < int (ratios.size()));
1515                 vc->set_ratio (ratios[n]);
1516         }
1517 }
1518
1519 void
1520 FilmEditor::sequence_video_changed (wxCommandEvent &)
1521 {
1522         _film->set_sequence_video (_sequence_video->GetValue ());
1523 }
1524
1525 void
1526 FilmEditor::content_right_click (wxListEvent& ev)
1527 {
1528         ContentList cl;
1529         cl.push_back (selected_content ());
1530         _menu.popup (cl, ev.GetPoint ());
1531 }