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