Add a little gap between player buttons and the position slider.
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "auto_crop_dialog.h"
23 #include "content_advanced_dialog.h"
24 #include "content_menu.h"
25 #include "content_properties_dialog.h"
26 #include "dir_dialog.h"
27 #include "file_dialog.h"
28 #include "film_viewer.h"
29 #include "repeat_dialog.h"
30 #include "timeline_video_content_view.h"
31 #include "timeline_audio_content_view.h"
32 #include "wx_util.h"
33 #include "lib/audio_content.h"
34 #include "lib/config.h"
35 #include "lib/content_factory.h"
36 #include "lib/constants.h"
37 #include "lib/copy_dcp_details_to_film.h"
38 #include "lib/dcp_content.h"
39 #include "lib/dcp_examiner.h"
40 #include "lib/examine_content_job.h"
41 #include "lib/exceptions.h"
42 #include "lib/ffmpeg_content.h"
43 #include "lib/film.h"
44 #include "lib/film_util.h"
45 #include "lib/find_missing.h"
46 #include "lib/guess_crop.h"
47 #include "lib/image_content.h"
48 #include "lib/job_manager.h"
49 #include "lib/playlist.h"
50 #include "lib/video_content.h"
51 #include <dcp/cpl.h>
52 #include <dcp/decrypted_kdm.h>
53 #include <dcp/exceptions.h>
54 #include <dcp/search.h>
55 #include <dcp/warnings.h>
56 LIBDCP_DISABLE_WARNINGS
57 #include <wx/dirdlg.h>
58 #include <wx/wx.h>
59 LIBDCP_ENABLE_WARNINGS
60
61
62 using std::dynamic_pointer_cast;
63 using std::exception;
64 using std::list;
65 using std::make_shared;
66 using std::shared_ptr;
67 using std::vector;
68 using std::weak_ptr;
69 using boost::optional;
70 #if BOOST_VERSION >= 106100
71 using namespace boost::placeholders;
72 #endif
73 using namespace dcpomatic;
74
75
76 enum {
77         /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
78         ID_repeat = 256,
79         ID_join,
80         ID_find_missing,
81         ID_properties,
82         ID_advanced,
83         ID_re_examine,
84         ID_auto_crop,
85         ID_kdm,
86         ID_ov,
87         ID_choose_cpl,
88         ID_set_dcp_settings,
89         ID_set_dcp_markers,
90         ID_remove
91 };
92
93
94 ContentMenu::ContentMenu(wxWindow* p, FilmViewer& viewer)
95         : _menu (new wxMenu)
96         , _parent (p)
97         , _pop_up_open (false)
98         , _viewer (viewer)
99 {
100         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
101         _join = _menu->Append (ID_join, _("Join"));
102         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
103         _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
104         _auto_crop = _menu->Append (ID_auto_crop, _("Auto-crop..."));
105         _properties = _menu->Append (ID_properties, _("Properties..."));
106         _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
107         _menu->AppendSeparator ();
108         _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
109         _ov = _menu->Append (ID_ov, _("Add OV..."));
110         _cpl_menu = new wxMenu ();
111         _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
112         _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
113         _set_dcp_markers = _menu->Append(ID_set_dcp_markers, _("Set project markers from this DCP"));
114         _menu->AppendSeparator ();
115         _remove = _menu->Append (ID_remove, _("Remove"));
116
117         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
118         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
119         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
120         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
121         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
122         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
123         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::auto_crop, this), ID_auto_crop);
124         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
125         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
126         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
127         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_markers, this), ID_set_dcp_markers);
128         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
129         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
130 }
131
132 void
133 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
134 {
135         _film = film;
136         _content = c;
137         _views = v;
138
139         int const N = _cpl_menu->GetMenuItemCount();
140         for (int i = 1; i <= N; ++i) {
141                 _cpl_menu->Delete (i);
142         }
143
144         _repeat->Enable (!_content.empty ());
145
146         int n = 0;
147         for (auto i: _content) {
148                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
149                         ++n;
150                 }
151         }
152
153         _join->Enable (n > 1);
154
155         _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
156         _properties->Enable (_content.size() == 1);
157         _advanced->Enable (_content.size() == 1);
158         _re_examine->Enable (!_content.empty ());
159         _auto_crop->Enable (_content.size() == 1);
160
161         if (_content.size() == 1) {
162                 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
163                 if (dcp) {
164                         _kdm->Enable (dcp->encrypted ());
165                         _ov->Enable (dcp->needs_assets ());
166                         _set_dcp_settings->Enable (static_cast<bool>(dcp));
167                         _set_dcp_markers->Enable(static_cast<bool>(dcp));
168                         try {
169                                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
170                                 _choose_cpl->Enable (cpls.size() > 1);
171                                 /* We can't have 0 as a menu item ID on OS X */
172                                 int id = 1;
173                                 for (auto i: cpls) {
174                                         auto item = _cpl_menu->AppendRadioItem (
175                                                 id++,
176                                                 wxString::Format (
177                                                         "%s (%s)",
178                                                         std_to_wx(i->annotation_text().get_value_or("")).data(),
179                                                         std_to_wx(i->id()).data()
180                                                         )
181                                                 );
182                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
183                                 }
184                         } catch (dcp::ReadError &) {
185                                 /* The DCP is probably missing */
186                         } catch (dcp::KDMDecryptionError &) {
187                                 /* We have an incorrect KDM */
188                         } catch (KDMError &) {
189                                 /* We have an incorrect KDM */
190                         }
191                 } else {
192                         _kdm->Enable (false);
193                         _ov->Enable (false);
194                         _choose_cpl->Enable (false);
195                         _set_dcp_settings->Enable (false);
196                         _set_dcp_markers->Enable(false);
197                 }
198         } else {
199                 _kdm->Enable (false);
200                 _set_dcp_settings->Enable (false);
201                 _set_dcp_markers->Enable(false);
202         }
203
204         _remove->Enable (!_content.empty ());
205
206         _pop_up_open = true;
207         _parent->PopupMenu (_menu, p);
208         _pop_up_open = false;
209 }
210
211
212 void
213 ContentMenu::set_dcp_settings ()
214 {
215         auto film = _film.lock ();
216         if (!film) {
217                 return;
218         }
219
220         DCPOMATIC_ASSERT (_content.size() == 1);
221         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
222         DCPOMATIC_ASSERT (dcp);
223         copy_dcp_settings_to_film(dcp, film);
224 }
225
226
227 void
228 ContentMenu::set_dcp_markers()
229 {
230         auto film = _film.lock();
231         if (!film) {
232                 return;
233         }
234
235         DCPOMATIC_ASSERT(_content.size() == 1);
236         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
237         DCPOMATIC_ASSERT(dcp);
238         copy_dcp_markers_to_film(dcp, film);
239 }
240
241
242 void
243 ContentMenu::repeat ()
244 {
245         if (_content.empty ()) {
246                 return;
247         }
248
249         auto d = make_wx<RepeatDialog>(_parent);
250         if (d->ShowModal() != wxID_OK) {
251                 return;
252         }
253
254         auto film = _film.lock ();
255         if (!film) {
256                 return;
257         }
258
259         film->repeat_content (_content, d->number ());
260
261         _content.clear ();
262         _views.clear ();
263 }
264
265
266 void
267 ContentMenu::join ()
268 {
269         vector<shared_ptr<Content>> fc;
270         for (auto i: _content) {
271                 auto f = dynamic_pointer_cast<FFmpegContent> (i);
272                 if (f) {
273                         fc.push_back (f);
274                 }
275         }
276
277         DCPOMATIC_ASSERT (fc.size() > 1);
278
279         auto film = _film.lock ();
280         if (!film) {
281                 return;
282         }
283
284         try {
285                 auto joined = make_shared<FFmpegContent>(fc);
286                 film->remove_content (_content);
287                 film->examine_and_add_content (joined);
288         } catch (JoinError& e) {
289                 error_dialog (_parent, std_to_wx (e.what ()));
290         }
291 }
292
293
294 void
295 ContentMenu::remove ()
296 {
297         if (_content.empty ()) {
298                 return;
299         }
300
301         auto film = _film.lock ();
302         if (!film) {
303                 return;
304         }
305
306         /* We are removing from the timeline if _views is not empty */
307         bool handled = false;
308         if (!_views.empty ()) {
309                 /* Special case: we only remove FFmpegContent if its video view is selected;
310                    if not, and its audio view is selected, we unmap the audio.
311                 */
312                 for (auto i: _content) {
313                         auto fc = dynamic_pointer_cast<FFmpegContent> (i);
314                         if (!fc) {
315                                 continue;
316                         }
317
318                         shared_ptr<TimelineVideoContentView> video;
319                         shared_ptr<TimelineAudioContentView> audio;
320
321                         for (auto j: _views) {
322                                 auto v = dynamic_pointer_cast<TimelineVideoContentView>(j);
323                                 auto a = dynamic_pointer_cast<TimelineAudioContentView>(j);
324                                 if (v && v->content() == fc) {
325                                         video = v;
326                                 } else if (a && a->content() == fc) {
327                                         audio = a;
328                                 }
329                         }
330
331                         if (!video && audio) {
332                                 auto m = fc->audio->mapping ();
333                                 m.unmap_all ();
334                                 fc->audio->set_mapping (m);
335                                 handled = true;
336                         }
337                 }
338         }
339
340         if (!handled) {
341                 film->remove_content (_content);
342         }
343
344         _content.clear ();
345         _views.clear ();
346 }
347
348
349 void
350 ContentMenu::find_missing ()
351 {
352         if (_content.size() != 1) {
353                 return;
354         }
355
356         auto film = _film.lock ();
357         if (!film) {
358                 return;
359         }
360
361         /* XXX: a bit nasty */
362         auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
363         auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
364
365         int r = wxID_CANCEL;
366         boost::filesystem::path path;
367
368         if ((ic && !ic->still ()) || dc) {
369                 auto d = make_wx<wxDirDialog>(nullptr, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
370                 r = d->ShowModal ();
371                 path = wx_to_std (d->GetPath());
372         } else {
373                 auto d = make_wx<wxFileDialog>(nullptr, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
374                 r = d->ShowModal ();
375                 path = wx_to_std (d->GetPath());
376         }
377
378         if (r == wxID_CANCEL) {
379                 return;
380         }
381
382         dcpomatic::find_missing (film->content(), path);
383 }
384
385 void
386 ContentMenu::re_examine ()
387 {
388         auto film = _film.lock ();
389         if (!film) {
390                 return;
391         }
392
393         for (auto i: _content) {
394                 JobManager::instance()->add(make_shared<ExamineContentJob>(film, i));
395         }
396 }
397
398
399 void
400 ContentMenu::kdm ()
401 {
402         DCPOMATIC_ASSERT (!_content.empty ());
403         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
404         DCPOMATIC_ASSERT (dcp);
405
406         FileDialog dialog(_parent, _("Select KDM"), wxT("XML files|*.xml|All files|*.*"), 0, "AddKDMPath");
407
408         if (!dialog.show()) {
409                 return;
410         }
411
412         optional<dcp::EncryptedKDM> kdm;
413         try {
414                 kdm = dcp::EncryptedKDM(dcp::file_to_string(dialog.path(), MAX_KDM_SIZE));
415         } catch (exception& e) {
416                 error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
417                 return;
418         }
419
420         /* Try to decrypt it to get an early preview of any errors */
421         try {
422                 decrypt_kdm_with_helpful_error (*kdm);
423         } catch (KDMError& e) {
424                 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
425                 return;
426         } catch (exception& e) {
427                 error_dialog (_parent, e.what());
428                 return;
429         }
430
431         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
432         bool const kdm_matches_any_cpl = std::any_of(cpls.begin(), cpls.end(), [kdm](shared_ptr<const dcp::CPL> cpl) { return cpl->id() == kdm->cpl_id(); });
433         bool const kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
434
435         if (!kdm_matches_any_cpl) {
436                 error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
437                 return;
438         }
439
440         if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
441                 message_dialog (_parent, _("This KDM was made for one of the CPLs in this DCP, but not the currently selected one.  To play the currently-selected CPL you will need a different KDM."));
442         }
443
444         dcp->add_kdm (*kdm);
445
446         auto film = _film.lock ();
447         DCPOMATIC_ASSERT (film);
448         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
449 }
450
451 void
452 ContentMenu::ov ()
453 {
454         DCPOMATIC_ASSERT (!_content.empty ());
455         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
456         DCPOMATIC_ASSERT (dcp);
457
458         auto film = _film.lock();
459         DCPOMATIC_ASSERT(film);
460         DirDialog dialog(_parent, _("Select OV"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path(film));
461
462         if (dialog.show()) {
463                 dcp->add_ov(dialog.path());
464                 auto film = _film.lock();
465                 DCPOMATIC_ASSERT (film);
466                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
467         }
468 }
469
470 void
471 ContentMenu::properties ()
472 {
473         auto film = _film.lock ();
474         DCPOMATIC_ASSERT (film);
475         auto d = make_wx<ContentPropertiesDialog>(_parent, film, _content.front());
476         d->ShowModal ();
477 }
478
479
480 void
481 ContentMenu::advanced ()
482 {
483         DCPOMATIC_ASSERT(!_content.empty());
484
485         auto content = _content.front();
486         auto dialog = make_wx<ContentAdvancedDialog>(_parent, content);
487
488         if (dialog->ShowModal() == wxID_CANCEL) {
489                 return;
490         }
491
492         if (content->video) {
493                 content->video->set_use(!dialog->ignore_video());
494                 content->video->set_burnt_subtitle_language(dialog->burnt_subtitle_language());
495         }
496
497         auto ffmpeg = dynamic_pointer_cast<FFmpegContent>(content);
498         if (ffmpeg) {
499                 ffmpeg->set_filters(dialog->filters());
500         }
501
502         if (dialog->video_frame_rate()) {
503                 auto film = _film.lock();
504                 DCPOMATIC_ASSERT(film);
505                 content->set_video_frame_rate(film, *dialog->video_frame_rate());
506         } else {
507                 content->unset_video_frame_rate();
508         }
509 }
510
511
512 void
513 ContentMenu::cpl_selected (wxCommandEvent& ev)
514 {
515         if (!_pop_up_open) {
516                 return;
517         }
518
519         DCPOMATIC_ASSERT (!_content.empty ());
520         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
521         DCPOMATIC_ASSERT (dcp);
522
523         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
524         DCPOMATIC_ASSERT (ev.GetId() > 0);
525         DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
526
527         auto i = cpls.begin ();
528         for (int j = 0; j < ev.GetId() - 1; ++j) {
529                 ++i;
530         }
531
532         dcp->set_cpl ((*i)->id ());
533         auto film = _film.lock ();
534         DCPOMATIC_ASSERT (film);
535         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
536 }
537
538
539 void
540 ContentMenu::auto_crop ()
541 {
542         DCPOMATIC_ASSERT (_content.size() == 1);
543
544         auto film = _film.lock ();
545         DCPOMATIC_ASSERT (film);
546
547         auto update_viewer = [this](Crop crop) {
548                 auto film = _film.lock();
549                 DCPOMATIC_ASSERT (film);
550                 auto const content = _content.front();
551                 auto const current_crop = content->video->actual_crop();
552                 _viewer.set_crop_guess(
553                         dcpomatic::Rect<float>(
554                                 static_cast<float>(std::max(0, crop.left - current_crop.left)) / content->video->size().width,
555                                 static_cast<float>(std::max(0, crop.top - current_crop.top)) / content->video->size().height,
556                                 1.0f - (static_cast<float>(std::max(0, crop.left - current_crop.left + crop.right - current_crop.right)) / content->video->size().width),
557                                 1.0f - (static_cast<float>(std::max(0, crop.top - current_crop.top + crop.bottom - current_crop.bottom)) / content->video->size().height)
558                                 ));
559         };
560
561         auto guess_crop_for_content = [this, film]() {
562                 auto position = _viewer.position_in_content(_content.front()).get_value_or(
563                         ContentTime::from_frames(_content.front()->video->length(), _content.front()->video_frame_rate().get_value_or(24))
564                         );
565                 return guess_crop(film, _content.front(), Config::instance()->auto_crop_threshold(), position);
566         };
567
568         /* Make an initial guess in the view and open the dialog */
569
570         auto const crop = guess_crop_for_content ();
571         update_viewer (crop);
572
573         _auto_crop_dialog.reset(_parent, crop);
574         _auto_crop_dialog->Show ();
575
576         /* Update the dialog and view when the crop threshold changes */
577         _auto_crop_config_connection = Config::instance()->Changed.connect([this, guess_crop_for_content, update_viewer](Config::Property property) {
578                 auto film = _film.lock();
579                 DCPOMATIC_ASSERT (film);
580                 if (property == Config::AUTO_CROP_THRESHOLD) {
581                         auto const crop = guess_crop_for_content();
582                         _auto_crop_dialog->set(crop);
583                         update_viewer(crop);
584                 }
585         });
586
587         /* Also update the dialog and view when we're looking at a different frame */
588         _auto_crop_viewer_connection = _viewer.ImageChanged.connect([this, guess_crop_for_content, update_viewer](shared_ptr<PlayerVideo>) {
589                 auto const crop = guess_crop_for_content();
590                 _auto_crop_dialog->set(crop);
591                 update_viewer(crop);
592         });
593
594         /* Handle the user closing the dialog (with OK or cancel) */
595         _auto_crop_dialog->Bind(wxEVT_BUTTON, [this](wxCommandEvent& ev) {
596                 _auto_crop_config_connection.disconnect ();
597                 _auto_crop_viewer_connection.disconnect ();
598                 if (ev.GetId() == wxID_OK) {
599                         _content.front()->video->set_crop(_auto_crop_dialog->get());
600                 }
601                 _auto_crop_dialog->Show (false);
602                 _viewer.unset_crop_guess ();
603         });
604
605         /* Update the view when something in the dialog is changed */
606         _auto_crop_dialog->Changed.connect([update_viewer](Crop crop) {
607                 update_viewer (crop);
608         });
609 }