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