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