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