Fix rebase onto master.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2016 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/audio_content.h"
31 #include "lib/text_subtitle_content.h"
32 #include "lib/video_content.h"
33 #include <boost/foreach.hpp>
34 #include <set>
35 #include <iostream>
36
37 using std::cout;
38 using std::string;
39 using std::set;
40 using boost::shared_ptr;
41 using boost::dynamic_pointer_cast;
42 using boost::optional;
43
44 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
45         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
46         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
47         : ContentSubPanel (p, S_("Timing|Timing"))
48         , _viewer (viewer)
49 {
50         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
51         _sizer->Add (grid, 0, wxALL, 8);
52
53         wxSize size = TimecodeBase::size (this);
54
55         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
56         //// TRANSLATORS: this is an abbreviation for "hours"
57         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
58 #ifdef DCPOMATIC_LINUX
59         /* Hack to work around failure to centre text on GTK */
60         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
61 #endif
62         labels->Add (t, 1, wxEXPAND);
63         add_label_to_sizer (labels, this, wxT (":"), false);
64         //// TRANSLATORS: this is an abbreviation for "minutes"
65         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
66 #ifdef DCPOMATIC_LINUX
67         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
68 #endif
69         labels->Add (t, 1, wxEXPAND);
70         add_label_to_sizer (labels, this, wxT (":"), false);
71         //// TRANSLATORS: this is an abbreviation for "seconds"
72         t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
73 #ifdef DCPOMATIC_LINUX
74         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
75 #endif
76         labels->Add (t, 1, wxEXPAND);
77         add_label_to_sizer (labels, this, wxT (":"), false);
78         //// TRANSLATORS: this is an abbreviation for "frames"
79         t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
80 #ifdef DCPOMATIC_LINUX
81         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
82 #endif
83         labels->Add (t, 1, wxEXPAND);
84         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
85         grid->Add (labels);
86
87         add_label_to_sizer (grid, this, _("Position"), true);
88         _position = new Timecode<DCPTime> (this);
89         grid->Add (_position);
90         add_label_to_sizer (grid, this, _("Full length"), true);
91         _full_length = new Timecode<DCPTime> (this);
92         grid->Add (_full_length);
93         add_label_to_sizer (grid, this, _("Trim from start"), true);
94         _trim_start = new Timecode<ContentTime> (this);
95         grid->Add (_trim_start);
96         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
97         grid->AddSpacer (0);
98         grid->Add (_trim_start_to_playhead);
99         add_label_to_sizer (grid, this, _("Trim from end"), true);
100         _trim_end = new Timecode<ContentTime> (this);
101         grid->Add (_trim_end);
102         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
103         grid->AddSpacer (0);
104         grid->Add (_trim_end_to_playhead);
105         add_label_to_sizer (grid, this, _("Play length"), true);
106         _play_length = new Timecode<DCPTime> (this);
107         grid->Add (_play_length);
108
109         {
110                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
111                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
112                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
113                 s->Add (_video_frame_rate, 1, wxEXPAND);
114                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
115                 _set_video_frame_rate->Enable (false);
116                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
117                 grid->Add (s, 1, wxEXPAND);
118         }
119
120         grid->AddSpacer (0);
121
122         /* We can't use Wrap() here as it doesn't work with markup:
123          * http://trac.wxwidgets.org/ticket/13389
124          */
125
126         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
127         wxString out;
128         int const width = 20;
129         int current = 0;
130         for (size_t i = 0; i < in.Length(); ++i) {
131                 if (in[i] == ' ' && current >= width) {
132                         out += '\n';
133                         current = 0;
134                 } else {
135                         out += in[i];
136                         ++current;
137                 }
138         }
139
140         t = new wxStaticText (this, wxID_ANY, wxT (""));
141         t->SetLabelMarkup (out);
142         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
143
144         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
145         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
146         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
147         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
148         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
149         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
150         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
151         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
152         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
153
154         _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
155
156         setup_sensitivity ();
157 }
158
159 void
160 TimingPanel::update_full_length ()
161 {
162         set<DCPTime> check;
163         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
164                 check.insert (i->full_length ());
165         }
166
167         if (check.size() == 1) {
168                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
169         } else {
170                 _full_length->clear ();
171         }
172 }
173
174 void
175 TimingPanel::update_play_length ()
176 {
177         set<DCPTime> check;
178         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
179                 check.insert (i->length_after_trim ());
180         }
181
182         if (check.size() == 1) {
183                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
184         } else {
185                 _play_length->clear ();
186         }
187 }
188
189 void
190 TimingPanel::film_content_changed (int property)
191 {
192         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
193
194         /* Here we check to see if we have exactly one different value of various
195            properties, and fill the controls with that value if so.
196         */
197
198         if (property == ContentProperty::POSITION) {
199
200                 set<DCPTime> check;
201                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
202                         check.insert (i->position ());
203                 }
204
205                 if (check.size() == 1) {
206                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
207                 } else {
208                         _position->clear ();
209                 }
210
211         } else if (
212                 property == ContentProperty::LENGTH ||
213                 property == VideoContentProperty::FRAME_RATE ||
214                 property == VideoContentProperty::FRAME_TYPE ||
215                 property == AudioContentProperty::VIDEO_FRAME_RATE ||
216                 property == SubtitleContentProperty::VIDEO_FRAME_RATE
217                 ) {
218
219                 update_full_length ();
220
221         } else if (property == ContentProperty::TRIM_START) {
222
223                 set<ContentTime> check;
224                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
225                         check.insert (i->trim_start ());
226                 }
227
228                 if (check.size() == 1) {
229                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
230                 } else {
231                         _trim_start->clear ();
232                 }
233
234         } else if (property == ContentProperty::TRIM_END) {
235
236                 set<ContentTime> check;
237                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
238                         check.insert (i->trim_end ());
239                 }
240
241                 if (check.size() == 1) {
242                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
243                 } else {
244                         _trim_end->clear ();
245                 }
246         }
247
248         if (
249                 property == ContentProperty::LENGTH ||
250                 property == ContentProperty::TRIM_START ||
251                 property == ContentProperty::TRIM_END ||
252                 property == VideoContentProperty::FRAME_RATE ||
253                 property == VideoContentProperty::FRAME_TYPE ||
254                 property == AudioContentProperty::VIDEO_FRAME_RATE ||
255                 property == SubtitleContentProperty::VIDEO_FRAME_RATE
256                 ) {
257
258                 update_play_length ();
259         }
260
261         if (property == VideoContentProperty::FRAME_RATE || property == SubtitleContentProperty::VIDEO_FRAME_RATE) {
262                 set<double> check_vc;
263                 shared_ptr<const Content> vc;
264                 int count_ac = 0;
265                 shared_ptr<const Content> ac;
266                 int count_sc = 0;
267                 shared_ptr<const Content> sc;
268                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
269                         if (i->video) {
270                                 check_vc.insert (i->video->frame_rate ());
271                                 vc = i;
272                         }
273                         if (i->audio) {
274                                 ++count_ac;
275                                 ac = i;
276                         }
277                         if (i->subtitle) {
278                                 ++count_sc;
279                                 sc = i;
280                         }
281
282                 }
283
284                 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
285
286                 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
287                         if (vc) {
288                                 checked_set (_video_frame_rate, raw_convert<string> (vc->video->frame_rate (), 5));
289                         } else if (ac) {
290                                 checked_set (_video_frame_rate, raw_convert<string> (ac->audio->video_frame_rate (), 5));
291                         } else if (sc) {
292                                 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle->video_frame_rate (), 5));
293                         }
294                         _video_frame_rate->Enable (true);
295                 } else {
296                         checked_set (_video_frame_rate, wxT (""));
297                         _video_frame_rate->Enable (false);
298                 }
299         }
300
301         bool have_still = false;
302         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
303                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
304                 if (ic && ic->still ()) {
305                         have_still = true;
306                 }
307         }
308
309         _full_length->set_editable (have_still);
310         _play_length->set_editable (!have_still);
311         _set_video_frame_rate->Enable (false);
312         setup_sensitivity ();
313 }
314
315 void
316 TimingPanel::position_changed ()
317 {
318         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
319                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
320         }
321 }
322
323 void
324 TimingPanel::full_length_changed ()
325 {
326         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
327                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
328                 if (ic && ic->still ()) {
329                         int const vfr = _parent->film()->video_frame_rate ();
330                         ic->video->set_length (_full_length->get (vfr).frames_round (vfr));
331                 }
332         }
333 }
334
335 void
336 TimingPanel::trim_start_changed ()
337 {
338         DCPTime const ph = _viewer->position ();
339
340         _viewer->set_coalesce_player_changes (true);
341
342         shared_ptr<Content> ref;
343         optional<FrameRateChange> ref_frc;
344         optional<DCPTime> ref_ph;
345         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
346                 if (i->position() <= ph && ph < i->end()) {
347                         /* The playhead is in i.  Use it as a reference to work out
348                            where to put the playhead post-trim; we're trying to keep the playhead
349                            at the same frame of content that we're looking at pre-trim.
350                         */
351                         ref = i;
352                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
353                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
354                 }
355
356                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
357         }
358
359         if (ref) {
360                 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
361         }
362
363         _viewer->set_coalesce_player_changes (false);
364 }
365
366 void
367 TimingPanel::trim_end_changed ()
368 {
369         _viewer->set_coalesce_player_changes (true);
370
371         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
372                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
373         }
374
375         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
376         if (_viewer->position() >= _parent->film()->length()) {
377                 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
378         }
379
380         _viewer->set_coalesce_player_changes (true);
381 }
382
383 void
384 TimingPanel::play_length_changed ()
385 {
386         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
387                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
388                 i->set_trim_end (
389                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
390                         - i->trim_start ()
391                         );
392         }
393 }
394
395 void
396 TimingPanel::video_frame_rate_changed ()
397 {
398         _set_video_frame_rate->Enable (true);
399 }
400
401 void
402 TimingPanel::set_video_frame_rate ()
403 {
404         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
405                 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
406                 shared_ptr<TextSubtitleContent> tsc = dynamic_pointer_cast<TextSubtitleContent> (i);
407                 double const fr = raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
408                 if (i->video) {
409                         i->video->set_frame_rate (fr);
410                 } else if (i->audio) {
411                         /* Audio but not video, i.e. SndfileContent */
412                         i->audio->set_video_frame_rate (fr);
413                 } else if (dsc) {
414                         dsc->subtitle->set_video_frame_rate (fr);
415                 } else if (tsc) {
416                         tsc->subtitle->set_video_frame_rate (fr);
417                 }
418                 _set_video_frame_rate->Enable (false);
419         }
420 }
421
422 void
423 TimingPanel::content_selection_changed ()
424 {
425         setup_sensitivity ();
426
427         film_content_changed (ContentProperty::POSITION);
428         film_content_changed (ContentProperty::LENGTH);
429         film_content_changed (ContentProperty::TRIM_START);
430         film_content_changed (ContentProperty::TRIM_END);
431         film_content_changed (VideoContentProperty::FRAME_RATE);
432         film_content_changed (SubtitleContentProperty::VIDEO_FRAME_RATE);
433         film_content_changed (AudioContentProperty::VIDEO_FRAME_RATE);
434 }
435
436 void
437 TimingPanel::film_changed (Film::Property p)
438 {
439         if (p == Film::VIDEO_FRAME_RATE) {
440                 update_full_length ();
441                 update_play_length ();
442         }
443 }
444
445 void
446 TimingPanel::trim_start_to_playhead_clicked ()
447 {
448         DCPTime const ph = _viewer->position ();
449         optional<DCPTime> new_ph;
450
451         _viewer->set_coalesce_player_changes (true);
452
453         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
454                 if (i->position() < ph && ph < i->end ()) {
455                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
456                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
457                         new_ph = i->position ();
458                 }
459         }
460
461         if (new_ph) {
462                 _viewer->set_position (new_ph.get());
463         }
464
465         _viewer->set_coalesce_player_changes (false);
466 }
467
468 void
469 TimingPanel::trim_end_to_playhead_clicked ()
470 {
471         DCPTime const ph = _viewer->position ();
472         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
473                 if (i->position() < ph && ph < i->end ()) {
474                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
475                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
476                 }
477         }
478 }
479
480 void
481 TimingPanel::setup_sensitivity ()
482 {
483         bool const e = !_parent->selected().empty ();
484
485         _position->Enable (e);
486         _full_length->Enable (e);
487         _trim_start->Enable (e);
488         _trim_end->Enable (e);
489         _play_length->Enable (e);
490         _video_frame_rate->Enable (e);
491
492         DCPTime const ph = _viewer->position ();
493         bool any_over_ph = false;
494         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
495                 if (i->position() <= ph && ph < i->end()) {
496                         any_over_ph = true;
497                 }
498         }
499
500         _trim_start_to_playhead->Enable (any_over_ph);
501         _trim_end_to_playhead->Enable (any_over_ph);
502 }