Fix content tab sensitivity when you load some audio-only content first.
[dcpomatic.git] / src / wx / content_panel.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "content_panel.h"
22 #include "wx_util.h"
23 #include "video_panel.h"
24 #include "audio_panel.h"
25 #include "subtitle_panel.h"
26 #include "timing_panel.h"
27 #include "timeline_dialog.h"
28 #include "image_sequence_dialog.h"
29 #include "film_viewer.h"
30 #include "lib/audio_content.h"
31 #include "lib/subtitle_content.h"
32 #include "lib/video_content.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/content_factory.h"
35 #include "lib/image_content.h"
36 #include "lib/dcp_content.h"
37 #include "lib/case_insensitive_sorter.h"
38 #include "lib/playlist.h"
39 #include "lib/config.h"
40 #include "lib/log.h"
41 #include "lib/compose.hpp"
42 #include <wx/wx.h>
43 #include <wx/notebook.h>
44 #include <wx/listctrl.h>
45 #include <boost/filesystem.hpp>
46 #include <boost/foreach.hpp>
47 #include <iostream>
48
49 using std::list;
50 using std::string;
51 using std::cout;
52 using std::vector;
53 using std::exception;
54 using boost::shared_ptr;
55 using boost::weak_ptr;
56 using boost::dynamic_pointer_cast;
57 using boost::optional;
58
59 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
60
61 ContentPanel::ContentPanel (wxNotebook* n, boost::shared_ptr<Film> film, FilmViewer* viewer)
62         : _timeline_dialog (0)
63         , _parent (n)
64         , _film (film)
65         , _film_viewer (viewer)
66         , _generally_sensitive (true)
67 {
68         _panel = new wxPanel (n);
69         _sizer = new wxBoxSizer (wxVERTICAL);
70         _panel->SetSizer (_sizer);
71
72         _menu = new ContentMenu (_panel);
73
74         {
75                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
76
77                 _content = new wxListCtrl (_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
78                 _content->DragAcceptFiles (true);
79                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
80
81                 _content->InsertColumn (0, wxT(""));
82                 _content->SetColumnWidth (0, 512);
83
84                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
85
86                 _add_file = new wxButton (_panel, wxID_ANY, _("Add file(s)..."));
87                 _add_file->SetToolTip (_("Add video, image, sound or subtitle files to the film."));
88                 b->Add (_add_file, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
89
90                 _add_folder = new wxButton (_panel, wxID_ANY, _("Add folder..."));
91                 _add_folder->SetToolTip (_("Add a folder of image files (which will be used as a moving image sequence) or a folder of sound files."));
92                 b->Add (_add_folder, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
93
94                 _add_dcp = new wxButton (_panel, wxID_ANY, _("Add DCP..."));
95                 _add_dcp->SetToolTip (_("Add a DCP."));
96                 b->Add (_add_dcp, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
97
98                 _remove = new wxButton (_panel, wxID_ANY, _("Remove"));
99                 _remove->SetToolTip (_("Remove the selected piece of content from the film."));
100                 b->Add (_remove, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
101
102                 _earlier = new wxButton (_panel, wxID_ANY, _("Earlier"));
103                 _earlier->SetToolTip (_("Move the selected piece of content earlier in the film."));
104                 b->Add (_earlier, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
105
106                 _later = new wxButton (_panel, wxID_ANY, _("Later"));
107                 _later->SetToolTip (_("Move the selected piece of content later in the film."));
108                 b->Add (_later, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
109
110                 _timeline = new wxButton (_panel, wxID_ANY, _("Timeline..."));
111                 _timeline->SetToolTip (_("Open the timeline for the film."));
112                 b->Add (_timeline, 0, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
113
114                 s->Add (b, 0, wxALL, 4);
115
116                 _sizer->Add (s, 0, wxEXPAND | wxALL, 6);
117         }
118
119         _notebook = new wxNotebook (_panel, wxID_ANY);
120         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
121
122         _video_panel = new VideoPanel (this);
123         _panels.push_back (_video_panel);
124         _audio_panel = new AudioPanel (this);
125         _panels.push_back (_audio_panel);
126         _subtitle_panel = new SubtitlePanel (this);
127         _panels.push_back (_subtitle_panel);
128         _timing_panel = new TimingPanel (this, _film_viewer);
129         _panels.push_back (_timing_panel);
130
131         _content->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::selection_changed, this));
132         _content->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::selection_changed, this));
133         _content->Bind (wxEVT_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
134         _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
135         _add_file->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_file_clicked, this));
136         _add_folder->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_folder_clicked, this));
137         _add_dcp->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::add_dcp_clicked, this));
138         _remove->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::remove_clicked, this, false));
139         _earlier->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::earlier_clicked, this));
140         _later->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::later_clicked, this));
141         _timeline->Bind (wxEVT_BUTTON, boost::bind (&ContentPanel::timeline_clicked, this));
142 }
143
144 ContentList
145 ContentPanel::selected ()
146 {
147         ContentList sel;
148         long int s = -1;
149         while (true) {
150                 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
151                 if (s == -1) {
152                         break;
153                 }
154
155                 if (s < int (_film->content().size ())) {
156                         sel.push_back (_film->content()[s]);
157                 }
158         }
159
160         return sel;
161 }
162
163 ContentList
164 ContentPanel::selected_video ()
165 {
166         ContentList vc;
167
168         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
169                 if (i->video) {
170                         vc.push_back (i);
171                 }
172         }
173
174         return vc;
175 }
176
177 ContentList
178 ContentPanel::selected_audio ()
179 {
180         ContentList ac;
181
182         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
183                 if (i->audio) {
184                         ac.push_back (i);
185                 }
186         }
187
188         return ac;
189 }
190
191 ContentList
192 ContentPanel::selected_subtitle ()
193 {
194         ContentList sc;
195
196         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
197                 if (i->subtitle) {
198                         sc.push_back (i);
199                 }
200         }
201
202         return sc;
203 }
204
205 FFmpegContentList
206 ContentPanel::selected_ffmpeg ()
207 {
208         FFmpegContentList sc;
209
210         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
211                 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (i);
212                 if (t) {
213                         sc.push_back (t);
214                 }
215         }
216
217         return sc;
218 }
219
220 void
221 ContentPanel::film_changed (Film::Property p)
222 {
223         switch (p) {
224         case Film::CONTENT:
225         case Film::CONTENT_ORDER:
226                 setup ();
227                 break;
228         default:
229                 break;
230         }
231
232         BOOST_FOREACH (ContentSubPanel* i, _panels) {
233                 i->film_changed (p);
234         }
235 }
236
237 void
238 ContentPanel::selection_changed ()
239 {
240         if (_last_selected == selected()) {
241                 /* This was triggered by a re-build of the view but the selection
242                    did not really change.
243                 */
244                 return;
245         }
246
247         _last_selected = selected ();
248
249         setup_sensitivity ();
250
251         BOOST_FOREACH (ContentSubPanel* i, _panels) {
252                 i->content_selection_changed ();
253         }
254
255         optional<DCPTime> go_to;
256         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
257                 if (!go_to || i->position() < go_to.get()) {
258                         go_to = i->position ();
259                 }
260         }
261
262         if (go_to && Config::instance()->jump_to_selected ()) {
263                 _film_viewer->set_position (go_to.get ());
264         }
265
266         if (_timeline_dialog) {
267                 _timeline_dialog->set_selection (selected ());
268         }
269 }
270
271 void
272 ContentPanel::add_file_clicked ()
273 {
274         /* This method is also called when Ctrl-A is pressed, so check that our notebook page
275            is visible.
276         */
277         if (_parent->GetCurrentPage() != _panel || !_film) {
278                 return;
279         }
280
281         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
282            non-Latin filenames or paths.
283         */
284         wxFileDialog* d = new wxFileDialog (
285                 _panel,
286                 _("Choose a file or files"),
287                 wxT (""),
288                 wxT (""),
289                 wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
290                 wxFD_MULTIPLE | wxFD_CHANGE_DIR
291                 );
292
293         int const r = d->ShowModal ();
294
295         if (r != wxID_OK) {
296                 d->Destroy ();
297                 return;
298         }
299
300         wxArrayString paths;
301         d->GetPaths (paths);
302         list<boost::filesystem::path> path_list;
303         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
304                 path_list.push_back (wx_to_std (paths[i]));
305         }
306         add_files (path_list);
307
308         d->Destroy ();
309 }
310
311 void
312 ContentPanel::add_folder_clicked ()
313 {
314         wxDirDialog* d = new wxDirDialog (_panel, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
315         int r = d->ShowModal ();
316         boost::filesystem::path const path (wx_to_std (d->GetPath ()));
317         d->Destroy ();
318
319         if (r != wxID_OK) {
320                 return;
321         }
322
323         list<shared_ptr<Content> > content;
324
325         try {
326                 content = content_factory (_film, path);
327         } catch (exception& e) {
328                 error_dialog (_parent, e.what());
329                 return;
330         }
331
332         if (content.empty ()) {
333                 error_dialog (_parent, _("No content found in this folder."));
334                 return;
335         }
336
337         BOOST_FOREACH (shared_ptr<Content> i, content) {
338                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
339                 if (ic) {
340                         ImageSequenceDialog* e = new ImageSequenceDialog (_panel);
341                         r = e->ShowModal ();
342                         float const frame_rate = e->frame_rate ();
343                         e->Destroy ();
344
345                         if (r != wxID_OK) {
346                                 return;
347                         }
348
349                         ic->set_video_frame_rate (frame_rate);
350                 }
351
352                 _film->examine_and_add_content (i);
353         }
354 }
355
356 void
357 ContentPanel::add_dcp_clicked ()
358 {
359         wxDirDialog* d = new wxDirDialog (_panel, _("Choose a DCP folder"), wxT (""), wxDD_DIR_MUST_EXIST);
360         int r = d->ShowModal ();
361         boost::filesystem::path const path (wx_to_std (d->GetPath ()));
362         d->Destroy ();
363
364         if (r != wxID_OK) {
365                 return;
366         }
367
368         try {
369                 _film->examine_and_add_content (shared_ptr<Content> (new DCPContent (_film, path)));
370         } catch (exception& e) {
371                 error_dialog (_parent, e.what());
372         }
373 }
374
375 /** @return true if this remove "click" should be ignored */
376 bool
377 ContentPanel::remove_clicked (bool hotkey)
378 {
379         /* If the method was called because Delete was pressed check that our notebook page
380            is visible and that the content list is focussed.
381         */
382         if (hotkey && (_parent->GetCurrentPage() != _panel || !_content->HasFocus())) {
383                 return true;
384         }
385
386         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
387                 _film->remove_content (i);
388         }
389
390         selection_changed ();
391         return false;
392 }
393
394 void
395 ContentPanel::timeline_clicked ()
396 {
397         if (_timeline_dialog) {
398                 _timeline_dialog->Destroy ();
399                 _timeline_dialog = 0;
400         }
401
402         _timeline_dialog = new TimelineDialog (this, _film);
403         _timeline_dialog->Show ();
404 }
405
406 void
407 ContentPanel::right_click (wxListEvent& ev)
408 {
409         _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
410 }
411
412 /** Set up broad sensitivity based on the type of content that is selected */
413 void
414 ContentPanel::setup_sensitivity ()
415 {
416         _add_file->Enable (_generally_sensitive);
417         _add_folder->Enable (_generally_sensitive);
418         _add_dcp->Enable (_generally_sensitive);
419
420         ContentList selection = selected ();
421         ContentList video_selection = selected_video ();
422         ContentList audio_selection = selected_audio ();
423
424         _remove->Enable   (_generally_sensitive && !selection.empty());
425         _earlier->Enable  (_generally_sensitive && selection.size() == 1);
426         _later->Enable    (_generally_sensitive && selection.size() == 1);
427         _timeline->Enable (_generally_sensitive && _film && !_film->content().empty());
428
429         _video_panel->Enable    (_generally_sensitive && video_selection.size() > 0);
430         _audio_panel->Enable    (_generally_sensitive && audio_selection.size() > 0);
431         _subtitle_panel->Enable (_generally_sensitive && selection.size() == 1 && selection.front()->subtitle);
432         _timing_panel->Enable   (_generally_sensitive);
433 }
434
435 void
436 ContentPanel::set_film (shared_ptr<Film> film)
437 {
438         _audio_panel->set_film (film);
439
440         _film = film;
441
442         film_changed (Film::CONTENT);
443         film_changed (Film::AUDIO_CHANNELS);
444         selection_changed ();
445         setup_sensitivity ();
446 }
447
448 void
449 ContentPanel::set_general_sensitivity (bool s)
450 {
451         _generally_sensitive = s;
452         setup_sensitivity ();
453 }
454
455 void
456 ContentPanel::earlier_clicked ()
457 {
458         ContentList sel = selected ();
459         if (sel.size() == 1) {
460                 _film->move_content_earlier (sel.front ());
461                 selection_changed ();
462         }
463 }
464
465 void
466 ContentPanel::later_clicked ()
467 {
468         ContentList sel = selected ();
469         if (sel.size() == 1) {
470                 _film->move_content_later (sel.front ());
471                 selection_changed ();
472         }
473 }
474
475 void
476 ContentPanel::set_selection (weak_ptr<Content> wc)
477 {
478         ContentList content = _film->content ();
479         for (size_t i = 0; i < content.size(); ++i) {
480                 if (content[i] == wc.lock ()) {
481                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
482                 } else {
483                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
484                 }
485         }
486 }
487
488 void
489 ContentPanel::set_selection (ContentList cl)
490 {
491         ContentList content = _film->content ();
492         for (size_t i = 0; i < content.size(); ++i) {
493                 if (find(cl.begin(), cl.end(), content[i]) != cl.end()) {
494                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
495                 } else {
496                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
497                 }
498         }
499 }
500
501 void
502 ContentPanel::film_content_changed (int property)
503 {
504         if (
505                 property == ContentProperty::PATH ||
506                 property == DCPContentProperty::NEEDS_ASSETS ||
507                 property == DCPContentProperty::NEEDS_KDM ||
508                 property == DCPContentProperty::NAME
509                 ) {
510
511                 setup ();
512         }
513
514         BOOST_FOREACH (ContentSubPanel* i, _panels) {
515                 i->film_content_changed (property);
516         }
517 }
518
519 void
520 ContentPanel::setup ()
521 {
522         ContentList content = _film->content ();
523
524         Content* selected_content = 0;
525         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
526         if (s != -1) {
527                 wxListItem item;
528                 item.SetId (s);
529                 item.SetMask (wxLIST_MASK_DATA);
530                 _content->GetItem (item);
531                 selected_content = reinterpret_cast<Content*> (item.GetData ());
532         }
533
534         _content->DeleteAllItems ();
535
536         BOOST_FOREACH (shared_ptr<Content> i, content) {
537                 int const t = _content->GetItemCount ();
538                 bool const valid = i->paths_valid ();
539
540                 /* Temporary debugging for Igor */
541                 BOOST_FOREACH (boost::filesystem::path j, i->paths()) {
542                         LOG_GENERAL ("Check %1 %2 answer %3", j.string(), boost::filesystem::exists(j) ? "yes" : "no", valid ? "yes" : "no");
543                 }
544
545                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i);
546                 bool const needs_kdm = dcp && dcp->needs_kdm ();
547                 bool const needs_assets = dcp && dcp->needs_assets ();
548
549                 wxString s = std_to_wx (i->summary ());
550
551                 if (!valid) {
552                         s = _("MISSING: ") + s;
553                 }
554
555                 if (needs_kdm) {
556                         s = _("NEEDS KDM: ") + s;
557                 }
558
559                 if (needs_assets) {
560                         s = _("NEEDS OV: ") + s;
561                 }
562
563                 wxListItem item;
564                 item.SetId (t);
565                 item.SetText (s);
566                 item.SetData (i.get ());
567                 _content->InsertItem (item);
568
569                 if (i.get() == selected_content) {
570                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
571                 }
572
573                 if (!valid || needs_kdm || needs_assets) {
574                         _content->SetItemTextColour (t, *wxRED);
575                 }
576         }
577
578         if (!selected_content && !content.empty ()) {
579                 /* Select the item of content if none was selected before */
580                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
581         }
582
583         setup_sensitivity ();
584 }
585
586 void
587 ContentPanel::files_dropped (wxDropFilesEvent& event)
588 {
589         if (!_film) {
590                 return;
591         }
592
593         wxString* paths = event.GetFiles ();
594         list<boost::filesystem::path> path_list;
595         for (int i = 0; i < event.GetNumberOfFiles(); i++) {
596                 path_list.push_back (wx_to_std (paths[i]));
597         }
598
599         add_files (path_list);
600 }
601
602 void
603 ContentPanel::add_files (list<boost::filesystem::path> paths)
604 {
605         /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
606            I can't reproduce that, but sort them anyway.  Don't use ImageFilenameSorter as a normal
607            alphabetical sort is expected here.
608         */
609
610         paths.sort (CaseInsensitiveSorter ());
611
612         /* XXX: check for lots of files here and do something */
613
614         try {
615                 BOOST_FOREACH (boost::filesystem::path i, paths) {
616                         BOOST_FOREACH (shared_ptr<Content> j, content_factory (_film, i)) {
617                                 _film->examine_and_add_content (j);
618                         }
619                 }
620         } catch (exception& e) {
621                 error_dialog (_parent, e.what());
622         }
623 }