Forward-port 1e6 -> 1000000 fix from master.
[dcpomatic.git] / src / wx / film_editor.cc
1 /*
2     Copyright (C) 2012-2013 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/film.h"
33 #include "lib/transcode_job.h"
34 #include "lib/exceptions.h"
35 #include "lib/job_manager.h"
36 #include "lib/filter.h"
37 #include "lib/ratio.h"
38 #include "lib/config.h"
39 #include "lib/still_image_content.h"
40 #include "lib/moving_image_content.h"
41 #include "lib/ffmpeg_content.h"
42 #include "lib/sndfile_content.h"
43 #include "lib/dcp_content_type.h"
44 #include "lib/sound_processor.h"
45 #include "lib/scaler.h"
46 #include "lib/playlist.h"
47 #include "timecode.h"
48 #include "wx_util.h"
49 #include "film_editor.h"
50 #include "dci_metadata_dialog.h"
51 #include "timeline_dialog.h"
52 #include "timing_panel.h"
53 #include "subtitle_panel.h"
54 #include "audio_panel.h"
55 #include "video_panel.h"
56
57 using std::string;
58 using std::cout;
59 using std::stringstream;
60 using std::pair;
61 using std::fixed;
62 using std::setprecision;
63 using std::list;
64 using std::vector;
65 using std::max;
66 using boost::shared_ptr;
67 using boost::weak_ptr;
68 using boost::dynamic_pointer_cast;
69 using boost::lexical_cast;
70
71 /** @param f Film to edit */
72 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
73         : wxPanel (parent)
74         , _menu (f, this)
75         , _generally_sensitive (true)
76         , _timeline_dialog (0)
77 {
78         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
79
80         _main_notebook = new wxNotebook (this, wxID_ANY);
81         s->Add (_main_notebook, 1);
82
83         make_content_panel ();
84         _main_notebook->AddPage (_content_panel, _("Content"), true);
85         make_dcp_panel ();
86         _main_notebook->AddPage (_dcp_panel, _("DCP"), false);
87         
88         set_film (f);
89         connect_to_widgets ();
90
91         JobManager::instance()->ActiveJobsChanged.connect (
92                 bind (&FilmEditor::active_jobs_changed, this, _1)
93                 );
94         
95         SetSizerAndFit (s);
96 }
97
98 void
99 FilmEditor::make_dcp_panel ()
100 {
101         _dcp_panel = new wxPanel (_main_notebook);
102         _dcp_sizer = new wxBoxSizer (wxVERTICAL);
103         _dcp_panel->SetSizer (_dcp_sizer);
104
105         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
106         _dcp_sizer->Add (grid, 0, wxEXPAND | wxALL, 8);
107
108         int r = 0;
109         
110         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Name"), true, wxGBPosition (r, 0));
111         _name = new wxTextCtrl (_dcp_panel, wxID_ANY);
112         grid->Add (_name, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND | wxLEFT | wxRIGHT);
113         ++r;
114         
115         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("DCP Name"), true, wxGBPosition (r, 0));
116         _dcp_name = new wxStaticText (_dcp_panel, wxID_ANY, wxT (""));
117         grid->Add (_dcp_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
118         ++r;
119
120         int flags = wxALIGN_CENTER_VERTICAL;
121 #ifdef __WXOSX__
122         flags |= wxALIGN_RIGHT;
123 #endif  
124
125         _use_dci_name = new wxCheckBox (_dcp_panel, wxID_ANY, _("Use DCI name"));
126         grid->Add (_use_dci_name, wxGBPosition (r, 0), wxDefaultSpan, flags);
127         _edit_dci_button = new wxButton (_dcp_panel, wxID_ANY, _("Details..."));
128         grid->Add (_edit_dci_button, wxGBPosition (r, 1), wxDefaultSpan);
129         ++r;
130
131         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Container"), true, wxGBPosition (r, 0));
132         _container = new wxChoice (_dcp_panel, wxID_ANY);
133         grid->Add (_container, wxGBPosition (r, 1), wxDefaultSpan, wxEXPAND);
134         ++r;
135
136         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Content Type"), true, wxGBPosition (r, 0));
137         _dcp_content_type = new wxChoice (_dcp_panel, wxID_ANY);
138         grid->Add (_dcp_content_type, wxGBPosition (r, 1));
139         ++r;
140
141         {
142                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Frame Rate"), true, wxGBPosition (r, 0));
143                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
144                 _frame_rate = new wxChoice (_dcp_panel, wxID_ANY);
145                 s->Add (_frame_rate, 1, wxALIGN_CENTER_VERTICAL);
146                 _best_frame_rate = new wxButton (_dcp_panel, wxID_ANY, _("Use best"));
147                 s->Add (_best_frame_rate, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND);
148                 grid->Add (s, wxGBPosition (r, 1));
149         }
150         ++r;
151
152         _encrypted = new wxCheckBox (_dcp_panel, wxID_ANY, wxT ("Encrypted"));
153         grid->Add (_encrypted, wxGBPosition (r, 0), wxGBSpan (1, 2));
154         ++r;
155
156         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Audio channels"), true, wxGBPosition (r, 0));
157         _audio_channels = new wxSpinCtrl (_dcp_panel, wxID_ANY);
158         grid->Add (_audio_channels, wxGBPosition (r, 1));
159         ++r;
160
161         _three_d = new wxCheckBox (_dcp_panel, wxID_ANY, _("3D"));
162         grid->Add (_three_d, wxGBPosition (r, 0), wxGBSpan (1, 2));
163         ++r;
164
165         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Resolution"), true, wxGBPosition (r, 0));
166         _resolution = new wxChoice (_dcp_panel, wxID_ANY);
167         grid->Add (_resolution, wxGBPosition (r, 1));
168         ++r;
169
170         {
171                 add_label_to_grid_bag_sizer (grid, _dcp_panel, _("JPEG2000 bandwidth"), true, wxGBPosition (r, 0));
172                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
173                 _j2k_bandwidth = new wxSpinCtrl (_dcp_panel, wxID_ANY);
174                 s->Add (_j2k_bandwidth, 1);
175                 add_label_to_sizer (s, _dcp_panel, _("MBps"), false);
176                 grid->Add (s, wxGBPosition (r, 1));
177         }
178         ++r;
179
180         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Standard"), true, wxGBPosition (r, 0));
181         _standard = new wxChoice (_dcp_panel, wxID_ANY);
182         grid->Add (_standard, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
183         ++r;
184
185         add_label_to_grid_bag_sizer (grid, _dcp_panel, _("Scaler"), true, wxGBPosition (r, 0));
186         _scaler = new wxChoice (_dcp_panel, wxID_ANY);
187         grid->Add (_scaler, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
188         ++r;
189
190         vector<Scaler const *> const sc = Scaler::all ();
191         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
192                 _scaler->Append (std_to_wx ((*i)->name()));
193         }
194
195         vector<Ratio const *> const ratio = Ratio::all ();
196         for (vector<Ratio const *>::const_iterator i = ratio.begin(); i != ratio.end(); ++i) {
197                 _container->Append (std_to_wx ((*i)->nickname ()));
198         }
199
200         vector<DCPContentType const *> const ct = DCPContentType::all ();
201         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
202                 _dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
203         }
204
205         list<int> const dfr = Config::instance()->allowed_dcp_frame_rates ();
206         for (list<int>::const_iterator i = dfr.begin(); i != dfr.end(); ++i) {
207                 _frame_rate->Append (std_to_wx (boost::lexical_cast<string> (*i)));
208         }
209
210         _audio_channels->SetRange (0, MAX_AUDIO_CHANNELS);
211         _j2k_bandwidth->SetRange (1, 250);
212
213         _resolution->Append (_("2K"));
214         _resolution->Append (_("4K"));
215
216         _standard->Append (_("SMPTE"));
217         _standard->Append (_("Interop"));
218 }
219
220 void
221 FilmEditor::connect_to_widgets ()
222 {
223         _name->Bind             (wxEVT_COMMAND_TEXT_UPDATED,          boost::bind (&FilmEditor::name_changed, this));
224         _use_dci_name->Bind     (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::use_dci_name_toggled, this));
225         _edit_dci_button->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::edit_dci_button_clicked, this));
226         _container->Bind        (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::container_changed, this));
227         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_SELECTED,    boost::bind (&FilmEditor::content_selection_changed, this));
228         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_DESELECTED,  boost::bind (&FilmEditor::content_selection_changed, this));
229         _content->Bind          (wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, boost::bind (&FilmEditor::content_right_click, this, _1));
230         _content_add_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_add_file_clicked, this));
231         _content_add_folder->Bind (wxEVT_COMMAND_BUTTON_CLICKED,      boost::bind (&FilmEditor::content_add_folder_clicked, this));
232         _content_remove->Bind   (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_remove_clicked, this));
233         _content_earlier->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_earlier_clicked, this));
234         _content_later->Bind    (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_later_clicked, this));
235         _content_timeline->Bind (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::content_timeline_clicked, this));
236         _scaler->Bind           (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::scaler_changed, this));
237         _dcp_content_type->Bind (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::dcp_content_type_changed, this));
238         _frame_rate->Bind       (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::frame_rate_changed, this));
239         _best_frame_rate->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,        boost::bind (&FilmEditor::best_frame_rate_clicked, this));
240         _encrypted->Bind        (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::encrypted_toggled, this));
241         _audio_channels->Bind   (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::audio_channels_changed, this));
242         _j2k_bandwidth->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED,      boost::bind (&FilmEditor::j2k_bandwidth_changed, this));
243         _resolution->Bind       (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::resolution_changed, this));
244         _sequence_video->Bind   (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::sequence_video_changed, this));
245         _three_d->Bind          (wxEVT_COMMAND_CHECKBOX_CLICKED,      boost::bind (&FilmEditor::three_d_changed, this));
246         _standard->Bind         (wxEVT_COMMAND_CHOICE_SELECTED,       boost::bind (&FilmEditor::standard_changed, this));
247 }
248
249 void
250 FilmEditor::make_content_panel ()
251 {
252         _content_panel = new wxPanel (_main_notebook);
253         _content_sizer = new wxBoxSizer (wxVERTICAL);
254         _content_panel->SetSizer (_content_sizer);
255
256         {
257                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
258                 
259                 _content = new wxListCtrl (_content_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL);
260                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
261
262                 _content->InsertColumn (0, wxT(""));
263                 _content->SetColumnWidth (0, 512);
264
265                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
266                 _content_add_file = new wxButton (_content_panel, wxID_ANY, _("Add file(s)..."));
267                 b->Add (_content_add_file, 1, wxEXPAND | wxLEFT | wxRIGHT);
268                 _content_add_folder = new wxButton (_content_panel, wxID_ANY, _("Add folder..."));
269                 b->Add (_content_add_folder, 1, wxEXPAND | wxLEFT | wxRIGHT);
270                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
271                 b->Add (_content_remove, 1, wxEXPAND | wxLEFT | wxRIGHT);
272                 _content_earlier = new wxButton (_content_panel, wxID_UP);
273                 b->Add (_content_earlier, 1, wxEXPAND);
274                 _content_later = new wxButton (_content_panel, wxID_DOWN);
275                 b->Add (_content_later, 1, wxEXPAND);
276                 _content_timeline = new wxButton (_content_panel, wxID_ANY, _("Timeline..."));
277                 b->Add (_content_timeline, 1, wxEXPAND | wxLEFT | wxRIGHT);
278
279                 s->Add (b, 0, wxALL, 4);
280
281                 _content_sizer->Add (s, 0.75, wxEXPAND | wxALL, 6);
282         }
283
284         _sequence_video = new wxCheckBox (_content_panel, wxID_ANY, _("Keep video in sequence"));
285         _content_sizer->Add (_sequence_video);
286
287         _content_notebook = new wxNotebook (_content_panel, wxID_ANY);
288         _content_sizer->Add (_content_notebook, 1, wxEXPAND | wxTOP, 6);
289
290         _video_panel = new VideoPanel (this);
291         _panels.push_back (_video_panel);
292         _audio_panel = new AudioPanel (this);
293         _panels.push_back (_audio_panel);
294         _subtitle_panel = new SubtitlePanel (this);
295         _panels.push_back (_subtitle_panel);
296         _timing_panel = new TimingPanel (this);
297         _panels.push_back (_timing_panel);
298 }
299
300 /** Called when the name widget has been changed */
301 void
302 FilmEditor::name_changed ()
303 {
304         if (!_film) {
305                 return;
306         }
307
308         _film->set_name (string (_name->GetValue().mb_str()));
309 }
310
311 void
312 FilmEditor::j2k_bandwidth_changed ()
313 {
314         if (!_film) {
315                 return;
316         }
317         
318         _film->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1000000);
319 }
320
321 void
322 FilmEditor::encrypted_toggled ()
323 {
324         if (!_film) {
325                 return;
326         }
327
328         _film->set_encrypted (_encrypted->GetValue ());
329 }
330                                
331 /** Called when the name widget has been changed */
332 void
333 FilmEditor::frame_rate_changed ()
334 {
335         if (!_film) {
336                 return;
337         }
338
339         _film->set_video_frame_rate (
340                 boost::lexical_cast<int> (
341                         wx_to_std (_frame_rate->GetString (_frame_rate->GetSelection ()))
342                         )
343                 );
344 }
345
346 void
347 FilmEditor::audio_channels_changed ()
348 {
349         if (!_film) {
350                 return;
351         }
352
353         _film->set_audio_channels (_audio_channels->GetValue ());
354 }
355
356 void
357 FilmEditor::resolution_changed ()
358 {
359         if (!_film) {
360                 return;
361         }
362
363         _film->set_resolution (_resolution->GetSelection() == 0 ? RESOLUTION_2K : RESOLUTION_4K);
364 }
365
366 void
367 FilmEditor::standard_changed ()
368 {
369         if (!_film) {
370                 return;
371         }
372
373         _film->set_interop (_standard->GetSelection() == 1);
374 }
375
376 /** Called when the metadata stored in the Film object has changed;
377  *  so that we can update the GUI.
378  *  @param p Property of the Film that has changed.
379  */
380 void
381 FilmEditor::film_changed (Film::Property p)
382 {
383         ensure_ui_thread ();
384         
385         if (!_film) {
386                 return;
387         }
388
389         stringstream s;
390
391         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
392                 (*i)->film_changed (p);
393         }
394                 
395         switch (p) {
396         case Film::NONE:
397                 break;
398         case Film::CONTENT:
399                 setup_content ();
400                 break;
401         case Film::CONTAINER:
402                 setup_container ();
403                 break;
404         case Film::NAME:
405                 checked_set (_name, _film->name());
406                 setup_dcp_name ();
407                 break;
408         case Film::WITH_SUBTITLES:
409                 setup_dcp_name ();
410                 break;
411         case Film::DCP_CONTENT_TYPE:
412                 checked_set (_dcp_content_type, DCPContentType::as_index (_film->dcp_content_type ()));
413                 setup_dcp_name ();
414                 break;
415         case Film::SCALER:
416                 checked_set (_scaler, Scaler::as_index (_film->scaler ()));
417                 break;
418         case Film::ENCRYPTED:
419                 checked_set (_encrypted, _film->encrypted ());
420                 break;
421         case Film::RESOLUTION:
422                 checked_set (_resolution, _film->resolution() == RESOLUTION_2K ? 0 : 1);
423                 setup_dcp_name ();
424                 break;
425         case Film::J2K_BANDWIDTH:
426                 checked_set (_j2k_bandwidth, double (_film->j2k_bandwidth()) / 1000000);
427                 break;
428         case Film::USE_DCI_NAME:
429                 checked_set (_use_dci_name, _film->use_dci_name ());
430                 setup_dcp_name ();
431                 break;
432         case Film::DCI_METADATA:
433                 setup_dcp_name ();
434                 break;
435         case Film::VIDEO_FRAME_RATE:
436         {
437                 bool done = false;
438                 for (unsigned int i = 0; i < _frame_rate->GetCount(); ++i) {
439                         if (wx_to_std (_frame_rate->GetString(i)) == boost::lexical_cast<string> (_film->video_frame_rate())) {
440                                 checked_set (_frame_rate, i);
441                                 done = true;
442                                 break;
443                         }
444                 }
445
446                 if (!done) {
447                         checked_set (_frame_rate, -1);
448                 }
449
450                 _best_frame_rate->Enable (_film->best_video_frame_rate () != _film->video_frame_rate ());
451                 break;
452         }
453         case Film::AUDIO_CHANNELS:
454                 checked_set (_audio_channels, _film->audio_channels ());
455                 setup_dcp_name ();
456                 break;
457         case Film::SEQUENCE_VIDEO:
458                 checked_set (_sequence_video, _film->sequence_video ());
459                 break;
460         case Film::THREE_D:
461                 checked_set (_three_d, _film->three_d ());
462                 setup_dcp_name ();
463                 break;
464         case Film::INTEROP:
465                 checked_set (_standard, _film->interop() ? 1 : 0);
466                 break;
467         }
468 }
469
470 void
471 FilmEditor::film_content_changed (weak_ptr<Content> weak_content, int property)
472 {
473         ensure_ui_thread ();
474         
475         if (!_film) {
476                 /* We call this method ourselves (as well as using it as a signal handler)
477                    so _film can be 0.
478                 */
479                 return;
480         }
481
482         shared_ptr<Content> content = weak_content.lock ();
483         if (!content || content != selected_content ()) {
484                 return;
485         }
486
487         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
488                 (*i)->film_content_changed (content, property);
489         }
490
491         if (property == FFmpegContentProperty::AUDIO_STREAM) {
492                 setup_dcp_name ();
493         }
494 }
495
496 void
497 FilmEditor::setup_container ()
498 {
499         int n = 0;
500         vector<Ratio const *> ratios = Ratio::all ();
501         vector<Ratio const *>::iterator i = ratios.begin ();
502         while (i != ratios.end() && *i != _film->container ()) {
503                 ++i;
504                 ++n;
505         }
506         
507         if (i == ratios.end()) {
508                 checked_set (_container, -1);
509         } else {
510                 checked_set (_container, n);
511         }
512         
513         setup_dcp_name ();
514 }       
515
516 /** Called when the container widget has been changed */
517 void
518 FilmEditor::container_changed ()
519 {
520         if (!_film) {
521                 return;
522         }
523
524         int const n = _container->GetSelection ();
525         if (n >= 0) {
526                 vector<Ratio const *> ratios = Ratio::all ();
527                 assert (n < int (ratios.size()));
528                 _film->set_container (ratios[n]);
529         }
530 }
531
532 /** Called when the DCP content type widget has been changed */
533 void
534 FilmEditor::dcp_content_type_changed ()
535 {
536         if (!_film) {
537                 return;
538         }
539
540         int const n = _dcp_content_type->GetSelection ();
541         if (n != wxNOT_FOUND) {
542                 _film->set_dcp_content_type (DCPContentType::from_index (n));
543         }
544 }
545
546 /** Sets the Film that we are editing */
547 void
548 FilmEditor::set_film (shared_ptr<Film> f)
549 {
550         set_general_sensitivity (f != 0);
551
552         if (_film == f) {
553                 return;
554         }
555         
556         _film = f;
557
558         if (_film) {
559                 _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
560                 _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
561         }
562
563         if (_film) {
564                 FileChanged (_film->directory ());
565         } else {
566                 FileChanged ("");
567         }
568
569         film_changed (Film::NAME);
570         film_changed (Film::USE_DCI_NAME);
571         film_changed (Film::CONTENT);
572         film_changed (Film::DCP_CONTENT_TYPE);
573         film_changed (Film::CONTAINER);
574         film_changed (Film::RESOLUTION);
575         film_changed (Film::SCALER);
576         film_changed (Film::WITH_SUBTITLES);
577         film_changed (Film::ENCRYPTED);
578         film_changed (Film::J2K_BANDWIDTH);
579         film_changed (Film::DCI_METADATA);
580         film_changed (Film::VIDEO_FRAME_RATE);
581         film_changed (Film::AUDIO_CHANNELS);
582         film_changed (Film::ENCRYPTED);
583         film_changed (Film::SEQUENCE_VIDEO);
584         film_changed (Film::THREE_D);
585         film_changed (Film::INTEROP);
586
587         if (!_film->content().empty ()) {
588                 set_selection (_film->content().front ());
589         }
590
591         content_selection_changed ();
592 }
593
594 void
595 FilmEditor::set_general_sensitivity (bool s)
596 {
597         _generally_sensitive = s;
598
599         /* Stuff in the Content / DCP tabs */
600         _name->Enable (s);
601         _use_dci_name->Enable (s);
602         _edit_dci_button->Enable (s);
603         _content->Enable (s);
604         _content_add_file->Enable (s);
605         _content_add_folder->Enable (s);
606         _content_remove->Enable (s);
607         _content_earlier->Enable (s);
608         _content_later->Enable (s);
609         _content_timeline->Enable (s);
610         _dcp_content_type->Enable (s);
611         _encrypted->Enable (s);
612         _frame_rate->Enable (s);
613         _audio_channels->Enable (s);
614         _j2k_bandwidth->Enable (s);
615         _container->Enable (s);
616         _best_frame_rate->Enable (s && _film && _film->best_video_frame_rate () != _film->video_frame_rate ());
617         _sequence_video->Enable (s);
618         _resolution->Enable (s);
619         _scaler->Enable (s);
620         _three_d->Enable (s);
621         _standard->Enable (s);
622
623         /* Set the panels in the content notebook */
624         for (list<FilmEditorPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
625                 (*i)->Enable (s);
626         }
627 }
628
629 /** Called when the scaler widget has been changed */
630 void
631 FilmEditor::scaler_changed ()
632 {
633         if (!_film) {
634                 return;
635         }
636
637         int const n = _scaler->GetSelection ();
638         if (n >= 0) {
639                 _film->set_scaler (Scaler::from_index (n));
640         }
641 }
642
643 void
644 FilmEditor::use_dci_name_toggled ()
645 {
646         if (!_film) {
647                 return;
648         }
649
650         _film->set_use_dci_name (_use_dci_name->GetValue ());
651 }
652
653 void
654 FilmEditor::edit_dci_button_clicked ()
655 {
656         if (!_film) {
657                 return;
658         }
659
660         DCIMetadataDialog* d = new DCIMetadataDialog (this, _film->dci_metadata ());
661         d->ShowModal ();
662         _film->set_dci_metadata (d->dci_metadata ());
663         d->Destroy ();
664 }
665
666 void
667 FilmEditor::active_jobs_changed (bool a)
668 {
669         set_general_sensitivity (!a);
670 }
671
672 void
673 FilmEditor::setup_dcp_name ()
674 {
675         string s = _film->dcp_name (true);
676         if (s.length() > 28) {
677                 _dcp_name->SetLabel (std_to_wx (s.substr (0, 28)) + N_("..."));
678                 _dcp_name->SetToolTip (std_to_wx (s));
679         } else {
680                 _dcp_name->SetLabel (std_to_wx (s));
681         }
682 }
683
684 void
685 FilmEditor::best_frame_rate_clicked ()
686 {
687         if (!_film) {
688                 return;
689         }
690         
691         _film->set_video_frame_rate (_film->best_video_frame_rate ());
692 }
693
694 void
695 FilmEditor::setup_content ()
696 {
697         string selected_summary;
698         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
699         if (s != -1) {
700                 selected_summary = wx_to_std (_content->GetItemText (s));
701         }
702         
703         _content->DeleteAllItems ();
704
705         ContentList content = _film->content ();
706         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
707                 int const t = _content->GetItemCount ();
708                 _content->InsertItem (t, std_to_wx ((*i)->summary ()));
709                 if ((*i)->summary() == selected_summary) {
710                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
711                 }
712         }
713
714         if (selected_summary.empty () && !content.empty ()) {
715                 /* Select the item of content if none was selected before */
716                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
717         }
718 }
719
720 void
721 FilmEditor::content_add_file_clicked ()
722 {
723         wxFileDialog* d = new wxFileDialog (this, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
724         int const r = d->ShowModal ();
725         d->Destroy ();
726
727         if (r != wxID_OK) {
728                 return;
729         }
730
731         wxArrayString paths;
732         d->GetPaths (paths);
733
734         /* XXX: check for lots of files here and do something */
735
736         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
737                 boost::filesystem::path p (wx_to_std (paths[i]));
738
739                 shared_ptr<Content> c;
740
741                 if (valid_image_file (p)) {
742                         c.reset (new StillImageContent (_film, p));
743                 } else if (SndfileContent::valid_file (p)) {
744                         c.reset (new SndfileContent (_film, p));
745                 } else {
746                         c.reset (new FFmpegContent (_film, p));
747                 }
748
749                 _film->examine_and_add_content (c);
750         }
751 }
752
753 void
754 FilmEditor::content_add_folder_clicked ()
755 {
756         wxDirDialog* d = new wxDirDialog (this, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
757         int const r = d->ShowModal ();
758         d->Destroy ();
759         
760         if (r != wxID_OK) {
761                 return;
762         }
763
764         _film->examine_and_add_content (
765                 shared_ptr<MovingImageContent> (
766                         new MovingImageContent (_film, boost::filesystem::path (wx_to_std (d->GetPath ())))
767                         )
768                 );
769 }
770
771 void
772 FilmEditor::content_remove_clicked ()
773 {
774         shared_ptr<Content> c = selected_content ();
775         if (c) {
776                 _film->remove_content (c);
777         }
778
779         content_selection_changed ();
780 }
781
782 void
783 FilmEditor::content_selection_changed ()
784 {
785         setup_content_sensitivity ();
786         shared_ptr<Content> s = selected_content ();
787
788         /* All other sensitivity in content panels should be triggered by
789            one of these.
790         */
791         film_content_changed (s, ContentProperty::POSITION);
792         film_content_changed (s, ContentProperty::LENGTH);
793         film_content_changed (s, ContentProperty::TRIM_START);
794         film_content_changed (s, ContentProperty::TRIM_END);
795         film_content_changed (s, VideoContentProperty::VIDEO_CROP);
796         film_content_changed (s, VideoContentProperty::VIDEO_RATIO);
797         film_content_changed (s, VideoContentProperty::VIDEO_FRAME_TYPE);
798         film_content_changed (s, VideoContentProperty::COLOUR_CONVERSION);
799         film_content_changed (s, AudioContentProperty::AUDIO_GAIN);
800         film_content_changed (s, AudioContentProperty::AUDIO_DELAY);
801         film_content_changed (s, AudioContentProperty::AUDIO_MAPPING);
802         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAM);
803         film_content_changed (s, FFmpegContentProperty::AUDIO_STREAMS);
804         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAM);
805         film_content_changed (s, FFmpegContentProperty::SUBTITLE_STREAMS);
806         film_content_changed (s, FFmpegContentProperty::FILTERS);
807         film_content_changed (s, SubtitleContentProperty::SUBTITLE_OFFSET);
808         film_content_changed (s, SubtitleContentProperty::SUBTITLE_SCALE);
809 }
810
811 /** Set up broad sensitivity based on the type of content that is selected */
812 void
813 FilmEditor::setup_content_sensitivity ()
814 {
815         _content_add_file->Enable (_generally_sensitive);
816         _content_add_folder->Enable (_generally_sensitive);
817
818         shared_ptr<Content> selection = selected_content ();
819
820         _content_remove->Enable (selection && _generally_sensitive);
821         _content_earlier->Enable (selection && _generally_sensitive);
822         _content_later->Enable (selection && _generally_sensitive);
823         _content_timeline->Enable (_generally_sensitive);
824
825         _video_panel->Enable    (selection && dynamic_pointer_cast<VideoContent>  (selection) && _generally_sensitive);
826         _audio_panel->Enable    (selection && dynamic_pointer_cast<AudioContent>  (selection) && _generally_sensitive);
827         _subtitle_panel->Enable (selection && dynamic_pointer_cast<FFmpegContent> (selection) && _generally_sensitive);
828         _timing_panel->Enable   (selection && _generally_sensitive);
829 }
830
831 shared_ptr<Content>
832 FilmEditor::selected_content ()
833 {
834         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
835         if (s == -1) {
836                 return shared_ptr<Content> ();
837         }
838
839         ContentList c = _film->content ();
840         if (s < 0 || size_t (s) >= c.size ()) {
841                 return shared_ptr<Content> ();
842         }
843         
844         return c[s];
845 }
846
847 shared_ptr<VideoContent>
848 FilmEditor::selected_video_content ()
849 {
850         shared_ptr<Content> c = selected_content ();
851         if (!c) {
852                 return shared_ptr<VideoContent> ();
853         }
854
855         return dynamic_pointer_cast<VideoContent> (c);
856 }
857
858 shared_ptr<AudioContent>
859 FilmEditor::selected_audio_content ()
860 {
861         shared_ptr<Content> c = selected_content ();
862         if (!c) {
863                 return shared_ptr<AudioContent> ();
864         }
865
866         return dynamic_pointer_cast<AudioContent> (c);
867 }
868
869 shared_ptr<SubtitleContent>
870 FilmEditor::selected_subtitle_content ()
871 {
872         shared_ptr<Content> c = selected_content ();
873         if (!c) {
874                 return shared_ptr<SubtitleContent> ();
875         }
876
877         return dynamic_pointer_cast<SubtitleContent> (c);
878 }
879
880 void
881 FilmEditor::content_timeline_clicked ()
882 {
883         if (_timeline_dialog) {
884                 _timeline_dialog->Destroy ();
885                 _timeline_dialog = 0;
886         }
887         
888         _timeline_dialog = new TimelineDialog (this, _film);
889         _timeline_dialog->Show ();
890 }
891
892 void
893 FilmEditor::set_selection (weak_ptr<Content> wc)
894 {
895         ContentList content = _film->content ();
896         for (size_t i = 0; i < content.size(); ++i) {
897                 if (content[i] == wc.lock ()) {
898                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
899                 } else {
900                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
901                 }
902         }
903 }
904
905 void
906 FilmEditor::sequence_video_changed ()
907 {
908         if (!_film) {
909                 return;
910         }
911         
912         _film->set_sequence_video (_sequence_video->GetValue ());
913 }
914
915 void
916 FilmEditor::content_right_click (wxListEvent& ev)
917 {
918         ContentList cl;
919         cl.push_back (selected_content ());
920         _menu.popup (cl, ev.GetPoint ());
921 }
922
923 void
924 FilmEditor::three_d_changed ()
925 {
926         if (!_film) {
927                 return;
928         }
929
930         _film->set_three_d (_three_d->GetValue ());
931 }
932
933 void
934 FilmEditor::content_earlier_clicked ()
935 {
936         _film->move_content_earlier (selected_content ());
937         content_selection_changed ();
938 }
939
940 void
941 FilmEditor::content_later_clicked ()
942 {
943         _film->move_content_later (selected_content ());
944         content_selection_changed ();
945 }