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/subrip_content.h"
31 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
41 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
42 /* horrid hack for apparent lack of context support with wxWidgets i18n code */
43 : ContentSubPanel (p, S_("Timing|Timing"))
46 wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
47 _sizer->Add (grid, 0, wxALL, 8);
49 wxSize size = TimecodeBase::size (this);
51 wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
52 //// TRANSLATORS: this is an abbreviation for "hours"
53 wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
54 #ifdef DCPOMATIC_LINUX
55 /* Hack to work around failure to centre text on GTK */
56 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
58 labels->Add (t, 1, wxEXPAND);
59 add_label_to_sizer (labels, this, wxT (":"), false);
60 //// TRANSLATORS: this is an abbreviation for "minutes"
61 t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
62 #ifdef DCPOMATIC_LINUX
63 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
65 labels->Add (t, 1, wxEXPAND);
66 add_label_to_sizer (labels, this, wxT (":"), false);
67 //// TRANSLATORS: this is an abbreviation for "seconds"
68 t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
69 #ifdef DCPOMATIC_LINUX
70 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
72 labels->Add (t, 1, wxEXPAND);
73 add_label_to_sizer (labels, this, wxT (":"), false);
74 //// TRANSLATORS: this is an abbreviation for "frames"
75 t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
76 #ifdef DCPOMATIC_LINUX
77 gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
79 labels->Add (t, 1, wxEXPAND);
80 grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
83 add_label_to_sizer (grid, this, _("Position"), true);
84 _position = new Timecode<DCPTime> (this);
85 grid->Add (_position);
86 add_label_to_sizer (grid, this, _("Full length"), true);
87 _full_length = new Timecode<DCPTime> (this);
88 grid->Add (_full_length);
89 add_label_to_sizer (grid, this, _("Trim from start"), true);
90 _trim_start = new Timecode<ContentTime> (this);
91 grid->Add (_trim_start);
92 _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
94 grid->Add (_trim_start_to_playhead);
95 add_label_to_sizer (grid, this, _("Trim from end"), true);
96 _trim_end = new Timecode<ContentTime> (this);
97 grid->Add (_trim_end);
98 _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
100 grid->Add (_trim_end_to_playhead);
101 add_label_to_sizer (grid, this, _("Play length"), true);
102 _play_length = new Timecode<DCPTime> (this);
103 grid->Add (_play_length);
106 add_label_to_sizer (grid, this, _("Video frame rate"), true);
107 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
108 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
109 s->Add (_video_frame_rate, 1, wxEXPAND);
110 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
111 _set_video_frame_rate->Enable (false);
112 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
113 grid->Add (s, 1, wxEXPAND);
118 /* We can't use Wrap() here as it doesn't work with markup:
119 * http://trac.wxwidgets.org/ticket/13389
122 wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
124 int const width = 20;
126 for (size_t i = 0; i < in.Length(); ++i) {
127 if (in[i] == ' ' && current >= width) {
136 t = new wxStaticText (this, wxID_ANY, wxT (""));
137 t->SetLabelMarkup (out);
138 grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
140 _position->Changed.connect (boost::bind (&TimingPanel::position_changed, 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_COMMAND_BUTTON_CLICKED, 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_COMMAND_BUTTON_CLICKED, 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_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
148 _set_video_frame_rate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
152 TimingPanel::update_full_length ()
155 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
156 check.insert (i->full_length ());
159 if (check.size() == 1) {
160 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
162 _full_length->clear ();
167 TimingPanel::update_play_length ()
170 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
171 check.insert (i->length_after_trim ());
174 if (check.size() == 1) {
175 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
177 _play_length->clear ();
182 TimingPanel::film_content_changed (int property)
184 int const film_video_frame_rate = _parent->film()->video_frame_rate ();
186 /* Here we check to see if we have exactly one different value of various
187 properties, and fill the controls with that value if so.
190 if (property == ContentProperty::POSITION) {
193 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
194 check.insert (i->position ());
197 if (check.size() == 1) {
198 _position->set (_parent->selected().front()->position(), film_video_frame_rate);
204 property == ContentProperty::LENGTH ||
205 property == VideoContentProperty::VIDEO_FRAME_RATE ||
206 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
207 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
210 update_full_length ();
212 } else if (property == ContentProperty::TRIM_START) {
214 set<ContentTime> check;
215 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
216 check.insert (i->trim_start ());
219 if (check.size() == 1) {
220 _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
222 _trim_start->clear ();
225 } else if (property == ContentProperty::TRIM_END) {
227 set<ContentTime> check;
228 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
229 check.insert (i->trim_end ());
232 if (check.size() == 1) {
233 _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
240 property == ContentProperty::LENGTH ||
241 property == ContentProperty::TRIM_START ||
242 property == ContentProperty::TRIM_END ||
243 property == VideoContentProperty::VIDEO_FRAME_RATE ||
244 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
245 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
248 update_play_length ();
251 if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
253 shared_ptr<const VideoContent> vc;
254 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
255 shared_ptr<const VideoContent> t = dynamic_pointer_cast<const VideoContent> (i);
257 check.insert (t->video_frame_rate ());
261 if (check.size() == 1) {
262 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
263 _video_frame_rate->Enable (true);
265 checked_set (_video_frame_rate, wxT (""));
266 _video_frame_rate->Enable (false);
270 if (property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
271 shared_ptr<const SubtitleContent> check;
273 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
274 shared_ptr<const SubtitleContent> t = dynamic_pointer_cast<const SubtitleContent> (i);
281 checked_set (_video_frame_rate, raw_convert<string> (check->subtitle_video_frame_rate (), 5));
282 _video_frame_rate->Enable (true);
284 checked_set (_video_frame_rate, wxT (""));
285 _video_frame_rate->Enable (false);
289 bool have_still = false;
290 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
291 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
292 if (ic && ic->still ()) {
297 _full_length->set_editable (have_still);
298 _play_length->set_editable (!have_still);
299 _set_video_frame_rate->Enable (false);
303 TimingPanel::position_changed ()
305 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
306 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
311 TimingPanel::full_length_changed ()
313 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
314 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
315 if (ic && ic->still ()) {
316 int const vfr = _parent->film()->video_frame_rate ();
317 ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
323 TimingPanel::trim_start_changed ()
325 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
326 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
332 TimingPanel::trim_end_changed ()
334 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
335 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
340 TimingPanel::play_length_changed ()
342 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
343 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
345 ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
352 TimingPanel::video_frame_rate_changed ()
354 _set_video_frame_rate->Enable (true);
358 TimingPanel::set_video_frame_rate ()
360 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
361 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
362 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
363 shared_ptr<SubRipContent> ssc = dynamic_pointer_cast<SubRipContent> (i);
365 vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
367 dsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
369 ssc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
371 _set_video_frame_rate->Enable (false);
376 TimingPanel::content_selection_changed ()
378 bool const e = !_parent->selected().empty ();
380 _position->Enable (e);
381 _full_length->Enable (e);
382 _trim_start->Enable (e);
383 _trim_end->Enable (e);
384 _play_length->Enable (e);
385 _video_frame_rate->Enable (e);
387 film_content_changed (ContentProperty::POSITION);
388 film_content_changed (ContentProperty::LENGTH);
389 film_content_changed (ContentProperty::TRIM_START);
390 film_content_changed (ContentProperty::TRIM_END);
391 film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
392 film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
396 TimingPanel::film_changed (Film::Property p)
398 if (p == Film::VIDEO_FRAME_RATE) {
399 update_full_length ();
400 update_play_length ();
405 TimingPanel::trim_start_to_playhead_clicked ()
407 DCPTime const ph = _viewer->position ();
408 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
409 if (i->position() < ph && ph < i->end ()) {
410 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
411 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
417 TimingPanel::trim_end_to_playhead_clicked ()
419 DCPTime const ph = _viewer->position ();
420 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
421 if (i->position() < ph && ph < i->end ()) {
422 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
423 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph, frc) - i->trim_start());