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