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