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