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