Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[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 "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 "lib/audio_content.h"
29 #include "lib/subtitle_content.h"
30 #include "lib/video_content.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/content_factory.h"
33 #include "lib/image_content.h"
34 #include "lib/dcp_content.h"
35 #include "lib/playlist.h"
36 #include <wx/wx.h>
37 #include <wx/notebook.h>
38 #include <wx/listctrl.h>
39 #include <boost/filesystem.hpp>
40 #include <boost/foreach.hpp>
41 #include <iostream>
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         VideoContentList vc;
151
152         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
153                 shared_ptr<VideoContent> t = dynamic_pointer_cast<VideoContent> (i);
154                 if (t) {
155                         vc.push_back (t);
156                 }
157         }
158
159         return vc;
160 }
161
162 AudioContentList
163 ContentPanel::selected_audio ()
164 {
165         AudioContentList ac;
166
167         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
168                 shared_ptr<AudioContent> t = dynamic_pointer_cast<AudioContent> (i);
169                 if (t) {
170                         ac.push_back (t);
171                 }
172         }
173
174         return ac;
175 }
176
177 SubtitleContentList
178 ContentPanel::selected_subtitle ()
179 {
180         SubtitleContentList sc;
181
182         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
183                 shared_ptr<SubtitleContent> t = dynamic_pointer_cast<SubtitleContent> (i);
184                 if (t) {
185                         sc.push_back (t);
186                 }
187         }
188
189         return sc;
190 }
191
192 FFmpegContentList
193 ContentPanel::selected_ffmpeg ()
194 {
195         FFmpegContentList sc;
196
197         BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
198                 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (i);
199                 if (t) {
200                         sc.push_back (t);
201                 }
202         }
203
204         return sc;
205 }
206
207 void
208 ContentPanel::film_changed (Film::Property p)
209 {
210         switch (p) {
211         case Film::CONTENT:
212                 setup ();
213                 break;
214         default:
215                 break;
216         }
217
218         BOOST_FOREACH (ContentSubPanel* i, _panels) {
219                 i->film_changed (p);
220         }
221 }
222
223 void
224 ContentPanel::selection_changed ()
225 {
226         setup_sensitivity ();
227
228         BOOST_FOREACH (ContentSubPanel* i, _panels) {
229                 i->content_selection_changed ();
230         }
231 }
232
233 void
234 ContentPanel::add_file_clicked ()
235 {
236         /* This method is also called when Ctrl-A is pressed, so check that our notebook page
237            is visible.
238         */
239         if (_parent->GetCurrentPage() != _panel) {
240                 return;
241         }
242
243         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
244            non-Latin filenames or paths.
245         */
246         wxFileDialog* d = new wxFileDialog (_panel, _("Choose a file or files"), wxT (""), wxT (""), wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"), wxFD_MULTIPLE | wxFD_CHANGE_DIR);
247         int const r = d->ShowModal ();
248
249         if (r != wxID_OK) {
250                 d->Destroy ();
251                 return;
252         }
253
254         wxArrayString paths;
255         d->GetPaths (paths);
256         list<boost::filesystem::path> path_list;
257         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
258                 path_list.push_back (wx_to_std (paths[i]));
259         }
260         add_files (path_list);
261
262         d->Destroy ();
263 }
264
265 void
266 ContentPanel::add_folder_clicked ()
267 {
268         wxDirDialog* d = new wxDirDialog (_panel, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
269         int r = d->ShowModal ();
270         boost::filesystem::path const path (wx_to_std (d->GetPath ()));
271         d->Destroy ();
272
273         if (r != wxID_OK) {
274                 return;
275         }
276
277         shared_ptr<Content> content = content_factory (_film, path);
278         if (!content) {
279                 error_dialog (_parent, _("No content found in this folder."));
280                 return;
281         }
282
283         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (content);
284         if (ic) {
285                 ImageSequenceDialog* e = new ImageSequenceDialog (_panel);
286                 r = e->ShowModal ();
287                 float const frame_rate = e->frame_rate ();
288                 e->Destroy ();
289
290                 if (r != wxID_OK) {
291                         return;
292                 }
293
294                 ic->set_video_frame_rate (frame_rate);
295         }
296
297         _film->examine_and_add_content (content);
298 }
299
300 void
301 ContentPanel::remove_clicked ()
302 {
303         ContentList c = selected ();
304         if (c.size() == 1) {
305                 _film->remove_content (c.front ());
306         }
307
308         selection_changed ();
309 }
310
311 void
312 ContentPanel::timeline_clicked ()
313 {
314         if (_timeline_dialog) {
315                 _timeline_dialog->Destroy ();
316                 _timeline_dialog = 0;
317         }
318
319         _timeline_dialog = new TimelineDialog (this, _film);
320         _timeline_dialog->Show ();
321 }
322
323 void
324 ContentPanel::right_click (wxListEvent& ev)
325 {
326         _menu->popup (_film, selected (), TimelineContentViewList (), ev.GetPoint ());
327 }
328
329 /** Set up broad sensitivity based on the type of content that is selected */
330 void
331 ContentPanel::setup_sensitivity ()
332 {
333         _add_file->Enable (_generally_sensitive);
334         _add_folder->Enable (_generally_sensitive);
335
336         ContentList selection = selected ();
337         VideoContentList video_selection = selected_video ();
338         AudioContentList audio_selection = selected_audio ();
339
340         _remove->Enable   (selection.size() == 1 && _generally_sensitive);
341         _earlier->Enable  (selection.size() == 1 && _generally_sensitive);
342         _later->Enable    (selection.size() == 1 && _generally_sensitive);
343         _timeline->Enable (!_film->content().empty() && _generally_sensitive);
344
345         _video_panel->Enable    (video_selection.size() > 0 && _generally_sensitive);
346         _audio_panel->Enable    (audio_selection.size() > 0 && _generally_sensitive);
347         _subtitle_panel->Enable (selection.size() == 1 && dynamic_pointer_cast<SubtitleContent> (selection.front()) && _generally_sensitive);
348         _timing_panel->Enable   (selection.size() == 1 && _generally_sensitive);
349 }
350
351 void
352 ContentPanel::set_film (shared_ptr<Film> film)
353 {
354         _film = film;
355
356         film_changed (Film::CONTENT);
357         film_changed (Film::AUDIO_CHANNELS);
358         selection_changed ();
359 }
360
361 void
362 ContentPanel::set_general_sensitivity (bool s)
363 {
364         _generally_sensitive = s;
365
366         _content->Enable (s);
367         _add_file->Enable (s);
368         _add_folder->Enable (s);
369         _remove->Enable (s);
370         _earlier->Enable (s);
371         _later->Enable (s);
372         _timeline->Enable (s);
373
374         /* Set the panels in the content notebook */
375         BOOST_FOREACH (ContentSubPanel* i, _panels) {
376                 i->Enable (s);
377         }
378 }
379
380 void
381 ContentPanel::earlier_clicked ()
382 {
383         ContentList sel = selected ();
384         if (sel.size() == 1) {
385                 _film->move_content_earlier (sel.front ());
386                 selection_changed ();
387         }
388 }
389
390 void
391 ContentPanel::later_clicked ()
392 {
393         ContentList sel = selected ();
394         if (sel.size() == 1) {
395                 _film->move_content_later (sel.front ());
396                 selection_changed ();
397         }
398 }
399
400 void
401 ContentPanel::set_selection (weak_ptr<Content> wc)
402 {
403         ContentList content = _film->content ();
404         for (size_t i = 0; i < content.size(); ++i) {
405                 if (content[i] == wc.lock ()) {
406                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
407                 } else {
408                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED);
409                 }
410         }
411 }
412
413 void
414 ContentPanel::film_content_changed (int property)
415 {
416         if (property == ContentProperty::PATH || property == ContentProperty::POSITION || property == DCPContentProperty::CAN_BE_PLAYED) {
417                 setup ();
418         }
419
420         BOOST_FOREACH (ContentSubPanel* i, _panels) {
421                 i->film_content_changed (property);
422         }
423 }
424
425 void
426 ContentPanel::setup ()
427 {
428         ContentList content = _film->content ();
429         sort (content.begin(), content.end(), ContentSorter ());
430
431         /* First, check to see if anything has changed and bail if not; this avoids
432            flickering on OS X.
433         */
434
435         vector<string> existing;
436         for (int i = 0; i < _content->GetItemCount(); ++i) {
437                 existing.push_back (wx_to_std (_content->GetItemText (i)));
438         }
439
440         vector<string> proposed;
441         BOOST_FOREACH (shared_ptr<Content> i, content) {
442                bool const valid = i->paths_valid ();
443
444                string s = i->summary ();
445                if (!valid) {
446                        s = _("MISSING: ") + s;
447                }
448
449                proposed.push_back (s);
450         }
451
452         if (existing == proposed) {
453                 return;
454         }
455
456         /* Something has changed: set up the control */
457
458         string selected_summary;
459         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
460         if (s != -1) {
461                 selected_summary = wx_to_std (_content->GetItemText (s));
462         }
463
464         _content->DeleteAllItems ();
465
466         BOOST_FOREACH (shared_ptr<Content> i, content) {
467                 int const t = _content->GetItemCount ();
468                 bool const valid = i->paths_valid ();
469                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i);
470                 bool const needs_kdm = dcp && !dcp->can_be_played ();
471
472                 string s = i->summary ();
473
474                 if (!valid) {
475                         s = _("MISSING: ") + s;
476                 }
477
478                 if (needs_kdm) {
479                         s = _("NEEDS KDM: ") + s;
480                 }
481
482                 _content->InsertItem (t, std_to_wx (s));
483
484                 if (i->summary() == selected_summary) {
485                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
486                 }
487
488                 if (!valid || needs_kdm) {
489                         _content->SetItemTextColour (t, *wxRED);
490                 }
491         }
492
493         if (selected_summary.empty () && !content.empty ()) {
494                 /* Select the item of content if none was selected before */
495                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
496         }
497 }
498
499 void
500 ContentPanel::files_dropped (wxDropFilesEvent& event)
501 {
502         if (!_film) {
503                 return;
504         }
505
506         wxString* paths = event.GetFiles ();
507         list<boost::filesystem::path> path_list;
508         for (int i = 0; i < event.GetNumberOfFiles(); i++) {
509                 path_list.push_back (wx_to_std (paths[i]));
510         }
511
512         add_files (path_list);
513 }
514
515 void
516 ContentPanel::add_files (list<boost::filesystem::path> paths)
517 {
518         /* It has been reported that the paths returned from e.g. wxFileDialog are not always sorted;
519            I can't reproduce that, but sort them anyway.  Don't use ImageFilenameSorter as a normal
520            alphabetical sort is expected here.
521         */
522
523         paths.sort ();
524
525         /* XXX: check for lots of files here and do something */
526
527         for (list<boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
528                 _film->examine_and_add_content (content_factory (_film, *i));
529         }
530 }