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/dcp_content.h"
38 #include "lib/dcpomatic_log.h"
39 #include "lib/ffmpeg_content.h"
40 #include "lib/image_content.h"
42 #include "lib/playlist.h"
43 #include "lib/string_text_file.h"
44 #include "lib/string_text_file_content.h"
45 #include "lib/text_content.h"
46 #include "lib/video_content.h"
48 #include <wx/notebook.h>
49 #include <wx/listctrl.h>
50 #include <wx/display.h>
51 #include <boost/filesystem.hpp>
56 using std::dynamic_pointer_cast;
59 using std::make_shared;
61 using std::shared_ptr;
65 using boost::optional;
66 using namespace dcpomatic;
67 #if BOOST_VERSION >= 106100
68 using namespace boost::placeholders;
72 ContentPanel::ContentPanel (wxNotebook* n, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
75 , _film_viewer (viewer)
76 , _generally_sensitive (true)
77 , _ignore_deselect (false)
78 , _no_check_selection (false)
80 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
84 _splitter = new LimitedSplitter (n);
85 _top_panel = new wxPanel (_splitter);
87 _menu = new ContentMenu (_splitter);
90 auto s = new wxBoxSizer (wxHORIZONTAL);
92 _content = new wxListCtrl (_top_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
93 _content->DragAcceptFiles (true);
94 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
96 _content->InsertColumn (0, wxT(""));
97 _content->SetColumnWidth (0, 512);
99 auto b = new wxBoxSizer (wxVERTICAL);
101 _add_file = new Button (_top_panel, _("Add file(s)..."));
102 _add_file->SetToolTip (_("Add video, image, sound or subtitle files to the film."));
103 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
105 _add_folder = new Button (_top_panel, _("Add folder..."));
106 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a folder of sound files."));
107 b->Add (_add_folder, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
109 _add_dcp = new Button (_top_panel, _("Add DCP..."));
110 _add_dcp->SetToolTip (_("Add a DCP."));
111 b->Add (_add_dcp, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
113 _remove = new Button (_top_panel, _("Remove"));
114 _remove->SetToolTip (_("Remove the selected piece of content from the film."));
115 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
117 _earlier = new Button (_top_panel, _("Earlier"));
118 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
119 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
121 _later = new Button (_top_panel, _("Later"));
122 _later->SetToolTip (_("Move the selected piece of content later in the film."));
123 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
125 _timeline = new Button (_top_panel, _("Timeline..."));
126 _timeline->SetToolTip (_("Open the timeline for the film."));
127 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
129 s->Add (b, 0, wxALL, 4);
130 _top_panel->SetSizer (s);
133 _notebook = new wxNotebook (_splitter, wxID_ANY);
135 _timing_panel = new TimingPanel (this, _film_viewer);
136 _notebook->AddPage (_timing_panel, _("Timing"), false);
138 _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::item_selected, this));
139 _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::item_deselected, this));
140 _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
141 _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
142 _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
143 _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
144 _add_dcp->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_dcp_clicked, this));
145 _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
146 _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
147 _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
148 _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
153 ContentPanel::first_shown ()
155 _splitter->first_shown (_top_panel, _notebook);
160 ContentPanel::selected ()
165 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
170 auto cl = _film->content();
171 if (s < int (cl.size())) {
172 sel.push_back (cl[s]);
181 ContentPanel::selected_video ()
185 for (auto i: selected()) {
196 ContentPanel::selected_audio ()
200 for (auto i: selected()) {
211 ContentPanel::selected_text ()
215 for (auto i: selected()) {
216 if (!i->text.empty()) {
226 ContentPanel::selected_ffmpeg ()
228 FFmpegContentList sc;
230 for (auto i: selected()) {
231 auto t = dynamic_pointer_cast<FFmpegContent> (i);
242 ContentPanel::film_changed (Film::Property p)
245 case Film::Property::CONTENT:
246 case Film::Property::CONTENT_ORDER:
253 for (auto i: panels()) {
260 ContentPanel::item_deselected ()
262 /* Maybe this is just a re-click on the same item; if not, _ignore_deselect will stay
263 false and item_deselected_foo will handle the deselection.
265 _ignore_deselect = false;
266 signal_manager->when_idle (boost::bind (&ContentPanel::item_deselected_idle, this));
271 ContentPanel::item_deselected_idle ()
273 if (!_ignore_deselect) {
280 ContentPanel::item_selected ()
282 _ignore_deselect = true;
288 ContentPanel::check_selection ()
290 if (_no_check_selection) {
294 setup_sensitivity ();
296 for (auto i: panels()) {
297 i->content_selection_changed ();
300 optional<DCPTime> go_to;
301 for (auto i: selected()) {
304 if (dynamic_pointer_cast<StringTextFileContent>(i) && i->paths_valid()) {
305 /* Rather special case; if we select a text subtitle file jump to its
308 StringTextFile ts (dynamic_pointer_cast<StringTextFileContent>(i));
310 p += DCPTime(ts.first().get(), _film->active_frame_rate_change(i->position()));
313 if (!go_to || p < go_to.get()) {
318 if (go_to && Config::instance()->jump_to_selected() && signal_manager) {
319 auto fv = _film_viewer.lock ();
320 DCPOMATIC_ASSERT (fv);
321 signal_manager->when_idle(boost::bind(&FilmViewer::seek, fv.get(), go_to.get().ceil(_film->video_frame_rate()), true));
324 if (_timeline_dialog) {
325 _timeline_dialog->set_selection (selected());
328 /* Make required tabs visible */
330 if (_notebook->GetPageCount() > 1) {
331 /* There's more than one tab in the notebook so the current selection could be meaningful
332 to the user; store it so that we can try to restore it later.
334 _last_selected_tab = 0;
335 if (_notebook->GetSelection() != wxNOT_FOUND) {
336 _last_selected_tab = _notebook->GetPage(_notebook->GetSelection());
340 bool have_video = false;
341 bool have_audio = false;
342 bool have_text[static_cast<int>(TextType::COUNT)] = { false, false };
343 for (auto i: selected()) {
350 for (auto j: i->text) {
351 have_text[static_cast<int>(j->original_type())] = true;
357 if (have_video && !_video_panel) {
358 _video_panel = new VideoPanel (this);
359 _notebook->InsertPage (off, _video_panel, _video_panel->name());
360 } else if (!have_video && _video_panel) {
361 _notebook->DeletePage (off);
369 if (have_audio && !_audio_panel) {
370 _audio_panel = new AudioPanel (this);
371 _notebook->InsertPage (off, _audio_panel, _audio_panel->name());
372 } else if (!have_audio && _audio_panel) {
373 _notebook->DeletePage (off);
381 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
382 if (have_text[i] && !_text_panel[i]) {
383 _text_panel[i] = new TextPanel (this, static_cast<TextType>(i));
384 _notebook->InsertPage (off, _text_panel[i], _text_panel[i]->name());
385 } else if (!have_text[i] && _text_panel[i]) {
386 _notebook->DeletePage (off);
394 /* Set up the tab selection */
397 for (size_t i = 0; i < _notebook->GetPageCount(); ++i) {
398 if (_notebook->GetPage(i) == _last_selected_tab) {
399 _notebook->SetSelection (i);
404 if (!done && _notebook->GetPageCount() > 0) {
405 _notebook->SetSelection (0);
408 setup_sensitivity ();
414 ContentPanel::add_file_clicked ()
416 /* This method is also called when Ctrl-A is pressed, so check that our notebook page
419 if (_parent->GetCurrentPage() != _splitter || !_film) {
423 /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
424 non-Latin filenames or paths.
426 auto d = new wxFileDialog (
428 _("Choose a file or files"),
431 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
432 wxFD_MULTIPLE | wxFD_CHANGE_DIR
435 int const r = d->ShowModal ();
444 list<boost::filesystem::path> path_list;
445 for (unsigned int i = 0; i < paths.GetCount(); ++i) {
446 path_list.push_back (wx_to_std (paths[i]));
448 add_files (path_list);
455 ContentPanel::add_folder_clicked ()
457 auto d = new wxDirDialog (_splitter, _("Choose a folder"), wxT(""), wxDD_DIR_MUST_EXIST);
458 int r = d->ShowModal ();
459 boost::filesystem::path const path (wx_to_std (d->GetPath ()));
466 list<shared_ptr<Content> > content;
469 content = content_factory (path);
470 } catch (exception& e) {
471 error_dialog (_parent, e.what());
475 if (content.empty ()) {
476 error_dialog (_parent, _("No content found in this folder."));
480 for (auto i: content) {
481 auto ic = dynamic_pointer_cast<ImageContent> (i);
483 auto e = new ImageSequenceDialog (_splitter);
485 auto const frame_rate = e->frame_rate ();
492 ic->set_video_frame_rate (frame_rate);
495 _film->examine_and_add_content (i);
501 ContentPanel::add_dcp_clicked ()
503 auto d = new wxDirDialog (_splitter, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST);
504 int r = d->ShowModal ();
505 boost::filesystem::path const path (wx_to_std (d->GetPath ()));
513 _film->examine_and_add_content (make_shared<DCPContent>(path));
514 } catch (ProjectFolderError &) {
518 "This looks like a DCP-o-matic project folder, which cannot be added to a different project. "
519 "Choose the DCP directory inside the DCP-o-matic project folder if that's what you want to import."
522 } catch (exception& e) {
523 error_dialog (_parent, e.what());
528 /** @return true if this remove "click" should be ignored */
530 ContentPanel::remove_clicked (bool hotkey)
532 /* If the method was called because Delete was pressed check that our notebook page
533 is visible and that the content list is focussed.
535 if (hotkey && (_parent->GetCurrentPage() != _splitter || !_content->HasFocus())) {
539 for (auto i: selected ()) {
540 _film->remove_content (i);
549 ContentPanel::timeline_clicked ()
555 if (_timeline_dialog) {
556 _timeline_dialog->Destroy ();
557 _timeline_dialog = nullptr;
560 _timeline_dialog = new TimelineDialog (this, _film, _film_viewer);
561 _timeline_dialog->set_selection (selected());
562 _timeline_dialog->Show ();
567 ContentPanel::right_click (wxListEvent& ev)
569 _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
573 /** Set up broad sensitivity based on the type of content that is selected */
575 ContentPanel::setup_sensitivity ()
577 _add_file->Enable (_generally_sensitive);
578 _add_folder->Enable (_generally_sensitive);
579 _add_dcp->Enable (_generally_sensitive);
581 auto selection = selected ();
582 auto video_selection = selected_video ();
583 auto audio_selection = selected_audio ();
585 _remove->Enable (_generally_sensitive && !selection.empty());
586 _earlier->Enable (_generally_sensitive && selection.size() == 1);
587 _later->Enable (_generally_sensitive && selection.size() == 1);
588 _timeline->Enable (_generally_sensitive && _film && !_film->content().empty());
591 _video_panel->Enable (_generally_sensitive && video_selection.size() > 0);
594 _audio_panel->Enable (_generally_sensitive && audio_selection.size() > 0);
596 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
597 if (_text_panel[i]) {
598 _text_panel[i]->Enable (_generally_sensitive && selection.size() == 1 && !selection.front()->text.empty());
601 _timing_panel->Enable (_generally_sensitive);
606 ContentPanel::set_film (shared_ptr<Film> film)
609 _audio_panel->set_film (film);
614 film_changed (Film::Property::CONTENT);
615 film_changed (Film::Property::AUDIO_CHANNELS);
621 setup_sensitivity ();
626 ContentPanel::set_general_sensitivity (bool s)
628 _generally_sensitive = s;
629 setup_sensitivity ();
634 ContentPanel::earlier_clicked ()
636 auto sel = selected ();
637 if (sel.size() == 1) {
638 _film->move_content_earlier (sel.front ());
645 ContentPanel::later_clicked ()
647 auto sel = selected ();
648 if (sel.size() == 1) {
649 _film->move_content_later (sel.front ());
656 ContentPanel::set_selection (weak_ptr<Content> wc)
658 auto content = _film->content ();
659 for (size_t i = 0; i < content.size(); ++i) {
660 if (content[i] == wc.lock ()) {
661 _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
663 _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
670 ContentPanel::set_selection (ContentList cl)
672 _no_check_selection = true;
674 auto content = _film->content ();
675 for (size_t i = 0; i < content.size(); ++i) {
676 if (find(cl.begin(), cl.end(), content[i]) != cl.end()) {
677 _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
679 _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
683 _no_check_selection = false;
689 ContentPanel::select_all ()
691 set_selection (_film->content());
696 ContentPanel::film_content_changed (int property)
699 property == ContentProperty::PATH ||
700 property == DCPContentProperty::NEEDS_ASSETS ||
701 property == DCPContentProperty::NEEDS_KDM ||
702 property == DCPContentProperty::NAME
708 for (auto i: panels()) {
709 i->film_content_changed (property);
715 ContentPanel::setup ()
718 _content->DeleteAllItems ();
719 setup_sensitivity ();
723 auto content = _film->content ();
725 Content* selected_content = nullptr;
726 auto const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
730 item.SetMask (wxLIST_MASK_DATA);
731 _content->GetItem (item);
732 selected_content = reinterpret_cast<Content*> (item.GetData ());
735 _content->DeleteAllItems ();
737 for (auto i: content) {
738 int const t = _content->GetItemCount ();
739 bool const valid = i->paths_valid ();
741 auto dcp = dynamic_pointer_cast<DCPContent> (i);
742 bool const needs_kdm = dcp && dcp->needs_kdm ();
743 bool const needs_assets = dcp && dcp->needs_assets ();
745 auto s = std_to_wx (i->summary ());
748 s = _("MISSING: ") + s;
752 s = _("NEEDS KDM: ") + s;
756 s = _("NEEDS OV: ") + s;
762 item.SetData (i.get ());
763 _content->InsertItem (item);
765 if (i.get() == selected_content) {
766 _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
769 if (!valid || needs_kdm || needs_assets) {
770 _content->SetItemTextColour (t, *wxRED);
774 if (!selected_content && !content.empty ()) {
775 /* Select the item of content if none was selected before */
776 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
779 setup_sensitivity ();
784 ContentPanel::files_dropped (wxDropFilesEvent& event)
790 auto paths = event.GetFiles ();
791 list<boost::filesystem::path> path_list;
792 for (int i = 0; i < event.GetNumberOfFiles(); i++) {
793 path_list.push_back (wx_to_std (paths[i]));
796 add_files (path_list);
801 ContentPanel::add_files (list<boost::filesystem::path> paths)
803 /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
804 I can't reproduce that, but sort them anyway. Don't use ImageFilenameSorter as a normal
805 alphabetical sort is expected here.
808 paths.sort (CaseInsensitiveSorter ());
810 /* XXX: check for lots of files here and do something */
813 for (auto i: paths) {
814 for (auto j: content_factory(i)) {
815 _film->examine_and_add_content (j);
818 } catch (exception& e) {
819 error_dialog (_parent, e.what());
824 list<ContentSubPanel*>
825 ContentPanel::panels () const
827 list<ContentSubPanel*> p;
829 p.push_back (_video_panel);
832 p.push_back (_audio_panel);
834 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
835 if (_text_panel[i]) {
836 p.push_back (_text_panel[i]);
839 p.push_back (_timing_panel);
844 LimitedSplitter::LimitedSplitter (wxWindow* parent)
845 : wxSplitterWindow (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
846 , _first_shown (false)
847 , _top_panel_minimum_size (350)
849 /* This value doesn't really mean much but we just want to stop double-click on the
850 divider from shrinking the bottom panel (#1601).
852 SetMinimumPaneSize (64);
854 Bind (wxEVT_SIZE, boost::bind(&LimitedSplitter::sized, this, _1));
859 LimitedSplitter::first_shown (wxWindow* top, wxWindow* bottom)
861 int const sn = wxDisplay::GetFromWindow(this);
863 wxRect const screen = wxDisplay(sn).GetClientArea();
864 /* This is a hack to try and make the content notebook a sensible size; large on big displays but small
865 enough on small displays to leave space for the content area.
867 SplitHorizontally (top, bottom, screen.height > 800 ? -600 : -_top_panel_minimum_size);
869 /* Fallback for when GetFromWindow fails for reasons that aren't clear */
870 SplitHorizontally (top, bottom, -600);
877 LimitedSplitter::sized (wxSizeEvent& ev)
879 if (_first_shown && GetSize().GetHeight() > _top_panel_minimum_size && GetSashPosition() < _top_panel_minimum_size) {
880 /* The window is now fairly big but the top panel is small; this happens when the DCP-o-matic window
881 * is shrunk and then made larger again. Try to set a sensible top panel size in this case (#1839).
883 SetSashPosition (_top_panel_minimum_size);