std::shared_ptr
[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 "lib/dcp_content.h"
37 #include "lib/ffmpeg_content.h"
38 #include <dcp/locale_convert.h>
39 #include "lib/warnings.h"
40 #if defined(__WXGTK20__) && !defined(__WXGTK3__)
41 #define TIMING_PANEL_ALIGNMENT_HACK 1
42 DCPOMATIC_DISABLE_WARNINGS
43 #include <gtk/gtk.h>
44 DCPOMATIC_ENABLE_WARNINGS
45 #endif
46 #include <boost/foreach.hpp>
47 #include <set>
48 #include <iostream>
49
50 using std::cout;
51 using std::string;
52 using std::set;
53 using std::shared_ptr;
54 using std::weak_ptr;
55 using std::dynamic_pointer_cast;
56 using boost::optional;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
59 #endif
60 using dcp::locale_convert;
61 using namespace dcpomatic;
62
63 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
64         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
65         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
66         : ContentSubPanel (p, S_("Timing|Timing"))
67         , _viewer (viewer)
68         , _film_content_changed_suspender (boost::bind(&TimingPanel::film_content_changed, this, _1))
69 {
70         wxSize size = TimecodeBase::size (this);
71
72         for (int i = 0; i < 3; ++i) {
73                 _colon[i] = create_label (this, wxT(":"), false);
74         }
75
76         //// TRANSLATORS: this is an abbreviation for "hours"
77         _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
78 #ifdef TIMING_PANEL_ALIGNMENT_HACK
79         /* Hack to work around failure to centre text on GTK */
80         gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
81 #endif
82         //// TRANSLATORS: this is an abbreviation for "minutes"
83         _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
84 #ifdef TIMING_PANEL_ALIGNMENT_HACK
85         gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
86 #endif
87         //// TRANSLATORS: this is an abbreviation for "seconds"
88         _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
89 #ifdef TIMING_PANEL_ALIGNMENT_HACK
90         gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
91 #endif
92         //// TRANSLATORS: this is an abbreviation for "frames"
93         _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
94 #ifdef TIMING_PANEL_ALIGNMENT_HACK
95         gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
96 #endif
97
98         _position_label = create_label (this, _("Position"), true);
99         _position = new Timecode<DCPTime> (this);
100         _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
101         _full_length_label = create_label (this, _("Full length"), true);
102         _full_length = new Timecode<DCPTime> (this);
103         _trim_start_label = create_label (this, _("Trim from start"), true);
104         _trim_start = new Timecode<ContentTime> (this);
105         _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
106         _trim_end_label = create_label (this, _("Trim from end"), true);
107         _trim_end = new Timecode<ContentTime> (this);
108         _trim_end_to_playhead = new Button (this, _("Trim from current position to end"));
109         _play_length_label = create_label (this, _("Play length"), true);
110         _play_length = new Timecode<DCPTime> (this);
111
112         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
113         _move_to_start_of_reel->Bind  (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
114         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
115         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
116         _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
117         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
118         _trim_end_to_playhead->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
119         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
120
121         shared_ptr<FilmViewer> fv = _viewer.lock ();
122         DCPOMATIC_ASSERT (fv);
123         fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
124
125         setup_sensitivity ();
126         add_to_grid ();
127 }
128
129 void
130 TimingPanel::add_to_grid ()
131 {
132         int r = 0;
133
134         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
135         labels->Add (_h_label, 1, wxEXPAND);
136         add_label_to_sizer (labels, _colon[0], false);
137         labels->Add (_m_label, 1, wxEXPAND);
138         add_label_to_sizer (labels, _colon[1], false);
139         labels->Add (_s_label, 1, wxEXPAND);
140         add_label_to_sizer (labels, _colon[2], false);
141         labels->Add (_f_label, 1, wxEXPAND);
142         _grid->Add (labels, wxGBPosition(r, 1));
143         ++r;
144
145         add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
146         _grid->Add (_position, wxGBPosition(r, 1));
147         ++r;
148
149         _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
150         ++r;
151
152         add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
153         _grid->Add (_full_length, wxGBPosition(r, 1));
154         ++r;
155
156         add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
157         _grid->Add (_trim_start, wxGBPosition(r, 1));
158         ++r;
159
160         _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
161         ++r;
162
163         add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
164         _grid->Add (_trim_end, wxGBPosition(r, 1));
165         ++r;
166
167         _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
168         ++r;
169
170         add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
171         _grid->Add (_play_length, wxGBPosition(r, 1));
172         ++r;
173
174         /* Completely speculative fix for #891 */
175         _grid->Layout ();
176 }
177
178 void
179 TimingPanel::update_full_length ()
180 {
181         set<DCPTime> check;
182         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
183                 check.insert (i->full_length(_parent->film()));
184         }
185
186         if (check.size() == 1) {
187                 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
188         } else {
189                 _full_length->clear ();
190         }
191 }
192
193 void
194 TimingPanel::update_play_length ()
195 {
196         set<DCPTime> check;
197         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
198                 check.insert (i->length_after_trim(_parent->film()));
199         }
200
201         if (check.size() == 1) {
202                 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
203         } else {
204                 _play_length->clear ();
205         }
206 }
207
208 void
209 TimingPanel::film_content_changed (int property)
210 {
211         if (_film_content_changed_suspender.check(property)) {
212                 return;
213         }
214
215         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
216
217         /* Here we check to see if we have exactly one different value of various
218            properties, and fill the controls with that value if so.
219         */
220
221         if (property == ContentProperty::POSITION) {
222
223                 set<DCPTime> check;
224                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
225                         check.insert (i->position ());
226                 }
227
228                 if (check.size() == 1) {
229                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
230                 } else {
231                         _position->clear ();
232                 }
233
234         } else if (
235                 property == ContentProperty::LENGTH ||
236                 property == ContentProperty::VIDEO_FRAME_RATE ||
237                 property == VideoContentProperty::FRAME_TYPE
238                 ) {
239
240                 update_full_length ();
241
242         } else if (property == ContentProperty::TRIM_START) {
243
244                 set<ContentTime> check;
245                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
246                         check.insert (i->trim_start ());
247                 }
248
249                 if (check.size() == 1) {
250                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
251                 } else {
252                         _trim_start->clear ();
253                 }
254
255         } else if (property == ContentProperty::TRIM_END) {
256
257                 set<ContentTime> check;
258                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
259                         check.insert (i->trim_end ());
260                 }
261
262                 if (check.size() == 1) {
263                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
264                 } else {
265                         _trim_end->clear ();
266                 }
267         }
268
269         if (
270                 property == ContentProperty::LENGTH ||
271                 property == ContentProperty::TRIM_START ||
272                 property == ContentProperty::TRIM_END ||
273                 property == ContentProperty::VIDEO_FRAME_RATE ||
274                 property == VideoContentProperty::FRAME_TYPE
275                 ) {
276
277                 update_play_length ();
278         }
279
280         if (property == ContentProperty::VIDEO_FRAME_RATE) {
281                 set<double> check_vc;
282                 shared_ptr<const Content> content;
283                 int count_ac = 0;
284                 int count_sc = 0;
285                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
286                         if (i->video && i->video_frame_rate()) {
287                                 check_vc.insert (i->video_frame_rate().get());
288                                 content = i;
289                         }
290                         if (i->audio && i->video_frame_rate()) {
291                                 ++count_ac;
292                                 content = i;
293                         }
294                         if (!i->text.empty() && i->video_frame_rate()) {
295                                 ++count_sc;
296                                 content = i;
297                         }
298
299                 }
300         }
301
302         bool have_still = false;
303         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
304                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
305                 if (ic && ic->still ()) {
306                         have_still = true;
307                 }
308         }
309
310         _full_length->set_editable (have_still);
311         _play_length->set_editable (!have_still);
312         setup_sensitivity ();
313 }
314
315 void
316 TimingPanel::position_changed ()
317 {
318         DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
319         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
320                 i->set_position (_parent->film(), pos);
321         }
322 }
323
324 void
325 TimingPanel::full_length_changed ()
326 {
327         int const vfr = _parent->film()->video_frame_rate ();
328         Frame const len = _full_length->get (vfr).frames_round (vfr);
329         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
330                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
331                 if (ic && ic->still ()) {
332                         ic->video->set_length (len);
333                 }
334         }
335 }
336
337 void
338 TimingPanel::trim_start_changed ()
339 {
340         shared_ptr<FilmViewer> fv = _viewer.lock ();
341         if (!fv) {
342                 return;
343         }
344
345         DCPTime const ph = fv->position ();
346
347         fv->set_coalesce_player_changes (true);
348
349         shared_ptr<Content> ref;
350         optional<FrameRateChange> ref_frc;
351         optional<DCPTime> ref_ph;
352
353         Suspender::Block bl = _film_content_changed_suspender.block ();
354         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
355                 if (i->position() <= ph && ph < i->end(_parent->film())) {
356                         /* The playhead is in i.  Use it as a reference to work out
357                            where to put the playhead post-trim; we're trying to keep the playhead
358                            at the same frame of content that we're looking at pre-trim.
359                         */
360                         ref = i;
361                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
362                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
363                 }
364
365                 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
366                 i->set_trim_start (trim);
367         }
368
369         if (ref) {
370                 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
371         }
372
373         fv->set_coalesce_player_changes (false);
374 }
375
376 void
377 TimingPanel::trim_end_changed ()
378 {
379         shared_ptr<FilmViewer> fv = _viewer.lock ();
380         if (!fv) {
381                 return;
382         }
383
384         fv->set_coalesce_player_changes (true);
385
386         Suspender::Block bl = _film_content_changed_suspender.block ();
387         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
388                 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
389                 i->set_trim_end (trim);
390         }
391
392         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
393         if (fv->position() >= _parent->film()->length()) {
394                 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
395         }
396
397         fv->set_coalesce_player_changes (false);
398 }
399
400 void
401 TimingPanel::play_length_changed ()
402 {
403         DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
404         Suspender::Block bl = _film_content_changed_suspender.block ();
405         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
406                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
407                 i->set_trim_end (
408                         ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
409                         );
410         }
411 }
412
413
414 void
415 TimingPanel::content_selection_changed ()
416 {
417         setup_sensitivity ();
418
419         film_content_changed (ContentProperty::POSITION);
420         film_content_changed (ContentProperty::LENGTH);
421         film_content_changed (ContentProperty::TRIM_START);
422         film_content_changed (ContentProperty::TRIM_END);
423         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
424 }
425
426 void
427 TimingPanel::film_changed (Film::Property p)
428 {
429         if (p == Film::VIDEO_FRAME_RATE) {
430                 update_full_length ();
431                 update_play_length ();
432         }
433 }
434
435 void
436 TimingPanel::trim_start_to_playhead_clicked ()
437 {
438         shared_ptr<FilmViewer> fv = _viewer.lock ();
439         if (!fv) {
440                 return;
441         }
442
443         shared_ptr<const Film> film = _parent->film ();
444         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
445         optional<DCPTime> new_ph;
446
447         fv->set_coalesce_player_changes (true);
448
449         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
450                 if (i->position() < ph && ph < i->end(film)) {
451                         FrameRateChange const frc = film->active_frame_rate_change (i->position());
452                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
453                         new_ph = i->position ();
454                 }
455         }
456
457         fv->set_coalesce_player_changes (false);
458
459         if (new_ph) {
460                 fv->seek (new_ph.get(), true);
461         }
462 }
463
464 void
465 TimingPanel::trim_end_to_playhead_clicked ()
466 {
467         shared_ptr<FilmViewer> fv = _viewer.lock ();
468         if (!fv) {
469                 return;
470         }
471
472         shared_ptr<const Film> film = _parent->film ();
473         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
474         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
475                 if (i->position() < ph && ph < i->end(film)) {
476                         FrameRateChange const frc = film->active_frame_rate_change (i->position ());
477                         i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph, frc) - i->trim_start());
478                 }
479         }
480 }
481
482 void
483 TimingPanel::setup_sensitivity ()
484 {
485         bool const e = !_parent->selected().empty ();
486
487         _position->Enable (e);
488         _move_to_start_of_reel->Enable (e);
489         _full_length->Enable (e);
490         _trim_start->Enable (e);
491         _trim_end->Enable (e);
492         _play_length->Enable (e);
493
494         shared_ptr<FilmViewer> fv = _viewer.lock ();
495         DCPOMATIC_ASSERT (fv);
496         DCPTime const ph = fv->position ();
497         bool any_over_ph = false;
498         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
499                 if (i->position() <= ph && ph < i->end(_parent->film())) {
500                         any_over_ph = true;
501                 }
502         }
503
504         _trim_start_to_playhead->Enable (any_over_ph);
505         _trim_end_to_playhead->Enable (any_over_ph);
506 }
507
508 void
509 TimingPanel::move_to_start_of_reel_clicked ()
510 {
511         /* Find common position of all selected content, if it exists */
512
513         optional<DCPTime> position;
514         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
515                 if (!position) {
516                         position = i->position();
517                 } else {
518                         if (position.get() != i->position()) {
519                                 position.reset ();
520                                 break;
521                         }
522                 }
523         }
524
525         MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
526
527         if (d->ShowModal() == wxID_OK) {
528                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
529                         i->set_position (_parent->film(), d->position());
530                 }
531         }
532         d->Destroy ();
533 }