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