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