2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "timing_panel.h"
22 #include "film_viewer.h"
24 #include "content_panel.h"
25 #include "lib/content.h"
26 #include "lib/image_content.h"
27 #include "lib/raw_convert.h"
28 #include "lib/subtitle_content.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/text_subtitle_content.h"
31 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
42 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
43 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
44 : ContentSubPanel (p, S_("Timing|Timing"))
47 wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
48 _sizer->Add (grid, 0, wxALL, 8);
50 wxSize size = TimecodeBase::size (this);
52 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
53 //// TRANSLATORS: this is an abbreviation for "hours"
54 wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
55 #ifdef DCPOMATIC_LINUX
56 /* Hack to work around failure to centre text on GTK */
57 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
59 labels->Add (t, 1, wxEXPAND);
60 add_label_to_sizer (labels, this, wxT (":"), false);
61 //// TRANSLATORS: this is an abbreviation for "minutes"
62 t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
63 #ifdef DCPOMATIC_LINUX
64 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
66 labels->Add (t, 1, wxEXPAND);
67 add_label_to_sizer (labels, this, wxT (":"), false);
68 //// TRANSLATORS: this is an abbreviation for "seconds"
69 t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
70 #ifdef DCPOMATIC_LINUX
71 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
73 labels->Add (t, 1, wxEXPAND);
74 add_label_to_sizer (labels, this, wxT (":"), false);
75 //// TRANSLATORS: this is an abbreviation for "frames"
76 t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
77 #ifdef DCPOMATIC_LINUX
78 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
80 labels->Add (t, 1, wxEXPAND);
81 grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
84 add_label_to_sizer (grid, this, _("Position"), true);
85 _position = new Timecode<DCPTime> (this);
86 grid->Add (_position);
87 add_label_to_sizer (grid, this, _("Full length"), true);
88 _full_length = new Timecode<DCPTime> (this);
89 grid->Add (_full_length);
90 add_label_to_sizer (grid, this, _("Trim from start"), true);
91 _trim_start = new Timecode<ContentTime> (this);
92 grid->Add (_trim_start);
93 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
95 grid->Add (_trim_start_to_playhead);
96 add_label_to_sizer (grid, this, _("Trim from end"), true);
97 _trim_end = new Timecode<ContentTime> (this);
98 grid->Add (_trim_end);
99 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
101 grid->Add (_trim_end_to_playhead);
102 add_label_to_sizer (grid, this, _("Play length"), true);
103 _play_length = new Timecode<DCPTime> (this);
104 grid->Add (_play_length);
107 add_label_to_sizer (grid, this, _("Video frame rate"), true);
108 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
109 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
110 s->Add (_video_frame_rate, 1, wxEXPAND);
111 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
112 _set_video_frame_rate->Enable (false);
113 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
114 grid->Add (s, 1, wxEXPAND);
119 /* We can't use Wrap() here as it doesn't work with markup:
120 * http://trac.wxwidgets.org/ticket/13389
123 wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
125 int const width = 20;
127 for (size_t i = 0; i < in.Length(); ++i) {
128 if (in[i] == ' ' && current >= width) {
137 t = new wxStaticText (this, wxID_ANY, wxT (""));
138 t->SetLabelMarkup (out);
139 grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
141 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, this));
142 _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
143 _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
144 _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
145 _trim_end->Changed.connect (boost::bind (&TimingPanel::trim_end_changed, this));
146 _trim_end_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
147 _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
148 _video_frame_rate->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
149 _set_video_frame_rate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
151 _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
153 setup_sensitivity ();
157 TimingPanel::update_full_length ()
160 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
161 check.insert (i->full_length ());
164 if (check.size() == 1) {
165 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
167 _full_length->clear ();
172 TimingPanel::update_play_length ()
175 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
176 check.insert (i->length_after_trim ());
179 if (check.size() == 1) {
180 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
182 _play_length->clear ();
187 TimingPanel::film_content_changed (int property)
189 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
191 /* Here we check to see if we have exactly one different value of various
192 properties, and fill the controls with that value if so.
195 if (property == ContentProperty::POSITION) {
198 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
199 check.insert (i->position ());
202 if (check.size() == 1) {
203 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
209 property == ContentProperty::LENGTH ||
210 property == VideoContentProperty::VIDEO_FRAME_RATE ||
211 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
212 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
215 update_full_length ();
217 } else if (property == ContentProperty::TRIM_START) {
219 set<ContentTime> check;
220 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
221 check.insert (i->trim_start ());
224 if (check.size() == 1) {
225 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
227 _trim_start->clear ();
230 } else if (property == ContentProperty::TRIM_END) {
232 set<ContentTime> check;
233 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
234 check.insert (i->trim_end ());
237 if (check.size() == 1) {
238 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
245 property == ContentProperty::LENGTH ||
246 property == ContentProperty::TRIM_START ||
247 property == ContentProperty::TRIM_END ||
248 property == VideoContentProperty::VIDEO_FRAME_RATE ||
249 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
250 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
253 update_play_length ();
256 if (property == VideoContentProperty::VIDEO_FRAME_RATE || property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
257 set<double> check_vc;
258 shared_ptr<const VideoContent> vc;
260 shared_ptr<const SubtitleContent> sc;
261 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
262 shared_ptr<const VideoContent> vt = dynamic_pointer_cast<const VideoContent> (i);
264 check_vc.insert (vt->video_frame_rate ());
267 shared_ptr<const SubtitleContent> st = dynamic_pointer_cast<const SubtitleContent> (i);
275 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
277 if ((check_vc.size() == 1 || count_sc == 1) && !single_frame_image_content) {
279 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
281 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle_video_frame_rate (), 5));
283 _video_frame_rate->Enable (true);
285 checked_set (_video_frame_rate, wxT (""));
286 _video_frame_rate->Enable (false);
290 bool have_still = false;
291 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
292 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
293 if (ic && ic->still ()) {
298 _full_length->set_editable (have_still);
299 _play_length->set_editable (!have_still);
300 _set_video_frame_rate->Enable (false);
301 setup_sensitivity ();
305 TimingPanel::position_changed ()
307 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
308 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
313 TimingPanel::full_length_changed ()
315 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
316 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
317 if (ic && ic->still ()) {
318 int const vfr = _parent->film()->video_frame_rate ();
319 ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
325 TimingPanel::trim_start_changed ()
327 DCPTime const ph = _viewer->position ();
329 _viewer->set_coalesce_player_changes (true);
331 shared_ptr<Content> ref;
332 optional<FrameRateChange> ref_frc;
333 optional<DCPTime> ref_ph;
334 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
335 if (i->position() <= ph && ph < i->end()) {
336 /* The playhead is in i. Use it as a reference to work out
337 where to put the playhead post-trim; we're trying to keep the playhead
338 at the same frame of content that we're looking at pre-trim.
341 ref_frc = _parent->film()->active_frame_rate_change (i->position ());
342 ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
345 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
349 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
352 _viewer->set_coalesce_player_changes (false);
356 TimingPanel::trim_end_changed ()
358 _viewer->set_coalesce_player_changes (true);
360 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
361 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
364 /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
365 if (_viewer->position() >= _parent->film()->length()) {
366 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
369 _viewer->set_coalesce_player_changes (true);
373 TimingPanel::play_length_changed ()
375 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
376 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
378 ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
385 TimingPanel::video_frame_rate_changed ()
387 _set_video_frame_rate->Enable (true);
391 TimingPanel::set_video_frame_rate ()
393 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
394 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
395 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
396 shared_ptr<TextSubtitleContent> tsc = dynamic_pointer_cast<TextSubtitleContent> (i);
398 vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
400 dsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
402 tsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
404 _set_video_frame_rate->Enable (false);
409 TimingPanel::content_selection_changed ()
411 setup_sensitivity ();
413 film_content_changed (ContentProperty::POSITION);
414 film_content_changed (ContentProperty::LENGTH);
415 film_content_changed (ContentProperty::TRIM_START);
416 film_content_changed (ContentProperty::TRIM_END);
417 film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
418 film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
422 TimingPanel::film_changed (Film::Property p)
424 if (p == Film::VIDEO_FRAME_RATE) {
425 update_full_length ();
426 update_play_length ();
431 TimingPanel::trim_start_to_playhead_clicked ()
433 DCPTime const ph = _viewer->position ();
434 optional<DCPTime> new_ph;
436 _viewer->set_coalesce_player_changes (true);
438 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
439 if (i->position() < ph && ph < i->end ()) {
440 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
441 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
442 new_ph = i->position ();
447 _viewer->set_position (new_ph.get());
450 _viewer->set_coalesce_player_changes (false);
454 TimingPanel::trim_end_to_playhead_clicked ()
456 DCPTime const ph = _viewer->position ();
457 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
458 if (i->position() < ph && ph < i->end ()) {
459 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
460 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
466 TimingPanel::setup_sensitivity ()
468 bool const e = !_parent->selected().empty ();
470 _position->Enable (e);
471 _full_length->Enable (e);
472 _trim_start->Enable (e);
473 _trim_end->Enable (e);
474 _play_length->Enable (e);
475 _video_frame_rate->Enable (e);
477 DCPTime const ph = _viewer->position ();
478 bool any_over_ph = false;
479 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
480 if (i->position() <= ph && ph < i->end()) {
485 _trim_start_to_playhead->Enable (any_over_ph);
486 _trim_end_to_playhead->Enable (any_over_ph);