2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "timing_panel.h"
23 #include "film_viewer.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
44 DCPOMATIC_ENABLE_WARNINGS
46 #include <boost/foreach.hpp>
53 using std::shared_ptr;
55 using std::dynamic_pointer_cast;
56 using boost::optional;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
60 using dcp::locale_convert;
61 using namespace dcpomatic;
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"))
68 , _film_content_changed_suspender (boost::bind(&TimingPanel::film_content_changed, this, _1))
70 wxSize size = TimecodeBase::size (this);
72 for (int i = 0; i < 3; ++i) {
73 _colon[i] = create_label (this, wxT(":"), false);
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);
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);
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);
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);
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);
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));
121 shared_ptr<FilmViewer> fv = _viewer.lock ();
122 DCPOMATIC_ASSERT (fv);
123 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
125 setup_sensitivity ();
130 TimingPanel::add_to_grid ()
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));
145 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
146 _grid->Add (_position, wxGBPosition(r, 1));
149 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
152 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
153 _grid->Add (_full_length, wxGBPosition(r, 1));
156 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
157 _grid->Add (_trim_start, wxGBPosition(r, 1));
160 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
163 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
164 _grid->Add (_trim_end, wxGBPosition(r, 1));
167 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
170 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
171 _grid->Add (_play_length, wxGBPosition(r, 1));
174 /* Completely speculative fix for #891 */
179 TimingPanel::update_full_length ()
182 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
183 check.insert (i->full_length(_parent->film()));
186 if (check.size() == 1) {
187 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
189 _full_length->clear ();
194 TimingPanel::update_play_length ()
197 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
198 check.insert (i->length_after_trim(_parent->film()));
201 if (check.size() == 1) {
202 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
204 _play_length->clear ();
209 TimingPanel::film_content_changed (int property)
211 if (_film_content_changed_suspender.check(property)) {
215 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
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.
221 if (property == ContentProperty::POSITION) {
224 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
225 check.insert (i->position ());
228 if (check.size() == 1) {
229 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
235 property == ContentProperty::LENGTH ||
236 property == ContentProperty::VIDEO_FRAME_RATE ||
237 property == VideoContentProperty::FRAME_TYPE
240 update_full_length ();
242 } else if (property == ContentProperty::TRIM_START) {
244 set<ContentTime> check;
245 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
246 check.insert (i->trim_start ());
249 if (check.size() == 1) {
250 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
252 _trim_start->clear ();
255 } else if (property == ContentProperty::TRIM_END) {
257 set<ContentTime> check;
258 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
259 check.insert (i->trim_end ());
262 if (check.size() == 1) {
263 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
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
277 update_play_length ();
280 if (property == ContentProperty::VIDEO_FRAME_RATE) {
281 set<double> check_vc;
282 shared_ptr<const Content> content;
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());
290 if (i->audio && i->video_frame_rate()) {
294 if (!i->text.empty() && i->video_frame_rate()) {
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 ()) {
310 _full_length->set_editable (have_still);
311 _play_length->set_editable (!have_still);
312 setup_sensitivity ();
316 TimingPanel::position_changed ()
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);
325 TimingPanel::full_length_changed ()
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);
338 TimingPanel::trim_start_changed ()
340 shared_ptr<FilmViewer> fv = _viewer.lock ();
345 DCPTime const ph = fv->position ();
347 fv->set_coalesce_player_changes (true);
349 shared_ptr<Content> ref;
350 optional<FrameRateChange> ref_frc;
351 optional<DCPTime> ref_ph;
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.
361 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
362 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
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);
370 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
373 fv->set_coalesce_player_changes (false);
377 TimingPanel::trim_end_changed ()
379 shared_ptr<FilmViewer> fv = _viewer.lock ();
384 fv->set_coalesce_player_changes (true);
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);
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);
397 fv->set_coalesce_player_changes (false);
401 TimingPanel::play_length_changed ()
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 ());
408 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
415 TimingPanel::content_selection_changed ()
417 setup_sensitivity ();
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);
427 TimingPanel::film_changed (Film::Property p)
429 if (p == Film::VIDEO_FRAME_RATE) {
430 update_full_length ();
431 update_play_length ();
436 TimingPanel::trim_start_to_playhead_clicked ()
438 shared_ptr<FilmViewer> fv = _viewer.lock ();
443 shared_ptr<const Film> film = _parent->film ();
444 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
445 optional<DCPTime> new_ph;
447 fv->set_coalesce_player_changes (true);
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 ();
457 fv->set_coalesce_player_changes (false);
460 fv->seek (new_ph.get(), true);
465 TimingPanel::trim_end_to_playhead_clicked ()
467 shared_ptr<FilmViewer> fv = _viewer.lock ();
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());
483 TimingPanel::setup_sensitivity ()
485 bool const e = !_parent->selected().empty ();
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);
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())) {
504 _trim_start_to_playhead->Enable (any_over_ph);
505 _trim_end_to_playhead->Enable (any_over_ph);
509 TimingPanel::move_to_start_of_reel_clicked ()
511 /* Find common position of all selected content, if it exists */
513 optional<DCPTime> position;
514 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
516 position = i->position();
518 if (position.get() != i->position()) {
525 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
527 if (d->ShowModal() == wxID_OK) {
528 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
529 i->set_position (_parent->film(), d->position());