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