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