2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
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"
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>
54 #include <wx/listctrl.h>
55 #include <wx/notebook.h>
57 LIBDCP_ENABLE_WARNINGS
58 #include <boost/filesystem.hpp>
61 using std::dynamic_pointer_cast;
64 using std::make_shared;
65 using std::shared_ptr;
69 using boost::optional;
70 using namespace dcpomatic;
71 #if BOOST_VERSION >= 106100
72 using namespace boost::placeholders;
76 class ContentDropTarget : public wxFileDropTarget
79 ContentDropTarget(ContentPanel* owner)
83 bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
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)) {
96 folders.push_back(path);
101 if (!filenames.empty()) {
102 _panel->add_files(files);
105 for (auto dcp: dcps) {
106 _panel->add_dcp(dcp);
109 for (auto dir: folders) {
110 _panel->add_folder(dir);
117 ContentPanel* _panel;
121 ContentPanel::ContentPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
124 , _film_viewer (viewer)
125 , _generally_sensitive (true)
126 , _ignore_deselect (false)
127 , _no_check_selection (false)
129 _splitter = new LimitedSplitter (n);
130 _top_panel = new wxPanel (_splitter);
132 _menu = new ContentMenu (_splitter, _film_viewer);
135 auto s = new wxBoxSizer (wxHORIZONTAL);
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);
141 _content->InsertColumn (0, wxT(""));
142 _content->SetColumnWidth (0, 512);
144 auto b = new wxBoxSizer (wxVERTICAL);
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);
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);
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);
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);
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);
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);
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);
174 s->Add (b, 0, wxALL, 4);
175 _top_panel->SetSizer (s);
178 _notebook = new wxNotebook (_splitter, wxID_ANY);
180 _timing_panel = new TimingPanel (this, _film_viewer);
181 _notebook->AddPage (_timing_panel, _("Timing"), false);
182 _timing_panel->create ();
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));
196 _content->SetDropTarget(new ContentDropTarget(this));
201 ContentPanel::first_shown ()
203 _splitter->first_shown (_top_panel, _notebook);
208 ContentPanel::selected ()
213 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
218 auto cl = _film->content();
219 if (s < int (cl.size())) {
220 sel.push_back (cl[s]);
229 ContentPanel::selected_video ()
233 for (auto i: selected()) {
244 ContentPanel::selected_audio ()
248 for (auto i: selected()) {
259 ContentPanel::selected_text ()
263 for (auto i: selected()) {
264 if (!i->text.empty()) {
274 ContentPanel::selected_ffmpeg ()
276 FFmpegContentList sc;
278 for (auto i: selected()) {
279 auto t = dynamic_pointer_cast<FFmpegContent> (i);
290 ContentPanel::film_changed (Film::Property p)
293 case Film::Property::CONTENT:
294 case Film::Property::CONTENT_ORDER:
301 for (auto i: panels()) {
308 ContentPanel::item_deselected ()
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.
313 _ignore_deselect = false;
314 signal_manager->when_idle (boost::bind (&ContentPanel::item_deselected_idle, this));
319 ContentPanel::item_deselected_idle ()
321 if (!_ignore_deselect) {
328 ContentPanel::item_selected ()
330 _ignore_deselect = true;
336 ContentPanel::check_selection ()
338 if (_no_check_selection) {
342 setup_sensitivity ();
344 for (auto i: panels()) {
345 i->content_selection_changed ();
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
356 StringTextFile ts(text_content);
357 if (auto first = ts.first()) {
358 position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
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()));
367 if (!go_to || position < go_to.get()) {
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));
377 if (_timeline_dialog) {
378 _timeline_dialog->set_selection (selected());
381 /* Make required tabs visible */
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.
387 _last_selected_tab = 0;
388 if (_notebook->GetSelection() != wxNOT_FOUND) {
389 _last_selected_tab = _notebook->GetPage(_notebook->GetSelection());
393 bool have_video = false;
394 bool have_audio = false;
395 EnumIndexedVector<bool, TextType> have_text;
396 for (auto i: selected()) {
403 for (auto j: i->text) {
404 have_text[static_cast<int>(j->original_type())] = true;
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);
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);
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;
450 /* Set up the tab selection */
453 for (size_t i = 0; i < _notebook->GetPageCount(); ++i) {
454 if (_notebook->GetPage(i) == _last_selected_tab) {
455 _notebook->SetSelection (i);
460 if (!done && _notebook->GetPageCount() > 0) {
461 _notebook->SetSelection (0);
464 setup_sensitivity ();
470 ContentPanel::add_file_clicked ()
472 /* This method is also called when Ctrl-A is pressed, so check that our notebook page
475 if (_parent->GetCurrentPage() != _splitter || !_film) {
479 auto path = Config::instance()->add_files_path();
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.
484 auto d = new wxFileDialog (
486 _("Choose a file or files"),
487 std_to_wx(path ? path->string() : home_directory().string()),
489 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
490 wxFD_MULTIPLE | wxFD_CHANGE_DIR
493 int const r = d->ShowModal ();
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]));
506 add_files (path_list);
508 if (!path_list.empty()) {
509 Config::instance()->set_add_files_path(path_list[0].parent_path());
517 ContentPanel::add_folder_clicked ()
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 ()));
533 ContentPanel::add_folder(boost::filesystem::path folder)
535 vector<shared_ptr<Content>> content;
538 content = content_factory(folder);
539 } catch (exception& e) {
540 error_dialog (_parent, e.what());
544 if (content.empty ()) {
545 error_dialog (_parent, _("No content found in this folder."));
549 for (auto i: content) {
550 auto ic = dynamic_pointer_cast<ImageContent> (i);
552 auto e = new ImageSequenceDialog (_splitter);
553 int const r = e->ShowModal();
554 auto const frame_rate = e->frame_rate ();
561 ic->set_video_frame_rate (frame_rate);
564 _film->examine_and_add_content (i);
570 ContentPanel::add_dcp_clicked ()
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 ()));
586 ContentPanel::add_dcp(boost::filesystem::path dcp)
589 _film->examine_and_add_content(make_shared<DCPContent>(dcp));
590 } catch (ProjectFolderError &) {
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."
598 } catch (exception& e) {
599 error_dialog(_parent, e.what());
604 /** @return true if this remove "click" should be ignored */
606 ContentPanel::remove_clicked (bool hotkey)
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.
611 if (hotkey && (_parent->GetCurrentPage() != _splitter || !_content->HasFocus())) {
615 for (auto i: selected ()) {
616 _film->remove_content (i);
625 ContentPanel::timeline_clicked ()
631 if (_timeline_dialog) {
632 _timeline_dialog->Destroy ();
633 _timeline_dialog = nullptr;
636 _timeline_dialog = new TimelineDialog (this, _film, _film_viewer);
637 _timeline_dialog->set_selection (selected());
638 _timeline_dialog->Show ();
643 ContentPanel::right_click (wxListEvent& ev)
645 _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
649 /** Set up broad sensitivity based on the type of content that is selected */
651 ContentPanel::setup_sensitivity ()
653 _add_file->Enable (_generally_sensitive);
654 _add_folder->Enable (_generally_sensitive);
655 _add_dcp->Enable (_generally_sensitive);
657 auto selection = selected ();
658 auto video_selection = selected_video ();
659 auto audio_selection = selected_audio ();
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());
667 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
670 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
672 for (auto text: _text_panel) {
674 text->Enable(_generally_sensitive && selection.size() == 1 && !selection.front()->text.empty());
677 _timing_panel->Enable (_generally_sensitive);
682 ContentPanel::set_film (shared_ptr<Film> film)
685 _audio_panel->set_film (film);
690 film_changed (Film::Property::CONTENT);
691 film_changed (Film::Property::AUDIO_CHANNELS);
697 setup_sensitivity ();
702 ContentPanel::set_general_sensitivity (bool s)
704 _generally_sensitive = s;
705 setup_sensitivity ();
710 ContentPanel::earlier_clicked ()
712 auto sel = selected ();
713 if (sel.size() == 1) {
714 _film->move_content_earlier (sel.front ());
721 ContentPanel::later_clicked ()
723 auto sel = selected ();
724 if (sel.size() == 1) {
725 _film->move_content_later (sel.front ());
732 ContentPanel::set_selection (weak_ptr<Content> wc)
734 auto content = _film->content ();
735 for (size_t i = 0; i < content.size(); ++i) {
736 set_selected_state(i, content[i] == wc.lock());
742 ContentPanel::set_selection (ContentList cl)
744 _no_check_selection = true;
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());
751 _no_check_selection = false;
757 ContentPanel::select_all ()
759 set_selection (_film->content());
764 ContentPanel::film_content_changed (int property)
767 property == ContentProperty::PATH ||
768 property == DCPContentProperty::NEEDS_ASSETS ||
769 property == DCPContentProperty::NEEDS_KDM ||
770 property == DCPContentProperty::NAME
776 for (auto i: panels()) {
777 i->film_content_changed (property);
783 ContentPanel::setup ()
786 _content->DeleteAllItems ();
787 setup_sensitivity ();
791 auto content = _film->content ();
793 Content* selected_content = nullptr;
794 auto const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
798 item.SetMask (wxLIST_MASK_DATA);
799 _content->GetItem (item);
800 selected_content = reinterpret_cast<Content*> (item.GetData ());
803 _content->DeleteAllItems ();
805 for (auto i: content) {
806 int const t = _content->GetItemCount ();
807 bool const valid = i->paths_valid ();
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 ();
813 auto s = std_to_wx (i->summary ());
816 s = _("MISSING: ") + s;
820 s = _("NEEDS KDM: ") + s;
824 s = _("NEEDS OV: ") + s;
830 item.SetData (i.get ());
831 _content->InsertItem (item);
833 if (i.get() == selected_content) {
834 set_selected_state(t, true);
837 if (!valid || needs_kdm || needs_assets) {
838 _content->SetItemTextColour (t, *wxRED);
842 if (!selected_content && !content.empty ()) {
843 /* Select the item of content if none was selected before */
844 set_selected_state(0, true);
847 setup_sensitivity ();
852 ContentPanel::files_dropped (wxDropFilesEvent& event)
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]));
864 add_files (path_list);
869 ContentPanel::add_files (vector<boost::filesystem::path> paths)
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.
880 std::sort (paths.begin(), paths.end(), CaseInsensitiveSorter());
882 /* XXX: check for lots of files here and do something */
885 for (auto i: paths) {
886 for (auto j: content_factory(i)) {
887 _film->examine_and_add_content (j);
890 } catch (exception& e) {
891 error_dialog (_parent, e.what());
896 list<ContentSubPanel*>
897 ContentPanel::panels () const
899 list<ContentSubPanel*> p;
901 p.push_back (_video_panel);
904 p.push_back (_audio_panel);
906 for (auto text: _text_panel) {
911 p.push_back (_timing_panel);
917 ContentPanel::set_selected_state(int item, bool state)
919 _content->SetItemState(item, state ? wxLIST_STATE_SELECTED : 0, wxLIST_STATE_SELECTED);
920 _content->SetItemState(item, state ? wxLIST_STATE_FOCUSED : 0, wxLIST_STATE_FOCUSED);
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)
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).
932 SetMinimumPaneSize (64);
934 Bind (wxEVT_SIZE, boost::bind(&LimitedSplitter::sized, this, _1));
939 LimitedSplitter::first_shown (wxWindow* top, wxWindow* bottom)
941 int const sn = wxDisplay::GetFromWindow(this);
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.
947 SplitHorizontally (top, bottom, screen.height > 800 ? -600 : -_top_panel_minimum_size);
949 /* Fallback for when GetFromWindow fails for reasons that aren't clear */
950 SplitHorizontally (top, bottom, -600);
957 LimitedSplitter::sized (wxSizeEvent& ev)
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).
963 SetSashPosition (_top_panel_minimum_size);