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