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