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 "content_timeline_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "dir_dialog.h"
27 #include "file_dialog.h"
28 #include "film_viewer.h"
29 #include "image_sequence_dialog.h"
30 #include "text_panel.h"
31 #include "timing_panel.h"
32 #include "video_panel.h"
34 #include "wx_variant.h"
35 #include "lib/audio_content.h"
36 #include "lib/case_insensitive_sorter.h"
37 #include "lib/compose.hpp"
38 #include "lib/config.h"
39 #include "lib/content_factory.h"
40 #include "lib/cross.h"
41 #include "lib/dcp_content.h"
42 #include "lib/dcp_subtitle_content.h"
43 #include "lib/dcp_subtitle_decoder.h"
44 #include "lib/dcpomatic_log.h"
45 #include "lib/ffmpeg_content.h"
47 #include "lib/film_util.h"
48 #include "lib/image_content.h"
50 #include "lib/playlist.h"
51 #include "lib/string_text_file.h"
52 #include "lib/string_text_file_content.h"
53 #include "lib/text_content.h"
54 #include "lib/video_content.h"
55 #include <dcp/filesystem.h>
56 #include <dcp/scope_guard.h>
57 #include <dcp/warnings.h>
58 LIBDCP_DISABLE_WARNINGS
59 #include <wx/display.h>
61 #include <wx/listctrl.h>
62 #include <wx/notebook.h>
64 LIBDCP_ENABLE_WARNINGS
65 #include <boost/filesystem.hpp>
68 using std::dynamic_pointer_cast;
71 using std::make_shared;
72 using std::shared_ptr;
76 using boost::optional;
77 using namespace dcpomatic;
78 #if BOOST_VERSION >= 106100
79 using namespace boost::placeholders;
83 class LimitedContentPanelSplitter : public wxSplitterWindow
86 LimitedContentPanelSplitter(wxWindow* parent)
87 : wxSplitterWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
89 /* This value doesn't really mean much but we just want to stop double-click on the
90 divider from shrinking the bottom panel (#1601).
92 SetMinimumPaneSize(64);
94 Bind(wxEVT_SIZE, boost::bind(&LimitedContentPanelSplitter::sized, this, _1));
97 bool OnSashPositionChange(int new_position) override
99 /* Try to stop the top bit of the splitter getting so small that buttons disappear */
100 auto const ok = new_position > 220;
102 Config::instance()->set_main_content_divider_sash_position(new_position);
107 void first_shown(wxWindow* top, wxWindow* bottom)
109 int const sn = wxDisplay::GetFromWindow(this);
110 /* Fallback for when GetFromWindow fails for reasons that aren't clear */
113 wxRect const screen = wxDisplay(sn).GetClientArea();
114 /* This is a hack to try and make the content notebook a sensible size; large on big displays but small
115 enough on small displays to leave space for the content area.
117 pos = screen.height > 800 ? -600 : -_top_panel_minimum_size;
119 SplitHorizontally(top, bottom, Config::instance()->main_content_divider_sash_position().get_value_or(pos));
124 void sized(wxSizeEvent& ev)
126 auto const height = GetSize().GetHeight();
127 if (_first_shown && (!_last_height || *_last_height != height) && height > _top_panel_minimum_size && GetSashPosition() < _top_panel_minimum_size) {
128 /* The window is now fairly big but the top panel is small; this happens when the DCP-o-matic window
129 * is shrunk and then made larger again. Try to set a sensible top panel size in this case (#1839).
131 SetSashPosition(Config::instance()->main_content_divider_sash_position().get_value_or(_top_panel_minimum_size));
135 _last_height = height;
138 bool _first_shown = false;
139 int const _top_panel_minimum_size = 350;
140 boost::optional<int> _last_height;
144 class ContentDropTarget : public wxFileDropTarget
147 ContentDropTarget(ContentPanel* owner)
151 bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
153 vector<boost::filesystem::path> files;
154 vector<boost::filesystem::path> dcps;
155 vector<boost::filesystem::path> folders;
156 for (size_t i = 0; i < filenames.GetCount(); ++i) {
157 auto path = boost::filesystem::path(wx_to_std(filenames[i]));
158 if (dcp::filesystem::is_regular_file(path)) {
159 files.push_back(path);
160 } else if (dcp::filesystem::is_directory(path)) {
161 if (contains_assetmap(path)) {
162 dcps.push_back(path);
164 folders.push_back(path);
169 if (!filenames.empty()) {
170 _panel->add_files(files);
173 for (auto dcp: dcps) {
174 _panel->add_dcp(dcp);
177 for (auto dir: folders) {
178 _panel->add_folder(dir);
185 ContentPanel* _panel;
189 /** A wxListCtrl that can middle-ellipsize its text */
190 class ContentListCtrl : public wxListCtrl
193 ContentListCtrl(wxWindow* parent)
194 : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxSize(320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_VIRTUAL)
196 _red.SetTextColour(*wxRED);
202 weak_ptr<Content> content;
206 void set(vector<Item> const& items)
209 SetItemCount(items.size());
212 wxString OnGetItemText(long item, long) const override
214 DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
215 wxClientDC dc(const_cast<wxWindow*>(static_cast<wxWindow const*>(this)));
216 return wxControl::Ellipsize(_items[item].text, dc, wxELLIPSIZE_MIDDLE, GetSize().GetWidth());
219 wxListItemAttr* OnGetItemAttr(long item) const override
221 DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
222 return _items[item].error ? const_cast<wxListItemAttr*>(&_red) : nullptr;
225 weak_ptr<Content> content_at_index(long index)
227 if (index < 0 || index >= static_cast<long>(_items.size())) {
230 return _items[index].content;
234 std::vector<Item> _items;
239 ContentPanel::ContentPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
242 , _film_viewer (viewer)
243 , _generally_sensitive (true)
244 , _ignore_deselect (false)
245 , _no_check_selection (false)
247 _splitter = new LimitedContentPanelSplitter(n);
248 _top_panel = new wxPanel (_splitter);
250 _menu = new ContentMenu (_splitter, _film_viewer);
253 auto s = new wxBoxSizer (wxHORIZONTAL);
255 _content = new ContentListCtrl(_top_panel);
256 _content->DragAcceptFiles (true);
257 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
259 _content->InsertColumn (0, wxT(""));
260 _content->SetColumnWidth(0, 2048);
262 auto b = new wxBoxSizer (wxVERTICAL);
264 _add_file = new Button (_top_panel, _("Add file(s)..."));
265 _add_file->SetToolTip(_("Add video, image, sound or subtitle files to the film (Ctrl+A)."));
266 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
268 _add_folder = new Button (_top_panel, _("Add folder..."));
269 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a folder of sound files."));
270 b->Add (_add_folder, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
272 _add_dcp = new Button (_top_panel, _("Add DCP..."));
273 _add_dcp->SetToolTip (_("Add a DCP."));
274 b->Add (_add_dcp, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
276 _remove = new Button (_top_panel, _("Remove"));
277 _remove->SetToolTip(_("Remove the selected piece of content from the film (Delete)."));
278 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
280 _earlier = new Button (_top_panel, _("Earlier"));
281 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
282 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
284 _later = new Button (_top_panel, _("Later"));
285 _later->SetToolTip (_("Move the selected piece of content later in the film."));
286 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
288 _timeline = new Button (_top_panel, _("Timeline..."));
289 _timeline->SetToolTip(_("Open the timeline for the film (Ctrl+T)."));
290 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
292 s->Add (b, 0, wxALL, 4);
293 _top_panel->SetSizer (s);
296 _notebook = new wxNotebook (_splitter, wxID_ANY);
298 _timing_panel = new TimingPanel (this, _film_viewer);
299 _notebook->AddPage (_timing_panel, _("Timing"), false);
300 _timing_panel->create ();
302 _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::item_selected, this));
303 _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::item_deselected, this));
304 _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
305 _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
306 _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
307 _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
308 _add_dcp->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_dcp_clicked, this));
309 _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
310 _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
311 _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
312 _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
314 _content->SetDropTarget(new ContentDropTarget(this));
319 ContentPanel::first_shown ()
321 _splitter->first_shown (_top_panel, _notebook);
326 ContentPanel::selected ()
331 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
336 auto weak = _content->content_at_index(s);
337 if (auto content = weak.lock()) {
338 sel.push_back(content);
347 ContentPanel::selected_video ()
351 for (auto i: selected()) {
362 ContentPanel::selected_audio ()
366 for (auto i: selected()) {
377 ContentPanel::selected_text ()
381 for (auto i: selected()) {
382 if (!i->text.empty()) {
392 ContentPanel::selected_ffmpeg ()
394 FFmpegContentList sc;
396 for (auto i: selected()) {
397 auto t = dynamic_pointer_cast<FFmpegContent> (i);
408 ContentPanel::film_changed(FilmProperty p)
411 case FilmProperty::CONTENT:
412 case FilmProperty::CONTENT_ORDER:
419 for (auto i: panels()) {
426 ContentPanel::item_deselected ()
428 /* Maybe this is just a re-click on the same item; if not, _ignore_deselect will stay
429 false and item_deselected_foo will handle the deselection.
431 _ignore_deselect = false;
432 signal_manager->when_idle (boost::bind (&ContentPanel::item_deselected_idle, this));
437 ContentPanel::item_deselected_idle ()
439 if (!_ignore_deselect) {
446 ContentPanel::item_selected ()
448 _ignore_deselect = true;
454 ContentPanel::check_selection ()
456 if (_no_check_selection) {
460 setup_sensitivity ();
462 for (auto i: panels()) {
463 i->content_selection_changed ();
466 optional<DCPTime> go_to;
467 for (auto content: selected()) {
468 if (content->paths_valid()) {
469 auto position = content->position();
470 if (auto text_content = dynamic_pointer_cast<StringTextFileContent>(content)) {
471 /* Rather special case; if we select a text subtitle file jump to its
474 StringTextFile ts(text_content);
475 if (auto first = ts.first()) {
476 position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
478 } else if (auto dcp_content = dynamic_pointer_cast<DCPSubtitleContent>(content)) {
479 /* Do the same for DCP subtitles */
480 DCPSubtitleDecoder ts(_film, dcp_content);
481 if (auto first = ts.first()) {
482 position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
485 if (!go_to || position < go_to.get()) {
491 if (go_to && Config::instance()->jump_to_selected() && signal_manager) {
492 signal_manager->when_idle(boost::bind(&FilmViewer::seek, &_film_viewer, go_to.get().ceil(_film->video_frame_rate()), true));
495 if (_timeline_dialog) {
496 _timeline_dialog->set_selection (selected());
499 /* Make required tabs visible */
501 if (_notebook->GetPageCount() > 1) {
502 /* There's more than one tab in the notebook so the current selection could be meaningful
503 to the user; store it so that we can try to restore it later.
505 _last_selected_tab = 0;
506 if (_notebook->GetSelection() != wxNOT_FOUND) {
507 _last_selected_tab = _notebook->GetPage(_notebook->GetSelection());
511 bool have_video = false;
512 bool have_audio = false;
513 EnumIndexedVector<bool, TextType> have_text;
514 for (auto i: selected()) {
521 for (auto j: i->text) {
522 have_text[static_cast<int>(j->original_type())] = true;
528 if (have_video && !_video_panel) {
529 _video_panel = new VideoPanel (this);
530 _notebook->InsertPage (off, _video_panel, _video_panel->name());
531 _video_panel->create ();
532 } else if (!have_video && _video_panel) {
533 _notebook->DeletePage (off);
534 _video_panel = nullptr;
541 if (have_audio && !_audio_panel) {
542 _audio_panel = new AudioPanel (this);
543 _notebook->InsertPage (off, _audio_panel, _audio_panel->name());
544 _audio_panel->create ();
545 } else if (!have_audio && _audio_panel) {
546 _notebook->DeletePage (off);
547 _audio_panel = nullptr;
554 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
555 if (have_text[i] && !_text_panel[i]) {
556 _text_panel[i] = new TextPanel (this, static_cast<TextType>(i));
557 _notebook->InsertPage (off, _text_panel[i], _text_panel[i]->name());
558 _text_panel[i]->create ();
559 } else if (!have_text[i] && _text_panel[i]) {
560 _notebook->DeletePage (off);
561 _text_panel[i] = nullptr;
568 /* Set up the tab selection */
571 for (size_t i = 0; i < _notebook->GetPageCount(); ++i) {
572 if (_notebook->GetPage(i) == _last_selected_tab) {
573 _notebook->SetSelection (i);
578 if (!done && _notebook->GetPageCount() > 0) {
579 _notebook->SetSelection (0);
582 setup_sensitivity ();
588 ContentPanel::add_file_clicked ()
590 /* This method is also called when Ctrl-A is pressed, so check that our notebook page
593 if (_parent->GetCurrentPage() != _splitter || !_film) {
597 /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
598 non-Latin filenames or paths.
602 _("Choose a file or files"),
603 wxT("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
604 wxFD_MULTIPLE | wxFD_CHANGE_DIR,
607 add_files_override_path(_film)
611 add_files(dialog.paths());
617 ContentPanel::add_folder_clicked ()
619 DirDialog dialog(_splitter, _("Choose a folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path(_film));
621 add_folder(dialog.path());
627 ContentPanel::add_folder(boost::filesystem::path folder)
629 vector<shared_ptr<Content>> content;
632 content = content_factory(folder);
633 } catch (exception& e) {
634 error_dialog (_parent, e.what());
638 if (content.empty ()) {
639 error_dialog (_parent, _("No content found in this folder."));
643 for (auto i: content) {
644 auto ic = dynamic_pointer_cast<ImageContent> (i);
646 ImageSequenceDialog dialog(_splitter);
648 if (dialog.ShowModal() != wxID_OK) {
651 ic->set_video_frame_rate(_film, dialog.frame_rate());
654 _film->examine_and_add_content (i);
660 ContentPanel::add_dcp_clicked ()
662 DirDialog dialog(_splitter, _("Choose a DCP folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path(_film));
664 add_dcp(dialog.path());
670 ContentPanel::add_dcp(boost::filesystem::path dcp)
673 _film->examine_and_add_content(make_shared<DCPContent>(dcp));
674 } catch (ProjectFolderError &) {
679 "This looks like a %s project folder, which cannot be added to a different project. "
680 "Choose the DCP folder inside the %s project folder if that's what you want to import."
682 variant::wx::dcpomatic(),
683 variant::wx::dcpomatic()
686 } catch (exception& e) {
687 error_dialog(_parent, e.what());
692 /** @return true if this remove "click" should be ignored */
694 ContentPanel::remove_clicked (bool hotkey)
696 /* If the method was called because Delete was pressed check that our notebook page
697 is visible and that the content list is focused.
699 if (hotkey && (_parent->GetCurrentPage() != _splitter || !_content->HasFocus())) {
703 for (auto i: selected ()) {
704 _film->remove_content (i);
713 ContentPanel::timeline_clicked ()
715 if (!_film || _film->content().empty()) {
719 _timeline_dialog.reset(this, _film, _film_viewer);
720 _timeline_dialog->set_selection (selected());
721 _timeline_dialog->Show ();
726 ContentPanel::right_click (wxListEvent& ev)
728 _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
732 /** Set up broad sensitivity based on the type of content that is selected */
734 ContentPanel::setup_sensitivity ()
736 _add_file->Enable (_generally_sensitive);
737 _add_folder->Enable (_generally_sensitive);
738 _add_dcp->Enable (_generally_sensitive);
740 auto selection = selected ();
741 auto video_selection = selected_video ();
742 auto audio_selection = selected_audio ();
744 _remove->Enable (_generally_sensitive && !selection.empty());
745 _earlier->Enable (_generally_sensitive && selection.size() == 1);
746 _later->Enable (_generally_sensitive && selection.size() == 1);
747 _timeline->Enable (_generally_sensitive && _film && !_film->content().empty());
750 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
753 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
755 for (auto text: _text_panel) {
757 text->Enable(_generally_sensitive && selection.size() == 1 && !selection.front()->text.empty());
760 _timing_panel->Enable (_generally_sensitive);
765 ContentPanel::set_film (shared_ptr<Film> film)
768 _audio_panel->set_film (film);
773 film_changed(FilmProperty::CONTENT);
774 film_changed(FilmProperty::AUDIO_CHANNELS);
780 setup_sensitivity ();
785 ContentPanel::set_general_sensitivity (bool s)
787 _generally_sensitive = s;
788 setup_sensitivity ();
793 ContentPanel::earlier_clicked ()
795 auto sel = selected ();
796 if (sel.size() == 1) {
797 _film->move_content_earlier (sel.front ());
804 ContentPanel::later_clicked ()
806 auto sel = selected ();
807 if (sel.size() == 1) {
808 _film->move_content_later (sel.front ());
815 ContentPanel::set_selection (weak_ptr<Content> wc)
817 auto content = _film->content ();
818 for (size_t i = 0; i < content.size(); ++i) {
819 set_selected_state(i, content[i] == wc.lock());
825 ContentPanel::set_selection (ContentList cl)
828 _no_check_selection = true;
829 dcp::ScopeGuard sg = [this]() { _no_check_selection = false; };
831 auto content = _film->content ();
832 for (size_t i = 0; i < content.size(); ++i) {
833 set_selected_state(i, find(cl.begin(), cl.end(), content[i]) != cl.end());
842 ContentPanel::select_all ()
844 set_selection (_film->content());
849 ContentPanel::film_content_changed (int property)
852 property == ContentProperty::PATH ||
853 property == DCPContentProperty::NEEDS_ASSETS ||
854 property == DCPContentProperty::NEEDS_KDM ||
855 property == DCPContentProperty::NAME
861 for (auto i: panels()) {
862 i->film_content_changed (property);
868 ContentPanel::setup ()
871 _content->DeleteAllItems ();
872 setup_sensitivity ();
876 auto content = _film->content ();
877 auto selection = selected();
879 vector<ContentListCtrl::Item> items;
881 for (auto i: content) {
882 bool const valid = i->paths_valid ();
884 auto dcp = dynamic_pointer_cast<DCPContent> (i);
885 bool const needs_kdm = dcp && dcp->needs_kdm ();
886 bool const needs_assets = dcp && dcp->needs_assets ();
888 auto s = std_to_wx (i->summary ());
891 s = _("MISSING: ") + s;
895 s = _("NEEDS KDM: ") + s;
899 s = _("NEEDS OV: ") + s;
902 items.push_back({s, i, !valid || needs_kdm || needs_assets});
905 _content->set(items);
907 if (selection.empty() && !content.empty()) {
908 set_selected_state(0, true);
910 set_selection(selection);
913 setup_sensitivity ();
918 ContentPanel::files_dropped (wxDropFilesEvent& event)
924 auto paths = event.GetFiles ();
925 vector<boost::filesystem::path> path_list;
926 for (int i = 0; i < event.GetNumberOfFiles(); i++) {
927 path_list.push_back (wx_to_std(paths[i]));
930 add_files (path_list);
935 ContentPanel::add_files (vector<boost::filesystem::path> paths)
941 /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
942 I can't reproduce that, but sort them anyway. Don't use ImageFilenameSorter as a normal
943 alphabetical sort is expected here.
946 std::sort (paths.begin(), paths.end(), CaseInsensitiveSorter());
948 /* XXX: check for lots of files here and do something */
951 for (auto i: paths) {
952 for (auto j: content_factory(i)) {
953 _film->examine_and_add_content (j);
956 } catch (exception& e) {
957 error_dialog (_parent, e.what());
962 list<ContentSubPanel*>
963 ContentPanel::panels () const
965 list<ContentSubPanel*> p;
967 p.push_back (_video_panel);
970 p.push_back (_audio_panel);
972 for (auto text: _text_panel) {
977 p.push_back (_timing_panel);
983 ContentPanel::set_selected_state(int item, bool state)
985 _content->SetItemState(item, state ? wxLIST_STATE_SELECTED : 0, wxLIST_STATE_SELECTED);
986 _content->SetItemState(item, state ? wxLIST_STATE_FOCUSED : 0, wxLIST_STATE_FOCUSED);
991 ContentPanel::window() const