Move trimming into the encoder; seems to be cleaner.
[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 <boost/thread.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include "lib/format.h"
31 #include "lib/film.h"
32 #include "lib/transcode_job.h"
33 #include "lib/exceptions.h"
34 #include "lib/ab_transcode_job.h"
35 #include "lib/job_manager.h"
36 #include "lib/filter.h"
37 #include "lib/screen.h"
38 #include "lib/config.h"
39 #include "filter_dialog.h"
40 #include "wx_util.h"
41 #include "film_editor.h"
42 #include "dcp_range_dialog.h"
43 #include "gain_calculator_dialog.h"
44 #include "sound_processor.h"
45 #include "dci_name_dialog.h"
46 #include "scaler.h"
47
48 using std::string;
49 using std::stringstream;
50 using std::pair;
51 using std::fixed;
52 using std::setprecision;
53 using std::list;
54 using std::vector;
55 using boost::shared_ptr;
56
57 /** @param f Film to edit */
58 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
59         : wxPanel (parent)
60         , _ignore_changes (Film::NONE)
61         , _film (f)
62 {
63         _sizer = new wxFlexGridSizer (2, 4, 4);
64         SetSizer (_sizer);
65
66         add_label_to_sizer (_sizer, this, "Name");
67         _name = new wxTextCtrl (this, wxID_ANY);
68         _sizer->Add (_name, 1, wxEXPAND);
69
70         add_label_to_sizer (_sizer, this, "DCP Name");
71         _dcp_name = new wxStaticText (this, wxID_ANY, wxT (""));
72         _sizer->Add (_dcp_name, 0, wxALIGN_CENTER_VERTICAL | wxSHRINK);
73
74         _use_dci_name = new wxCheckBox (this, wxID_ANY, wxT ("Use DCI name"));
75         _sizer->Add (_use_dci_name, 1, wxEXPAND);
76         _edit_dci_button = new wxButton (this, wxID_ANY, wxT ("Details..."));
77         _sizer->Add (_edit_dci_button, 0);
78
79         add_label_to_sizer (_sizer, this, "Content");
80         _content = new wxFilePickerCtrl (this, wxID_ANY, wxT (""), wxT ("Select Content File"), wxT("*.*"));
81         _sizer->Add (_content, 1, wxEXPAND);
82
83         add_label_to_sizer (_sizer, this, "Content Type");
84         _dcp_content_type = new wxComboBox (this, wxID_ANY);
85         _sizer->Add (_dcp_content_type);
86
87         add_label_to_sizer (_sizer, this, "Format");
88         _format = new wxComboBox (this, wxID_ANY);
89         _sizer->Add (_format);
90
91         {
92                 add_label_to_sizer (_sizer, this, "Crop");
93                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
94
95                 add_label_to_sizer (s, this, "L");
96                 _left_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
97                 s->Add (_left_crop, 0);
98                 add_label_to_sizer (s, this, "R");
99                 _right_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
100                 s->Add (_right_crop, 0);
101                 add_label_to_sizer (s, this, "T");
102                 _top_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
103                 s->Add (_top_crop, 0);
104                 add_label_to_sizer (s, this, "B");
105                 _bottom_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
106                 s->Add (_bottom_crop, 0);
107
108                 _sizer->Add (s);
109         }
110
111         /* VIDEO-only stuff */
112         {
113                 video_control (add_label_to_sizer (_sizer, this, "Filters"));
114                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
115                 _filters = new wxStaticText (this, wxID_ANY, wxT ("None"));
116                 video_control (_filters);
117                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
118                 _filters_button = new wxButton (this, wxID_ANY, wxT ("Edit..."));
119                 video_control (_filters_button);
120                 s->Add (_filters_button, 0);
121                 _sizer->Add (s, 1);
122         }
123
124         video_control (add_label_to_sizer (_sizer, this, "Scaler"));
125         _scaler = new wxComboBox (this, wxID_ANY);
126         _sizer->Add (video_control (_scaler), 1);
127
128         {
129                 video_control (add_label_to_sizer (_sizer, this, "Audio Stream"));
130                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
131                 _audio_stream = new wxComboBox (this, wxID_ANY);
132                 s->Add (video_control (_audio_stream), 1);
133                 _audio = new wxStaticText (this, wxID_ANY, wxT (""));
134                 s->Add (video_control (_audio), 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
135                 _sizer->Add (s, 1, wxEXPAND);
136         }
137
138         {
139                 video_control (add_label_to_sizer (_sizer, this, "Audio Gain"));
140                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
141                 _audio_gain = new wxSpinCtrl (this);
142                 s->Add (video_control (_audio_gain), 1);
143                 video_control (add_label_to_sizer (s, this, "dB"));
144                 _audio_gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
145                 video_control (_audio_gain_calculate_button);
146                 s->Add (_audio_gain_calculate_button, 1, wxEXPAND);
147                 _sizer->Add (s);
148         }
149
150         {
151                 video_control (add_label_to_sizer (_sizer, this, "Audio Delay"));
152                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
153                 _audio_delay = new wxSpinCtrl (this);
154                 s->Add (video_control (_audio_delay), 1);
155                 video_control (add_label_to_sizer (s, this, "ms"));
156                 _sizer->Add (s);
157         }
158
159         _with_subtitles = new wxCheckBox (this, wxID_ANY, wxT("With Subtitles"));
160         video_control (_with_subtitles);
161         _sizer->Add (_with_subtitles, 1);
162         
163         _subtitle_stream = new wxComboBox (this, wxID_ANY);
164         _sizer->Add (_subtitle_stream);
165
166         video_control (add_label_to_sizer (_sizer, this, "Subtitle Offset"));
167         _subtitle_offset = new wxSpinCtrl (this);
168         _sizer->Add (video_control (_subtitle_offset), 1);
169
170         {
171                 video_control (add_label_to_sizer (_sizer, this, "Subtitle Scale"));
172                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
173                 _subtitle_scale = new wxSpinCtrl (this);
174                 s->Add (video_control (_subtitle_scale));
175                 video_control (add_label_to_sizer (s, this, "%"));
176                 _sizer->Add (s);
177         }
178         
179         video_control (add_label_to_sizer (_sizer, this, "Frames Per Second"));
180         _frames_per_second = new wxStaticText (this, wxID_ANY, wxT (""));
181         _sizer->Add (video_control (_frames_per_second), 1, wxALIGN_CENTER_VERTICAL);
182         
183         video_control (add_label_to_sizer (_sizer, this, "Original Size"));
184         _original_size = new wxStaticText (this, wxID_ANY, wxT (""));
185         _sizer->Add (video_control (_original_size), 1, wxALIGN_CENTER_VERTICAL);
186         
187         video_control (add_label_to_sizer (_sizer, this, "Length"));
188         _length = new wxStaticText (this, wxID_ANY, wxT (""));
189         _sizer->Add (video_control (_length), 1, wxALIGN_CENTER_VERTICAL);
190
191
192         {
193                 video_control (add_label_to_sizer (_sizer, this, "Range"));
194                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
195                 _dcp_range = new wxStaticText (this, wxID_ANY, wxT (""));
196                 s->Add (video_control (_dcp_range), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
197                 _change_dcp_range_button = new wxButton (this, wxID_ANY, wxT ("Edit..."));
198                 s->Add (video_control (_change_dcp_range_button), 0, 0, 6);
199                 _sizer->Add (s);
200         }
201
202         _dcp_ab = new wxCheckBox (this, wxID_ANY, wxT ("A/B"));
203         video_control (_dcp_ab);
204         _sizer->Add (_dcp_ab, 1);
205         _sizer->AddSpacer (0);
206
207         /* STILL-only stuff */
208         {
209                 still_control (add_label_to_sizer (_sizer, this, "Duration"));
210                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
211                 _still_duration = new wxSpinCtrl (this);
212                 still_control (_still_duration);
213                 s->Add (_still_duration, 1, wxEXPAND);
214                 still_control (add_label_to_sizer (s, this, "s"));
215                 _sizer->Add (s);
216         }
217
218         /* Set up our editing widgets */
219         
220         _left_crop->SetRange (0, 1024);
221         _top_crop->SetRange (0, 1024);
222         _right_crop->SetRange (0, 1024);
223         _bottom_crop->SetRange (0, 1024);
224         _audio_gain->SetRange (-60, 60);
225         _audio_delay->SetRange (-1000, 1000);
226         _still_duration->SetRange (0, 60 * 60);
227         _subtitle_offset->SetRange (-1024, 1024);
228         _subtitle_scale->SetRange (1, 1000);
229
230         vector<DCPContentType const *> const ct = DCPContentType::all ();
231         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
232                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
233         }
234
235         vector<Scaler const *> const sc = Scaler::all ();
236         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
237                 _scaler->Append (std_to_wx ((*i)->name()));
238         }
239
240         JobManager::instance()->ActiveJobsChanged.connect (
241                 bind (&FilmEditor::active_jobs_changed, this, _1)
242                 );
243
244         /* And set their values from the Film */
245         set_film (f);
246         
247         /* Now connect to them, since initial values are safely set */
248         _name->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (FilmEditor::name_changed), 0, this);
249         _use_dci_name->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
250         _edit_dci_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
251         _format->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::format_changed), 0, this);
252         _content->Connect (wxID_ANY, wxEVT_COMMAND_FILEPICKER_CHANGED, wxCommandEventHandler (FilmEditor::content_changed), 0, this);
253         _left_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
254         _right_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
255         _top_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
256         _bottom_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
257         _filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
258         _scaler->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
259         _dcp_content_type->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
260         _dcp_ab->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::dcp_ab_toggled), 0, this);
261         _audio_gain->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
262         _audio_gain_calculate_button->Connect (
263                 wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::audio_gain_calculate_button_clicked), 0, this
264                 );
265         _audio_delay->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
266         _still_duration->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::still_duration_changed), 0, this);
267         _change_dcp_range_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::change_dcp_range_clicked), 0, this);
268         _with_subtitles->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::with_subtitles_toggled), 0, this);
269         _subtitle_offset->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::subtitle_offset_changed), 0, this);
270         _subtitle_scale->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::subtitle_scale_changed), 0, this);
271         _audio_stream->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::audio_stream_changed), 0, this);
272         _subtitle_stream->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (FilmEditor::subtitle_stream_changed), 0, this);
273
274         setup_visibility ();
275         setup_formats ();
276 }
277
278 /** Called when the left crop widget has been changed */
279 void
280 FilmEditor::left_crop_changed (wxCommandEvent &)
281 {
282         if (!_film) {
283                 return;
284         }
285
286         _ignore_changes = Film::CROP;
287         _film->set_left_crop (_left_crop->GetValue ());
288         _ignore_changes = Film::NONE;
289 }
290
291 /** Called when the right crop widget has been changed */
292 void
293 FilmEditor::right_crop_changed (wxCommandEvent &)
294 {
295         if (!_film) {
296                 return;
297         }
298
299         _ignore_changes = Film::CROP;
300         _film->set_right_crop (_right_crop->GetValue ());
301         _ignore_changes = Film::NONE;
302 }
303
304 /** Called when the top crop widget has been changed */
305 void
306 FilmEditor::top_crop_changed (wxCommandEvent &)
307 {
308         if (!_film) {
309                 return;
310         }
311
312         _ignore_changes = Film::CROP;
313         _film->set_top_crop (_top_crop->GetValue ());
314         _ignore_changes = Film::NONE;
315 }
316
317 /** Called when the bottom crop value has been changed */
318 void
319 FilmEditor::bottom_crop_changed (wxCommandEvent &)
320 {
321         if (!_film) {
322                 return;
323         }
324
325         _ignore_changes = Film::CROP;
326         _film->set_bottom_crop (_bottom_crop->GetValue ());
327         _ignore_changes = Film::NONE;
328 }
329
330 /** Called when the content filename has been changed */
331 void
332 FilmEditor::content_changed (wxCommandEvent &)
333 {
334         if (!_film) {
335                 return;
336         }
337
338         _ignore_changes = Film::CONTENT;
339         
340         try {
341                 _film->set_content (wx_to_std (_content->GetPath ()));
342         } catch (std::exception& e) {
343                 _content->SetPath (std_to_wx (_film->directory ()));
344                 error_dialog (this, String::compose ("Could not set content: %1", e.what ()));
345         }
346
347         _ignore_changes = Film::NONE;
348
349         setup_visibility ();
350         setup_formats ();
351         setup_subtitle_button ();
352         setup_streams ();
353 }
354
355 /** Called when the DCP A/B switch has been toggled */
356 void
357 FilmEditor::dcp_ab_toggled (wxCommandEvent &)
358 {
359         if (!_film) {
360                 return;
361         }
362         
363         _ignore_changes = Film::DCP_AB;
364         _film->set_dcp_ab (_dcp_ab->GetValue ());
365         _ignore_changes = Film::NONE;
366 }
367
368 /** Called when the name widget has been changed */
369 void
370 FilmEditor::name_changed (wxCommandEvent &)
371 {
372         if (!_film) {
373                 return;
374         }
375
376         _ignore_changes = Film::NAME;
377         _film->set_name (string (_name->GetValue().mb_str()));
378         _ignore_changes = Film::NONE;
379
380         _film->set_dci_date_today ();
381         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
382 }
383
384 void
385 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
386 {
387         if (!_film) {
388                 return;
389         }
390
391         _ignore_changes = Film::SUBTITLE_OFFSET;
392         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
393         _ignore_changes = Film::NONE;
394 }
395
396 void
397 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
398 {
399         if (!_film) {
400                 return;
401         }
402
403         _ignore_changes = Film::SUBTITLE_OFFSET;
404         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
405         _ignore_changes = Film::NONE;
406 }
407
408
409 /** Called when the metadata stored in the Film object has changed;
410  *  so that we can update the GUI.
411  *  @param p Property of the Film that has changed.
412  */
413 void
414 FilmEditor::film_changed (Film::Property p)
415 {
416         ensure_ui_thread ();
417         
418         if (!_film || _ignore_changes == p) {
419                 return;
420         }
421
422         stringstream s;
423                 
424         switch (p) {
425         case Film::NONE:
426                 break;
427         case Film::CONTENT:
428                 _content->SetPath (std_to_wx (_film->content ()));
429                 setup_visibility ();
430                 setup_formats ();
431                 setup_subtitle_button ();
432                 setup_streams ();
433                 break;
434         case Film::HAS_SUBTITLES:
435                 setup_subtitle_button ();
436                 setup_streams ();
437                 break;
438         case Film::AUDIO_STREAMS:
439         case Film::SUBTITLE_STREAMS:
440                 setup_streams ();
441                 break;
442         case Film::FORMAT:
443         {
444                 int n = 0;
445                 vector<Format const *>::iterator i = _formats.begin ();
446                 while (i != _formats.end() && *i != _film->format ()) {
447                         ++i;
448                         ++n;
449                 }
450                 _format->SetSelection (n);
451                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
452                 break;
453         }
454         case Film::CROP:
455                 _left_crop->SetValue (_film->crop().left);
456                 _right_crop->SetValue (_film->crop().right);
457                 _top_crop->SetValue (_film->crop().top);
458                 _bottom_crop->SetValue (_film->crop().bottom);
459                 break;
460         case Film::FILTERS:
461         {
462                 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
463                 if (p.first.empty () && p.second.empty ()) {
464                         _filters->SetLabel (_("None"));
465                 } else {
466                         string const b = p.first + " " + p.second;
467                         _filters->SetLabel (std_to_wx (b));
468                 }
469                 _sizer->Layout ();
470                 break;
471         }
472         case Film::NAME:
473                 _name->ChangeValue (std_to_wx (_film->name ()));
474                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
475                 break;
476         case Film::FRAMES_PER_SECOND:
477                 s << fixed << setprecision(2) << _film->frames_per_second();
478                 _frames_per_second->SetLabel (std_to_wx (s.str ()));
479                 break;
480         case Film::AUDIO_SAMPLE_RATE:
481                 setup_audio_details ();
482                 break;
483         case Film::SIZE:
484                 if (_film->size().width == 0 && _film->size().height == 0) {
485                         _original_size->SetLabel (wxT (""));
486                 } else {
487                         s << _film->size().width << " x " << _film->size().height;
488                         _original_size->SetLabel (std_to_wx (s.str ()));
489                 }
490                 break;
491         case Film::LENGTH:
492                 if (_film->frames_per_second() > 0 && _film->length()) {
493                         s << _film->length().get() << " frames; " << seconds_to_hms (_film->length().get() / _film->frames_per_second());
494                 } else if (_film->length()) {
495                         s << _film->length().get() << " frames";
496                 } 
497                 _length->SetLabel (std_to_wx (s.str ()));
498                 break;
499         case Film::DCP_CONTENT_TYPE:
500                 _dcp_content_type->SetSelection (DCPContentType::as_index (_film->dcp_content_type ()));
501                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
502                 break;
503         case Film::THUMBS:
504                 break;
505         case Film::DCP_AB:
506                 _dcp_ab->SetValue (_film->dcp_ab ());
507                 break;
508         case Film::SCALER:
509                 _scaler->SetSelection (Scaler::as_index (_film->scaler ()));
510                 break;
511         case Film::DCP_TRIM_START:
512         case Film::DCP_TRIM_END:
513                 if (_film->dcp_trim_start() == 0 && _film->dcp_trim_end() == 0) {
514                         _dcp_range->SetLabel (wxT ("Whole film"));
515                 } else {
516                         _dcp_range->SetLabel (
517                                 std_to_wx (String::compose ("Trim %1 frames from start and %2 frames from end", _film->dcp_trim_start(), _film->dcp_trim_end()))
518                                 );
519                 }
520                 _sizer->Layout ();
521                 break;
522         case Film::AUDIO_GAIN:
523                 _audio_gain->SetValue (_film->audio_gain ());
524                 break;
525         case Film::AUDIO_DELAY:
526                 _audio_delay->SetValue (_film->audio_delay ());
527                 break;
528         case Film::STILL_DURATION:
529                 _still_duration->SetValue (_film->still_duration ());
530                 break;
531         case Film::WITH_SUBTITLES:
532                 _with_subtitles->SetValue (_film->with_subtitles ());
533                 _subtitle_scale->Enable (_film->with_subtitles ());
534                 _subtitle_offset->Enable (_film->with_subtitles ());
535                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
536                 break;
537         case Film::SUBTITLE_OFFSET:
538                 _subtitle_offset->SetValue (_film->subtitle_offset ());
539                 break;
540         case Film::SUBTITLE_SCALE:
541                 _subtitle_scale->SetValue (_film->subtitle_scale() * 100);
542                 break;
543         case Film::USE_DCI_NAME:
544                 _use_dci_name->SetValue (_film->use_dci_name ());
545                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
546                 break;
547         case Film::DCI_METADATA:
548                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
549                 break;
550         case Film::AUDIO_STREAM:
551                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
552                 _audio_stream->SetSelection (_film->audio_stream_index ());
553                 setup_audio_details ();
554                 break;
555         case Film::SUBTITLE_STREAM:
556                 _subtitle_stream->SetSelection (_film->subtitle_stream_index ());
557                 break;
558         }
559 }
560
561 /** Called when the format widget has been changed */
562 void
563 FilmEditor::format_changed (wxCommandEvent &)
564 {
565         if (!_film) {
566                 return;
567         }
568
569         _ignore_changes = Film::FORMAT;
570         int const n = _format->GetSelection ();
571         if (n >= 0) {
572                 assert (n < int (_formats.size()));
573                 _film->set_format (_formats[n]);
574         }
575         _ignore_changes = Film::NONE;
576
577         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
578 }
579
580 /** Called when the DCP content type widget has been changed */
581 void
582 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
583 {
584         if (!_film) {
585                 return;
586         }
587
588         _ignore_changes = Film::DCP_CONTENT_TYPE;
589         int const n = _dcp_content_type->GetSelection ();
590         if (n >= 0) {
591                 _film->set_dcp_content_type (DCPContentType::from_index (n));
592         }
593         _ignore_changes = Film::NONE;
594
595         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
596 }
597
598 /** Sets the Film that we are editing */
599 void
600 FilmEditor::set_film (shared_ptr<Film> f)
601 {
602         _film = f;
603
604         set_things_sensitive (_film != 0);
605
606         if (_film) {
607                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
608         }
609
610         if (_film) {
611                 FileChanged (_film->directory ());
612         } else {
613                 FileChanged ("");
614         }
615         
616         film_changed (Film::NAME);
617         film_changed (Film::CONTENT);
618         film_changed (Film::DCP_CONTENT_TYPE);
619         film_changed (Film::FORMAT);
620         film_changed (Film::CROP);
621         film_changed (Film::FILTERS);
622         film_changed (Film::DCP_TRIM_START);
623         film_changed (Film::DCP_TRIM_END);
624         film_changed (Film::DCP_AB);
625         film_changed (Film::SIZE);
626         film_changed (Film::LENGTH);
627         film_changed (Film::FRAMES_PER_SECOND);
628         film_changed (Film::AUDIO_SAMPLE_RATE);
629         film_changed (Film::SCALER);
630         film_changed (Film::AUDIO_GAIN);
631         film_changed (Film::AUDIO_DELAY);
632         film_changed (Film::STILL_DURATION);
633         film_changed (Film::WITH_SUBTITLES);
634         film_changed (Film::HAS_SUBTITLES);
635         film_changed (Film::SUBTITLE_OFFSET);
636         film_changed (Film::SUBTITLE_SCALE);
637         film_changed (Film::USE_DCI_NAME);
638         film_changed (Film::DCI_METADATA);
639 }
640
641 /** Updates the sensitivity of lots of widgets to a given value.
642  *  @param s true to make sensitive, false to make insensitive.
643  */
644 void
645 FilmEditor::set_things_sensitive (bool s)
646 {
647         _name->Enable (s);
648         _use_dci_name->Enable (s);
649         _edit_dci_button->Enable (s);
650         _format->Enable (s);
651         _content->Enable (s);
652         _left_crop->Enable (s);
653         _right_crop->Enable (s);
654         _top_crop->Enable (s);
655         _bottom_crop->Enable (s);
656         _filters_button->Enable (s);
657         _scaler->Enable (s);
658         _audio_stream->Enable (s);
659         _dcp_content_type->Enable (s);
660         _change_dcp_range_button->Enable (s);
661         _dcp_ab->Enable (s);
662         _audio_gain->Enable (s);
663         _audio_gain_calculate_button->Enable (s);
664         _audio_delay->Enable (s);
665         _subtitle_stream->Enable (s);
666         _still_duration->Enable (s);
667         _with_subtitles->Enable (s);
668         _subtitle_offset->Enable (s);
669         _subtitle_scale->Enable (s);
670 }
671
672 /** Called when the `Edit filters' button has been clicked */
673 void
674 FilmEditor::edit_filters_clicked (wxCommandEvent &)
675 {
676         FilterDialog* d = new FilterDialog (this, _film->filters());
677         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
678         d->ShowModal ();
679         d->Destroy ();
680 }
681
682 /** Called when the scaler widget has been changed */
683 void
684 FilmEditor::scaler_changed (wxCommandEvent &)
685 {
686         if (!_film) {
687                 return;
688         }
689
690         _ignore_changes = Film::SCALER;
691         int const n = _scaler->GetSelection ();
692         if (n >= 0) {
693                 _film->set_scaler (Scaler::from_index (n));
694         }
695         _ignore_changes = Film::NONE;
696 }
697
698 void
699 FilmEditor::audio_gain_changed (wxCommandEvent &)
700 {
701         if (!_film) {
702                 return;
703         }
704
705         _ignore_changes = Film::AUDIO_GAIN;
706         _film->set_audio_gain (_audio_gain->GetValue ());
707         _ignore_changes = Film::NONE;
708 }
709
710 void
711 FilmEditor::audio_delay_changed (wxCommandEvent &)
712 {
713         if (!_film) {
714                 return;
715         }
716
717         _ignore_changes = Film::AUDIO_DELAY;
718         _film->set_audio_delay (_audio_delay->GetValue ());
719         _ignore_changes = Film::NONE;
720 }
721
722 wxControl *
723 FilmEditor::video_control (wxControl* c)
724 {
725         _video_controls.push_back (c);
726         return c;
727 }
728
729 wxControl *
730 FilmEditor::still_control (wxControl* c)
731 {
732         _still_controls.push_back (c);
733         return c;
734 }
735
736 void
737 FilmEditor::setup_visibility ()
738 {
739         ContentType c = VIDEO;
740
741         if (_film) {
742                 c = _film->content_type ();
743         }
744
745         for (list<wxControl*>::iterator i = _video_controls.begin(); i != _video_controls.end(); ++i) {
746                 (*i)->Show (c == VIDEO);
747         }
748
749         for (list<wxControl*>::iterator i = _still_controls.begin(); i != _still_controls.end(); ++i) {
750                 (*i)->Show (c == STILL);
751         }
752
753         _sizer->Layout ();
754 }
755
756 void
757 FilmEditor::still_duration_changed (wxCommandEvent &)
758 {
759         if (!_film) {
760                 return;
761         }
762
763         _ignore_changes = Film::STILL_DURATION;
764         _film->set_still_duration (_still_duration->GetValue ());
765         _ignore_changes = Film::NONE;
766 }
767
768 void
769 FilmEditor::change_dcp_range_clicked (wxCommandEvent &)
770 {
771         DCPRangeDialog* d = new DCPRangeDialog (this, _film);
772         d->Changed.connect (bind (&FilmEditor::dcp_range_changed, this, _1, _2));
773         d->ShowModal ();
774 }
775
776 void
777 FilmEditor::dcp_range_changed (int start, int end)
778 {
779         _film->set_dcp_trim_start (start);
780         _film->set_dcp_trim_end (end);
781 }
782
783 void
784 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
785 {
786         GainCalculatorDialog* d = new GainCalculatorDialog (this);
787         d->ShowModal ();
788
789         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
790                 d->Destroy ();
791                 return;
792         }
793         
794         _audio_gain->SetValue (
795                 Config::instance()->sound_processor()->db_for_fader_change (
796                         d->wanted_fader (),
797                         d->actual_fader ()
798                         )
799                 );
800
801         /* This appears to be necessary, as the change is not signalled,
802            I think.
803         */
804         wxCommandEvent dummy;
805         audio_gain_changed (dummy);
806         
807         d->Destroy ();
808 }
809
810 void
811 FilmEditor::setup_formats ()
812 {
813         ContentType c = VIDEO;
814
815         if (_film) {
816                 c = _film->content_type ();
817         }
818         
819         _formats.clear ();
820
821         vector<Format const *> fmt = Format::all ();
822         for (vector<Format const *>::iterator i = fmt.begin(); i != fmt.end(); ++i) {
823                 if (c == VIDEO && dynamic_cast<FixedFormat const *> (*i)) {
824                         _formats.push_back (*i);
825                 } else if (c == STILL && dynamic_cast<VariableFormat const *> (*i)) {
826                         _formats.push_back (*i);
827                 }
828         }
829
830         _format->Clear ();
831         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
832                 _format->Append (std_to_wx ((*i)->name ()));
833         }
834
835         _sizer->Layout ();
836 }
837
838 void
839 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
840 {
841         if (!_film) {
842                 return;
843         }
844
845         _ignore_changes = Film::WITH_SUBTITLES;
846         _film->set_with_subtitles (_with_subtitles->GetValue ());
847         _ignore_changes = Film::NONE;
848
849         _subtitle_scale->Enable (_film->with_subtitles ());
850         _subtitle_offset->Enable (_film->with_subtitles ());
851 }
852
853 void
854 FilmEditor::setup_subtitle_button ()
855 {
856         _with_subtitles->Enable (_film->has_subtitles ());
857         if (!_film->has_subtitles ()) {
858                 _with_subtitles->SetValue (false);
859         }
860 }
861
862 void
863 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
864 {
865         if (!_film) {
866                 return;
867         }
868
869         _ignore_changes = Film::USE_DCI_NAME;
870         _film->set_use_dci_name (_use_dci_name->GetValue ());
871         _ignore_changes = Film::NONE;
872
873         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
874 }
875
876 void
877 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
878 {
879         if (!_film) {
880                 return;
881         }
882
883         DCINameDialog* d = new DCINameDialog (this, _film);
884         d->ShowModal ();
885         d->Destroy ();
886 }
887
888 void
889 FilmEditor::setup_streams ()
890 {
891         _audio_stream->Clear ();
892         vector<AudioStream> a = _film->audio_streams ();
893         for (vector<AudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
894                 _audio_stream->Append (std_to_wx (i->name()));
895         }
896         _audio_stream->SetSelection (_film->audio_stream_index ());
897
898         _subtitle_stream->Clear ();
899         vector<SubtitleStream> s = _film->subtitle_streams ();
900         for (vector<SubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
901                 _subtitle_stream->Append (std_to_wx (i->name()));
902         }
903         _subtitle_stream->SetSelection (_film->subtitle_stream_index ());
904 }
905
906 void
907 FilmEditor::audio_stream_changed (wxCommandEvent &)
908 {
909         if (!_film) {
910                 return;
911         }
912
913         _ignore_changes = Film::AUDIO_STREAM;
914         _film->set_audio_stream (_audio_stream->GetSelection ());
915         _ignore_changes = Film::NONE;
916
917         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
918         setup_audio_details ();
919 }
920
921 void
922 FilmEditor::subtitle_stream_changed (wxCommandEvent &)
923 {
924         if (!_film) {
925                 return;
926         }
927
928         _ignore_changes = Film::SUBTITLE_STREAM;
929         _film->set_subtitle_stream (_subtitle_stream->GetSelection ());
930         _ignore_changes = Film::NONE;
931 }
932
933 void
934 FilmEditor::setup_audio_details ()
935 {
936         if (_film->audio_channels() == 0 && _film->audio_sample_rate() == 0) {
937                 _audio->SetLabel (wxT (""));
938         } else {
939                 stringstream s;
940                 s << _film->audio_channels () << " channels, " << _film->audio_sample_rate() << "Hz";
941                 _audio->SetLabel (std_to_wx (s.str ()));
942         }
943 }
944
945 void
946 FilmEditor::active_jobs_changed (bool a)
947 {
948         set_things_sensitive (!a);
949 }