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