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