Supporters update.
[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                 for (auto i: _parent->selected()) {
290                         if (i->video && i->video_frame_rate()) {
291                                 check_vc.insert (i->video_frame_rate().get());
292                                 content = i;
293                         }
294                         if (i->audio && i->video_frame_rate()) {
295                                 content = i;
296                         }
297                         if (!i->text.empty() && i->video_frame_rate()) {
298                                 content = i;
299                         }
300
301                 }
302         }
303
304         bool have_still = false;
305         for (auto i: _parent->selected()) {
306                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
307                 if (ic && ic->still ()) {
308                         have_still = true;
309                 }
310         }
311
312         _full_length->set_editable (have_still);
313         _play_length->set_editable (!have_still);
314         setup_sensitivity ();
315 }
316
317 void
318 TimingPanel::position_changed ()
319 {
320         DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
321         for (auto i: _parent->selected()) {
322                 i->set_position (_parent->film(), pos);
323         }
324 }
325
326 void
327 TimingPanel::full_length_changed ()
328 {
329         int const vfr = _parent->film()->video_frame_rate ();
330         Frame const len = _full_length->get (vfr).frames_round (vfr);
331         for (auto i: _parent->selected()) {
332                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
333                 if (ic && ic->still ()) {
334                         ic->video->set_length (len);
335                 }
336         }
337 }
338
339 void
340 TimingPanel::trim_start_changed ()
341 {
342         DCPTime const ph = _viewer.position();
343
344         _viewer.set_coalesce_player_changes(true);
345
346         shared_ptr<Content> ref;
347         optional<FrameRateChange> ref_frc;
348         optional<DCPTime> ref_ph;
349
350         Suspender::Block bl = _film_content_changed_suspender.block ();
351         for (auto i: _parent->selected()) {
352                 if (i->position() <= ph && ph < i->end(_parent->film())) {
353                         /* The playhead is in i.  Use it as a reference to work out
354                            where to put the playhead post-trim; we're trying to keep the playhead
355                            at the same frame of content that we're looking at pre-trim.
356                         */
357                         ref = i;
358                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
359                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
360                 }
361
362                 ContentTime const trim = _trim_start->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
363                 i->set_trim_start(_parent->film(), trim);
364         }
365
366         if (ref) {
367                 _viewer.seek(max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
368         }
369
370         _viewer.set_coalesce_player_changes(false);
371 }
372
373 void
374 TimingPanel::trim_end_changed ()
375 {
376         _viewer.set_coalesce_player_changes(true);
377
378         Suspender::Block bl = _film_content_changed_suspender.block ();
379         for (auto i: _parent->selected()) {
380                 ContentTime const trim = _trim_end->get (i->video_frame_rate().get_value_or(_parent->film()->video_frame_rate()));
381                 i->set_trim_end (trim);
382         }
383
384         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
385         if (_viewer.position() >= _parent->film()->length()) {
386                 _viewer.seek(_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
387         }
388
389         _viewer.set_coalesce_player_changes(false);
390 }
391
392 void
393 TimingPanel::play_length_changed ()
394 {
395         DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
396         Suspender::Block bl = _film_content_changed_suspender.block ();
397         for (auto i: _parent->selected()) {
398                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
399                 auto dcp = max(DCPTime(), i->full_length(_parent->film()) - play_length);
400                 i->set_trim_end (max(ContentTime(), ContentTime(dcp, frc) - i->trim_start()));
401         }
402 }
403
404
405 void
406 TimingPanel::content_selection_changed ()
407 {
408         setup_sensitivity ();
409
410         film_content_changed (ContentProperty::POSITION);
411         film_content_changed (ContentProperty::LENGTH);
412         film_content_changed (ContentProperty::TRIM_START);
413         film_content_changed (ContentProperty::TRIM_END);
414         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
415 }
416
417 void
418 TimingPanel::film_changed(FilmProperty p)
419 {
420         if (p == FilmProperty::VIDEO_FRAME_RATE) {
421                 update_full_length ();
422                 update_play_length ();
423         }
424 }
425
426 void
427 TimingPanel::trim_start_to_playhead_clicked ()
428 {
429         auto film = _parent->film ();
430         DCPTime const ph = _viewer.position().floor(film->video_frame_rate());
431         optional<DCPTime> new_ph;
432
433         _viewer.set_coalesce_player_changes(true);
434
435         for (auto i: _parent->selected()) {
436                 if (i->position() < ph && ph < i->end(film)) {
437                         FrameRateChange const frc = film->active_frame_rate_change (i->position());
438                         i->set_trim_start(film, i->trim_start() + ContentTime(ph - i->position(), frc));
439                         new_ph = i->position ();
440                 }
441         }
442
443         _viewer.set_coalesce_player_changes(false);
444
445         if (new_ph) {
446                 _viewer.seek(new_ph.get(), true);
447         }
448 }
449
450 void
451 TimingPanel::trim_end_to_playhead_clicked ()
452 {
453         auto film = _parent->film ();
454         auto const ph = _viewer.position().floor(film->video_frame_rate());
455         for (auto i: _parent->selected()) {
456                 if (i->position() < ph && ph < i->end(film)) {
457                         FrameRateChange const frc = film->active_frame_rate_change (i->position ());
458                         i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph, frc) - i->trim_start());
459                 }
460         }
461 }
462
463 void
464 TimingPanel::setup_sensitivity ()
465 {
466         bool const e = !_parent->selected().empty ();
467
468         _position->Enable (e);
469         _move_to_start_of_reel->Enable (e);
470         _full_length->Enable (e);
471         _trim_start->Enable (e);
472         _trim_end->Enable (e);
473         _play_length->Enable (e);
474
475         auto const ph = _viewer.position();
476         bool any_over_ph = false;
477         for (auto i: _parent->selected()) {
478                 if (i->position() <= ph && ph < i->end(_parent->film())) {
479                         any_over_ph = true;
480                 }
481         }
482
483         _trim_start_to_playhead->Enable (any_over_ph);
484         _trim_end_to_playhead->Enable (any_over_ph);
485 }
486
487 void
488 TimingPanel::move_to_start_of_reel_clicked ()
489 {
490         /* Find common position of all selected content, if it exists */
491
492         optional<DCPTime> position;
493         for (auto i: _parent->selected()) {
494                 if (!position) {
495                         position = i->position();
496                 } else {
497                         if (position.get() != i->position()) {
498                                 position.reset ();
499                                 break;
500                         }
501                 }
502         }
503
504         auto d = make_wx<MoveToDialog>(this, position, _parent->film());
505
506         if (d->ShowModal() == wxID_OK) {
507                 for (auto i: _parent->selected()) {
508                         i->set_position (_parent->film(), d->position());
509                 }
510         }
511 }