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