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