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