c643d208044613a42d58d52a56fa587da4ba694a
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2018 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 "static_text.h"
28 #include "dcpomatic_button.h"
29 #include "lib/content.h"
30 #include "lib/image_content.h"
31 #include "lib/text_content.h"
32 #include "lib/dcp_subtitle_content.h"
33 #include "lib/audio_content.h"
34 #include "lib/string_text_file_content.h"
35 #include "lib/video_content.h"
36 #include "lib/dcp_content.h"
37 #include "lib/ffmpeg_content.h"
38 #include <dcp/locale_convert.h>
39 #include "lib/warnings.h"
40 #if defined(__WXGTK20__) && !defined(__WXGTK3__)
41 #define TIMING_PANEL_ALIGNMENT_HACK 1
42 DCPOMATIC_DISABLE_WARNINGS
43 #include <gtk/gtk.h>
44 DCPOMATIC_ENABLE_WARNINGS
45 #endif
46 #include <boost/foreach.hpp>
47 #include <set>
48 #include <iostream>
49
50 using std::cout;
51 using std::string;
52 using std::set;
53 using boost::shared_ptr;
54 using boost::weak_ptr;
55 using boost::dynamic_pointer_cast;
56 using boost::optional;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
59 #endif
60 using dcp::locale_convert;
61 using namespace dcpomatic;
62
63 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
64         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
65         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
66         : ContentSubPanel (p, S_("Timing|Timing"))
67         , _viewer (viewer)
68         , _film_content_changed_suspender (boost::bind(&TimingPanel::film_content_changed, this, _1))
69 {
70         wxSize size = TimecodeBase::size (this);
71
72         for (int i = 0; i < 3; ++i) {
73                 _colon[i] = create_label (this, wxT(":"), false);
74         }
75
76         //// TRANSLATORS: this is an abbreviation for "hours"
77         _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
78 #ifdef TIMING_PANEL_ALIGNMENT_HACK
79         /* Hack to work around failure to centre text on GTK */
80         gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
81 #endif
82         //// TRANSLATORS: this is an abbreviation for "minutes"
83         _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
84 #ifdef TIMING_PANEL_ALIGNMENT_HACK
85         gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
86 #endif
87         //// TRANSLATORS: this is an abbreviation for "seconds"
88         _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
89 #ifdef TIMING_PANEL_ALIGNMENT_HACK
90         gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
91 #endif
92         //// TRANSLATORS: this is an abbreviation for "frames"
93         _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
94 #ifdef TIMING_PANEL_ALIGNMENT_HACK
95         gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
96 #endif
97
98         _position_label = create_label (this, _("Position"), true);
99         _position = new Timecode<DCPTime> (this);
100         _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
101         _full_length_label = create_label (this, _("Full length"), true);
102         _full_length = new Timecode<DCPTime> (this);
103         _trim_start_label = create_label (this, _("Trim from start"), true);
104         _trim_start = new Timecode<ContentTime> (this);
105         _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
106         _trim_end_label = create_label (this, _("Trim from end"), true);
107         _trim_end = new Timecode<ContentTime> (this);
108         _trim_end_to_playhead = new Button (this, _("Trim from current position to end"));
109         _play_length_label = create_label (this, _("Play length"), true);
110         _play_length = new Timecode<DCPTime> (this);
111
112         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
113         _move_to_start_of_reel->Bind  (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
114         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
115         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
116         _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
117         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
118         _trim_end_to_playhead->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
119         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
120
121         shared_ptr<FilmViewer> fv = _viewer.lock ();
122         DCPOMATIC_ASSERT (fv);
123         fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
124
125         setup_sensitivity ();
126         add_to_grid ();
127 }
128
129 void
130 TimingPanel::add_to_grid ()
131 {
132         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
133
134         int r = 0;
135
136         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
137         labels->Add (_h_label, 1, wxEXPAND);
138         add_label_to_sizer (labels, _colon[0], false);
139         labels->Add (_m_label, 1, wxEXPAND);
140         add_label_to_sizer (labels, _colon[1], false);
141         labels->Add (_s_label, 1, wxEXPAND);
142         add_label_to_sizer (labels, _colon[2], false);
143         labels->Add (_f_label, 1, wxEXPAND);
144         _grid->Add (labels, wxGBPosition(r, 1));
145         ++r;
146
147         add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
148         _grid->Add (_position, wxGBPosition(r, 1));
149         ++r;
150
151         _move_to_start_of_reel->Show (full);
152         _full_length_label->Show (full);
153         _full_length->Show (full);
154         _play_length_label->Show (full);
155         _play_length->Show (full);
156
157         if (full) {
158                 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
159                 ++r;
160
161                 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
162                 _grid->Add (_full_length, wxGBPosition(r, 1));
163                 ++r;
164         }
165
166         add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
167         _grid->Add (_trim_start, wxGBPosition(r, 1));
168         ++r;
169
170         _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
171         ++r;
172
173         add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
174         _grid->Add (_trim_end, wxGBPosition(r, 1));
175         ++r;
176
177         _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
178         ++r;
179
180         if (full) {
181                 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
182                 _grid->Add (_play_length, wxGBPosition(r, 1));
183                 ++r;
184         }
185
186         /* Completely speculative fix for #891 */
187         _grid->Layout ();
188 }
189
190 void
191 TimingPanel::update_full_length ()
192 {
193         set<DCPTime> check;
194         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
195                 check.insert (i->full_length(_parent->film()));
196         }
197
198         if (check.size() == 1) {
199                 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
200         } else {
201                 _full_length->clear ();
202         }
203 }
204
205 void
206 TimingPanel::update_play_length ()
207 {
208         set<DCPTime> check;
209         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
210                 check.insert (i->length_after_trim(_parent->film()));
211         }
212
213         if (check.size() == 1) {
214                 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
215         } else {
216                 _play_length->clear ();
217         }
218 }
219
220 void
221 TimingPanel::film_content_changed (int property)
222 {
223         if (_film_content_changed_suspender.check(property)) {
224                 return;
225         }
226
227         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
228
229         /* Here we check to see if we have exactly one different value of various
230            properties, and fill the controls with that value if so.
231         */
232
233         if (property == ContentProperty::POSITION) {
234
235                 set<DCPTime> check;
236                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
237                         check.insert (i->position ());
238                 }
239
240                 if (check.size() == 1) {
241                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
242                 } else {
243                         _position->clear ();
244                 }
245
246         } else if (
247                 property == ContentProperty::LENGTH ||
248                 property == ContentProperty::VIDEO_FRAME_RATE ||
249                 property == VideoContentProperty::FRAME_TYPE
250                 ) {
251
252                 update_full_length ();
253
254         } else if (property == ContentProperty::TRIM_START) {
255
256                 set<ContentTime> check;
257                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
258                         check.insert (i->trim_start ());
259                 }
260
261                 if (check.size() == 1) {
262                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
263                 } else {
264                         _trim_start->clear ();
265                 }
266
267         } else if (property == ContentProperty::TRIM_END) {
268
269                 set<ContentTime> check;
270                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
271                         check.insert (i->trim_end ());
272                 }
273
274                 if (check.size() == 1) {
275                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
276                 } else {
277                         _trim_end->clear ();
278                 }
279         }
280
281         if (
282                 property == ContentProperty::LENGTH ||
283                 property == ContentProperty::TRIM_START ||
284                 property == ContentProperty::TRIM_END ||
285                 property == ContentProperty::VIDEO_FRAME_RATE ||
286                 property == VideoContentProperty::FRAME_TYPE
287                 ) {
288
289                 update_play_length ();
290         }
291
292         if (property == ContentProperty::VIDEO_FRAME_RATE) {
293                 set<double> check_vc;
294                 shared_ptr<const Content> content;
295                 int count_ac = 0;
296                 int count_sc = 0;
297                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
298                         if (i->video && i->video_frame_rate()) {
299                                 check_vc.insert (i->video_frame_rate().get());
300                                 content = i;
301                         }
302                         if (i->audio && i->video_frame_rate()) {
303                                 ++count_ac;
304                                 content = i;
305                         }
306                         if (!i->text.empty() && i->video_frame_rate()) {
307                                 ++count_sc;
308                                 content = i;
309                         }
310
311                 }
312         }
313
314         bool have_still = false;
315         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
316                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
317                 if (ic && ic->still ()) {
318                         have_still = true;
319                 }
320         }
321
322         _full_length->set_editable (have_still);
323         _play_length->set_editable (!have_still);
324         setup_sensitivity ();
325 }
326
327 void
328 TimingPanel::position_changed ()
329 {
330         DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
331         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
332                 i->set_position (_parent->film(), pos);
333         }
334 }
335
336 void
337 TimingPanel::full_length_changed ()
338 {
339         int const vfr = _parent->film()->video_frame_rate ();
340         Frame const len = _full_length->get (vfr).frames_round (vfr);
341         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
342                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
343                 if (ic && ic->still ()) {
344                         ic->video->set_length (len);
345                 }
346         }
347 }
348
349 void
350 TimingPanel::trim_start_changed ()
351 {
352         shared_ptr<FilmViewer> fv = _viewer.lock ();
353         if (!fv) {
354                 return;
355         }
356
357         DCPTime const ph = fv->position ();
358
359         fv->set_coalesce_player_changes (true);
360
361         shared_ptr<Content> ref;
362         optional<FrameRateChange> ref_frc;
363         optional<DCPTime> ref_ph;
364
365         Suspender::Block bl = _film_content_changed_suspender.block ();
366         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
367                 if (i->position() <= ph && ph < i->end(_parent->film())) {
368                         /* The playhead is in i.  Use it as a reference to work out
369                            where to put the playhead post-trim; we're trying to keep the playhead
370                            at the same frame of content that we're looking at pre-trim.
371                         */
372                         ref = i;
373                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
374                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
375                 }
376
377                 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
378                 i->set_trim_start (trim);
379         }
380
381         if (ref) {
382                 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
383         }
384
385         fv->set_coalesce_player_changes (false);
386 }
387
388 void
389 TimingPanel::trim_end_changed ()
390 {
391         shared_ptr<FilmViewer> fv = _viewer.lock ();
392         if (!fv) {
393                 return;
394         }
395
396         fv->set_coalesce_player_changes (true);
397
398         Suspender::Block bl = _film_content_changed_suspender.block ();
399         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
400                 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
401                 i->set_trim_end (trim);
402         }
403
404         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
405         if (fv->position() >= _parent->film()->length()) {
406                 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
407         }
408
409         fv->set_coalesce_player_changes (false);
410 }
411
412 void
413 TimingPanel::play_length_changed ()
414 {
415         DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
416         Suspender::Block bl = _film_content_changed_suspender.block ();
417         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
418                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
419                 i->set_trim_end (
420                         ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
421                         );
422         }
423 }
424
425
426 void
427 TimingPanel::content_selection_changed ()
428 {
429         setup_sensitivity ();
430
431         film_content_changed (ContentProperty::POSITION);
432         film_content_changed (ContentProperty::LENGTH);
433         film_content_changed (ContentProperty::TRIM_START);
434         film_content_changed (ContentProperty::TRIM_END);
435         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
436 }
437
438 void
439 TimingPanel::film_changed (Film::Property p)
440 {
441         if (p == Film::VIDEO_FRAME_RATE) {
442                 update_full_length ();
443                 update_play_length ();
444         }
445 }
446
447 void
448 TimingPanel::trim_start_to_playhead_clicked ()
449 {
450         shared_ptr<FilmViewer> fv = _viewer.lock ();
451         if (!fv) {
452                 return;
453         }
454
455         shared_ptr<const Film> film = _parent->film ();
456         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
457         optional<DCPTime> new_ph;
458
459         fv->set_coalesce_player_changes (true);
460
461         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
462                 if (i->position() < ph && ph < i->end(film)) {
463                         FrameRateChange const frc = film->active_frame_rate_change (i->position());
464                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
465                         new_ph = i->position ();
466                 }
467         }
468
469         fv->set_coalesce_player_changes (false);
470
471         if (new_ph) {
472                 fv->seek (new_ph.get(), true);
473         }
474 }
475
476 void
477 TimingPanel::trim_end_to_playhead_clicked ()
478 {
479         shared_ptr<FilmViewer> fv = _viewer.lock ();
480         if (!fv) {
481                 return;
482         }
483
484         shared_ptr<const Film> film = _parent->film ();
485         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
486         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
487                 if (i->position() < ph && ph < i->end(film)) {
488                         FrameRateChange const frc = film->active_frame_rate_change (i->position ());
489                         i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph, frc) - i->trim_start());
490                 }
491         }
492 }
493
494 void
495 TimingPanel::setup_sensitivity ()
496 {
497         bool const e = !_parent->selected().empty ();
498
499         _position->Enable (e);
500         _move_to_start_of_reel->Enable (e);
501         _full_length->Enable (e);
502         _trim_start->Enable (e);
503         _trim_end->Enable (e);
504         _play_length->Enable (e);
505
506         shared_ptr<FilmViewer> fv = _viewer.lock ();
507         DCPOMATIC_ASSERT (fv);
508         DCPTime const ph = fv->position ();
509         bool any_over_ph = false;
510         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
511                 if (i->position() <= ph && ph < i->end(_parent->film())) {
512                         any_over_ph = true;
513                 }
514         }
515
516         _trim_start_to_playhead->Enable (any_over_ph);
517         _trim_end_to_playhead->Enable (any_over_ph);
518 }
519
520 void
521 TimingPanel::move_to_start_of_reel_clicked ()
522 {
523         /* Find common position of all selected content, if it exists */
524
525         optional<DCPTime> position;
526         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
527                 if (!position) {
528                         position = i->position();
529                 } else {
530                         if (position.get() != i->position()) {
531                                 position.reset ();
532                                 break;
533                         }
534                 }
535         }
536
537         MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
538
539         if (d->ShowModal() == wxID_OK) {
540                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
541                         i->set_position (_parent->film(), d->position());
542                 }
543         }
544         d->Destroy ();
545 }