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