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