More player debugging for butler video-full states.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2018 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 #include "timing_panel.h"
22 #include "wx_util.h"
23 #include "film_viewer.h"
24 #include "timecode.h"
25 #include "content_panel.h"
26 #include "move_to_dialog.h"
27 #include "static_text.h"
28 #include "dcpomatic_button.h"
29 #include "lib/content.h"
30 #include "lib/image_content.h"
31 #include "lib/text_content.h"
32 #include "lib/dcp_subtitle_content.h"
33 #include "lib/audio_content.h"
34 #include "lib/string_text_file_content.h"
35 #include "lib/video_content.h"
36 #include <dcp/locale_convert.h>
37 #include <boost/foreach.hpp>
38 #include <set>
39 #include <iostream>
40
41 using std::cout;
42 using std::string;
43 using std::set;
44 using boost::shared_ptr;
45 using boost::weak_ptr;
46 using boost::dynamic_pointer_cast;
47 using boost::optional;
48 using dcp::locale_convert;
49
50 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
51         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
52         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
53         : ContentSubPanel (p, S_("Timing|Timing"))
54         , _viewer (viewer)
55 {
56         wxSize size = TimecodeBase::size (this);
57
58         for (int i = 0; i < 3; ++i) {
59                 _colon[i] = create_label (this, wxT(":"), false);
60         }
61
62         //// TRANSLATORS: this is an abbreviation for "hours"
63         _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
64 #ifdef DCPOMATIC_LINUX
65         /* Hack to work around failure to centre text on GTK */
66         gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
67 #endif
68         //// TRANSLATORS: this is an abbreviation for "minutes"
69         _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
70 #ifdef DCPOMATIC_LINUX
71         gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
72 #endif
73         //// TRANSLATORS: this is an abbreviation for "seconds"
74         _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
75 #ifdef DCPOMATIC_LINUX
76         gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
77 #endif
78         //// TRANSLATORS: this is an abbreviation for "frames"
79         _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
80 #ifdef DCPOMATIC_LINUX
81         gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
82 #endif
83
84         _position_label = create_label (this, _("Position"), true);
85         _position = new Timecode<DCPTime> (this);
86         _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
87         _full_length_label = create_label (this, _("Full length"), true);
88         _full_length = new Timecode<DCPTime> (this);
89         _trim_start_label = create_label (this, _("Trim from start"), true);
90         _trim_start = new Timecode<ContentTime> (this);
91         _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
92         _trim_end_label = create_label (this, _("Trim from end"), true);
93         _trim_end = new Timecode<ContentTime> (this);
94         _trim_end_to_playhead = new Button (this, _("Trim after current position"));
95         _play_length_label = create_label (this, _("Play length"), true);
96         _play_length = new Timecode<DCPTime> (this);
97
98         _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
99         _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
100         _set_video_frame_rate = new Button (this, _("Set"));
101         _set_video_frame_rate->Enable (false);
102
103         /* We can't use Wrap() here as it doesn't work with markup:
104          * http://trac.wxwidgets.org/ticket/13389
105          */
106
107         wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
108         wxString out;
109         int const width = 20;
110         int current = 0;
111         for (size_t i = 0; i < in.Length(); ++i) {
112                 if (in[i] == ' ' && current >= width) {
113                         out += '\n';
114                         current = 0;
115                 } else {
116                         out += in[i];
117                         ++current;
118                 }
119         }
120
121         _tip = new StaticText (this, wxT (""));
122         _tip->SetLabelMarkup (out);
123 #ifdef DCPOMATIC_OSX
124         /* Hack to stop hidden text on some versions of OS X */
125         _tip->SetMinSize (wxSize (-1, 256));
126 #endif
127
128         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
129         _move_to_start_of_reel->Bind  (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
130         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
131         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
132         _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
133         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
134         _trim_end_to_playhead->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
135         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
136         _video_frame_rate->Bind       (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
137         _set_video_frame_rate->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
138
139         shared_ptr<FilmViewer> fv = _viewer.lock ();
140         DCPOMATIC_ASSERT (fv);
141         fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
142
143         setup_sensitivity ();
144         add_to_grid ();
145 }
146
147 void
148 TimingPanel::add_to_grid ()
149 {
150         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
151
152         int r = 0;
153
154         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
155         labels->Add (_h_label, 1, wxEXPAND);
156         add_label_to_sizer (labels, _colon[0], false);
157         labels->Add (_m_label, 1, wxEXPAND);
158         add_label_to_sizer (labels, _colon[1], false);
159         labels->Add (_s_label, 1, wxEXPAND);
160         add_label_to_sizer (labels, _colon[2], false);
161         labels->Add (_f_label, 1, wxEXPAND);
162         _grid->Add (labels, wxGBPosition(r, 1));
163         ++r;
164
165         add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
166         _grid->Add (_position, wxGBPosition(r, 1));
167         ++r;
168
169         _move_to_start_of_reel->Show (full);
170         _full_length_label->Show (full);
171         _full_length->Show (full);
172         _play_length_label->Show (full);
173         _play_length->Show (full);
174         _video_frame_rate_label->Show (full);
175         _video_frame_rate->Show (full);
176         _set_video_frame_rate->Show (full);
177         _tip->Show (full);
178
179         if (full) {
180                 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
181                 ++r;
182
183                 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
184                 _grid->Add (_full_length, wxGBPosition(r, 1));
185                 ++r;
186         }
187
188         add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
189         _grid->Add (_trim_start, wxGBPosition(r, 1));
190         ++r;
191
192         _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
193         ++r;
194
195         add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
196         _grid->Add (_trim_end, wxGBPosition(r, 1));
197         ++r;
198
199         _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
200         ++r;
201
202         if (full) {
203                 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
204                 _grid->Add (_play_length, wxGBPosition(r, 1));
205                 ++r;
206
207                 {
208                         add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
209                         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
210                         s->Add (_video_frame_rate, 1, wxEXPAND);
211                         s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
212                         _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
213                 }
214                 ++r;
215
216                 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
217         }
218
219         /* Completely speculative fix for #891 */
220         _grid->Layout ();
221 }
222
223 void
224 TimingPanel::update_full_length ()
225 {
226         set<DCPTime> check;
227         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
228                 check.insert (i->full_length(_parent->film()));
229         }
230
231         if (check.size() == 1) {
232                 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
233         } else {
234                 _full_length->clear ();
235         }
236 }
237
238 void
239 TimingPanel::update_play_length ()
240 {
241         set<DCPTime> check;
242         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
243                 check.insert (i->length_after_trim(_parent->film()));
244         }
245
246         if (check.size() == 1) {
247                 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
248         } else {
249                 _play_length->clear ();
250         }
251 }
252
253 void
254 TimingPanel::film_content_changed (int property)
255 {
256         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
257
258         /* Here we check to see if we have exactly one different value of various
259            properties, and fill the controls with that value if so.
260         */
261
262         if (property == ContentProperty::POSITION) {
263
264                 set<DCPTime> check;
265                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
266                         check.insert (i->position ());
267                 }
268
269                 if (check.size() == 1) {
270                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
271                 } else {
272                         _position->clear ();
273                 }
274
275         } else if (
276                 property == ContentProperty::LENGTH ||
277                 property == ContentProperty::VIDEO_FRAME_RATE ||
278                 property == VideoContentProperty::FRAME_TYPE
279                 ) {
280
281                 update_full_length ();
282
283         } else if (property == ContentProperty::TRIM_START) {
284
285                 set<ContentTime> check;
286                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
287                         check.insert (i->trim_start ());
288                 }
289
290                 if (check.size() == 1) {
291                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
292                 } else {
293                         _trim_start->clear ();
294                 }
295
296         } else if (property == ContentProperty::TRIM_END) {
297
298                 set<ContentTime> check;
299                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
300                         check.insert (i->trim_end ());
301                 }
302
303                 if (check.size() == 1) {
304                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
305                 } else {
306                         _trim_end->clear ();
307                 }
308         }
309
310         if (
311                 property == ContentProperty::LENGTH ||
312                 property == ContentProperty::TRIM_START ||
313                 property == ContentProperty::TRIM_END ||
314                 property == ContentProperty::VIDEO_FRAME_RATE ||
315                 property == VideoContentProperty::FRAME_TYPE
316                 ) {
317
318                 update_play_length ();
319         }
320
321         if (property == ContentProperty::VIDEO_FRAME_RATE) {
322                 set<double> check_vc;
323                 shared_ptr<const Content> content;
324                 int count_ac = 0;
325                 int count_sc = 0;
326                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
327                         if (i->video && i->video_frame_rate()) {
328                                 check_vc.insert (i->video_frame_rate().get());
329                                 content = i;
330                         }
331                         if (i->audio && i->video_frame_rate()) {
332                                 ++count_ac;
333                                 content = i;
334                         }
335                         if (!i->text.empty() && i->video_frame_rate()) {
336                                 ++count_sc;
337                                 content = i;
338                         }
339
340                 }
341
342                 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
343
344                 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
345                         checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
346                         _video_frame_rate->Enable (true);
347                 } else {
348                         checked_set (_video_frame_rate, wxT (""));
349                         _video_frame_rate->Enable (false);
350                 }
351         }
352
353         bool have_still = false;
354         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
355                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
356                 if (ic && ic->still ()) {
357                         have_still = true;
358                 }
359         }
360
361         _full_length->set_editable (have_still);
362         _play_length->set_editable (!have_still);
363         _set_video_frame_rate->Enable (false);
364         setup_sensitivity ();
365 }
366
367 void
368 TimingPanel::position_changed ()
369 {
370         DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
371         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
372                 i->set_position (_parent->film(), pos);
373         }
374 }
375
376 void
377 TimingPanel::full_length_changed ()
378 {
379         int const vfr = _parent->film()->video_frame_rate ();
380         Frame const len = _full_length->get (vfr).frames_round (vfr);
381         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
382                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
383                 if (ic && ic->still ()) {
384                         ic->video->set_length (len);
385                 }
386         }
387 }
388
389 void
390 TimingPanel::trim_start_changed ()
391 {
392         shared_ptr<FilmViewer> fv = _viewer.lock ();
393         if (!fv) {
394                 return;
395         }
396
397         DCPTime const ph = fv->position ();
398
399         fv->set_coalesce_player_changes (true);
400
401         shared_ptr<Content> ref;
402         optional<FrameRateChange> ref_frc;
403         optional<DCPTime> ref_ph;
404
405         ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
406
407         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
408                 if (i->position() <= ph && ph < i->end(_parent->film())) {
409                         /* The playhead is in i.  Use it as a reference to work out
410                            where to put the playhead post-trim; we're trying to keep the playhead
411                            at the same frame of content that we're looking at pre-trim.
412                         */
413                         ref = i;
414                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
415                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
416                 }
417
418                 i->set_trim_start (trim);
419         }
420
421         if (ref) {
422                 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
423         }
424
425         fv->set_coalesce_player_changes (false);
426 }
427
428 void
429 TimingPanel::trim_end_changed ()
430 {
431         shared_ptr<FilmViewer> fv = _viewer.lock ();
432         if (!fv) {
433                 return;
434         }
435
436         fv->set_coalesce_player_changes (true);
437
438         ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
439         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
440                 i->set_trim_end (trim);
441         }
442
443         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
444         if (fv->position() >= _parent->film()->length()) {
445                 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
446         }
447
448         fv->set_coalesce_player_changes (true);
449 }
450
451 void
452 TimingPanel::play_length_changed ()
453 {
454         DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
455         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
456                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
457                 i->set_trim_end (
458                         ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
459                         );
460         }
461 }
462
463 void
464 TimingPanel::video_frame_rate_changed ()
465 {
466         _set_video_frame_rate->Enable (true);
467 }
468
469 void
470 TimingPanel::set_video_frame_rate ()
471 {
472         optional<double> fr;
473         if (_video_frame_rate->GetValue() != wxT("")) {
474                 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
475         }
476         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
477                 if (fr) {
478                         i->set_video_frame_rate (*fr);
479                 } else {
480                         i->unset_video_frame_rate ();
481                 }
482         }
483
484         _set_video_frame_rate->Enable (false);
485 }
486
487 void
488 TimingPanel::content_selection_changed ()
489 {
490         setup_sensitivity ();
491
492         film_content_changed (ContentProperty::POSITION);
493         film_content_changed (ContentProperty::LENGTH);
494         film_content_changed (ContentProperty::TRIM_START);
495         film_content_changed (ContentProperty::TRIM_END);
496         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
497 }
498
499 void
500 TimingPanel::film_changed (Film::Property p)
501 {
502         if (p == Film::VIDEO_FRAME_RATE) {
503                 update_full_length ();
504                 update_play_length ();
505         }
506 }
507
508 void
509 TimingPanel::trim_start_to_playhead_clicked ()
510 {
511         shared_ptr<FilmViewer> fv = _viewer.lock ();
512         if (!fv) {
513                 return;
514         }
515
516         shared_ptr<const Film> film = _parent->film ();
517         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
518         optional<DCPTime> new_ph;
519
520         fv->set_coalesce_player_changes (true);
521
522         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
523                 if (i->position() < ph && ph < i->end(film)) {
524                         FrameRateChange const frc = film->active_frame_rate_change (i->position());
525                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
526                         new_ph = i->position ();
527                 }
528         }
529
530         if (new_ph) {
531                 fv->seek (new_ph.get(), true);
532         }
533
534         fv->set_coalesce_player_changes (false);
535 }
536
537 void
538 TimingPanel::trim_end_to_playhead_clicked ()
539 {
540         shared_ptr<FilmViewer> fv = _viewer.lock ();
541         if (!fv) {
542                 return;
543         }
544
545         shared_ptr<const Film> film = _parent->film ();
546         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
547         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
548                 if (i->position() < ph && ph < i->end(film)) {
549                         FrameRateChange const frc = film->active_frame_rate_change (i->position ());
550                         i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
551                 }
552         }
553 }
554
555 void
556 TimingPanel::setup_sensitivity ()
557 {
558         bool const e = !_parent->selected().empty ();
559
560         _position->Enable (e);
561         _move_to_start_of_reel->Enable (e);
562         _full_length->Enable (e);
563         _trim_start->Enable (e);
564         _trim_end->Enable (e);
565         _play_length->Enable (e);
566         _video_frame_rate->Enable (e);
567
568         shared_ptr<FilmViewer> fv = _viewer.lock ();
569         DCPOMATIC_ASSERT (fv);
570         DCPTime const ph = fv->position ();
571         bool any_over_ph = false;
572         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
573                 if (i->position() <= ph && ph < i->end(_parent->film())) {
574                         any_over_ph = true;
575                 }
576         }
577
578         _trim_start_to_playhead->Enable (any_over_ph);
579         _trim_end_to_playhead->Enable (any_over_ph);
580 }
581
582 void
583 TimingPanel::move_to_start_of_reel_clicked ()
584 {
585         /* Find common position of all selected content, if it exists */
586
587         optional<DCPTime> position;
588         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
589                 if (!position) {
590                         position = i->position();
591                 } else {
592                         if (position.get() != i->position()) {
593                                 position.reset ();
594                                 break;
595                         }
596                 }
597         }
598
599         MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
600
601         if (d->ShowModal() == wxID_OK) {
602                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
603                         i->set_position (_parent->film(), d->position());
604                 }
605         }
606         d->Destroy ();
607 }