More various bits.
[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 <wx/notebook.h>
28 #include <wx/listctrl.h>
29 #include <boost/thread.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include "lib/format.h"
33 #include "lib/film.h"
34 #include "lib/transcode_job.h"
35 #include "lib/exceptions.h"
36 #include "lib/ab_transcode_job.h"
37 #include "lib/job_manager.h"
38 #include "lib/filter.h"
39 #include "lib/config.h"
40 #include "lib/ffmpeg_decoder.h"
41 #include "lib/imagemagick_content.h"
42 #include "lib/sndfile_content.h"
43 #include "filter_dialog.h"
44 #include "wx_util.h"
45 #include "film_editor.h"
46 #include "gain_calculator_dialog.h"
47 #include "sound_processor.h"
48 #include "dci_metadata_dialog.h"
49 #include "scaler.h"
50 #include "audio_dialog.h"
51
52 using std::string;
53 using std::cout;
54 using std::stringstream;
55 using std::pair;
56 using std::fixed;
57 using std::setprecision;
58 using std::list;
59 using std::vector;
60 using boost::shared_ptr;
61 using boost::dynamic_pointer_cast;
62
63 /** @param f Film to edit */
64 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
65         : wxPanel (parent)
66         , _film (f)
67         , _generally_sensitive (true)
68         , _audio_dialog (0)
69 {
70         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
71         _notebook = new wxNotebook (this, wxID_ANY);
72         s->Add (_notebook, 1);
73
74         make_film_panel ();
75         _notebook->AddPage (_film_panel, _("Film"), true);
76         make_content_panel ();
77         _notebook->AddPage (_content_panel, _("Content"), false);
78         make_video_panel ();
79         _notebook->AddPage (_video_panel, _("Video"), false);
80         make_audio_panel ();
81         _notebook->AddPage (_audio_panel, _("Audio"), false);
82         make_subtitle_panel ();
83         _notebook->AddPage (_subtitle_panel, _("Subtitles"), false);
84
85         set_film (_film);
86         connect_to_widgets ();
87
88         JobManager::instance()->ActiveJobsChanged.connect (
89                 bind (&FilmEditor::active_jobs_changed, this, _1)
90                 );
91         
92         setup_formats ();
93
94         SetSizerAndFit (s);
95 }
96
97 void
98 FilmEditor::make_film_panel ()
99 {
100         _film_panel = new wxPanel (_notebook);
101         _film_sizer = new wxBoxSizer (wxVERTICAL);
102         _film_panel->SetSizer (_film_sizer);
103
104         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
105         _film_sizer->Add (grid, 0, wxALL, 8);
106
107         int r = 0;
108         
109         add_label_to_grid_bag_sizer (grid, _film_panel, _("Name"), wxGBPosition (r, 0));
110         _name = new wxTextCtrl (_film_panel, wxID_ANY);
111         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
112         ++r;
113         
114         add_label_to_grid_bag_sizer (grid, _film_panel, _("DCP Name"), wxGBPosition (r, 0));
115         _dcp_name = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
116         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
117         ++r;
118
119         _use_dci_name = new wxCheckBox (_film_panel, wxID_ANY, _("Use DCI name"));
120         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
121         _edit_dci_button = new wxButton (_film_panel, wxID_ANY, _("Details..."));
122         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
123         ++r;
124
125         _trust_content_headers = new wxCheckBox (_film_panel, wxID_ANY, _("Trust content's header"));
126         grid->Add (_trust_content_headers, wxGBPosition (r, 0), wxGBSpan(1, 2));
127         ++r;
128
129         add_label_to_grid_bag_sizer (grid, _film_panel, _("Content Type"), wxGBPosition (r, 0));
130         _dcp_content_type = new wxChoice (_film_panel, wxID_ANY);
131         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
132         ++r;
133
134         {
135                 add_label_to_grid_bag_sizer (grid, _film_panel, _("DCP Frame Rate"), wxGBPosition (r, 0));
136                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
137                 _dcp_frame_rate = new wxChoice (_film_panel, wxID_ANY);
138                 s->Add (_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
139                 _best_dcp_frame_rate = new wxButton (_film_panel, wxID_ANY, _("Use best"));
140                 s->Add (_best_dcp_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 6);
141                 grid->Add (s, wxGBPosition (r, 1));
142         }
143         ++r;
144
145         _frame_rate_description = new wxStaticText (_film_panel, wxID_ANY, wxT (" \n "), wxDefaultPosition, wxDefaultSize);
146         grid->Add (_frame_rate_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
147         wxFont font = _frame_rate_description->GetFont();
148         font.SetStyle(wxFONTSTYLE_ITALIC);
149         font.SetPointSize(font.GetPointSize() - 1);
150         _frame_rate_description->SetFont(font);
151         ++r;
152         
153         add_label_to_grid_bag_sizer (grid, _film_panel, _("Length"), wxGBPosition (r, 0));
154         _length = new wxStaticText (_film_panel, wxID_ANY, wxT (""));
155         grid->Add (_length, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
156         ++r;
157
158
159         {
160                 add_label_to_grid_bag_sizer (grid, _film_panel, _("Trim frames"), wxGBPosition (r, 0));
161                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
162                 add_label_to_sizer (s, _film_panel, _("Start"));
163                 _trim_start = new wxSpinCtrl (_film_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
164                 s->Add (_trim_start);
165                 add_label_to_sizer (s, _film_panel, _("End"));
166                 _trim_end = new wxSpinCtrl (_film_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
167                 s->Add (_trim_end);
168
169                 grid->Add (s, wxGBPosition (r, 1));
170         }
171         ++r;
172
173         _ab = new wxCheckBox (_film_panel, wxID_ANY, _("A/B"));
174         grid->Add (_ab, wxGBPosition (r, 0));
175         ++r;
176
177         vector<DCPContentType const *> const ct = DCPContentType::all ();
178         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
179                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
180         }
181
182         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
183         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
184                 _dcp_frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
185         }
186 }
187
188 void
189 FilmEditor::connect_to_widgets ()
190 {
191         _name->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (FilmEditor::name_changed), 0, this);
192         _use_dci_name->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::use_dci_name_toggled), 0, this);
193         _edit_dci_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
194         _format->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::format_changed), 0, this);
195         _trust_content_headers->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::trust_content_headers_changed), 0, this);
196         _content->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (FilmEditor::content_item_selected), 0, this);
197         _content_add->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_add_clicked), 0, this);
198         _content_remove->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_remove_clicked), 0, this);
199         _content_earlier->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_earlier_clicked), 0, this);
200         _content_later->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_later_clicked), 0, this);
201         _left_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
202         _right_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::right_crop_changed), 0, this);
203         _top_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::top_crop_changed), 0, this);
204         _bottom_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::bottom_crop_changed), 0, this);
205         _filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::edit_filters_clicked), 0, this);
206         _scaler->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::scaler_changed), 0, this);
207         _dcp_content_type->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::dcp_content_type_changed), 0, this);
208         _dcp_frame_rate->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::dcp_frame_rate_changed), 0, this);
209         _best_dcp_frame_rate->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::best_dcp_frame_rate_clicked), 0, this);
210         _ab->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::ab_toggled), 0, this);
211         _trim_start->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::trim_start_changed), 0, this);
212         _trim_end->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::trim_end_changed), 0, this);
213         _with_subtitles->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::with_subtitles_toggled), 0, this);
214         _subtitle_offset->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::subtitle_offset_changed), 0, this);
215         _subtitle_scale->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::subtitle_scale_changed), 0, this);
216         _colour_lut->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::colour_lut_changed), 0, this);
217         _j2k_bandwidth->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::j2k_bandwidth_changed), 0, this);
218         _ffmpeg_subtitle_stream->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::ffmpeg_subtitle_stream_changed), 0, this);
219         _ffmpeg_audio_stream->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::ffmpeg_audio_stream_changed), 0, this);
220         _audio_gain->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::audio_gain_changed), 0, this);
221         _audio_gain_calculate_button->Connect (
222                 wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::audio_gain_calculate_button_clicked), 0, this
223                 );
224         _show_audio->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::show_audio_clicked), 0, this);
225         _audio_delay->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::audio_delay_changed), 0, this);
226 }
227
228 void
229 FilmEditor::make_video_panel ()
230 {
231         _video_panel = new wxPanel (_notebook);
232         _video_sizer = new wxBoxSizer (wxVERTICAL);
233         _video_panel->SetSizer (_video_sizer);
234         
235         wxGridBagSizer* grid = new wxGridBagSizer (4, 4);
236         _video_sizer->Add (grid, 0, wxALL, 8);
237
238         int r = 0;
239         add_label_to_grid_bag_sizer (grid, _video_panel, _("Format"), wxGBPosition (r, 0));
240         _format = new wxChoice (_video_panel, wxID_ANY);
241         grid->Add (_format, wxGBPosition (r, 1));
242         ++r;
243
244         _format_description = new wxStaticText (_video_panel, wxID_ANY, wxT (""), wxDefaultPosition, wxDefaultSize);
245         grid->Add (_format_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
246         wxFont font = _format_description->GetFont();
247         font.SetStyle(wxFONTSTYLE_ITALIC);
248         font.SetPointSize(font.GetPointSize() - 1);
249         _format_description->SetFont(font);
250         ++r;
251
252         add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), wxGBPosition (r, 0));
253         _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
254         grid->Add (_left_crop, wxGBPosition (r, 1));
255         ++r;
256
257         add_label_to_grid_bag_sizer (grid, _video_panel, _("Right crop"), wxGBPosition (r, 0));
258         _right_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
259         grid->Add (_right_crop, wxGBPosition (r, 1));
260         ++r;
261         
262         add_label_to_grid_bag_sizer (grid, _video_panel, _("Top crop"), wxGBPosition (r, 0));
263         _top_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
264         grid->Add (_top_crop, wxGBPosition (r, 1));
265         ++r;
266         
267         add_label_to_grid_bag_sizer (grid, _video_panel, _("Bottom crop"), wxGBPosition (r, 0));
268         _bottom_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
269         grid->Add (_bottom_crop, wxGBPosition (r, 1));
270         ++r;
271
272         /* VIDEO-only stuff */
273         {
274                 add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), wxGBPosition (r, 0));
275                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
276                 _filters = new wxStaticText (_video_panel, wxID_ANY, _("None"));
277                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
278                 _filters_button = new wxButton (_video_panel, wxID_ANY, _("Edit..."));
279                 s->Add (_filters_button, 0);
280                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
281         }
282         ++r;
283
284         add_label_to_grid_bag_sizer (grid, _video_panel, _("Scaler"), wxGBPosition (r, 0));
285         _scaler = new wxChoice (_video_panel, wxID_ANY);
286         grid->Add (_scaler, wxGBPosition (r, 1));
287         ++r;
288
289         vector<Scaler const *> const sc = Scaler::all ();
290         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
291                 _scaler->Append (std_to_wx ((*i)->name()));
292         }
293
294         add_label_to_grid_bag_sizer (grid, _video_panel, _("Colour look-up table"), wxGBPosition (r, 0));
295         _colour_lut = new wxChoice (_video_panel, wxID_ANY);
296         for (int i = 0; i < 2; ++i) {
297                 _colour_lut->Append (std_to_wx (colour_lut_index_to_name (i)));
298         }
299         _colour_lut->SetSelection (0);
300         grid->Add (_colour_lut, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
301         ++r;
302
303         {
304                 add_label_to_grid_bag_sizer (grid, _video_panel, _("JPEG2000 bandwidth"), wxGBPosition (r, 0));
305                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
306                 _j2k_bandwidth = new wxSpinCtrl (_video_panel, wxID_ANY);
307                 s->Add (_j2k_bandwidth, 1);
308                 add_label_to_sizer (s, _video_panel, _("MBps"));
309                 grid->Add (s, wxGBPosition (r, 1));
310         }
311         ++r;
312
313         _left_crop->SetRange (0, 1024);
314         _top_crop->SetRange (0, 1024);
315         _right_crop->SetRange (0, 1024);
316         _bottom_crop->SetRange (0, 1024);
317         _trim_start->SetRange (0, 100);
318         _trim_end->SetRange (0, 100);
319         _j2k_bandwidth->SetRange (50, 250);
320 }
321
322 void
323 FilmEditor::make_content_panel ()
324 {
325         _content_panel = new wxPanel (_notebook);
326         _content_sizer = new wxBoxSizer (wxVERTICAL);
327         _content_panel->SetSizer (_content_sizer);
328         
329         {
330                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
331                 
332                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
333                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
334
335                 _content->InsertColumn (0, "");
336                 _content->SetColumnWidth (0, 512);
337
338                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
339                 _content_add = new wxButton (_content_panel, wxID_ANY, _("Add..."));
340                 b->Add (_content_add);
341                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
342                 b->Add (_content_remove);
343                 _content_earlier = new wxButton (_content_panel, wxID_ANY, _("Earlier"));
344                 b->Add (_content_earlier);
345                 _content_later = new wxButton (_content_panel, wxID_ANY, _("Later"));
346                 b->Add (_content_later);
347
348                 s->Add (b, 0, wxALL, 4);
349
350                 _content_sizer->Add (s, 1, wxEXPAND | wxALL, 6);
351         }
352
353         _content_information = new wxTextCtrl (_content_panel, wxID_ANY, wxT ("\n\n\n\n"), wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxTE_MULTILINE);
354         _content_sizer->Add (_content_information, 1, wxEXPAND | wxALL, 6);
355 }
356
357 void
358 FilmEditor::make_audio_panel ()
359 {
360         _audio_panel = new wxPanel (_notebook);
361         _audio_sizer = new wxBoxSizer (wxVERTICAL);
362         _audio_panel->SetSizer (_audio_sizer);
363         
364         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
365         _audio_sizer->Add (grid, 0, wxALL, 8);
366
367         _show_audio = new wxButton (_audio_panel, wxID_ANY, _("Show Audio..."));
368         grid->Add (_show_audio, 1);
369         grid->AddSpacer (0);
370
371         {
372                 add_label_to_sizer (grid, _audio_panel, _("Audio Gain"));
373                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
374                 _audio_gain = new wxSpinCtrl (_audio_panel);
375                 s->Add (_audio_gain, 1);
376                 add_label_to_sizer (s, _audio_panel, _("dB"));
377                 _audio_gain_calculate_button = new wxButton (_audio_panel, wxID_ANY, _("Calculate..."));
378                 s->Add (_audio_gain_calculate_button, 1, wxEXPAND);
379                 grid->Add (s);
380         }
381
382         {
383                 add_label_to_sizer (grid, _audio_panel, _("Audio Delay"));
384                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
385                 _audio_delay = new wxSpinCtrl (_audio_panel);
386                 s->Add (_audio_delay, 1);
387                 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
388                 add_label_to_sizer (s, _audio_panel, _("ms"));
389                 grid->Add (s);
390         }
391
392         {
393                 add_label_to_sizer (grid, _audio_panel, _("Audio Stream"));
394                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
395                 _ffmpeg_audio_stream = new wxChoice (_audio_panel, wxID_ANY);
396                 s->Add (_ffmpeg_audio_stream, 1);
397                 _audio = new wxStaticText (_audio_panel, wxID_ANY, wxT (""));
398                 s->Add (_audio, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
399                 grid->Add (s, 1, wxEXPAND);
400         }
401
402         _audio_gain->SetRange (-60, 60);
403         _audio_delay->SetRange (-1000, 1000);
404 }
405
406 void
407 FilmEditor::make_subtitle_panel ()
408 {
409         _subtitle_panel = new wxPanel (_notebook);
410         _subtitle_sizer = new wxBoxSizer (wxVERTICAL);
411         _subtitle_panel->SetSizer (_subtitle_sizer);
412         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
413         _subtitle_sizer->Add (grid, 0, wxALL, 8);
414
415         _with_subtitles = new wxCheckBox (_subtitle_panel, wxID_ANY, _("With Subtitles"));
416         grid->Add (_with_subtitles, 1);
417         
418         _ffmpeg_subtitle_stream = new wxChoice (_subtitle_panel, wxID_ANY);
419         grid->Add (_ffmpeg_subtitle_stream);
420
421         add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"));
422         _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
423         grid->Add (_subtitle_offset, 1);
424
425         {
426                 add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale"));
427                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
428                 _subtitle_scale = new wxSpinCtrl (_subtitle_panel);
429                 s->Add (_subtitle_scale);
430                 add_label_to_sizer (s, _subtitle_panel, _("%"));
431                 grid->Add (s);
432         }
433
434         _subtitle_offset->SetRange (-1024, 1024);
435         _subtitle_scale->SetRange (1, 1000);
436 }
437
438 /** Called when the left crop widget has been changed */
439 void
440 FilmEditor::left_crop_changed (wxCommandEvent &)
441 {
442         if (!_film) {
443                 return;
444         }
445
446         _film->set_left_crop (_left_crop->GetValue ());
447 }
448
449 /** Called when the right crop widget has been changed */
450 void
451 FilmEditor::right_crop_changed (wxCommandEvent &)
452 {
453         if (!_film) {
454                 return;
455         }
456
457         _film->set_right_crop (_right_crop->GetValue ());
458 }
459
460 /** Called when the top crop widget has been changed */
461 void
462 FilmEditor::top_crop_changed (wxCommandEvent &)
463 {
464         if (!_film) {
465                 return;
466         }
467
468         _film->set_top_crop (_top_crop->GetValue ());
469 }
470
471 /** Called when the bottom crop value has been changed */
472 void
473 FilmEditor::bottom_crop_changed (wxCommandEvent &)
474 {
475         if (!_film) {
476                 return;
477         }
478
479         _film->set_bottom_crop (_bottom_crop->GetValue ());
480 }
481
482 void
483 FilmEditor::trust_content_headers_changed (wxCommandEvent &)
484 {
485         if (!_film) {
486                 return;
487         }
488
489         _film->set_trust_content_headers (_trust_content_headers->GetValue ());
490 }
491
492 /** Called when the DCP A/B switch has been toggled */
493 void
494 FilmEditor::ab_toggled (wxCommandEvent &)
495 {
496         if (!_film) {
497                 return;
498         }
499         
500         _film->set_ab (_ab->GetValue ());
501 }
502
503 /** Called when the name widget has been changed */
504 void
505 FilmEditor::name_changed (wxCommandEvent &)
506 {
507         if (!_film) {
508                 return;
509         }
510
511         _film->set_name (string (_name->GetValue().mb_str()));
512 }
513
514 void
515 FilmEditor::subtitle_offset_changed (wxCommandEvent &)
516 {
517         if (!_film) {
518                 return;
519         }
520
521         _film->set_subtitle_offset (_subtitle_offset->GetValue ());
522 }
523
524 void
525 FilmEditor::subtitle_scale_changed (wxCommandEvent &)
526 {
527         if (!_film) {
528                 return;
529         }
530
531         _film->set_subtitle_scale (_subtitle_scale->GetValue() / 100.0);
532 }
533
534 void
535 FilmEditor::colour_lut_changed (wxCommandEvent &)
536 {
537         if (!_film) {
538                 return;
539         }
540         
541         _film->set_colour_lut (_colour_lut->GetSelection ());
542 }
543
544 void
545 FilmEditor::j2k_bandwidth_changed (wxCommandEvent &)
546 {
547         if (!_film) {
548                 return;
549         }
550         
551         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
552 }
553
554 void
555 FilmEditor::dcp_frame_rate_changed (wxCommandEvent &)
556 {
557         if (!_film) {
558                 return;
559         }
560
561         _film->set_dcp_frame_rate (
562                 boost::lexical_cast<int> (
563                         wx_to_std (_dcp_frame_rate->GetString (_dcp_frame_rate->GetSelection ()))
564                         )
565                 );
566 }
567
568
569 /** Called when the metadata stored in the Film object has changed;
570  *  so that we can update the GUI.
571  *  @param p Property of the Film that has changed.
572  */
573 void
574 FilmEditor::film_changed (Film::Property p)
575 {
576         ensure_ui_thread ();
577         
578         if (!_film) {
579                 return;
580         }
581
582         stringstream s;
583                 
584         switch (p) {
585         case Film::NONE:
586                 break;
587         case Film::CONTENT:
588                 setup_content ();
589                 setup_formats ();
590                 setup_format ();
591                 setup_subtitle_control_sensitivity ();
592                 setup_streams ();
593                 setup_show_audio_sensitivity ();
594                 setup_length ();
595                 break;
596         case Film::TRUST_CONTENT_HEADERS:
597                 checked_set (_trust_content_headers, _film->trust_content_headers ());
598                 break;
599         case Film::FORMAT:
600                 setup_format ();
601                 break;
602         case Film::CROP:
603                 checked_set (_left_crop, _film->crop().left);
604                 checked_set (_right_crop, _film->crop().right);
605                 checked_set (_top_crop, _film->crop().top);
606                 checked_set (_bottom_crop, _film->crop().bottom);
607                 break;
608         case Film::FILTERS:
609         {
610                 pair<string, string> p = Filter::ffmpeg_strings (_film->filters ());
611                 if (p.first.empty () && p.second.empty ()) {
612                         _filters->SetLabel (_("None"));
613                 } else {
614                         string const b = p.first + " " + p.second;
615                         _filters->SetLabel (std_to_wx (b));
616                 }
617                 _film_sizer->Layout ();
618                 break;
619         }
620         case Film::NAME:
621                 checked_set (_name, _film->name());
622                 setup_dcp_name ();
623                 break;
624         case Film::DCP_CONTENT_TYPE:
625                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
626                 setup_dcp_name ();
627                 break;
628         case Film::AB:
629                 checked_set (_ab, _film->ab ());
630                 break;
631         case Film::SCALER:
632                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
633                 break;
634         case Film::TRIM_START:
635                 checked_set (_trim_start, _film->trim_start());
636                 break;
637         case Film::TRIM_END:
638                 checked_set (_trim_end, _film->trim_end());
639                 break;
640         case Film::AUDIO_GAIN:
641                 checked_set (_audio_gain, _film->audio_gain ());
642                 break;
643         case Film::AUDIO_DELAY:
644                 checked_set (_audio_delay, _film->audio_delay ());
645                 break;
646         case Film::WITH_SUBTITLES:
647                 checked_set (_with_subtitles, _film->with_subtitles ());
648                 setup_subtitle_control_sensitivity ();
649                 setup_dcp_name ();
650                 break;
651         case Film::SUBTITLE_OFFSET:
652                 checked_set (_subtitle_offset, _film->subtitle_offset ());
653                 break;
654         case Film::SUBTITLE_SCALE:
655                 checked_set (_subtitle_scale, _film->subtitle_scale() * 100);
656                 break;
657         case Film::COLOUR_LUT:
658                 checked_set (_colour_lut, _film->colour_lut ());
659                 break;
660         case Film::J2K_BANDWIDTH:
661                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1e6);
662                 break;
663         case Film::USE_DCI_NAME:
664                 checked_set (_use_dci_name, _film->use_dci_name ());
665                 setup_dcp_name ();
666                 break;
667         case Film::DCI_METADATA:
668                 setup_dcp_name ();
669                 break;
670         case Film::DCP_FRAME_RATE:
671                 for (unsigned int i = 0; i < _dcp_frame_rate->GetCount(); ++i) {
672                         if (wx_to_std (_dcp_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->dcp_frame_rate())) {
673                                 if (_dcp_frame_rate->GetSelection() != int(i)) {
674                                         _dcp_frame_rate->SetSelection (i);
675                                         break;
676                                 }
677                         }
678                 }
679
680                 if (_film->video_frame_rate()) {
681                         _frame_rate_description->SetLabel (std_to_wx (FrameRateConversion (_film->video_frame_rate(), _film->dcp_frame_rate()).description));
682                         _best_dcp_frame_rate->Enable (best_dcp_frame_rate (_film->video_frame_rate ()) != _film->dcp_frame_rate ());
683                 } else {
684                         _frame_rate_description->SetLabel (wxT (""));
685                         _best_dcp_frame_rate->Disable ();
686                 }
687         }
688 }
689
690 void
691 FilmEditor::film_content_changed (int p)
692 {
693         if (!_film) {
694                 /* We call this method ourselves (as well as using it as a signal handler)
695                    so _film can be 0.
696                 */
697                 return;
698         }
699                 
700         if (p == FFmpegContentProperty::SUBTITLE_STREAMS) {
701                 setup_subtitle_control_sensitivity ();
702                 setup_streams ();
703         } else if (p == FFmpegContentProperty::AUDIO_STREAMS) {
704                 setup_streams ();
705                 setup_show_audio_sensitivity ();
706         } else if (p == VideoContentProperty::VIDEO_LENGTH) {
707                 setup_length ();
708                 setup_content_information ();
709         } else if (p == FFmpegContentProperty::AUDIO_STREAM) {
710                 if (_film->ffmpeg_audio_stream()) {
711                         checked_set (_ffmpeg_audio_stream, boost::lexical_cast<string> (_film->ffmpeg_audio_stream()->id));
712                 }
713                 setup_dcp_name ();
714                 setup_audio_details ();
715                 setup_show_audio_sensitivity ();
716         } else if (p == FFmpegContentProperty::SUBTITLE_STREAM) {
717                 if (_film->ffmpeg_subtitle_stream()) {
718                         checked_set (_ffmpeg_subtitle_stream, boost::lexical_cast<string> (_film->ffmpeg_subtitle_stream()->id));
719                 }
720         }
721 }
722
723 void
724 FilmEditor::setup_format ()
725 {
726         int n = 0;
727         vector<Format const *>::iterator i = _formats.begin ();
728         while (i != _formats.end() && *i != _film->format ()) {
729                 ++i;
730                 ++n;
731         }
732         if (i == _formats.end()) {
733                 checked_set (_format, -1);
734         } else {
735                 checked_set (_format, n);
736         }
737         setup_dcp_name ();
738         
739         if (_film->format ()) {
740                 _format_description->SetLabel (std_to_wx (_film->format()->description ()));
741         } else {
742                 _format_description->SetLabel (wxT (""));
743         }
744 }       
745
746 void
747 FilmEditor::setup_length ()
748 {
749         stringstream s;
750         if (_film->video_frame_rate() > 0 && _film->video_length()) {
751                 s << _film->video_length() << " "
752                   << wx_to_std (_("frames")) << "; " << seconds_to_hms (_film->video_length() / _film->video_frame_rate());
753         } else if (_film->video_length()) {
754                 s << _film->video_length() << " "
755                   << wx_to_std (_("frames"));
756         } 
757         _length->SetLabel (std_to_wx (s.str ()));
758         if (_film->video_length()) {
759                 _trim_start->SetRange (0, _film->video_length());
760                 _trim_end->SetRange (0, _film->video_length());
761         }
762 }       
763
764
765 /** Called when the format widget has been changed */
766 void
767 FilmEditor::format_changed (wxCommandEvent &)
768 {
769         if (!_film) {
770                 return;
771         }
772
773         int const n = _format->GetSelection ();
774         if (n >= 0) {
775                 assert (n < int (_formats.size()));
776                 _film->set_format (_formats[n]);
777         }
778 }
779
780 /** Called when the DCP content type widget has been changed */
781 void
782 FilmEditor::dcp_content_type_changed (wxCommandEvent &)
783 {
784         if (!_film) {
785                 return;
786         }
787
788         int const n = _dcp_content_type->GetSelection ();
789         if (n != wxNOT_FOUND) {
790                 _film->set_dcp_content_type (DCPContentType::from_index (n));
791         }
792 }
793
794 /** Sets the Film that we are editing */
795 void
796 FilmEditor::set_film (shared_ptr<Film> f)
797 {
798         _film = f;
799
800         set_things_sensitive (_film != 0);
801
802         if (_film) {
803                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
804                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1));
805         }
806
807         if (_film) {
808                 FileChanged (_film->directory ());
809         } else {
810                 FileChanged ("");
811         }
812
813         if (_audio_dialog) {
814                 _audio_dialog->set_film (_film);
815         }
816         
817         film_changed (Film::NAME);
818         film_changed (Film::USE_DCI_NAME);
819         film_changed (Film::CONTENT);
820         film_changed (Film::TRUST_CONTENT_HEADERS);
821         film_changed (Film::DCP_CONTENT_TYPE);
822         film_changed (Film::FORMAT);
823         film_changed (Film::CROP);
824         film_changed (Film::FILTERS);
825         film_changed (Film::SCALER);
826         film_changed (Film::TRIM_START);
827         film_changed (Film::TRIM_END);
828         film_changed (Film::AB);
829         film_changed (Film::AUDIO_GAIN);
830         film_changed (Film::AUDIO_DELAY);
831         film_changed (Film::WITH_SUBTITLES);
832         film_changed (Film::SUBTITLE_OFFSET);
833         film_changed (Film::SUBTITLE_SCALE);
834         film_changed (Film::COLOUR_LUT);
835         film_changed (Film::J2K_BANDWIDTH);
836         film_changed (Film::DCI_METADATA);
837         film_changed (Film::DCP_FRAME_RATE);
838
839         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
840         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAM);
841         film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
842         film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
843 }
844
845 /** Updates the sensitivity of lots of widgets to a given value.
846  *  @param s true to make sensitive, false to make insensitive.
847  */
848 void
849 FilmEditor::set_things_sensitive (bool s)
850 {
851         _generally_sensitive = s;
852         
853         _name->Enable (s);
854         _use_dci_name->Enable (s);
855         _edit_dci_button->Enable (s);
856         _format->Enable (s);
857         _content->Enable (s);
858         _trust_content_headers->Enable (s);
859         _content->Enable (s);
860         _left_crop->Enable (s);
861         _right_crop->Enable (s);
862         _top_crop->Enable (s);
863         _bottom_crop->Enable (s);
864         _filters_button->Enable (s);
865         _scaler->Enable (s);
866         _ffmpeg_audio_stream->Enable (s);
867         _dcp_content_type->Enable (s);
868         _dcp_frame_rate->Enable (s);
869         _trim_start->Enable (s);
870         _trim_end->Enable (s);
871         _ab->Enable (s);
872         _colour_lut->Enable (s);
873         _j2k_bandwidth->Enable (s);
874         _audio_gain->Enable (s);
875         _audio_gain_calculate_button->Enable (s);
876         _show_audio->Enable (s);
877         _audio_delay->Enable (s);
878
879         setup_subtitle_control_sensitivity ();
880         setup_show_audio_sensitivity ();
881         setup_content_button_sensitivity ();
882 }
883
884 /** Called when the `Edit filters' button has been clicked */
885 void
886 FilmEditor::edit_filters_clicked (wxCommandEvent &)
887 {
888         FilterDialog* d = new FilterDialog (this, _film->filters());
889         d->ActiveChanged.connect (bind (&Film::set_filters, _film, _1));
890         d->ShowModal ();
891         d->Destroy ();
892 }
893
894 /** Called when the scaler widget has been changed */
895 void
896 FilmEditor::scaler_changed (wxCommandEvent &)
897 {
898         if (!_film) {
899                 return;
900         }
901
902         int const n = _scaler->GetSelection ();
903         if (n >= 0) {
904                 _film->set_scaler (Scaler::from_index (n));
905         }
906 }
907
908 void
909 FilmEditor::audio_gain_changed (wxCommandEvent &)
910 {
911         if (!_film) {
912                 return;
913         }
914
915         _film->set_audio_gain (_audio_gain->GetValue ());
916 }
917
918 void
919 FilmEditor::audio_delay_changed (wxCommandEvent &)
920 {
921         if (!_film) {
922                 return;
923         }
924
925         _film->set_audio_delay (_audio_delay->GetValue ());
926 }
927
928 void
929 FilmEditor::trim_start_changed (wxCommandEvent &)
930 {
931         if (!_film) {
932                 return;
933         }
934
935         _film->set_trim_start (_trim_start->GetValue ());
936 }
937
938 void
939 FilmEditor::trim_end_changed (wxCommandEvent &)
940 {
941         if (!_film) {
942                 return;
943         }
944
945         _film->set_trim_end (_trim_end->GetValue ());
946 }
947
948 void
949 FilmEditor::audio_gain_calculate_button_clicked (wxCommandEvent &)
950 {
951         GainCalculatorDialog* d = new GainCalculatorDialog (this);
952         d->ShowModal ();
953
954         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
955                 d->Destroy ();
956                 return;
957         }
958         
959         _audio_gain->SetValue (
960                 Config::instance()->sound_processor()->db_for_fader_change (
961                         d->wanted_fader (),
962                         d->actual_fader ()
963                         )
964                 );
965
966         /* This appears to be necessary, as the change is not signalled,
967            I think.
968         */
969         wxCommandEvent dummy;
970         audio_gain_changed (dummy);
971         
972         d->Destroy ();
973 }
974
975 void
976 FilmEditor::setup_formats ()
977 {
978         _formats = Format::all ();
979
980         _format->Clear ();
981         for (vector<Format const *>::iterator i = _formats.begin(); i != _formats.end(); ++i) {
982                 _format->Append (std_to_wx ((*i)->name ()));
983         }
984
985         _film_sizer->Layout ();
986 }
987
988 void
989 FilmEditor::with_subtitles_toggled (wxCommandEvent &)
990 {
991         if (!_film) {
992                 return;
993         }
994
995         _film->set_with_subtitles (_with_subtitles->GetValue ());
996 }
997
998 void
999 FilmEditor::setup_subtitle_control_sensitivity ()
1000 {
1001         bool h = false;
1002         if (_generally_sensitive && _film) {
1003                 h = !_film->ffmpeg_subtitle_streams().empty();
1004         }
1005         
1006         _with_subtitles->Enable (h);
1007
1008         bool j = false;
1009         if (_film) {
1010                 j = _film->with_subtitles ();
1011         }
1012         
1013         _ffmpeg_subtitle_stream->Enable (j);
1014         _subtitle_offset->Enable (j);
1015         _subtitle_scale->Enable (j);
1016 }
1017
1018 void
1019 FilmEditor::use_dci_name_toggled (wxCommandEvent &)
1020 {
1021         if (!_film) {
1022                 return;
1023         }
1024
1025         _film->set_use_dci_name (_use_dci_name->GetValue ());
1026 }
1027
1028 void
1029 FilmEditor::edit_dci_button_clicked (wxCommandEvent &)
1030 {
1031         if (!_film) {
1032                 return;
1033         }
1034
1035         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
1036         d->ShowModal ();
1037         _film->set_dci_metadata (d->dci_metadata ());
1038         d->Destroy ();
1039 }
1040
1041 void
1042 FilmEditor::setup_streams ()
1043 {
1044         if (!_film) {
1045                 return;
1046         }
1047         
1048         _ffmpeg_audio_stream->Clear ();
1049         vector<FFmpegAudioStream> a = _film->ffmpeg_audio_streams ();
1050         for (vector<FFmpegAudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
1051                 _ffmpeg_audio_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (boost::lexical_cast<string> (i->id))));
1052         }
1053         
1054         if (_film->ffmpeg_audio_stream()) {
1055                 checked_set (_ffmpeg_audio_stream, boost::lexical_cast<string> (_film->ffmpeg_audio_stream()->id));
1056         }
1057
1058         _ffmpeg_subtitle_stream->Clear ();
1059         vector<FFmpegSubtitleStream> s = _film->ffmpeg_subtitle_streams ();
1060         for (vector<FFmpegSubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
1061                 _ffmpeg_subtitle_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (boost::lexical_cast<string> (i->id))));
1062         }
1063         
1064         if (_film->ffmpeg_subtitle_stream()) {
1065                 checked_set (_ffmpeg_subtitle_stream, boost::lexical_cast<string> (_film->ffmpeg_subtitle_stream()->id));
1066         } else {
1067                 _ffmpeg_subtitle_stream->SetSelection (wxNOT_FOUND);
1068         }
1069 }
1070
1071 void
1072 FilmEditor::ffmpeg_audio_stream_changed (wxCommandEvent &)
1073 {
1074         if (!_film) {
1075                 return;
1076         }
1077
1078         vector<FFmpegAudioStream> a = _film->ffmpeg_audio_streams ();
1079         vector<FFmpegAudioStream>::iterator i = a.begin ();
1080         string const s = string_client_data (_ffmpeg_audio_stream->GetClientObject (_ffmpeg_audio_stream->GetSelection ()));
1081         while (i != a.end() && i->id != s) {
1082                 ++i;
1083         }
1084
1085         if (i != a.end ()) {
1086                 _film->set_ffmpeg_audio_stream (*i);
1087         }
1088 }
1089
1090 void
1091 FilmEditor::ffmpeg_subtitle_stream_changed (wxCommandEvent &)
1092 {
1093         if (!_film) {
1094                 return;
1095         }
1096
1097         vector<FFmpegSubtitleStream> a = _film->ffmpeg_subtitle_streams ();
1098         vector<FFmpegSubtitleStream>::iterator i = a.begin ();
1099         string const s = string_client_data (_ffmpeg_subtitle_stream->GetClientObject (_ffmpeg_subtitle_stream->GetSelection ()));
1100         while (i != a.end() && i->id != s) {
1101                 ++i;
1102         }
1103
1104         if (i != a.end ()) {
1105                 _film->set_ffmpeg_subtitle_stream (*i);
1106         }
1107 }
1108
1109 void
1110 FilmEditor::setup_audio_details ()
1111 {
1112         if (!_film->ffmpeg_audio_stream()) {
1113                 _audio->SetLabel (wxT (""));
1114         } else {
1115                 stringstream s;
1116                 if (_film->audio_channels() == 1) {
1117                         s << wx_to_std (_("1 channel"));
1118                 } else {
1119                         s << _film->audio_channels() << " " << wx_to_std (_("channels"));
1120                 }
1121                 s << ", " << _film->audio_frame_rate() << wx_to_std (_("Hz"));
1122                 _audio->SetLabel (std_to_wx (s.str ()));
1123         }
1124 }
1125
1126 void
1127 FilmEditor::active_jobs_changed (bool a)
1128 {
1129         set_things_sensitive (!a);
1130 }
1131
1132 void
1133 FilmEditor::setup_dcp_name ()
1134 {
1135         string s = _film->dcp_name (true);
1136         if (s.length() > 28) {
1137                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
1138                 _dcp_name->SetToolTip (std_to_wx (s));
1139         } else {
1140                 _dcp_name->SetLabel (std_to_wx (s));
1141         }
1142 }
1143
1144 void
1145 FilmEditor::show_audio_clicked (wxCommandEvent &)
1146 {
1147         if (_audio_dialog) {
1148                 _audio_dialog->Destroy ();
1149                 _audio_dialog = 0;
1150         }
1151         
1152         _audio_dialog = new AudioDialog (this);
1153         _audio_dialog->Show ();
1154         _audio_dialog->set_film (_film);
1155 }
1156
1157 void
1158 FilmEditor::best_dcp_frame_rate_clicked (wxCommandEvent &)
1159 {
1160         if (!_film) {
1161                 return;
1162         }
1163         
1164         _film->set_dcp_frame_rate (best_dcp_frame_rate (_film->video_frame_rate ()));
1165 }
1166
1167 void
1168 FilmEditor::setup_show_audio_sensitivity ()
1169 {
1170         _show_audio->Enable (_film && _film->has_audio ());
1171 }
1172
1173 void
1174 FilmEditor::setup_content ()
1175 {
1176         string selected_summary;
1177         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1178         if (s != -1) {
1179                 selected_summary = wx_to_std (_content->GetItemText (s));
1180         }
1181         
1182         _content->DeleteAllItems ();
1183
1184         ContentList content = _film->content ();
1185         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
1186                 int const t = _content->GetItemCount ();
1187                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
1188                 if ((*i)->summary() == selected_summary) {
1189                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1190                 }
1191         }
1192
1193         if (selected_summary.empty () && !content.empty ()) {
1194                 /* Select the first item of content if non was selected before */
1195                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
1196         }
1197 }
1198
1199 void
1200 FilmEditor::content_add_clicked (wxCommandEvent &)
1201 {
1202         wxFileDialog* d = new wxFileDialog (this);
1203         int const r = d->ShowModal ();
1204         d->Destroy ();
1205
1206         if (r != wxID_OK) {
1207                 return;
1208         }
1209
1210         boost::filesystem::path p (wx_to_std (d->GetPath()));
1211
1212         if (ImageMagickContent::valid_file (p)) {
1213                 _film->add_content (shared_ptr<ImageMagickContent> (new ImageMagickContent (p)));
1214         } else if (SndfileContent::valid_file (p)) {
1215                 _film->add_content (shared_ptr<SndfileContent> (new SndfileContent (p)));
1216         } else {
1217                 _film->add_content (shared_ptr<FFmpegContent> (new FFmpegContent (p)));
1218         }
1219         
1220 }
1221
1222 void
1223 FilmEditor::content_remove_clicked (wxCommandEvent &)
1224 {
1225         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1226         if (s == -1) {
1227                 return;
1228         }
1229
1230         ContentList c = _film->content ();
1231         assert (s >= 0 && size_t (s) < c.size ());
1232         _film->remove_content (c[s]);
1233 }
1234
1235 void
1236 FilmEditor::content_earlier_clicked (wxCommandEvent &)
1237 {
1238         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1239         if (s == -1) {
1240                 return;
1241         }
1242
1243         ContentList c = _film->content ();
1244         assert (s >= 0 && size_t (s) < c.size ());
1245         _film->move_content_earlier (c[s]);
1246 }
1247
1248 void
1249 FilmEditor::content_later_clicked (wxCommandEvent &)
1250 {
1251         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1252         if (s == -1) {
1253                 return;
1254         }
1255
1256         ContentList c = _film->content ();
1257         assert (s >= 0 && size_t (s) < c.size ());
1258         _film->move_content_later (c[s]);
1259 }
1260
1261 void
1262 FilmEditor::content_item_selected (wxListEvent &)
1263 {
1264         setup_content_button_sensitivity ();
1265         setup_content_information ();
1266 }
1267
1268 void
1269 FilmEditor::setup_content_information ()
1270 {
1271         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1272         if (s == -1) {
1273                 _content_information->SetValue ("");
1274                 return;
1275         }
1276
1277         ContentList c = _film->content ();
1278         assert (s >= 0 && size_t (s) < c.size ());
1279         _content_information->SetValue (std_to_wx (c[s]->information ()));
1280 }
1281
1282 void
1283 FilmEditor::setup_content_button_sensitivity ()
1284 {
1285         _content_add->Enable (_generally_sensitive);
1286
1287         bool const have_selection = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1;
1288         _content_remove->Enable (have_selection && _generally_sensitive);
1289         _content_earlier->Enable (have_selection && _generally_sensitive);
1290         _content_later->Enable (have_selection && _generally_sensitive);
1291 }