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 "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"
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"
46 #include "lib/image_content.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>
59 #include <wx/listctrl.h>
60 #include <wx/notebook.h>
62 LIBDCP_ENABLE_WARNINGS
63 #include <boost/filesystem.hpp>
66 using std::dynamic_pointer_cast;
69 using std::make_shared;
70 using std::shared_ptr;
74 using boost::optional;
75 using namespace dcpomatic;
76 #if BOOST_VERSION >= 106100
77 using namespace boost::placeholders;
81 class LimitedContentPanelSplitter : public wxSplitterWindow
84 LimitedContentPanelSplitter(wxWindow* parent)
85 : wxSplitterWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
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).
90 SetMinimumPaneSize(64);
92 Bind(wxEVT_SIZE, boost::bind(&LimitedContentPanelSplitter::sized, this, _1));
95 bool OnSashPositionChange(int new_position) override
97 /* Try to stop the top bit of the splitter getting so small that buttons disappear */
98 auto const ok = new_position > 220;
100 Config::instance()->set_main_content_divider_sash_position(new_position);
105 void first_shown(wxWindow* top, wxWindow* bottom)
107 int const sn = wxDisplay::GetFromWindow(this);
108 /* Fallback for when GetFromWindow fails for reasons that aren't clear */
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.
115 pos = screen.height > 800 ? -600 : -_top_panel_minimum_size;
117 SplitHorizontally(top, bottom, Config::instance()->main_content_divider_sash_position().get_value_or(pos));
122 void sized(wxSizeEvent& ev)
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).
129 SetSashPosition(Config::instance()->main_content_divider_sash_position().get_value_or(_top_panel_minimum_size));
133 _last_height = height;
136 bool _first_shown = false;
137 int const _top_panel_minimum_size = 350;
138 boost::optional<int> _last_height;
142 class ContentDropTarget : public wxFileDropTarget
145 ContentDropTarget(ContentPanel* owner)
149 bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
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);
162 folders.push_back(path);
167 if (!filenames.empty()) {
168 _panel->add_files(files);
171 for (auto dcp: dcps) {
172 _panel->add_dcp(dcp);
175 for (auto dir: folders) {
176 _panel->add_folder(dir);
183 ContentPanel* _panel;
187 /** A wxListCtrl that can middle-ellipsize its text */
188 class ContentListCtrl : public wxListCtrl
191 ContentListCtrl(wxWindow* parent)
192 : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxSize(320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_VIRTUAL)
194 _red.SetTextColour(*wxRED);
200 weak_ptr<Content> content;
204 void set(vector<Item> const& items)
207 SetItemCount(items.size());
210 wxString OnGetItemText(long item, long) const override
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());
217 wxListItemAttr* OnGetItemAttr(long item) const override
219 DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
220 return _items[item].error ? const_cast<wxListItemAttr*>(&_red) : nullptr;
223 weak_ptr<Content> content_at_index(long index)
225 if (index < 0 || index >= static_cast<long>(_items.size())) {
228 return _items[index].content;
232 std::vector<Item> _items;
237 ContentPanel::ContentPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
240 , _film_viewer (viewer)
241 , _generally_sensitive (true)
242 , _ignore_deselect (false)
243 , _no_check_selection (false)
245 _splitter = new LimitedContentPanelSplitter(n);
246 _top_panel = new wxPanel (_splitter);
248 _menu = new ContentMenu (_splitter, _film_viewer);
251 auto s = new wxBoxSizer (wxHORIZONTAL);
253 _content = new ContentListCtrl(_top_panel);
254 _content->DragAcceptFiles (true);
255 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
257 _content->InsertColumn (0, wxT(""));
258 _content->SetColumnWidth(0, 2048);
260 auto b = new wxBoxSizer (wxVERTICAL);
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);
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);
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);
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);
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);
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);
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);
290 s->Add (b, 0, wxALL, 4);
291 _top_panel->SetSizer (s);
294 _notebook = new wxNotebook (_splitter, wxID_ANY);
296 _timing_panel = new TimingPanel (this, _film_viewer);
297 _notebook->AddPage (_timing_panel, _("Timing"), false);
298 _timing_panel->create ();
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));
312 _content->SetDropTarget(new ContentDropTarget(this));
317 ContentPanel::first_shown ()
319 _splitter->first_shown (_top_panel, _notebook);
324 ContentPanel::selected ()
329 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
334 auto weak = _content->content_at_index(s);
335 if (auto content = weak.lock()) {
336 sel.push_back(content);
345 ContentPanel::selected_video ()
349 for (auto i: selected()) {
360 ContentPanel::selected_audio ()
364 for (auto i: selected()) {
375 ContentPanel::selected_text ()
379 for (auto i: selected()) {
380 if (!i->text.empty()) {
390 ContentPanel::selected_ffmpeg ()
392 FFmpegContentList sc;
394 for (auto i: selected()) {
395 auto t = dynamic_pointer_cast<FFmpegContent> (i);
406 ContentPanel::film_changed(FilmProperty p)
409 case FilmProperty::CONTENT:
410 case FilmProperty::CONTENT_ORDER:
417 for (auto i: panels()) {
424 ContentPanel::item_deselected ()
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.
429 _ignore_deselect = false;
430 signal_manager->when_idle (boost::bind (&ContentPanel::item_deselected_idle, this));
435 ContentPanel::item_deselected_idle ()
437 if (!_ignore_deselect) {
444 ContentPanel::item_selected ()
446 _ignore_deselect = true;
452 ContentPanel::check_selection ()
454 if (_no_check_selection) {
458 setup_sensitivity ();
460 for (auto i: panels()) {
461 i->content_selection_changed ();
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
472 StringTextFile ts(text_content);
473 if (auto first = ts.first()) {
474 position += DCPTime(first.get(), _film->active_frame_rate_change(content->position()));
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()));
483 if (!go_to || position < go_to.get()) {
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));
493 if (_timeline_dialog) {
494 _timeline_dialog->set_selection (selected());
497 /* Make required tabs visible */
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.
503 _last_selected_tab = 0;
504 if (_notebook->GetSelection() != wxNOT_FOUND) {
505 _last_selected_tab = _notebook->GetPage(_notebook->GetSelection());
509 bool have_video = false;
510 bool have_audio = false;
511 EnumIndexedVector<bool, TextType> have_text;
512 for (auto i: selected()) {
519 for (auto j: i->text) {
520 have_text[static_cast<int>(j->original_type())] = true;
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;
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;
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;
566 /* Set up the tab selection */
569 for (size_t i = 0; i < _notebook->GetPageCount(); ++i) {
570 if (_notebook->GetPage(i) == _last_selected_tab) {
571 _notebook->SetSelection (i);
576 if (!done && _notebook->GetPageCount() > 0) {
577 _notebook->SetSelection (0);
580 setup_sensitivity ();
585 optional<boost::filesystem::path>
586 ContentPanel::add_files_override_path() const
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>();
597 ContentPanel::add_file_clicked ()
599 /* This method is also called when Ctrl-A is pressed, so check that our notebook page
602 if (_parent->GetCurrentPage() != _splitter || !_film) {
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.
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,
615 add_files_override_path()
619 add_files(dialog.paths());
625 ContentPanel::add_folder_clicked ()
627 DirDialog dialog(_splitter, _("Choose a folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path());
629 add_folder(dialog.path());
635 ContentPanel::add_folder(boost::filesystem::path folder)
637 vector<shared_ptr<Content>> content;
640 content = content_factory(folder);
641 } catch (exception& e) {
642 error_dialog (_parent, e.what());
646 if (content.empty ()) {
647 error_dialog (_parent, _("No content found in this folder."));
651 for (auto i: content) {
652 auto ic = dynamic_pointer_cast<ImageContent> (i);
654 ImageSequenceDialog dialog(_splitter);
656 if (dialog.ShowModal() != wxID_OK) {
659 ic->set_video_frame_rate(_film, dialog.frame_rate());
662 _film->examine_and_add_content (i);
668 ContentPanel::add_dcp_clicked ()
670 DirDialog dialog(_splitter, _("Choose a DCP folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path());
672 add_dcp(dialog.path());
678 ContentPanel::add_dcp(boost::filesystem::path dcp)
681 _film->examine_and_add_content(make_shared<DCPContent>(dcp));
682 } catch (ProjectFolderError &) {
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."
690 } catch (exception& e) {
691 error_dialog(_parent, e.what());
696 /** @return true if this remove "click" should be ignored */
698 ContentPanel::remove_clicked (bool hotkey)
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.
703 if (hotkey && (_parent->GetCurrentPage() != _splitter || !_content->HasFocus())) {
707 for (auto i: selected ()) {
708 _film->remove_content (i);
717 ContentPanel::timeline_clicked ()
719 if (!_film || _film->content().empty()) {
723 _timeline_dialog.reset(this, _film, _film_viewer);
724 _timeline_dialog->set_selection (selected());
725 _timeline_dialog->Show ();
730 ContentPanel::right_click (wxListEvent& ev)
732 _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
736 /** Set up broad sensitivity based on the type of content that is selected */
738 ContentPanel::setup_sensitivity ()
740 _add_file->Enable (_generally_sensitive);
741 _add_folder->Enable (_generally_sensitive);
742 _add_dcp->Enable (_generally_sensitive);
744 auto selection = selected ();
745 auto video_selection = selected_video ();
746 auto audio_selection = selected_audio ();
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());
754 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
757 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
759 for (auto text: _text_panel) {
761 text->Enable(_generally_sensitive && selection.size() == 1 && !selection.front()->text.empty());
764 _timing_panel->Enable (_generally_sensitive);
769 ContentPanel::set_film (shared_ptr<Film> film)
772 _audio_panel->set_film (film);
777 film_changed(FilmProperty::CONTENT);
778 film_changed(FilmProperty::AUDIO_CHANNELS);
784 setup_sensitivity ();
789 ContentPanel::set_general_sensitivity (bool s)
791 _generally_sensitive = s;
792 setup_sensitivity ();
797 ContentPanel::earlier_clicked ()
799 auto sel = selected ();
800 if (sel.size() == 1) {
801 _film->move_content_earlier (sel.front ());
808 ContentPanel::later_clicked ()
810 auto sel = selected ();
811 if (sel.size() == 1) {
812 _film->move_content_later (sel.front ());
819 ContentPanel::set_selection (weak_ptr<Content> wc)
821 auto content = _film->content ();
822 for (size_t i = 0; i < content.size(); ++i) {
823 set_selected_state(i, content[i] == wc.lock());
829 ContentPanel::set_selection (ContentList cl)
832 _no_check_selection = true;
833 ScopeGuard sg = [this]() { _no_check_selection = false; };
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());
846 ContentPanel::select_all ()
848 set_selection (_film->content());
853 ContentPanel::film_content_changed (int property)
856 property == ContentProperty::PATH ||
857 property == DCPContentProperty::NEEDS_ASSETS ||
858 property == DCPContentProperty::NEEDS_KDM ||
859 property == DCPContentProperty::NAME
865 for (auto i: panels()) {
866 i->film_content_changed (property);
872 ContentPanel::setup ()
875 _content->DeleteAllItems ();
876 setup_sensitivity ();
880 auto content = _film->content ();
881 auto selection = selected();
883 vector<ContentListCtrl::Item> items;
885 for (auto i: content) {
886 bool const valid = i->paths_valid ();
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 ();
892 auto s = std_to_wx (i->summary ());
895 s = _("MISSING: ") + s;
899 s = _("NEEDS KDM: ") + s;
903 s = _("NEEDS OV: ") + s;
906 items.push_back({s, i, !valid || needs_kdm || needs_assets});
909 _content->set(items);
911 if (selection.empty() && !content.empty()) {
912 set_selected_state(0, true);
914 set_selection(selection);
917 setup_sensitivity ();
922 ContentPanel::files_dropped (wxDropFilesEvent& event)
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]));
934 add_files (path_list);
939 ContentPanel::add_files (vector<boost::filesystem::path> paths)
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.
950 std::sort (paths.begin(), paths.end(), CaseInsensitiveSorter());
952 /* XXX: check for lots of files here and do something */
955 for (auto i: paths) {
956 for (auto j: content_factory(i)) {
957 _film->examine_and_add_content (j);
960 } catch (exception& e) {
961 error_dialog (_parent, e.what());
966 list<ContentSubPanel*>
967 ContentPanel::panels () const
969 list<ContentSubPanel*> p;
971 p.push_back (_video_panel);
974 p.push_back (_audio_panel);
976 for (auto text: _text_panel) {
981 p.push_back (_timing_panel);
987 ContentPanel::set_selected_state(int item, bool state)
989 _content->SetItemState(item, state ? wxLIST_STATE_SELECTED : 0, wxLIST_STATE_SELECTED);
990 _content->SetItemState(item, state ? wxLIST_STATE_FOCUSED : 0, wxLIST_STATE_FOCUSED);
995 ContentPanel::window() const