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