Fix shared_ptr for Film.
[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         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
381 }
382
383 void
384 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
385 {
386         if (!_film) {
387                 return;
388         }
389
390         _ignore_changes = Film::SUBTITLE_OFFSET;
391         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
392         _ignore_changes = Film::NONE;
393 }
394
395 void
396 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
397 {
398         if (!_film) {
399                 return;
400         }
401
402         _ignore_changes = Film::SUBTITLE_OFFSET;
403         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
404         _ignore_changes = Film::NONE;
405 }
406
407
408 /** Called when the metadata stored in the Film object has changed;
409  *  so that we can update the GUI.
410  *  @param p Property of the Film that has changed.
411  */
412 void
413 FilmEditor::film_changed (Film::Property p)
414 {
415         ensure_ui_thread ();
416         
417         if (!_film || _ignore_changes == p) {
418                 return;
419         }
420
421         stringstream s;
422                 
423         switch (p) {
424         case Film::NONE:
425                 break;
426         case Film::CONTENT:
427                 _content->SetPath (std_to_wx (_film->content ()));
428                 setup_visibility ();
429                 setup_formats ();
430                 setup_subtitle_button ();
431                 setup_streams ();
432                 break;
433         case Film::HAS_SUBTITLES:
434                 setup_subtitle_button ();
435                 setup_streams ();
436                 break;
437         case Film::AUDIO_STREAMS:
438         case Film::SUBTITLE_STREAMS:
439                 setup_streams ();
440                 break;
441         case Film::FORMAT:
442         {
443                 int n = 0;
444                 vector<Format const *>::iterator i = _formats.begin ();
445                 while (i != _formats.end() && *i != _film->format ()) {
446                         ++i;
447                         ++n;
448                 }
449                 _format->SetSelection (n);
450                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
451                 break;
452         }
453         case Film::CROP:
454                 _left_crop->SetValue (_film->crop().left);
455                 _right_crop->SetValue (_film->crop().right);
456                 _top_crop->SetValue (_film->crop().top);
457                 _bottom_crop->SetValue (_film->crop().bottom);
458                 break;
459         case Film::FILTERS:
460         {
461                 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
462                 if (p.first.empty () && p.second.empty ()) {
463                         _filters->SetLabel (_("None"));
464                 } else {
465                         string const b = p.first + " " + p.second;
466                         _filters->SetLabel (std_to_wx (b));
467                 }
468                 _sizer->Layout ();
469                 break;
470         }
471         case Film::NAME:
472                 _name->ChangeValue (std_to_wx (_film->name ()));
473                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
474                 break;
475         case Film::FRAMES_PER_SECOND:
476                 s << fixed << setprecision(2) << _film->frames_per_second();
477                 _frames_per_second->SetLabel (std_to_wx (s.str ()));
478                 break;
479         case Film::AUDIO_SAMPLE_RATE:
480                 setup_audio_details ();
481                 break;
482         case Film::SIZE:
483                 if (_film->size().width == 0 && _film->size().height == 0) {
484                         _original_size->SetLabel (wxT (""));
485                 } else {
486                         s << _film->size().width << " x " << _film->size().height;
487                         _original_size->SetLabel (std_to_wx (s.str ()));
488                 }
489                 break;
490         case Film::LENGTH:
491                 if (_film->frames_per_second() > 0 && _film->length()) {
492                         s << _film->length().get() << " frames; " << seconds_to_hms (_film->length().get() / _film->frames_per_second());
493                 } else if (_film->length()) {
494                         s << _film->length().get() << " frames";
495                 } 
496                 _length->SetLabel (std_to_wx (s.str ()));
497                 break;
498         case Film::DCP_CONTENT_TYPE:
499                 _dcp_content_type->SetSelection (DCPContentType::as_index (_film->dcp_content_type ()));
500                 _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
501                 break;
502         case Film::THUMBS:
503                 break;
504         case Film::DCP_FRAMES:
505                 if (_film->dcp_frames() == 0) {
506                         _dcp_range->SetLabel (wxT ("Whole film"));
507                 } else {
508                         stringstream s;
509                         s << "First " << _film->dcp_frames() << " frames";
510                         _dcp_range->SetLabel (std_to_wx (s.str ()));
511                 }
512                 _sizer->Layout ();
513                 break;
514         case Film::DCP_TRIM_ACTION:
515                 break;
516         case Film::DCP_AB:
517                 _dcp_ab->SetValue (_film->dcp_ab ());
518                 break;
519         case Film::SCALER:
520                 _scaler->SetSelection (Scaler::as_index (_film->scaler ()));
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_FRAMES);
623         film_changed (Film::DCP_TRIM_ACTION);
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::SUBTITLE_OFFSET);
635         film_changed (Film::SUBTITLE_SCALE);
636         film_changed (Film::USE_DCI_NAME);
637         film_changed (Film::DCI_METADATA);
638 }
639
640 /** Updates the sensitivity of lots of widgets to a given value.
641  *  @param s true to make sensitive, false to make insensitive.
642  */
643 void
644 FilmEditor::set_things_sensitive (bool s)
645 {
646         _name->Enable (s);
647         _use_dci_name->Enable (s);
648         _edit_dci_button->Enable (s);
649         _frames_per_second->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         _dcp_content_type->Enable (s);
659         _dcp_range->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         _still_duration->Enable (s);
666         _with_subtitles->Enable (s);
667         _subtitle_offset->Enable (s);
668         _subtitle_scale->Enable (s);
669 }
670
671 /** Called when the `Edit filters' button has been clicked */
672 void
673 FilmEditor::edit_filters_clicked (wxCommandEvent &)
674 {
675         FilterDialog* d = new FilterDialog (this, _film->filters());
676         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
677         d->ShowModal ();
678         d->Destroy ();
679 }
680
681 /** Called when the scaler widget has been changed */
682 void
683 FilmEditor::scaler_changed (wxCommandEvent &)
684 {
685         if (!_film) {
686                 return;
687         }
688
689         _ignore_changes = Film::SCALER;
690         int const n = _scaler->GetSelection ();
691         if (n >= 0) {
692                 _film->set_scaler (Scaler::from_index (n));
693         }
694         _ignore_changes = Film::NONE;
695 }
696
697 void
698 FilmEditor::audio_gain_changed (wxCommandEvent &)
699 {
700         if (!_film) {
701                 return;
702         }
703
704         _ignore_changes = Film::AUDIO_GAIN;
705         _film->set_audio_gain (_audio_gain->GetValue ());
706         _ignore_changes = Film::NONE;
707 }
708
709 void
710 FilmEditor::audio_delay_changed (wxCommandEvent &)
711 {
712         if (!_film) {
713                 return;
714         }
715
716         _ignore_changes = Film::AUDIO_DELAY;
717         _film->set_audio_delay (_audio_delay->GetValue ());
718         _ignore_changes = Film::NONE;
719 }
720
721 wxControl *
722 FilmEditor::video_control (wxControl* c)
723 {
724         _video_controls.push_back (c);
725         return c;
726 }
727
728 wxControl *
729 FilmEditor::still_control (wxControl* c)
730 {
731         _still_controls.push_back (c);
732         return c;
733 }
734
735 void
736 FilmEditor::setup_visibility ()
737 {
738         ContentType c = VIDEO;
739
740         if (_film) {
741                 c = _film->content_type ();
742         }
743
744         for (list<wxControl*>::iterator i = _video_controls.begin(); i != _video_controls.end(); ++i) {
745                 (*i)->Show (c == VIDEO);
746         }
747
748         for (list<wxControl*>::iterator i = _still_controls.begin(); i != _still_controls.end(); ++i) {
749                 (*i)->Show (c == STILL);
750         }
751
752         _sizer->Layout ();
753 }
754
755 void
756 FilmEditor::still_duration_changed (wxCommandEvent &)
757 {
758         if (!_film) {
759                 return;
760         }
761
762         _ignore_changes = Film::STILL_DURATION;
763         _film->set_still_duration (_still_duration->GetValue ());
764         _ignore_changes = Film::NONE;
765 }
766
767 void
768 FilmEditor::change_dcp_range_clicked (wxCommandEvent &)
769 {
770         DCPRangeDialog* d = new DCPRangeDialog (this, _film);
771         d->Changed.connect (bind (&FilmEditor::dcp_range_changed, this, _1, _2));
772         d->ShowModal ();
773 }
774
775 void
776 FilmEditor::dcp_range_changed (int frames, TrimAction action)
777 {
778         _film->set_dcp_frames (frames);
779         _film->set_dcp_trim_action (action);
780 }
781
782 void
783 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
784 {
785         GainCalculatorDialog* d = new GainCalculatorDialog (this);
786         d->ShowModal ();
787
788         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
789                 d->Destroy ();
790                 return;
791         }
792         
793         _audio_gain->SetValue (
794                 Config::instance()->sound_processor()->db_for_fader_change (
795                         d->wanted_fader (),
796                         d->actual_fader ()
797                         )
798                 );
799
800         /* This appears to be necessary, as the change is not signalled,
801            I think.
802         */
803         wxCommandEvent dummy;
804         audio_gain_changed (dummy);
805         
806         d->Destroy ();
807 }
808
809 void
810 FilmEditor::setup_formats ()
811 {
812         ContentType c = VIDEO;
813
814         if (_film) {
815                 c = _film->content_type ();
816         }
817         
818         _formats.clear ();
819
820         vector<Format const *> fmt = Format::all ();
821         for (vector<Format const *>::iterator i = fmt.begin(); i != fmt.end(); ++i) {
822                 if (c == VIDEO && dynamic_cast<FixedFormat const *> (*i)) {
823                         _formats.push_back (*i);
824                 } else if (c == STILL && dynamic_cast<VariableFormat const *> (*i)) {
825                         _formats.push_back (*i);
826                 }
827         }
828
829         _format->Clear ();
830         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
831                 _format->Append (std_to_wx ((*i)->name ()));
832         }
833
834         _sizer->Layout ();
835 }
836
837 void
838 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
839 {
840         if (!_film) {
841                 return;
842         }
843
844         _ignore_changes = Film::WITH_SUBTITLES;
845         _film->set_with_subtitles (_with_subtitles->GetValue ());
846         _ignore_changes = Film::NONE;
847
848         _subtitle_scale->Enable (_film->with_subtitles ());
849         _subtitle_offset->Enable (_film->with_subtitles ());
850 }
851
852 void
853 FilmEditor::setup_subtitle_button ()
854 {
855         _with_subtitles->Enable (_film->has_subtitles ());
856         if (!_film->has_subtitles ()) {
857                 _with_subtitles->SetValue (false);
858         }
859 }
860
861 void
862 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
863 {
864         if (!_film) {
865                 return;
866         }
867
868         _ignore_changes = Film::USE_DCI_NAME;
869         _film->set_use_dci_name (_use_dci_name->GetValue ());
870         _ignore_changes = Film::NONE;
871
872         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
873 }
874
875 void
876 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
877 {
878         if (!_film) {
879                 return;
880         }
881
882         DCINameDialog* d = new DCINameDialog (this, _film);
883         d->ShowModal ();
884         d->Destroy ();
885 }
886
887 void
888 FilmEditor::setup_streams ()
889 {
890         _audio_stream->Clear ();
891         vector<AudioStream> a = _film->audio_streams ();
892         for (vector<AudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
893                 _audio_stream->Append (std_to_wx (i->name()));
894         }
895         _audio_stream->SetSelection (_film->audio_stream_index ());
896
897         _subtitle_stream->Clear ();
898         vector<SubtitleStream> s = _film->subtitle_streams ();
899         for (vector<SubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
900                 _subtitle_stream->Append (std_to_wx (i->name()));
901         }
902         _subtitle_stream->SetSelection (_film->subtitle_stream_index ());
903 }
904
905 void
906 FilmEditor::audio_stream_changed (wxCommandEvent &)
907 {
908         if (!_film) {
909                 return;
910         }
911
912         _ignore_changes = Film::AUDIO_STREAM;
913         _film->set_audio_stream (_audio_stream->GetSelection ());
914         _ignore_changes = Film::NONE;
915
916         _dcp_name->SetLabel (std_to_wx (_film->dcp_name ()));
917         setup_audio_details ();
918 }
919
920 void
921 FilmEditor::subtitle_stream_changed (wxCommandEvent &)
922 {
923         if (!_film) {
924                 return;
925         }
926
927         _ignore_changes = Film::SUBTITLE_STREAM;
928         _film->set_subtitle_stream (_subtitle_stream->GetSelection ());
929         _ignore_changes = Film::NONE;
930 }
931
932 void
933 FilmEditor::setup_audio_details ()
934 {
935         if (_film->audio_channels() == 0 && _film->audio_sample_rate() == 0) {
936                 _audio->SetLabel (wxT (""));
937         } else {
938                 stringstream s;
939                 s << _film->audio_channels () << " channels, " << _film->audio_sample_rate() << "Hz";
940                 _audio->SetLabel (std_to_wx (s.str ()));
941         }
942 }
943
944 void
945 FilmEditor::active_jobs_changed (bool a)
946 {
947         set_things_sensitive (!a);
948 }