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