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