Remove trim action settings.
[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_FRAMES:
506                 if (!_film->dcp_frames()) {
507                         _dcp_range->SetLabel (wxT ("Whole film"));
508                 } else {
509                         _dcp_range->SetLabel (std_to_wx (String::compose ("First %1 frames", _film->dcp_frames().get())));
510                 }
511                 _sizer->Layout ();
512                 break;
513         case Film::DCP_AB:
514                 _dcp_ab->SetValue (_film->dcp_ab ());
515                 break;
516         case Film::SCALER:
517                 _scaler->SetSelection (Scaler::as_index (_film->scaler ()));
518                 break;
519         case Film::AUDIO_GAIN:
520                 _audio_gain->SetValue (_film->audio_gain ());
521                 break;
522         case Film::AUDIO_DELAY:
523                 _audio_delay->SetValue (_film->audio_delay ());
524                 break;
525         case Film::STILL_DURATION:
526                 _still_duration->SetValue (_film->still_duration ());
527                 break;
528         case Film::WITH_SUBTITLES:
529                 _with_subtitles->SetValue (_film->with_subtitles ());
530                 _subtitle_scale->Enable (_film->with_subtitles ());
531                 _subtitle_offset->Enable (_film->with_subtitles ());
532                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
533                 break;
534         case Film::SUBTITLE_OFFSET:
535                 _subtitle_offset->SetValue (_film->subtitle_offset ());
536                 break;
537         case Film::SUBTITLE_SCALE:
538                 _subtitle_scale->SetValue (_film->subtitle_scale() * 100);
539                 break;
540         case Film::USE_DCI_NAME:
541                 _use_dci_name->SetValue (_film->use_dci_name ());
542                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
543                 break;
544         case Film::DCI_METADATA:
545                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
546                 break;
547         case Film::AUDIO_STREAM:
548                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
549                 _audio_stream->SetSelection (_film->audio_stream_index ());
550                 setup_audio_details ();
551                 break;
552         case Film::SUBTITLE_STREAM:
553                 _subtitle_stream->SetSelection (_film->subtitle_stream_index ());
554                 break;
555         }
556 }
557
558 /** Called when the format widget has been changed */
559 void
560 FilmEditor::format_changed (wxCommandEvent &)
561 {
562         if (!_film) {
563                 return;
564         }
565
566         _ignore_changes = Film::FORMAT;
567         int const n = _format->GetSelection ();
568         if (n >= 0) {
569                 assert (n < int (_formats.size()));
570                 _film->set_format (_formats[n]);
571         }
572         _ignore_changes = Film::NONE;
573
574         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
575 }
576
577 /** Called when the DCP content type widget has been changed */
578 void
579 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
580 {
581         if (!_film) {
582                 return;
583         }
584
585         _ignore_changes = Film::DCP_CONTENT_TYPE;
586         int const n = _dcp_content_type->GetSelection ();
587         if (n >= 0) {
588                 _film->set_dcp_content_type (DCPContentType::from_index (n));
589         }
590         _ignore_changes = Film::NONE;
591
592         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
593 }
594
595 /** Sets the Film that we are editing */
596 void
597 FilmEditor::set_film (shared_ptr<Film> f)
598 {
599         _film = f;
600
601         set_things_sensitive (_film != 0);
602
603         if (_film) {
604                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
605         }
606
607         if (_film) {
608                 FileChanged (_film->directory ());
609         } else {
610                 FileChanged ("");
611         }
612         
613         film_changed (Film::NAME);
614         film_changed (Film::CONTENT);
615         film_changed (Film::DCP_CONTENT_TYPE);
616         film_changed (Film::FORMAT);
617         film_changed (Film::CROP);
618         film_changed (Film::FILTERS);
619         film_changed (Film::DCP_FRAMES);
620         film_changed (Film::DCP_AB);
621         film_changed (Film::SIZE);
622         film_changed (Film::LENGTH);
623         film_changed (Film::FRAMES_PER_SECOND);
624         film_changed (Film::AUDIO_SAMPLE_RATE);
625         film_changed (Film::SCALER);
626         film_changed (Film::AUDIO_GAIN);
627         film_changed (Film::AUDIO_DELAY);
628         film_changed (Film::STILL_DURATION);
629         film_changed (Film::WITH_SUBTITLES);
630         film_changed (Film::SUBTITLE_OFFSET);
631         film_changed (Film::SUBTITLE_SCALE);
632         film_changed (Film::USE_DCI_NAME);
633         film_changed (Film::DCI_METADATA);
634 }
635
636 /** Updates the sensitivity of lots of widgets to a given value.
637  *  @param s true to make sensitive, false to make insensitive.
638  */
639 void
640 FilmEditor::set_things_sensitive (bool s)
641 {
642         _name->Enable (s);
643         _use_dci_name->Enable (s);
644         _edit_dci_button->Enable (s);
645         _format->Enable (s);
646         _content->Enable (s);
647         _left_crop->Enable (s);
648         _right_crop->Enable (s);
649         _top_crop->Enable (s);
650         _bottom_crop->Enable (s);
651         _filters_button->Enable (s);
652         _scaler->Enable (s);
653         _audio_stream->Enable (s);
654         _dcp_content_type->Enable (s);
655         _change_dcp_range_button->Enable (s);
656         _dcp_ab->Enable (s);
657         _audio_gain->Enable (s);
658         _audio_gain_calculate_button->Enable (s);
659         _audio_delay->Enable (s);
660         _subtitle_stream->Enable (s);
661         _still_duration->Enable (s);
662         _with_subtitles->Enable (s);
663         _subtitle_offset->Enable (s);
664         _subtitle_scale->Enable (s);
665 }
666
667 /** Called when the `Edit filters' button has been clicked */
668 void
669 FilmEditor::edit_filters_clicked (wxCommandEvent &)
670 {
671         FilterDialog* d = new FilterDialog (this, _film->filters());
672         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
673         d->ShowModal ();
674         d->Destroy ();
675 }
676
677 /** Called when the scaler widget has been changed */
678 void
679 FilmEditor::scaler_changed (wxCommandEvent &)
680 {
681         if (!_film) {
682                 return;
683         }
684
685         _ignore_changes = Film::SCALER;
686         int const n = _scaler->GetSelection ();
687         if (n >= 0) {
688                 _film->set_scaler (Scaler::from_index (n));
689         }
690         _ignore_changes = Film::NONE;
691 }
692
693 void
694 FilmEditor::audio_gain_changed (wxCommandEvent &)
695 {
696         if (!_film) {
697                 return;
698         }
699
700         _ignore_changes = Film::AUDIO_GAIN;
701         _film->set_audio_gain (_audio_gain->GetValue ());
702         _ignore_changes = Film::NONE;
703 }
704
705 void
706 FilmEditor::audio_delay_changed (wxCommandEvent &)
707 {
708         if (!_film) {
709                 return;
710         }
711
712         _ignore_changes = Film::AUDIO_DELAY;
713         _film->set_audio_delay (_audio_delay->GetValue ());
714         _ignore_changes = Film::NONE;
715 }
716
717 wxControl *
718 FilmEditor::video_control (wxControl* c)
719 {
720         _video_controls.push_back (c);
721         return c;
722 }
723
724 wxControl *
725 FilmEditor::still_control (wxControl* c)
726 {
727         _still_controls.push_back (c);
728         return c;
729 }
730
731 void
732 FilmEditor::setup_visibility ()
733 {
734         ContentType c = VIDEO;
735
736         if (_film) {
737                 c = _film->content_type ();
738         }
739
740         for (list<wxControl*>::iterator i = _video_controls.begin(); i != _video_controls.end(); ++i) {
741                 (*i)->Show (c == VIDEO);
742         }
743
744         for (list<wxControl*>::iterator i = _still_controls.begin(); i != _still_controls.end(); ++i) {
745                 (*i)->Show (c == STILL);
746         }
747
748         _sizer->Layout ();
749 }
750
751 void
752 FilmEditor::still_duration_changed (wxCommandEvent &)
753 {
754         if (!_film) {
755                 return;
756         }
757
758         _ignore_changes = Film::STILL_DURATION;
759         _film->set_still_duration (_still_duration->GetValue ());
760         _ignore_changes = Film::NONE;
761 }
762
763 void
764 FilmEditor::change_dcp_range_clicked (wxCommandEvent &)
765 {
766         DCPRangeDialog* d = new DCPRangeDialog (this, _film);
767         d->Changed.connect (bind (&FilmEditor::dcp_range_changed, this, _1));
768         d->ShowModal ();
769 }
770
771 void
772 FilmEditor::dcp_range_changed (int frames)
773 {
774         if (frames == 0) {
775                 _film->unset_dcp_frames ();
776         } else {
777                 _film->set_dcp_frames (frames);
778         }
779 }
780
781 void
782 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
783 {
784         GainCalculatorDialog* d = new GainCalculatorDialog (this);
785         d->ShowModal ();
786
787         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
788                 d->Destroy ();
789                 return;
790         }
791         
792         _audio_gain->SetValue (
793                 Config::instance()->sound_processor()->db_for_fader_change (
794                         d->wanted_fader (),
795                         d->actual_fader ()
796                         )
797                 );
798
799         /* This appears to be necessary, as the change is not signalled,
800            I think.
801         */
802         wxCommandEvent dummy;
803         audio_gain_changed (dummy);
804         
805         d->Destroy ();
806 }
807
808 void
809 FilmEditor::setup_formats ()
810 {
811         ContentType c = VIDEO;
812
813         if (_film) {
814                 c = _film->content_type ();
815         }
816         
817         _formats.clear ();
818
819         vector<Format const *> fmt = Format::all ();
820         for (vector<Format const *>::iterator i = fmt.begin(); i != fmt.end(); ++i) {
821                 if (c == VIDEO && dynamic_cast<FixedFormat const *> (*i)) {
822                         _formats.push_back (*i);
823                 } else if (c == STILL && dynamic_cast<VariableFormat const *> (*i)) {
824                         _formats.push_back (*i);
825                 }
826         }
827
828         _format->Clear ();
829         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
830                 _format->Append (std_to_wx ((*i)->name ()));
831         }
832
833         _sizer->Layout ();
834 }
835
836 void
837 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
838 {
839         if (!_film) {
840                 return;
841         }
842
843         _ignore_changes = Film::WITH_SUBTITLES;
844         _film->set_with_subtitles (_with_subtitles->GetValue ());
845         _ignore_changes = Film::NONE;
846
847         _subtitle_scale->Enable (_film->with_subtitles ());
848         _subtitle_offset->Enable (_film->with_subtitles ());
849 }
850
851 void
852 FilmEditor::setup_subtitle_button ()
853 {
854         _with_subtitles->Enable (_film->has_subtitles ());
855         if (!_film->has_subtitles ()) {
856                 _with_subtitles->SetValue (false);
857         }
858 }
859
860 void
861 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
862 {
863         if (!_film) {
864                 return;
865         }
866
867         _ignore_changes = Film::USE_DCI_NAME;
868         _film->set_use_dci_name (_use_dci_name->GetValue ());
869         _ignore_changes = Film::NONE;
870
871         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
872 }
873
874 void
875 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
876 {
877         if (!_film) {
878                 return;
879         }
880
881         DCINameDialog* d = new DCINameDialog (this, _film);
882         d->ShowModal ();
883         d->Destroy ();
884 }
885
886 void
887 FilmEditor::setup_streams ()
888 {
889         _audio_stream->Clear ();
890         vector<AudioStream> a = _film->audio_streams ();
891         for (vector<AudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
892                 _audio_stream->Append (std_to_wx (i->name()));
893         }
894         _audio_stream->SetSelection (_film->audio_stream_index ());
895
896         _subtitle_stream->Clear ();
897         vector<SubtitleStream> s = _film->subtitle_streams ();
898         for (vector<SubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
899                 _subtitle_stream->Append (std_to_wx (i->name()));
900         }
901         _subtitle_stream->SetSelection (_film->subtitle_stream_index ());
902 }
903
904 void
905 FilmEditor::audio_stream_changed (wxCommandEvent &)
906 {
907         if (!_film) {
908                 return;
909         }
910
911         _ignore_changes = Film::AUDIO_STREAM;
912         _film->set_audio_stream (_audio_stream->GetSelection ());
913         _ignore_changes = Film::NONE;
914
915         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
916         setup_audio_details ();
917 }
918
919 void
920 FilmEditor::subtitle_stream_changed (wxCommandEvent &)
921 {
922         if (!_film) {
923                 return;
924         }
925
926         _ignore_changes = Film::SUBTITLE_STREAM;
927         _film->set_subtitle_stream (_subtitle_stream->GetSelection ());
928         _ignore_changes = Film::NONE;
929 }
930
931 void
932 FilmEditor::setup_audio_details ()
933 {
934         if (_film->audio_channels() == 0 && _film->audio_sample_rate() == 0) {
935                 _audio->SetLabel (wxT (""));
936         } else {
937                 stringstream s;
938                 s << _film->audio_channels () << " channels, " << _film->audio_sample_rate() << "Hz";
939                 _audio->SetLabel (std_to_wx (s.str ()));
940         }
941 }
942
943 void
944 FilmEditor::active_jobs_changed (bool a)
945 {
946         set_things_sensitive (!a);
947 }