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 using dcp::locale_convert;
58 using namespace dcpomatic;
60 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
61 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
62 /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
63 : ContentSubPanel (p, S_("Timing|Timing"))
65 , _film_content_changed_suspender (boost::bind(&TimingPanel::film_content_changed, this, _1))
67 wxSize size = TimecodeBase::size (this);
69 for (int i = 0; i < 3; ++i) {
70 _colon[i] = create_label (this, wxT(":"), false);
73 //// TRANSLATORS: this is an abbreviation for "hours"
74 _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
75 #ifdef TIMING_PANEL_ALIGNMENT_HACK
76 /* Hack to work around failure to centre text on GTK */
77 gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
79 //// TRANSLATORS: this is an abbreviation for "minutes"
80 _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
81 #ifdef TIMING_PANEL_ALIGNMENT_HACK
82 gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
84 //// TRANSLATORS: this is an abbreviation for "seconds"
85 _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
86 #ifdef TIMING_PANEL_ALIGNMENT_HACK
87 gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
89 //// TRANSLATORS: this is an abbreviation for "frames"
90 _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
91 #ifdef TIMING_PANEL_ALIGNMENT_HACK
92 gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
95 _position_label = create_label (this, _("Position"), true);
96 _position = new Timecode<DCPTime> (this);
97 _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
98 _full_length_label = create_label (this, _("Full length"), true);
99 _full_length = new Timecode<DCPTime> (this);
100 _trim_start_label = create_label (this, _("Trim from start"), true);
101 _trim_start = new Timecode<ContentTime> (this);
102 _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
103 _trim_end_label = create_label (this, _("Trim from end"), true);
104 _trim_end = new Timecode<ContentTime> (this);
105 _trim_end_to_playhead = new Button (this, _("Trim after current position"));
106 _play_length_label = create_label (this, _("Play length"), true);
107 _play_length = new Timecode<DCPTime> (this);
109 _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
110 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
111 _set_video_frame_rate = new Button (this, _("Set"));
112 _set_video_frame_rate->Enable (false);
114 /* We can't use Wrap() here as it doesn't work with markup:
115 * http://trac.wxwidgets.org/ticket/13389
118 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
120 int const width = 20;
122 for (size_t i = 0; i < in.Length(); ++i) {
123 if (in[i] == ' ' && current >= width) {
132 _tip = new StaticText (this, wxT (""));
133 _tip->SetLabelMarkup (out);
135 /* Hack to stop hidden text on some versions of OS X */
136 _tip->SetMinSize (wxSize (-1, 256));
139 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
140 _move_to_start_of_reel->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
141 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
142 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
143 _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
144 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
145 _trim_end_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
146 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
147 _video_frame_rate->Bind (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
148 _set_video_frame_rate->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
150 shared_ptr<FilmViewer> fv = _viewer.lock ();
151 DCPOMATIC_ASSERT (fv);
152 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
154 setup_sensitivity ();
159 TimingPanel::add_to_grid ()
161 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
165 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
166 labels->Add (_h_label, 1, wxEXPAND);
167 add_label_to_sizer (labels, _colon[0], false);
168 labels->Add (_m_label, 1, wxEXPAND);
169 add_label_to_sizer (labels, _colon[1], false);
170 labels->Add (_s_label, 1, wxEXPAND);
171 add_label_to_sizer (labels, _colon[2], false);
172 labels->Add (_f_label, 1, wxEXPAND);
173 _grid->Add (labels, wxGBPosition(r, 1));
176 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
177 _grid->Add (_position, wxGBPosition(r, 1));
180 _move_to_start_of_reel->Show (full);
181 _full_length_label->Show (full);
182 _full_length->Show (full);
183 _play_length_label->Show (full);
184 _play_length->Show (full);
185 _video_frame_rate_label->Show (full);
186 _video_frame_rate->Show (full);
187 _set_video_frame_rate->Show (full);
191 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
194 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
195 _grid->Add (_full_length, wxGBPosition(r, 1));
199 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
200 _grid->Add (_trim_start, wxGBPosition(r, 1));
203 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
206 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
207 _grid->Add (_trim_end, wxGBPosition(r, 1));
210 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
214 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
215 _grid->Add (_play_length, wxGBPosition(r, 1));
219 add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
220 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
221 s->Add (_video_frame_rate, 1, wxEXPAND);
222 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
223 _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
227 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
230 /* Completely speculative fix for #891 */
235 TimingPanel::update_full_length ()
238 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
239 check.insert (i->full_length(_parent->film()));
242 if (check.size() == 1) {
243 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
245 _full_length->clear ();
250 TimingPanel::update_play_length ()
253 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
254 check.insert (i->length_after_trim(_parent->film()));
257 if (check.size() == 1) {
258 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
260 _play_length->clear ();
265 TimingPanel::film_content_changed (int property)
267 if (_film_content_changed_suspender.check(property)) {
271 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
273 /* Here we check to see if we have exactly one different value of various
274 properties, and fill the controls with that value if so.
277 if (property == ContentProperty::POSITION) {
280 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
281 check.insert (i->position ());
284 if (check.size() == 1) {
285 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
291 property == ContentProperty::LENGTH ||
292 property == ContentProperty::VIDEO_FRAME_RATE ||
293 property == VideoContentProperty::FRAME_TYPE
296 update_full_length ();
298 } else if (property == ContentProperty::TRIM_START) {
300 set<ContentTime> check;
301 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
302 check.insert (i->trim_start ());
305 if (check.size() == 1) {
306 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
308 _trim_start->clear ();
311 } else if (property == ContentProperty::TRIM_END) {
313 set<ContentTime> check;
314 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
315 check.insert (i->trim_end ());
318 if (check.size() == 1) {
319 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
326 property == ContentProperty::LENGTH ||
327 property == ContentProperty::TRIM_START ||
328 property == ContentProperty::TRIM_END ||
329 property == ContentProperty::VIDEO_FRAME_RATE ||
330 property == VideoContentProperty::FRAME_TYPE
333 update_play_length ();
336 if (property == ContentProperty::VIDEO_FRAME_RATE) {
337 set<double> check_vc;
338 shared_ptr<const Content> content;
341 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
342 if (i->video && i->video_frame_rate()) {
343 check_vc.insert (i->video_frame_rate().get());
346 if (i->audio && i->video_frame_rate()) {
350 if (!i->text.empty() && i->video_frame_rate()) {
357 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
359 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
360 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
361 _video_frame_rate->Enable (true);
363 checked_set (_video_frame_rate, wxT (""));
364 _video_frame_rate->Enable (false);
368 bool have_still = false;
369 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
370 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
371 if (ic && ic->still ()) {
376 _full_length->set_editable (have_still);
377 _play_length->set_editable (!have_still);
378 _set_video_frame_rate->Enable (false);
379 setup_sensitivity ();
383 TimingPanel::position_changed ()
385 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
386 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
387 i->set_position (_parent->film(), pos);
392 TimingPanel::full_length_changed ()
394 int const vfr = _parent->film()->video_frame_rate ();
395 Frame const len = _full_length->get (vfr).frames_round (vfr);
396 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
397 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
398 if (ic && ic->still ()) {
399 ic->video->set_length (len);
405 TimingPanel::trim_start_changed ()
407 shared_ptr<FilmViewer> fv = _viewer.lock ();
412 DCPTime const ph = fv->position ();
414 fv->set_coalesce_player_changes (true);
416 shared_ptr<Content> ref;
417 optional<FrameRateChange> ref_frc;
418 optional<DCPTime> ref_ph;
420 Suspender::Block bl = _film_content_changed_suspender.block ();
421 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
422 if (i->position() <= ph && ph < i->end(_parent->film())) {
423 /* The playhead is in i. Use it as a reference to work out
424 where to put the playhead post-trim; we're trying to keep the playhead
425 at the same frame of content that we're looking at pre-trim.
428 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
429 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
432 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
433 i->set_trim_start (trim);
437 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
440 fv->set_coalesce_player_changes (false);
444 TimingPanel::trim_end_changed ()
446 shared_ptr<FilmViewer> fv = _viewer.lock ();
451 fv->set_coalesce_player_changes (true);
453 Suspender::Block bl = _film_content_changed_suspender.block ();
454 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
455 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
456 i->set_trim_end (trim);
459 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
460 if (fv->position() >= _parent->film()->length()) {
461 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
464 fv->set_coalesce_player_changes (false);
468 TimingPanel::play_length_changed ()
470 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
471 Suspender::Block bl = _film_content_changed_suspender.block ();
472 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
473 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
475 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
481 TimingPanel::video_frame_rate_changed ()
484 if (_video_frame_rate->GetValue() == wxT("")) {
485 /* No frame rate has been entered; if the user clicks "set" now it would unset the video
486 frame rate in the selected content. This can't be allowed for some content types.
488 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
490 dynamic_pointer_cast<DCPContent>(i) ||
491 dynamic_pointer_cast<FFmpegContent>(i)
498 _set_video_frame_rate->Enable (enable);
502 TimingPanel::set_video_frame_rate ()
505 if (_video_frame_rate->GetValue() != wxT("")) {
506 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
508 Suspender::Block bl = _film_content_changed_suspender.block ();
509 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
511 i->set_video_frame_rate (*fr);
513 i->unset_video_frame_rate ();
517 _set_video_frame_rate->Enable (false);
521 TimingPanel::content_selection_changed ()
523 setup_sensitivity ();
525 film_content_changed (ContentProperty::POSITION);
526 film_content_changed (ContentProperty::LENGTH);
527 film_content_changed (ContentProperty::TRIM_START);
528 film_content_changed (ContentProperty::TRIM_END);
529 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
533 TimingPanel::film_changed (Film::Property p)
535 if (p == Film::VIDEO_FRAME_RATE) {
536 update_full_length ();
537 update_play_length ();
542 TimingPanel::trim_start_to_playhead_clicked ()
544 shared_ptr<FilmViewer> fv = _viewer.lock ();
549 shared_ptr<const Film> film = _parent->film ();
550 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
551 optional<DCPTime> new_ph;
553 fv->set_coalesce_player_changes (true);
555 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
556 if (i->position() < ph && ph < i->end(film)) {
557 FrameRateChange const frc = film->active_frame_rate_change (i->position());
558 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
559 new_ph = i->position ();
564 fv->seek (new_ph.get(), true);
567 fv->set_coalesce_player_changes (false);
571 TimingPanel::trim_end_to_playhead_clicked ()
573 shared_ptr<FilmViewer> fv = _viewer.lock ();
578 shared_ptr<const Film> film = _parent->film ();
579 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
580 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
581 if (i->position() < ph && ph < i->end(film)) {
582 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
583 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
589 TimingPanel::setup_sensitivity ()
591 bool const e = !_parent->selected().empty ();
593 _position->Enable (e);
594 _move_to_start_of_reel->Enable (e);
595 _full_length->Enable (e);
596 _trim_start->Enable (e);
597 _trim_end->Enable (e);
598 _play_length->Enable (e);
599 _video_frame_rate->Enable (e);
601 shared_ptr<FilmViewer> fv = _viewer.lock ();
602 DCPOMATIC_ASSERT (fv);
603 DCPTime const ph = fv->position ();
604 bool any_over_ph = false;
605 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
606 if (i->position() <= ph && ph < i->end(_parent->film())) {
611 _trim_start_to_playhead->Enable (any_over_ph);
612 _trim_end_to_playhead->Enable (any_over_ph);
616 TimingPanel::move_to_start_of_reel_clicked ()
618 /* Find common position of all selected content, if it exists */
620 optional<DCPTime> position;
621 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
623 position = i->position();
625 if (position.get() != i->position()) {
632 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
634 if (d->ShowModal() == wxID_OK) {
635 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
636 i->set_position (_parent->film(), d->position());