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 || property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
252 set<double> check_vc;
253 shared_ptr<const VideoContent> vc;
255 shared_ptr<const SubtitleContent> sc;
256 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
257 shared_ptr<const VideoContent> vt = dynamic_pointer_cast<const VideoContent> (i);
259 check_vc.insert (vt->video_frame_rate ());
262 shared_ptr<const SubtitleContent> st = dynamic_pointer_cast<const SubtitleContent> (i);
270 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
272 if ((check_vc.size() == 1 || count_sc == 1) && !single_frame_image_content) {
274 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
276 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle_video_frame_rate (), 5));
278 _video_frame_rate->Enable (true);
280 checked_set (_video_frame_rate, wxT (""));
281 _video_frame_rate->Enable (false);
285 bool have_still = false;
286 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
287 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
288 if (ic && ic->still ()) {
293 _full_length->set_editable (have_still);
294 _play_length->set_editable (!have_still);
295 _set_video_frame_rate->Enable (false);
299 TimingPanel::position_changed ()
301 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
302 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
307 TimingPanel::full_length_changed ()
309 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
310 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
311 if (ic && ic->still ()) {
312 int const vfr = _parent->film()->video_frame_rate ();
313 ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
319 TimingPanel::trim_start_changed ()
321 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
322 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
328 TimingPanel::trim_end_changed ()
330 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
331 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
336 TimingPanel::play_length_changed ()
338 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
339 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
341 ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
348 TimingPanel::video_frame_rate_changed ()
350 _set_video_frame_rate->Enable (true);
354 TimingPanel::set_video_frame_rate ()
356 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
357 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
358 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
359 shared_ptr<SubRipContent> ssc = dynamic_pointer_cast<SubRipContent> (i);
361 vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
363 dsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
365 ssc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
367 _set_video_frame_rate->Enable (false);
372 TimingPanel::content_selection_changed ()
374 bool const e = !_parent->selected().empty ();
376 _position->Enable (e);
377 _full_length->Enable (e);
378 _trim_start->Enable (e);
379 _trim_end->Enable (e);
380 _play_length->Enable (e);
381 _video_frame_rate->Enable (e);
383 film_content_changed (ContentProperty::POSITION);
384 film_content_changed (ContentProperty::LENGTH);
385 film_content_changed (ContentProperty::TRIM_START);
386 film_content_changed (ContentProperty::TRIM_END);
387 film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
388 film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
392 TimingPanel::film_changed (Film::Property p)
394 if (p == Film::VIDEO_FRAME_RATE) {
395 update_full_length ();
396 update_play_length ();
401 TimingPanel::trim_start_to_playhead_clicked ()
403 DCPTime const ph = _viewer->position ();
404 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
405 if (i->position() < ph && ph < i->end ()) {
406 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
407 i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
413 TimingPanel::trim_end_to_playhead_clicked ()
415 DCPTime const ph = _viewer->position ();
416 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
417 if (i->position() < ph && ph < i->end ()) {
418 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
419 i->set_trim_end (ContentTime (i->position() + i->full_length() - ph, frc) - i->trim_start());