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