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