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