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