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