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 boost::shared_ptr;
54 using boost::weak_ptr;
55 using boost::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 ()
132 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
136 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
137 labels->Add (_h_label, 1, wxEXPAND);
138 add_label_to_sizer (labels, _colon[0], false);
139 labels->Add (_m_label, 1, wxEXPAND);
140 add_label_to_sizer (labels, _colon[1], false);
141 labels->Add (_s_label, 1, wxEXPAND);
142 add_label_to_sizer (labels, _colon[2], false);
143 labels->Add (_f_label, 1, wxEXPAND);
144 _grid->Add (labels, wxGBPosition(r, 1));
147 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
148 _grid->Add (_position, wxGBPosition(r, 1));
151 _move_to_start_of_reel->Show (full);
152 _full_length_label->Show (full);
153 _full_length->Show (full);
154 _play_length_label->Show (full);
155 _play_length->Show (full);
158 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
161 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
162 _grid->Add (_full_length, wxGBPosition(r, 1));
166 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
167 _grid->Add (_trim_start, wxGBPosition(r, 1));
170 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
173 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
174 _grid->Add (_trim_end, wxGBPosition(r, 1));
177 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
181 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
182 _grid->Add (_play_length, wxGBPosition(r, 1));
186 /* Completely speculative fix for #891 */
191 TimingPanel::update_full_length ()
194 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
195 check.insert (i->full_length(_parent->film()));
198 if (check.size() == 1) {
199 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
201 _full_length->clear ();
206 TimingPanel::update_play_length ()
209 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
210 check.insert (i->length_after_trim(_parent->film()));
213 if (check.size() == 1) {
214 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
216 _play_length->clear ();
221 TimingPanel::film_content_changed (int property)
223 if (_film_content_changed_suspender.check(property)) {
227 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
229 /* Here we check to see if we have exactly one different value of various
230 properties, and fill the controls with that value if so.
233 if (property == ContentProperty::POSITION) {
236 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
237 check.insert (i->position ());
240 if (check.size() == 1) {
241 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
247 property == ContentProperty::LENGTH ||
248 property == ContentProperty::VIDEO_FRAME_RATE ||
249 property == VideoContentProperty::FRAME_TYPE
252 update_full_length ();
254 } else if (property == ContentProperty::TRIM_START) {
256 set<ContentTime> check;
257 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
258 check.insert (i->trim_start ());
261 if (check.size() == 1) {
262 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
264 _trim_start->clear ();
267 } else if (property == ContentProperty::TRIM_END) {
269 set<ContentTime> check;
270 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
271 check.insert (i->trim_end ());
274 if (check.size() == 1) {
275 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
282 property == ContentProperty::LENGTH ||
283 property == ContentProperty::TRIM_START ||
284 property == ContentProperty::TRIM_END ||
285 property == ContentProperty::VIDEO_FRAME_RATE ||
286 property == VideoContentProperty::FRAME_TYPE
289 update_play_length ();
292 if (property == ContentProperty::VIDEO_FRAME_RATE) {
293 set<double> check_vc;
294 shared_ptr<const Content> content;
297 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
298 if (i->video && i->video_frame_rate()) {
299 check_vc.insert (i->video_frame_rate().get());
302 if (i->audio && i->video_frame_rate()) {
306 if (!i->text.empty() && i->video_frame_rate()) {
314 bool have_still = false;
315 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
316 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
317 if (ic && ic->still ()) {
322 _full_length->set_editable (have_still);
323 _play_length->set_editable (!have_still);
324 setup_sensitivity ();
328 TimingPanel::position_changed ()
330 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
331 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
332 i->set_position (_parent->film(), pos);
337 TimingPanel::full_length_changed ()
339 int const vfr = _parent->film()->video_frame_rate ();
340 Frame const len = _full_length->get (vfr).frames_round (vfr);
341 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
342 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
343 if (ic && ic->still ()) {
344 ic->video->set_length (len);
350 TimingPanel::trim_start_changed ()
352 shared_ptr<FilmViewer> fv = _viewer.lock ();
357 DCPTime const ph = fv->position ();
359 fv->set_coalesce_player_changes (true);
361 shared_ptr<Content> ref;
362 optional<FrameRateChange> ref_frc;
363 optional<DCPTime> ref_ph;
365 Suspender::Block bl = _film_content_changed_suspender.block ();
366 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
367 if (i->position() <= ph && ph < i->end(_parent->film())) {
368 /* The playhead is in i. Use it as a reference to work out
369 where to put the playhead post-trim; we're trying to keep the playhead
370 at the same frame of content that we're looking at pre-trim.
373 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
374 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
377 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
378 i->set_trim_start (trim);
382 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
385 fv->set_coalesce_player_changes (false);
389 TimingPanel::trim_end_changed ()
391 shared_ptr<FilmViewer> fv = _viewer.lock ();
396 fv->set_coalesce_player_changes (true);
398 Suspender::Block bl = _film_content_changed_suspender.block ();
399 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
400 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
401 i->set_trim_end (trim);
404 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
405 if (fv->position() >= _parent->film()->length()) {
406 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
409 fv->set_coalesce_player_changes (false);
413 TimingPanel::play_length_changed ()
415 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
416 Suspender::Block bl = _film_content_changed_suspender.block ();
417 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
418 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
420 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
427 TimingPanel::content_selection_changed ()
429 setup_sensitivity ();
431 film_content_changed (ContentProperty::POSITION);
432 film_content_changed (ContentProperty::LENGTH);
433 film_content_changed (ContentProperty::TRIM_START);
434 film_content_changed (ContentProperty::TRIM_END);
435 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
439 TimingPanel::film_changed (Film::Property p)
441 if (p == Film::VIDEO_FRAME_RATE) {
442 update_full_length ();
443 update_play_length ();
448 TimingPanel::trim_start_to_playhead_clicked ()
450 shared_ptr<FilmViewer> fv = _viewer.lock ();
455 shared_ptr<const Film> film = _parent->film ();
456 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
457 optional<DCPTime> new_ph;
459 fv->set_coalesce_player_changes (true);
461 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
462 if (i->position() < ph && ph < i->end(film)) {
463 FrameRateChange const frc = film->active_frame_rate_change (i->position());
464 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
465 new_ph = i->position ();
469 fv->set_coalesce_player_changes (false);
472 fv->seek (new_ph.get(), true);
477 TimingPanel::trim_end_to_playhead_clicked ()
479 shared_ptr<FilmViewer> fv = _viewer.lock ();
484 shared_ptr<const Film> film = _parent->film ();
485 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
486 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
487 if (i->position() < ph && ph < i->end(film)) {
488 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
489 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph, frc) - i->trim_start());
495 TimingPanel::setup_sensitivity ()
497 bool const e = !_parent->selected().empty ();
499 _position->Enable (e);
500 _move_to_start_of_reel->Enable (e);
501 _full_length->Enable (e);
502 _trim_start->Enable (e);
503 _trim_end->Enable (e);
504 _play_length->Enable (e);
506 shared_ptr<FilmViewer> fv = _viewer.lock ();
507 DCPOMATIC_ASSERT (fv);
508 DCPTime const ph = fv->position ();
509 bool any_over_ph = false;
510 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
511 if (i->position() <= ph && ph < i->end(_parent->film())) {
516 _trim_start_to_playhead->Enable (any_over_ph);
517 _trim_end_to_playhead->Enable (any_over_ph);
521 TimingPanel::move_to_start_of_reel_clicked ()
523 /* Find common position of all selected content, if it exists */
525 optional<DCPTime> position;
526 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
528 position = i->position();
530 if (position.get() != i->position()) {
537 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
539 if (d->ShowModal() == wxID_OK) {
540 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
541 i->set_position (_parent->film(), d->position());