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