Handle video frame rates and subtitles a bit more correctly.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "timing_panel.h"
21 #include "wx_util.h"
22 #include "film_viewer.h"
23 #include "timecode.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>
32 #include <set>
33 #include <iostream>
34
35 using std::cout;
36 using std::string;
37 using std::set;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40
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"))
44         , _viewer (viewer)
45 {
46         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
47         _sizer->Add (grid, 0, wxALL, 8);
48
49         wxSize size = TimecodeBase::size (this);
50
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);
57 #endif
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);
64 #endif
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);
71 #endif
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);
78 #endif
79         labels->Add (t, 1, wxEXPAND);
80         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
81         grid->Add (labels);
82
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"));
93         grid->AddSpacer (0);
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"));
99         grid->AddSpacer (0);
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);
104
105         {
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);
114         }
115
116         grid->AddSpacer (0);
117
118         /* We can't use Wrap() here as it doesn't work with markup:
119          * http://trac.wxwidgets.org/ticket/13389
120          */
121
122         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
123         wxString out;
124         int const width = 20;
125         int current = 0;
126         for (size_t i = 0; i < in.Length(); ++i) {
127                 if (in[i] == ' ' && current >= width) {
128                         out += '\n';
129                         current = 0;
130                 } else {
131                         out += in[i];
132                         ++current;
133                 }
134         }
135
136         t = new wxStaticText (this, wxID_ANY, wxT (""));
137         t->SetLabelMarkup (out);
138         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
139
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));
149 }
150
151 void
152 TimingPanel::update_full_length ()
153 {
154         set<DCPTime> check;
155         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
156                 check.insert (i->full_length ());
157         }
158
159         if (check.size() == 1) {
160                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
161         } else {
162                 _full_length->clear ();
163         }
164 }
165
166 void
167 TimingPanel::update_play_length ()
168 {
169         set<DCPTime> check;
170         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
171                 check.insert (i->length_after_trim ());
172         }
173
174         if (check.size() == 1) {
175                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
176         } else {
177                 _play_length->clear ();
178         }
179 }
180
181 void
182 TimingPanel::film_content_changed (int property)
183 {
184         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
185
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.
188         */
189
190         if (property == ContentProperty::POSITION) {
191
192                 set<DCPTime> check;
193                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
194                         check.insert (i->position ());
195                 }
196
197                 if (check.size() == 1) {
198                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
199                 } else {
200                         _position->clear ();
201                 }
202
203         } else if (
204                 property == ContentProperty::LENGTH ||
205                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
206                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
207                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
208                 ) {
209
210                 update_full_length ();
211
212         } else if (property == ContentProperty::TRIM_START) {
213
214                 set<ContentTime> check;
215                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
216                         check.insert (i->trim_start ());
217                 }
218
219                 if (check.size() == 1) {
220                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
221                 } else {
222                         _trim_start->clear ();
223                 }
224
225         } else if (property == ContentProperty::TRIM_END) {
226
227                 set<ContentTime> check;
228                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
229                         check.insert (i->trim_end ());
230                 }
231
232                 if (check.size() == 1) {
233                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
234                 } else {
235                         _trim_end->clear ();
236                 }
237         }
238
239         if (
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
246                 ) {
247
248                 update_play_length ();
249         }
250
251         if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
252                 set<double> check;
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);
256                         if (t) {
257                                 check.insert (t->video_frame_rate ());
258                                 vc = t;
259                         }
260                 }
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);
264                 } else {
265                         checked_set (_video_frame_rate, wxT (""));
266                         _video_frame_rate->Enable (false);
267                 }
268         }
269
270         if (property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
271                 shared_ptr<const SubtitleContent> check;
272                 int count = 0;
273                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
274                         shared_ptr<const SubtitleContent> t = dynamic_pointer_cast<const SubtitleContent> (i);
275                         if (t) {
276                                 check = t;
277                                 ++count;
278                         }
279                 }
280                 if (count == 1) {
281                         checked_set (_video_frame_rate, raw_convert<string> (check->subtitle_video_frame_rate (), 5));
282                         _video_frame_rate->Enable (true);
283                 } else {
284                         checked_set (_video_frame_rate, wxT (""));
285                         _video_frame_rate->Enable (false);
286                 }
287         }
288
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 ()) {
293                         have_still = true;
294                 }
295         }
296
297         _full_length->set_editable (have_still);
298         _play_length->set_editable (!have_still);
299         _set_video_frame_rate->Enable (false);
300 }
301
302 void
303 TimingPanel::position_changed ()
304 {
305         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
306                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
307         }
308 }
309
310 void
311 TimingPanel::full_length_changed ()
312 {
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));
318                 }
319         }
320 }
321
322 void
323 TimingPanel::trim_start_changed ()
324 {
325         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
326                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
327         }
328 }
329
330
331 void
332 TimingPanel::trim_end_changed ()
333 {
334         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
335                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
336         }
337 }
338
339 void
340 TimingPanel::play_length_changed ()
341 {
342         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
343                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
344                 i->set_trim_end (
345                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
346                         - i->trim_start ()
347                         );
348         }
349 }
350
351 void
352 TimingPanel::video_frame_rate_changed ()
353 {
354         _set_video_frame_rate->Enable (true);
355 }
356
357 void
358 TimingPanel::set_video_frame_rate ()
359 {
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);
364                 if (vc) {
365                         vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
366                 } else if (dsc) {
367                         dsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
368                 } else if (ssc) {
369                         ssc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
370                 }
371                 _set_video_frame_rate->Enable (false);
372         }
373 }
374
375 void
376 TimingPanel::content_selection_changed ()
377 {
378         bool const e = !_parent->selected().empty ();
379
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);
386
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);
393 }
394
395 void
396 TimingPanel::film_changed (Film::Property p)
397 {
398         if (p == Film::VIDEO_FRAME_RATE) {
399                 update_full_length ();
400                 update_play_length ();
401         }
402 }
403
404 void
405 TimingPanel::trim_start_to_playhead_clicked ()
406 {
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));
412                 }
413         }
414 }
415
416 void
417 TimingPanel::trim_end_to_playhead_clicked ()
418 {
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());
424                 }
425
426         }
427 }