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