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