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