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