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 LimitedContentPanelSplitter : public wxSplitterWindow
79 LimitedContentPanelSplitter(wxWindow* parent)
80 : wxSplitterWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
82 /* This value doesn't really mean much but we just want to stop double-click on the
83 divider from shrinking the bottom panel (#1601).
85 SetMinimumPaneSize(64);
87 Bind(wxEVT_SIZE, boost::bind(&LimitedContentPanelSplitter::sized, this, _1));
90 bool OnSashPositionChange(int new_position) override
92 /* Try to stop the top bit of the splitter getting so small that buttons disappear */
93 auto const ok = new_position > 220;
95 Config::instance()->set_main_content_divider_sash_position(new_position);
100 void first_shown(wxWindow* top, wxWindow* bottom)
102 int const sn = wxDisplay::GetFromWindow(this);
103 /* Fallback for when GetFromWindow fails for reasons that aren't clear */
106 wxRect const screen = wxDisplay(sn).GetClientArea();
107 /* This is a hack to try and make the content notebook a sensible size; large on big displays but small
108 enough on small displays to leave space for the content area.
110 pos = screen.height > 800 ? -600 : -_top_panel_minimum_size;
112 SplitHorizontally(top, bottom, Config::instance()->main_content_divider_sash_position().get_value_or(pos));
117 void sized(wxSizeEvent& ev)
119 auto const height = GetSize().GetHeight();
120 if (_first_shown && (!_last_height || *_last_height != height) && height > _top_panel_minimum_size && GetSashPosition() < _top_panel_minimum_size) {
121 /* The window is now fairly big but the top panel is small; this happens when the DCP-o-matic window
122 * is shrunk and then made larger again. Try to set a sensible top panel size in this case (#1839).
124 SetSashPosition(Config::instance()->main_content_divider_sash_position().get_value_or(_top_panel_minimum_size));
128 _last_height = height;
131 bool _first_shown = false;
132 int const _top_panel_minimum_size = 350;
133 boost::optional<int> _last_height;
137 class ContentDropTarget : public wxFileDropTarget
140 ContentDropTarget(ContentPanel* owner)
144 bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
146 vector<boost::filesystem::path> files;
147 vector<boost::filesystem::path> dcps;
148 vector<boost::filesystem::path> folders;
149 for (size_t i = 0; i < filenames.GetCount(); ++i) {
150 auto path = boost::filesystem::path(wx_to_std(filenames[i]));
151 if (boost::filesystem::is_regular_file(path)) {
152 files.push_back(path);
153 } else if (boost::filesystem::is_directory(path)) {
154 if (contains_assetmap(path)) {
155 dcps.push_back(path);
157 folders.push_back(path);
162 if (!filenames.empty()) {
163 _panel->add_files(files);
166 for (auto dcp: dcps) {
167 _panel->add_dcp(dcp);
170 for (auto dir: folders) {
171 _panel->add_folder(dir);
178 ContentPanel* _panel;
182 ContentPanel::ContentPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
185 , _film_viewer (viewer)
186 , _generally_sensitive (true)
187 , _ignore_deselect (false)
188 , _no_check_selection (false)
190 _splitter = new LimitedContentPanelSplitter(n);
191 _top_panel = new wxPanel (_splitter);
193 _menu = new ContentMenu (_splitter, _film_viewer);
196 auto s = new wxBoxSizer (wxHORIZONTAL);
198 _content = new wxListCtrl (_top_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
199 _content->DragAcceptFiles (true);
200 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
202 _content->InsertColumn (0, wxT(""));
203 _content->SetColumnWidth (0, 512);
205 auto b = new wxBoxSizer (wxVERTICAL);
207 _add_file = new Button (_top_panel, _("Add file(s)..."));
208 _add_file->SetToolTip (_("Add video, image, sound or subtitle files to the film."));
209 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
211 _add_folder = new Button (_top_panel, _("Add folder..."));
212 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a folder of sound files."));
213 b->Add (_add_folder, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
215 _add_dcp = new Button (_top_panel, _("Add DCP..."));
216 _add_dcp->SetToolTip (_("Add a DCP."));
217 b->Add (_add_dcp, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
219 _remove = new Button (_top_panel, _("Remove"));
220 _remove->SetToolTip (_("Remove the selected piece of content from the film."));
221 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
223 _earlier = new Button (_top_panel, _("Earlier"));
224 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
225 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
227 _later = new Button (_top_panel, _("Later"));
228 _later->SetToolTip (_("Move the selected piece of content later in the film."));
229 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
231 _timeline = new Button (_top_panel, _("Timeline..."));
232 _timeline->SetToolTip (_("Open the timeline for the film."));
233 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
235 s->Add (b, 0, wxALL, 4);
236 _top_panel->SetSizer (s);
239 _notebook = new wxNotebook (_splitter, wxID_ANY);
241 _timing_panel = new TimingPanel (this, _film_viewer);
242 _notebook->AddPage (_timing_panel, _("Timing"), false);
243 _timing_panel->create ();
245 _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::item_selected, this));
246 _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::item_deselected, this));
247 _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
248 _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
249 _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
250 _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
251 _add_dcp->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_dcp_clicked, this));
252 _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
253 _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
254 _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
255 _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
257 _content->SetDropTarget(new ContentDropTarget(this));
262 ContentPanel::first_shown ()
264 _splitter->first_shown (_top_panel, _notebook);
269 ContentPanel::selected ()
274 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
279 auto cl = _film->content();
280 if (s < int (cl.size())) {
281 sel.push_back (cl[s]);
290 ContentPanel::selected_video ()
294 for (auto i: selected()) {
305 ContentPanel::selected_audio ()
309 for (auto i: selected()) {
320 ContentPanel::selected_text ()
324 for (auto i: selected()) {
325 if (!i->text.empty()) {
335 ContentPanel::selected_ffmpeg ()
337 FFmpegContentList sc;
339 for (auto i: selected()) {
340 auto t = dynamic_pointer_cast<FFmpegContent> (i);
351 ContentPanel::film_changed (Film::Property p)
354 case Film::Property::CONTENT:
355 case Film::Property::CONTENT_ORDER:
362 for (auto i: panels()) {
369 ContentPanel::item_deselected ()
371 /* Maybe this is just a re-click on the same item; if not, _ignore_deselect will stay
372 false and item_deselected_foo will handle the deselection.
374 _ignore_deselect = false;
375 signal_manager->when_idle (boost::bind (&ContentPanel::item_deselected_idle, this));
380 ContentPanel::item_deselected_idle ()
382 if (!_ignore_deselect) {
389 ContentPanel::item_selected ()
391 _ignore_deselect = true;
397 ContentPanel::check_selection ()
399 if (_no_check_selection) {
403 setup_sensitivity ();
405 for (auto i: panels()) {
406 i->content_selection_changed ();
409 optional<DCPTime> go_to;
410 for (auto content: selected()) {
411 if (content->paths_valid()) {
412 auto position = content->position();
413 if (auto text_content = dynamic_pointer_cast<StringTextFileContent>(content)) {
414 /* Rather special case; if we select a text subtitle file jump to its
417 StringTextFile ts(text_content);
418 if (auto first = ts.first()) {
419 position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
421 } else if (auto dcp_content = dynamic_pointer_cast<DCPSubtitleContent>(content)) {
422 /* Do the same for DCP subtitles */
423 DCPSubtitleDecoder ts(_film, dcp_content);
424 if (auto first = ts.first()) {
425 position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
428 if (!go_to || position < go_to.get()) {
434 if (go_to && Config::instance()->jump_to_selected() && signal_manager) {
435 signal_manager->when_idle(boost::bind(&FilmViewer::seek, &_film_viewer, go_to.get().ceil(_film->video_frame_rate()), true));
438 if (_timeline_dialog) {
439 _timeline_dialog->set_selection (selected());
442 /* Make required tabs visible */
444 if (_notebook->GetPageCount() > 1) {
445 /* There's more than one tab in the notebook so the current selection could be meaningful
446 to the user; store it so that we can try to restore it later.
448 _last_selected_tab = 0;
449 if (_notebook->GetSelection() != wxNOT_FOUND) {
450 _last_selected_tab = _notebook->GetPage(_notebook->GetSelection());
454 bool have_video = false;
455 bool have_audio = false;
456 EnumIndexedVector<bool, TextType> have_text;
457 for (auto i: selected()) {
464 for (auto j: i->text) {
465 have_text[static_cast<int>(j->original_type())] = true;
471 if (have_video && !_video_panel) {
472 _video_panel = new VideoPanel (this);
473 _notebook->InsertPage (off, _video_panel, _video_panel->name());
474 _video_panel->create ();
475 } else if (!have_video && _video_panel) {
476 _notebook->DeletePage (off);
484 if (have_audio && !_audio_panel) {
485 _audio_panel = new AudioPanel (this);
486 _notebook->InsertPage (off, _audio_panel, _audio_panel->name());
487 _audio_panel->create ();
488 } else if (!have_audio && _audio_panel) {
489 _notebook->DeletePage (off);
497 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
498 if (have_text[i] && !_text_panel[i]) {
499 _text_panel[i] = new TextPanel (this, static_cast<TextType>(i));
500 _notebook->InsertPage (off, _text_panel[i], _text_panel[i]->name());
501 _text_panel[i]->create ();
502 } else if (!have_text[i] && _text_panel[i]) {
503 _notebook->DeletePage (off);
504 _text_panel[i] = nullptr;
511 /* Set up the tab selection */
514 for (size_t i = 0; i < _notebook->GetPageCount(); ++i) {
515 if (_notebook->GetPage(i) == _last_selected_tab) {
516 _notebook->SetSelection (i);
521 if (!done && _notebook->GetPageCount() > 0) {
522 _notebook->SetSelection (0);
525 setup_sensitivity ();
531 ContentPanel::add_file_clicked ()
533 /* This method is also called when Ctrl-A is pressed, so check that our notebook page
536 if (_parent->GetCurrentPage() != _splitter || !_film) {
540 auto path = Config::instance()->add_files_path();
542 /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
543 non-Latin filenames or paths.
545 auto d = new wxFileDialog (
547 _("Choose a file or files"),
548 std_to_wx(path ? path->string() : home_directory().string()),
550 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
551 wxFD_MULTIPLE | wxFD_CHANGE_DIR
554 int const r = d->ShowModal ();
563 vector<boost::filesystem::path> path_list;
564 for (unsigned int i = 0; i < paths.GetCount(); ++i) {
565 path_list.push_back (wx_to_std(paths[i]));
567 add_files (path_list);
569 if (!path_list.empty()) {
570 Config::instance()->set_add_files_path(path_list[0].parent_path());
578 ContentPanel::add_folder_clicked ()
580 auto d = new wxDirDialog (_splitter, _("Choose a folder"), wxT(""), wxDD_DIR_MUST_EXIST);
581 int r = d->ShowModal ();
582 boost::filesystem::path const path (wx_to_std (d->GetPath ()));
594 ContentPanel::add_folder(boost::filesystem::path folder)
596 vector<shared_ptr<Content>> content;
599 content = content_factory(folder);
600 } catch (exception& e) {
601 error_dialog (_parent, e.what());
605 if (content.empty ()) {
606 error_dialog (_parent, _("No content found in this folder."));
610 for (auto i: content) {
611 auto ic = dynamic_pointer_cast<ImageContent> (i);
613 auto e = new ImageSequenceDialog (_splitter);
614 int const r = e->ShowModal();
615 auto const frame_rate = e->frame_rate ();
622 ic->set_video_frame_rate (frame_rate);
625 _film->examine_and_add_content (i);
631 ContentPanel::add_dcp_clicked ()
633 auto d = new wxDirDialog (_splitter, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST);
634 int r = d->ShowModal ();
635 boost::filesystem::path const path (wx_to_std (d->GetPath ()));
647 ContentPanel::add_dcp(boost::filesystem::path dcp)
650 _film->examine_and_add_content(make_shared<DCPContent>(dcp));
651 } catch (ProjectFolderError &) {
655 "This looks like a DCP-o-matic project folder, which cannot be added to a different project. "
656 "Choose the DCP folder inside the DCP-o-matic project folder if that's what you want to import."
659 } catch (exception& e) {
660 error_dialog(_parent, e.what());
665 /** @return true if this remove "click" should be ignored */
667 ContentPanel::remove_clicked (bool hotkey)
669 /* If the method was called because Delete was pressed check that our notebook page
670 is visible and that the content list is focused.
672 if (hotkey && (_parent->GetCurrentPage() != _splitter || !_content->HasFocus())) {
676 for (auto i: selected ()) {
677 _film->remove_content (i);
686 ContentPanel::timeline_clicked ()
692 if (_timeline_dialog) {
693 _timeline_dialog->Destroy ();
694 _timeline_dialog = nullptr;
697 _timeline_dialog = new TimelineDialog (this, _film, _film_viewer);
698 _timeline_dialog->set_selection (selected());
699 _timeline_dialog->Show ();
704 ContentPanel::right_click (wxListEvent& ev)
706 _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
710 /** Set up broad sensitivity based on the type of content that is selected */
712 ContentPanel::setup_sensitivity ()
714 _add_file->Enable (_generally_sensitive);
715 _add_folder->Enable (_generally_sensitive);
716 _add_dcp->Enable (_generally_sensitive);
718 auto selection = selected ();
719 auto video_selection = selected_video ();
720 auto audio_selection = selected_audio ();
722 _remove->Enable (_generally_sensitive && !selection.empty());
723 _earlier->Enable (_generally_sensitive && selection.size() == 1);
724 _later->Enable (_generally_sensitive && selection.size() == 1);
725 _timeline->Enable (_generally_sensitive && _film && !_film->content().empty());
728 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
731 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
733 for (auto text: _text_panel) {
735 text->Enable(_generally_sensitive && selection.size() == 1 && !selection.front()->text.empty());
738 _timing_panel->Enable (_generally_sensitive);
743 ContentPanel::set_film (shared_ptr<Film> film)
746 _audio_panel->set_film (film);
751 film_changed (Film::Property::CONTENT);
752 film_changed (Film::Property::AUDIO_CHANNELS);
758 setup_sensitivity ();
763 ContentPanel::set_general_sensitivity (bool s)
765 _generally_sensitive = s;
766 setup_sensitivity ();
771 ContentPanel::earlier_clicked ()
773 auto sel = selected ();
774 if (sel.size() == 1) {
775 _film->move_content_earlier (sel.front ());
782 ContentPanel::later_clicked ()
784 auto sel = selected ();
785 if (sel.size() == 1) {
786 _film->move_content_later (sel.front ());
793 ContentPanel::set_selection (weak_ptr<Content> wc)
795 auto content = _film->content ();
796 for (size_t i = 0; i < content.size(); ++i) {
797 set_selected_state(i, content[i] == wc.lock());
803 ContentPanel::set_selection (ContentList cl)
805 _no_check_selection = true;
807 auto content = _film->content ();
808 for (size_t i = 0; i < content.size(); ++i) {
809 set_selected_state(i, find(cl.begin(), cl.end(), content[i]) != cl.end());
812 _no_check_selection = false;
818 ContentPanel::select_all ()
820 set_selection (_film->content());
825 ContentPanel::film_content_changed (int property)
828 property == ContentProperty::PATH ||
829 property == DCPContentProperty::NEEDS_ASSETS ||
830 property == DCPContentProperty::NEEDS_KDM ||
831 property == DCPContentProperty::NAME
837 for (auto i: panels()) {
838 i->film_content_changed (property);
844 ContentPanel::setup ()
847 _content->DeleteAllItems ();
848 setup_sensitivity ();
852 auto content = _film->content ();
854 Content* selected_content = nullptr;
855 auto const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
859 item.SetMask (wxLIST_MASK_DATA);
860 _content->GetItem (item);
861 selected_content = reinterpret_cast<Content*> (item.GetData ());
864 _content->DeleteAllItems ();
866 for (auto i: content) {
867 int const t = _content->GetItemCount ();
868 bool const valid = i->paths_valid ();
870 auto dcp = dynamic_pointer_cast<DCPContent> (i);
871 bool const needs_kdm = dcp && dcp->needs_kdm ();
872 bool const needs_assets = dcp && dcp->needs_assets ();
874 auto s = std_to_wx (i->summary ());
877 s = _("MISSING: ") + s;
881 s = _("NEEDS KDM: ") + s;
885 s = _("NEEDS OV: ") + s;
891 item.SetData (i.get ());
892 _content->InsertItem (item);
894 if (i.get() == selected_content) {
895 set_selected_state(t, true);
898 if (!valid || needs_kdm || needs_assets) {
899 _content->SetItemTextColour (t, *wxRED);
903 if (!selected_content && !content.empty ()) {
904 /* Select the item of content if none was selected before */
905 set_selected_state(0, true);
908 setup_sensitivity ();
913 ContentPanel::files_dropped (wxDropFilesEvent& event)
919 auto paths = event.GetFiles ();
920 vector<boost::filesystem::path> path_list;
921 for (int i = 0; i < event.GetNumberOfFiles(); i++) {
922 path_list.push_back (wx_to_std(paths[i]));
925 add_files (path_list);
930 ContentPanel::add_files (vector<boost::filesystem::path> paths)
936 /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
937 I can't reproduce that, but sort them anyway. Don't use ImageFilenameSorter as a normal
938 alphabetical sort is expected here.
941 std::sort (paths.begin(), paths.end(), CaseInsensitiveSorter());
943 /* XXX: check for lots of files here and do something */
946 for (auto i: paths) {
947 for (auto j: content_factory(i)) {
948 _film->examine_and_add_content (j);
951 } catch (exception& e) {
952 error_dialog (_parent, e.what());
957 list<ContentSubPanel*>
958 ContentPanel::panels () const
960 list<ContentSubPanel*> p;
962 p.push_back (_video_panel);
965 p.push_back (_audio_panel);
967 for (auto text: _text_panel) {
972 p.push_back (_timing_panel);
978 ContentPanel::set_selected_state(int item, bool state)
980 _content->SetItemState(item, state ? wxLIST_STATE_SELECTED : 0, wxLIST_STATE_SELECTED);
981 _content->SetItemState(item, state ? wxLIST_STATE_FOCUSED : 0, wxLIST_STATE_FOCUSED);
986 ContentPanel::window() const