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 <dcp/locale_convert.h>
37 #include <boost/foreach.hpp>
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;
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"))
56 wxSize size = TimecodeBase::size (this);
58 for (int i = 0; i < 3; ++i) {
59 _colon[i] = create_label (this, wxT(":"), false);
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);
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);
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);
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);
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);
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);
103 /* We can't use Wrap() here as it doesn't work with markup:
104 * http://trac.wxwidgets.org/ticket/13389
107 wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
109 int const width = 20;
111 for (size_t i = 0; i < in.Length(); ++i) {
112 if (in[i] == ' ' && current >= width) {
121 _tip = new StaticText (this, wxT (""));
122 _tip->SetLabelMarkup (out);
124 /* Hack to stop hidden text on some versions of OS X */
125 _tip->SetMinSize (wxSize (-1, 256));
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));
139 shared_ptr<FilmViewer> fv = _viewer.lock ();
140 DCPOMATIC_ASSERT (fv);
141 fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
143 setup_sensitivity ();
148 TimingPanel::add_to_grid ()
150 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
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));
165 add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
166 _grid->Add (_position, wxGBPosition(r, 1));
169 _move_to_start_of_reel->Show (full);
170 _full_length_label->Show (full);
171 _full_length->Show (full);
172 _trim_start_label->Show (full);
173 _trim_start->Show (full);
174 _trim_start_to_playhead->Show (full);
175 _trim_end_label->Show (full);
176 _trim_end->Show (full);
177 _trim_end_to_playhead->Show (full);
178 _play_length_label->Show (full);
179 _play_length->Show (full);
180 _video_frame_rate_label->Show (full);
181 _video_frame_rate->Show (full);
182 _set_video_frame_rate->Show (full);
186 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
189 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
190 _grid->Add (_full_length, wxGBPosition(r, 1));
193 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
194 _grid->Add (_trim_start, wxGBPosition(r, 1));
197 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
200 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
201 _grid->Add (_trim_end, wxGBPosition(r, 1));
204 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
207 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
208 _grid->Add (_play_length, wxGBPosition(r, 1));
212 add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
213 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
214 s->Add (_video_frame_rate, 1, wxEXPAND);
215 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
216 _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
220 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
223 /* Completely speculative fix for #891 */
228 TimingPanel::update_full_length ()
231 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
232 check.insert (i->full_length(_parent->film()));
235 if (check.size() == 1) {
236 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
238 _full_length->clear ();
243 TimingPanel::update_play_length ()
246 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
247 check.insert (i->length_after_trim(_parent->film()));
250 if (check.size() == 1) {
251 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
253 _play_length->clear ();
258 TimingPanel::film_content_changed (int property)
260 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
262 /* Here we check to see if we have exactly one different value of various
263 properties, and fill the controls with that value if so.
266 if (property == ContentProperty::POSITION) {
269 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
270 check.insert (i->position ());
273 if (check.size() == 1) {
274 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
280 property == ContentProperty::LENGTH ||
281 property == ContentProperty::VIDEO_FRAME_RATE ||
282 property == VideoContentProperty::FRAME_TYPE
285 update_full_length ();
287 } else if (property == ContentProperty::TRIM_START) {
289 set<ContentTime> check;
290 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
291 check.insert (i->trim_start ());
294 if (check.size() == 1) {
295 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
297 _trim_start->clear ();
300 } else if (property == ContentProperty::TRIM_END) {
302 set<ContentTime> check;
303 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
304 check.insert (i->trim_end ());
307 if (check.size() == 1) {
308 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
315 property == ContentProperty::LENGTH ||
316 property == ContentProperty::TRIM_START ||
317 property == ContentProperty::TRIM_END ||
318 property == ContentProperty::VIDEO_FRAME_RATE ||
319 property == VideoContentProperty::FRAME_TYPE
322 update_play_length ();
325 if (property == ContentProperty::VIDEO_FRAME_RATE) {
326 set<double> check_vc;
327 shared_ptr<const Content> content;
330 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
331 if (i->video && i->video_frame_rate()) {
332 check_vc.insert (i->video_frame_rate().get());
335 if (i->audio && i->video_frame_rate()) {
339 if (!i->text.empty() && i->video_frame_rate()) {
346 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
348 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
349 checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
350 _video_frame_rate->Enable (true);
352 checked_set (_video_frame_rate, wxT (""));
353 _video_frame_rate->Enable (false);
357 bool have_still = false;
358 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
359 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
360 if (ic && ic->still ()) {
365 _full_length->set_editable (have_still);
366 _play_length->set_editable (!have_still);
367 _set_video_frame_rate->Enable (false);
368 setup_sensitivity ();
372 TimingPanel::position_changed ()
374 DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
375 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
376 i->set_position (_parent->film(), pos);
381 TimingPanel::full_length_changed ()
383 int const vfr = _parent->film()->video_frame_rate ();
384 Frame const len = _full_length->get (vfr).frames_round (vfr);
385 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
386 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
387 if (ic && ic->still ()) {
388 ic->video->set_length (len);
394 TimingPanel::trim_start_changed ()
396 shared_ptr<FilmViewer> fv = _viewer.lock ();
401 DCPTime const ph = fv->position ();
403 fv->set_coalesce_player_changes (true);
405 shared_ptr<Content> ref;
406 optional<FrameRateChange> ref_frc;
407 optional<DCPTime> ref_ph;
409 ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
411 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
412 if (i->position() <= ph && ph < i->end(_parent->film())) {
413 /* The playhead is in i. Use it as a reference to work out
414 where to put the playhead post-trim; we're trying to keep the playhead
415 at the same frame of content that we're looking at pre-trim.
418 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
419 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
422 i->set_trim_start (trim);
426 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
429 fv->set_coalesce_player_changes (false);
433 TimingPanel::trim_end_changed ()
435 shared_ptr<FilmViewer> fv = _viewer.lock ();
440 fv->set_coalesce_player_changes (true);
442 ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
443 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
444 i->set_trim_end (trim);
447 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
448 if (fv->position() >= _parent->film()->length()) {
449 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
452 fv->set_coalesce_player_changes (true);
456 TimingPanel::play_length_changed ()
458 DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
459 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
460 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
462 ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
468 TimingPanel::video_frame_rate_changed ()
470 _set_video_frame_rate->Enable (true);
474 TimingPanel::set_video_frame_rate ()
477 if (_video_frame_rate->GetValue() != wxT("")) {
478 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
480 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
482 i->set_video_frame_rate (*fr);
484 i->unset_video_frame_rate ();
488 _set_video_frame_rate->Enable (false);
492 TimingPanel::content_selection_changed ()
494 setup_sensitivity ();
496 film_content_changed (ContentProperty::POSITION);
497 film_content_changed (ContentProperty::LENGTH);
498 film_content_changed (ContentProperty::TRIM_START);
499 film_content_changed (ContentProperty::TRIM_END);
500 film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
504 TimingPanel::film_changed (Film::Property p)
506 if (p == Film::VIDEO_FRAME_RATE) {
507 update_full_length ();
508 update_play_length ();
513 TimingPanel::trim_start_to_playhead_clicked ()
515 shared_ptr<FilmViewer> fv = _viewer.lock ();
520 shared_ptr<const Film> film = _parent->film ();
521 DCPTime const ph = fv->position().floor (film->video_frame_rate ());
522 optional<DCPTime> new_ph;
524 fv->set_coalesce_player_changes (true);
526 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
527 if (i->position() < ph && ph < i->end(film)) {
528 FrameRateChange const frc = film->active_frame_rate_change (i->position());
529 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
530 new_ph = i->position ();
535 fv->seek (new_ph.get(), true);
538 fv->set_coalesce_player_changes (false);
542 TimingPanel::trim_end_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 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
552 if (i->position() < ph && ph < i->end(film)) {
553 FrameRateChange const frc = film->active_frame_rate_change (i->position ());
554 i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
560 TimingPanel::setup_sensitivity ()
562 bool const e = !_parent->selected().empty ();
564 _position->Enable (e);
565 _move_to_start_of_reel->Enable (e);
566 _full_length->Enable (e);
567 _trim_start->Enable (e);
568 _trim_end->Enable (e);
569 _play_length->Enable (e);
570 _video_frame_rate->Enable (e);
572 shared_ptr<FilmViewer> fv = _viewer.lock ();
573 DCPOMATIC_ASSERT (fv);
574 DCPTime const ph = fv->position ();
575 bool any_over_ph = false;
576 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
577 if (i->position() <= ph && ph < i->end(_parent->film())) {
582 _trim_start_to_playhead->Enable (any_over_ph);
583 _trim_end_to_playhead->Enable (any_over_ph);
587 TimingPanel::move_to_start_of_reel_clicked ()
589 /* Find common position of all selected content, if it exists */
591 optional<DCPTime> position;
592 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
594 position = i->position();
596 if (position.get() != i->position()) {
603 MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
605 if (d->ShowModal() == wxID_OK) {
606 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
607 i->set_position (_parent->film(), d->position());