Cleanup: remove some unnecessary includes.
[dcpomatic.git] / src / wx / content_panel.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "audio_panel.h"
23 #include "content_panel.h"
24 #include "dcpomatic_button.h"
25 #include "dir_dialog.h"
26 #include "file_dialog.h"
27 #include "film_viewer.h"
28 #include "image_sequence_dialog.h"
29 #include "text_panel.h"
30 #include "timeline_dialog.h"
31 #include "timing_panel.h"
32 #include "video_panel.h"
33 #include "wx_util.h"
34 #include "lib/audio_content.h"
35 #include "lib/case_insensitive_sorter.h"
36 #include "lib/compose.hpp"
37 #include "lib/config.h"
38 #include "lib/content_factory.h"
39 #include "lib/cross.h"
40 #include "lib/dcp_content.h"
41 #include "lib/dcp_subtitle_content.h"
42 #include "lib/dcp_subtitle_decoder.h"
43 #include "lib/dcpomatic_log.h"
44 #include "lib/ffmpeg_content.h"
45 #include "lib/image_content.h"
46 #include "lib/log.h"
47 #include "lib/playlist.h"
48 #include "lib/scope_guard.h"
49 #include "lib/string_text_file.h"
50 #include "lib/string_text_file_content.h"
51 #include "lib/text_content.h"
52 #include "lib/video_content.h"
53 #include <dcp/warnings.h>
54 LIBDCP_DISABLE_WARNINGS
55 #include <wx/display.h>
56 #include <wx/dnd.h>
57 #include <wx/listctrl.h>
58 #include <wx/notebook.h>
59 #include <wx/wx.h>
60 LIBDCP_ENABLE_WARNINGS
61 #include <boost/filesystem.hpp>
62
63
64 using std::dynamic_pointer_cast;
65 using std::exception;
66 using std::list;
67 using std::make_shared;
68 using std::shared_ptr;
69 using std::string;
70 using std::vector;
71 using std::weak_ptr;
72 using boost::optional;
73 using namespace dcpomatic;
74 #if BOOST_VERSION >= 106100
75 using namespace boost::placeholders;
76 #endif
77
78
79 class LimitedContentPanelSplitter : public wxSplitterWindow
80 {
81 public:
82         LimitedContentPanelSplitter(wxWindow* parent)
83                 : wxSplitterWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
84         {
85                 /* This value doesn't really mean much but we just want to stop double-click on the
86                    divider from shrinking the bottom panel (#1601).
87                    */
88                 SetMinimumPaneSize(64);
89
90                 Bind(wxEVT_SIZE, boost::bind(&LimitedContentPanelSplitter::sized, this, _1));
91         }
92
93         bool OnSashPositionChange(int new_position) override
94         {
95                 /* Try to stop the top bit of the splitter getting so small that buttons disappear */
96                 auto const ok = new_position > 220;
97                 if (ok) {
98                         Config::instance()->set_main_content_divider_sash_position(new_position);
99                 }
100                 return ok;
101         }
102
103         void first_shown(wxWindow* top, wxWindow* bottom)
104         {
105                 int const sn = wxDisplay::GetFromWindow(this);
106                 /* Fallback for when GetFromWindow fails for reasons that aren't clear */
107                 int pos = -600;
108                 if (sn >= 0) {
109                         wxRect const screen = wxDisplay(sn).GetClientArea();
110                         /* This is a hack to try and make the content notebook a sensible size; large on big displays but small
111                            enough on small displays to leave space for the content area.
112                            */
113                         pos = screen.height > 800 ? -600 : -_top_panel_minimum_size;
114                 }
115                 SplitHorizontally(top, bottom, Config::instance()->main_content_divider_sash_position().get_value_or(pos));
116                 _first_shown = true;
117         }
118
119 private:
120         void sized(wxSizeEvent& ev)
121         {
122                 auto const height = GetSize().GetHeight();
123                 if (_first_shown && (!_last_height || *_last_height != height) && height > _top_panel_minimum_size && GetSashPosition() < _top_panel_minimum_size) {
124                         /* The window is now fairly big but the top panel is small; this happens when the DCP-o-matic window
125                          * is shrunk and then made larger again.  Try to set a sensible top panel size in this case (#1839).
126                          */
127                         SetSashPosition(Config::instance()->main_content_divider_sash_position().get_value_or(_top_panel_minimum_size));
128                 }
129
130                 ev.Skip ();
131                 _last_height = height;
132         }
133
134         bool _first_shown = false;
135         int const _top_panel_minimum_size = 350;
136         boost::optional<int> _last_height;
137 };
138
139
140 class ContentDropTarget : public wxFileDropTarget
141 {
142 public:
143         ContentDropTarget(ContentPanel* owner)
144                 : _panel(owner)
145         {}
146
147         bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
148         {
149                 vector<boost::filesystem::path> files;
150                 vector<boost::filesystem::path> dcps;
151                 vector<boost::filesystem::path> folders;
152                 for (size_t i = 0; i < filenames.GetCount(); ++i) {
153                         auto path = boost::filesystem::path(wx_to_std(filenames[i]));
154                         if (boost::filesystem::is_regular_file(path)) {
155                                 files.push_back(path);
156                         } else if (boost::filesystem::is_directory(path)) {
157                                 if (contains_assetmap(path)) {
158                                         dcps.push_back(path);
159                                 } else {
160                                         folders.push_back(path);
161                                 }
162                         }
163                 }
164
165                 if (!filenames.empty()) {
166                         _panel->add_files(files);
167                 }
168
169                 for (auto dcp: dcps) {
170                         _panel->add_dcp(dcp);
171                 }
172
173                 for (auto dir: folders) {
174                         _panel->add_folder(dir);
175                 }
176
177                 return true;
178         };
179
180 private:
181         ContentPanel* _panel;
182 };
183
184
185 /** A wxListCtrl that can middle-ellipsize its text */
186 class ContentListCtrl : public wxListCtrl
187 {
188 public:
189         ContentListCtrl(wxWindow* parent)
190                 : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxSize(320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_VIRTUAL)
191         {
192                 _red.SetTextColour(*wxRED);
193         }
194
195         struct Item
196         {
197                 wxString text;
198                 weak_ptr<Content> content;
199                 bool error;
200         };
201
202         void set(vector<Item> const& items)
203         {
204                 _items = items;
205                 SetItemCount(items.size());
206         }
207
208         wxString OnGetItemText(long item, long) const override
209         {
210                 DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
211                 wxClientDC dc(const_cast<wxWindow*>(static_cast<wxWindow const*>(this)));
212                 return wxControl::Ellipsize(_items[item].text, dc, wxELLIPSIZE_MIDDLE, GetSize().GetWidth());
213         }
214
215         wxListItemAttr* OnGetItemAttr(long item) const override
216         {
217                 DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
218                 return _items[item].error ? const_cast<wxListItemAttr*>(&_red) : nullptr;
219         }
220
221         weak_ptr<Content> content_at_index(long index)
222         {
223                 if (index < 0 || index >= static_cast<long>(_items.size())) {
224                         return {};
225                 }
226                 return _items[index].content;
227         }
228
229 private:
230         std::vector<Item> _items;
231         wxListItemAttr _red;
232 };
233
234
235 ContentPanel::ContentPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
236         : _parent (n)
237         , _film (film)
238         , _film_viewer (viewer)
239         , _generally_sensitive (true)
240         , _ignore_deselect (false)
241         , _no_check_selection (false)
242 {
243         _splitter = new LimitedContentPanelSplitter(n);
244         _top_panel = new wxPanel (_splitter);
245
246         _menu = new ContentMenu (_splitter, _film_viewer);
247
248         {
249                 auto s = new wxBoxSizer (wxHORIZONTAL);
250
251                 _content = new ContentListCtrl(_top_panel);
252                 _content->DragAcceptFiles (true);
253                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
254
255                 _content->InsertColumn (0, wxT(""));
256                 _content->SetColumnWidth(0, 2048);
257
258                 auto b = new wxBoxSizer (wxVERTICAL);
259
260                 _add_file = new Button (_top_panel, _("Add file(s)..."));
261                 _add_file->SetToolTip (_("Add video, image, sound or subtitle files to the film."));
262                 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
263
264                 _add_folder = new Button (_top_panel, _("Add folder..."));
265                 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a folder of sound files."));
266                 b->Add (_add_folder, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
267
268                 _add_dcp = new Button (_top_panel, _("Add DCP..."));
269                 _add_dcp->SetToolTip (_("Add a DCP."));
270                 b->Add (_add_dcp, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
271
272                 _remove = new Button (_top_panel, _("Remove"));
273                 _remove->SetToolTip (_("Remove the selected piece of content from the film."));
274                 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
275
276                 _earlier = new Button (_top_panel, _("Earlier"));
277                 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
278                 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
279
280                 _later = new Button (_top_panel, _("Later"));
281                 _later->SetToolTip (_("Move the selected piece of content later in the film."));
282                 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
283
284                 _timeline = new Button (_top_panel, _("Timeline..."));
285                 _timeline->SetToolTip (_("Open the timeline for the film."));
286                 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
287
288                 s->Add (b, 0, wxALL, 4);
289                 _top_panel->SetSizer (s);
290         }
291
292         _notebook = new wxNotebook (_splitter, wxID_ANY);
293
294         _timing_panel = new TimingPanel (this, _film_viewer);
295         _notebook->AddPage (_timing_panel, _("Timing"), false);
296         _timing_panel->create ();
297
298         _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::item_selected, this));
299         _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::item_deselected, this));
300         _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
301         _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
302         _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
303         _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
304         _add_dcp->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_dcp_clicked, this));
305         _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
306         _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
307         _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
308         _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
309
310         _content->SetDropTarget(new ContentDropTarget(this));
311 }
312
313
314 void
315 ContentPanel::first_shown ()
316 {
317         _splitter->first_shown (_top_panel, _notebook);
318 }
319
320
321 ContentList
322 ContentPanel::selected ()
323 {
324         ContentList sel;
325         long int s = -1;
326         while (true) {
327                 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
328                 if (s == -1) {
329                         break;
330                 }
331
332                 auto weak = _content->content_at_index(s);
333                 if (auto content = weak.lock()) {
334                         sel.push_back(content);
335                 }
336         }
337
338         return sel;
339 }
340
341
342 ContentList
343 ContentPanel::selected_video ()
344 {
345         ContentList vc;
346
347         for (auto i: selected()) {
348                 if (i->video) {
349                         vc.push_back (i);
350                 }
351         }
352
353         return vc;
354 }
355
356
357 ContentList
358 ContentPanel::selected_audio ()
359 {
360         ContentList ac;
361
362         for (auto i: selected()) {
363                 if (i->audio) {
364                         ac.push_back (i);
365                 }
366         }
367
368         return ac;
369 }
370
371
372 ContentList
373 ContentPanel::selected_text ()
374 {
375         ContentList sc;
376
377         for (auto i: selected()) {
378                 if (!i->text.empty()) {
379                         sc.push_back (i);
380                 }
381         }
382
383         return sc;
384 }
385
386
387 FFmpegContentList
388 ContentPanel::selected_ffmpeg ()
389 {
390         FFmpegContentList sc;
391
392         for (auto i: selected()) {
393                 auto t = dynamic_pointer_cast<FFmpegContent> (i);
394                 if (t) {
395                         sc.push_back (t);
396                 }
397         }
398
399         return sc;
400 }
401
402
403 void
404 ContentPanel::film_changed (Film::Property p)
405 {
406         switch (p) {
407         case Film::Property::CONTENT:
408         case Film::Property::CONTENT_ORDER:
409                 setup ();
410                 break;
411         default:
412                 break;
413         }
414
415         for (auto i: panels()) {
416                 i->film_changed (p);
417         }
418 }
419
420
421 void
422 ContentPanel::item_deselected ()
423 {
424         /* Maybe this is just a re-click on the same item; if not, _ignore_deselect will stay
425            false and item_deselected_foo will handle the deselection.
426         */
427         _ignore_deselect = false;
428         signal_manager->when_idle (boost::bind (&ContentPanel::item_deselected_idle, this));
429 }
430
431
432 void
433 ContentPanel::item_deselected_idle ()
434 {
435         if (!_ignore_deselect) {
436                 check_selection ();
437         }
438 }
439
440
441 void
442 ContentPanel::item_selected ()
443 {
444         _ignore_deselect = true;
445         check_selection ();
446 }
447
448
449 void
450 ContentPanel::check_selection ()
451 {
452         if (_no_check_selection) {
453                 return;
454         }
455
456         setup_sensitivity ();
457
458         for (auto i: panels()) {
459                 i->content_selection_changed ();
460         }
461
462         optional<DCPTime> go_to;
463         for (auto content: selected()) {
464                 if (content->paths_valid()) {
465                         auto position = content->position();
466                         if (auto text_content = dynamic_pointer_cast<StringTextFileContent>(content)) {
467                                 /* Rather special case; if we select a text subtitle file jump to its
468                                    first subtitle.
469                                 */
470                                 StringTextFile ts(text_content);
471                                 if (auto first = ts.first()) {
472                                         position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
473                                 }
474                         } else if (auto dcp_content = dynamic_pointer_cast<DCPSubtitleContent>(content)) {
475                                 /* Do the same for DCP subtitles */
476                                 DCPSubtitleDecoder ts(_film, dcp_content);
477                                 if (auto first = ts.first()) {
478                                         position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
479                                 }
480                         }
481                         if (!go_to || position < go_to.get()) {
482                                 go_to = position;
483                         }
484                 }
485         }
486
487         if (go_to && Config::instance()->jump_to_selected() && signal_manager) {
488                 signal_manager->when_idle(boost::bind(&FilmViewer::seek, &_film_viewer, go_to.get().ceil(_film->video_frame_rate()), true));
489         }
490
491         if (_timeline_dialog) {
492                 _timeline_dialog->set_selection (selected());
493         }
494
495         /* Make required tabs visible */
496
497         if (_notebook->GetPageCount() > 1) {
498                 /* There's more than one tab in the notebook so the current selection could be meaningful
499                    to the user; store it so that we can try to restore it later.
500                 */
501                 _last_selected_tab = 0;
502                 if (_notebook->GetSelection() != wxNOT_FOUND) {
503                         _last_selected_tab = _notebook->GetPage(_notebook->GetSelection());
504                 }
505         }
506
507         bool have_video = false;
508         bool have_audio = false;
509         EnumIndexedVector<bool, TextType> have_text;
510         for (auto i: selected()) {
511                 if (i->video) {
512                         have_video = true;
513                 }
514                 if (i->audio) {
515                         have_audio = true;
516                 }
517                 for (auto j: i->text) {
518                         have_text[static_cast<int>(j->original_type())] = true;
519                 }
520         }
521
522         int off = 0;
523
524         if (have_video && !_video_panel) {
525                 _video_panel = new VideoPanel (this);
526                 _notebook->InsertPage (off, _video_panel, _video_panel->name());
527                 _video_panel->create ();
528         } else if (!have_video && _video_panel) {
529                 _notebook->DeletePage (off);
530                 _video_panel = 0;
531         }
532
533         if (have_video) {
534                 ++off;
535         }
536
537         if (have_audio && !_audio_panel) {
538                 _audio_panel = new AudioPanel (this);
539                 _notebook->InsertPage (off, _audio_panel, _audio_panel->name());
540                 _audio_panel->create ();
541         } else if (!have_audio && _audio_panel) {
542                 _notebook->DeletePage (off);
543                 _audio_panel = 0;
544         }
545
546         if (have_audio) {
547                 ++off;
548         }
549
550         for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
551                 if (have_text[i] && !_text_panel[i]) {
552                         _text_panel[i] = new TextPanel (this, static_cast<TextType>(i));
553                         _notebook->InsertPage (off, _text_panel[i], _text_panel[i]->name());
554                         _text_panel[i]->create ();
555                 } else if (!have_text[i] && _text_panel[i]) {
556                         _notebook->DeletePage (off);
557                         _text_panel[i] = nullptr;
558                 }
559                 if (have_text[i]) {
560                         ++off;
561                 }
562         }
563
564         /* Set up the tab selection */
565
566         auto done = false;
567         for (size_t i = 0; i < _notebook->GetPageCount(); ++i) {
568                 if (_notebook->GetPage(i) == _last_selected_tab) {
569                         _notebook->SetSelection (i);
570                         done = true;
571                 }
572         }
573
574         if (!done && _notebook->GetPageCount() > 0) {
575                 _notebook->SetSelection (0);
576         }
577
578         setup_sensitivity ();
579         SelectionChanged ();
580 }
581
582
583 optional<boost::filesystem::path>
584 ContentPanel::add_files_override_path() const
585 {
586         DCPOMATIC_ASSERT(_film->directory());
587         return Config::instance()->default_add_file_location() == Config::DefaultAddFileLocation::SAME_AS_PROJECT
588                 ? _film->directory()->parent_path()
589                 : boost::optional<boost::filesystem::path>();
590
591 }
592
593
594 void
595 ContentPanel::add_file_clicked ()
596 {
597         /* This method is also called when Ctrl-A is pressed, so check that our notebook page
598            is visible.
599         */
600         if (_parent->GetCurrentPage() != _splitter || !_film) {
601                 return;
602         }
603
604         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
605            non-Latin filenames or paths.
606         */
607         FileDialog dialog(
608                 _splitter,
609                 _("Choose a file or files"),
610                 wxT("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
611                 wxFD_MULTIPLE | wxFD_CHANGE_DIR,
612                 "AddFilesPath",
613                 add_files_override_path()
614                 );
615
616         if (dialog.show()) {
617                 add_files(dialog.paths());
618         }
619 }
620
621
622 void
623 ContentPanel::add_folder_clicked ()
624 {
625         DirDialog dialog(_splitter, _("Choose a folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path());
626         if (dialog.show()) {
627                 add_folder(dialog.path());
628         }
629 }
630
631
632 void
633 ContentPanel::add_folder(boost::filesystem::path folder)
634 {
635         vector<shared_ptr<Content>> content;
636
637         try {
638                 content = content_factory(folder);
639         } catch (exception& e) {
640                 error_dialog (_parent, e.what());
641                 return;
642         }
643
644         if (content.empty ()) {
645                 error_dialog (_parent, _("No content found in this folder."));
646                 return;
647         }
648
649         for (auto i: content) {
650                 auto ic = dynamic_pointer_cast<ImageContent> (i);
651                 if (ic) {
652                         ImageSequenceDialog dialog(_splitter);
653
654                         if (dialog.ShowModal() != wxID_OK) {
655                                 return;
656                         }
657                         ic->set_video_frame_rate(_film, dialog.frame_rate());
658                 }
659
660                 _film->examine_and_add_content (i);
661         }
662 }
663
664
665 void
666 ContentPanel::add_dcp_clicked ()
667 {
668         DirDialog dialog(_splitter, _("Choose a DCP folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path());
669         if (dialog.show()) {
670                 add_dcp(dialog.path());
671         }
672 }
673
674
675 void
676 ContentPanel::add_dcp(boost::filesystem::path dcp)
677 {
678         try {
679                 _film->examine_and_add_content(make_shared<DCPContent>(dcp));
680         } catch (ProjectFolderError &) {
681                 error_dialog (
682                         _parent,
683                         _(
684                                 "This looks like a DCP-o-matic project folder, which cannot be added to a different project.  "
685                                 "Choose the DCP folder inside the DCP-o-matic project folder if that's what you want to import."
686                          )
687                         );
688         } catch (exception& e) {
689                 error_dialog(_parent, e.what());
690         }
691 }
692
693
694 /** @return true if this remove "click" should be ignored */
695 bool
696 ContentPanel::remove_clicked (bool hotkey)
697 {
698         /* If the method was called because Delete was pressed check that our notebook page
699            is visible and that the content list is focused.
700         */
701         if (hotkey && (_parent->GetCurrentPage() != _splitter || !_content->HasFocus())) {
702                 return true;
703         }
704
705         for (auto i: selected ()) {
706                 _film->remove_content (i);
707         }
708
709         check_selection ();
710         return false;
711 }
712
713
714 void
715 ContentPanel::timeline_clicked ()
716 {
717         if (!_film) {
718                 return;
719         }
720
721         _timeline_dialog.reset(this, _film, _film_viewer);
722         _timeline_dialog->set_selection (selected());
723         _timeline_dialog->Show ();
724 }
725
726
727 void
728 ContentPanel::right_click (wxListEvent& ev)
729 {
730         _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
731 }
732
733
734 /** Set up broad sensitivity based on the type of content that is selected */
735 void
736 ContentPanel::setup_sensitivity ()
737 {
738         _add_file->Enable (_generally_sensitive);
739         _add_folder->Enable (_generally_sensitive);
740         _add_dcp->Enable (_generally_sensitive);
741
742         auto selection = selected ();
743         auto video_selection = selected_video ();
744         auto audio_selection = selected_audio ();
745
746         _remove->Enable   (_generally_sensitive && !selection.empty());
747         _earlier->Enable  (_generally_sensitive && selection.size() == 1);
748         _later->Enable    (_generally_sensitive && selection.size() == 1);
749         _timeline->Enable (_generally_sensitive && _film && !_film->content().empty());
750
751         if (_video_panel) {
752                 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
753         }
754         if (_audio_panel) {
755                 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
756         }
757         for (auto text: _text_panel) {
758                 if (text) {
759                         text->Enable(_generally_sensitive && selection.size() == 1 && !selection.front()->text.empty());
760                 }
761         }
762         _timing_panel->Enable (_generally_sensitive);
763 }
764
765
766 void
767 ContentPanel::set_film (shared_ptr<Film> film)
768 {
769         if (_audio_panel) {
770                 _audio_panel->set_film (film);
771         }
772
773         _film = film;
774
775         film_changed (Film::Property::CONTENT);
776         film_changed (Film::Property::AUDIO_CHANNELS);
777
778         if (_film) {
779                 check_selection ();
780         }
781
782         setup_sensitivity ();
783 }
784
785
786 void
787 ContentPanel::set_general_sensitivity (bool s)
788 {
789         _generally_sensitive = s;
790         setup_sensitivity ();
791 }
792
793
794 void
795 ContentPanel::earlier_clicked ()
796 {
797         auto sel = selected ();
798         if (sel.size() == 1) {
799                 _film->move_content_earlier (sel.front ());
800                 check_selection ();
801         }
802 }
803
804
805 void
806 ContentPanel::later_clicked ()
807 {
808         auto sel = selected ();
809         if (sel.size() == 1) {
810                 _film->move_content_later (sel.front ());
811                 check_selection ();
812         }
813 }
814
815
816 void
817 ContentPanel::set_selection (weak_ptr<Content> wc)
818 {
819         auto content = _film->content ();
820         for (size_t i = 0; i < content.size(); ++i) {
821                 set_selected_state(i, content[i] == wc.lock());
822         }
823 }
824
825
826 void
827 ContentPanel::set_selection (ContentList cl)
828 {
829         {
830                 _no_check_selection = true;
831                 ScopeGuard sg = [this]() { _no_check_selection = false; };
832
833                 auto content = _film->content ();
834                 for (size_t i = 0; i < content.size(); ++i) {
835                         set_selected_state(i, find(cl.begin(), cl.end(), content[i]) != cl.end());
836                 }
837         }
838
839         check_selection ();
840 }
841
842
843 void
844 ContentPanel::select_all ()
845 {
846         set_selection (_film->content());
847 }
848
849
850 void
851 ContentPanel::film_content_changed (int property)
852 {
853         if (
854                 property == ContentProperty::PATH ||
855                 property == DCPContentProperty::NEEDS_ASSETS ||
856                 property == DCPContentProperty::NEEDS_KDM ||
857                 property == DCPContentProperty::NAME
858                 ) {
859
860                 setup ();
861         }
862
863         for (auto i: panels()) {
864                 i->film_content_changed (property);
865         }
866 }
867
868
869 void
870 ContentPanel::setup ()
871 {
872         if (!_film) {
873                 _content->DeleteAllItems ();
874                 setup_sensitivity ();
875                 return;
876         }
877
878         auto content = _film->content ();
879         auto selection = selected();
880
881         vector<ContentListCtrl::Item> items;
882
883         for (auto i: content) {
884                 bool const valid = i->paths_valid ();
885
886                 auto dcp = dynamic_pointer_cast<DCPContent> (i);
887                 bool const needs_kdm = dcp && dcp->needs_kdm ();
888                 bool const needs_assets = dcp && dcp->needs_assets ();
889
890                 auto s = std_to_wx (i->summary ());
891
892                 if (!valid) {
893                         s = _("MISSING: ") + s;
894                 }
895
896                 if (needs_kdm) {
897                         s = _("NEEDS KDM: ") + s;
898                 }
899
900                 if (needs_assets) {
901                         s = _("NEEDS OV: ") + s;
902                 }
903
904                 items.push_back({s, i, !valid || needs_kdm || needs_assets});
905         }
906
907         _content->set(items);
908
909         if (selection.empty() && !content.empty()) {
910                 set_selected_state(0, true);
911         } else {
912                 set_selection(selection);
913         }
914
915         setup_sensitivity ();
916 }
917
918
919 void
920 ContentPanel::files_dropped (wxDropFilesEvent& event)
921 {
922         if (!_film) {
923                 return;
924         }
925
926         auto paths = event.GetFiles ();
927         vector<boost::filesystem::path> path_list;
928         for (int i = 0; i < event.GetNumberOfFiles(); i++) {
929                 path_list.push_back (wx_to_std(paths[i]));
930         }
931
932         add_files (path_list);
933 }
934
935
936 void
937 ContentPanel::add_files (vector<boost::filesystem::path> paths)
938 {
939         if (!_film) {
940                 return;
941         }
942
943         /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
944            I can't reproduce that, but sort them anyway.  Don't use ImageFilenameSorter as a normal
945            alphabetical sort is expected here.
946         */
947
948         std::sort (paths.begin(), paths.end(), CaseInsensitiveSorter());
949
950         /* XXX: check for lots of files here and do something */
951
952         try {
953                 for (auto i: paths) {
954                         for (auto j: content_factory(i)) {
955                                 _film->examine_and_add_content (j);
956                         }
957                 }
958         } catch (exception& e) {
959                 error_dialog (_parent, e.what());
960         }
961 }
962
963
964 list<ContentSubPanel*>
965 ContentPanel::panels () const
966 {
967         list<ContentSubPanel*> p;
968         if (_video_panel) {
969                 p.push_back (_video_panel);
970         }
971         if (_audio_panel) {
972                 p.push_back (_audio_panel);
973         }
974         for (auto text: _text_panel) {
975                 if (text) {
976                         p.push_back(text);
977                 }
978         }
979         p.push_back (_timing_panel);
980         return p;
981 }
982
983
984 void
985 ContentPanel::set_selected_state(int item, bool state)
986 {
987         _content->SetItemState(item, state ? wxLIST_STATE_SELECTED : 0, wxLIST_STATE_SELECTED);
988         _content->SetItemState(item, state ? wxLIST_STATE_FOCUSED : 0, wxLIST_STATE_FOCUSED);
989 }
990
991
992 wxWindow*
993 ContentPanel::window() const
994 {
995         return _splitter;
996 }