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