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