Support buttons.
[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 <dcp/locale_convert.h>
37 #include <boost/foreach.hpp>
38 #include <set>
39 #include <iostream>
40
41 using std::cout;
42 using std::string;
43 using std::set;
44 using boost::shared_ptr;
45 using boost::weak_ptr;
46 using boost::dynamic_pointer_cast;
47 using boost::optional;
48 using dcp::locale_convert;
49
50 TimingPanel::TimingPanel (ContentPanel* p, weak_ptr<FilmViewer> viewer)
51         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
52         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
53         : ContentSubPanel (p, S_("Timing|Timing"))
54         , _viewer (viewer)
55 {
56         wxSize size = TimecodeBase::size (this);
57
58         for (int i = 0; i < 3; ++i) {
59                 _colon[i] = create_label (this, wxT(":"), false);
60         }
61
62         //// TRANSLATORS: this is an abbreviation for "hours"
63         _h_label = new StaticText (this, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
64 #ifdef DCPOMATIC_LINUX
65         /* Hack to work around failure to centre text on GTK */
66         gtk_label_set_line_wrap (GTK_LABEL(_h_label->GetHandle()), FALSE);
67 #endif
68         //// TRANSLATORS: this is an abbreviation for "minutes"
69         _m_label = new StaticText (this, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
70 #ifdef DCPOMATIC_LINUX
71         gtk_label_set_line_wrap (GTK_LABEL (_m_label->GetHandle()), FALSE);
72 #endif
73         //// TRANSLATORS: this is an abbreviation for "seconds"
74         _s_label = new StaticText (this, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
75 #ifdef DCPOMATIC_LINUX
76         gtk_label_set_line_wrap (GTK_LABEL(_s_label->GetHandle()), FALSE);
77 #endif
78         //// TRANSLATORS: this is an abbreviation for "frames"
79         _f_label = new StaticText (this, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
80 #ifdef DCPOMATIC_LINUX
81         gtk_label_set_line_wrap (GTK_LABEL(_f_label->GetHandle()), FALSE);
82 #endif
83
84         _position_label = create_label (this, _("Position"), true);
85         _position = new Timecode<DCPTime> (this);
86         _move_to_start_of_reel = new Button (this, _("Move to start of reel"));
87         _full_length_label = create_label (this, _("Full length"), true);
88         _full_length = new Timecode<DCPTime> (this);
89         _trim_start_label = create_label (this, _("Trim from start"), true);
90         _trim_start = new Timecode<ContentTime> (this);
91         _trim_start_to_playhead = new Button (this, _("Trim up to current position"));
92         _trim_end_label = create_label (this, _("Trim from end"), true);
93         _trim_end = new Timecode<ContentTime> (this);
94         _trim_end_to_playhead = new Button (this, _("Trim after current position"));
95         _play_length_label = create_label (this, _("Play length"), true);
96         _play_length = new Timecode<DCPTime> (this);
97
98         _video_frame_rate_label = create_label (this, _("Video frame rate"), true);
99         _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
100         _set_video_frame_rate = new Button (this, _("Set"));
101         _set_video_frame_rate->Enable (false);
102
103         /* We can't use Wrap() here as it doesn't work with markup:
104          * http://trac.wxwidgets.org/ticket/13389
105          */
106
107         wxString in = _("<i>Only change this if the content's frame rate has been read incorrectly.</i>");
108         wxString out;
109         int const width = 20;
110         int current = 0;
111         for (size_t i = 0; i < in.Length(); ++i) {
112                 if (in[i] == ' ' && current >= width) {
113                         out += '\n';
114                         current = 0;
115                 } else {
116                         out += in[i];
117                         ++current;
118                 }
119         }
120
121         _tip = new StaticText (this, wxT (""));
122         _tip->SetLabelMarkup (out);
123 #ifdef DCPOMATIC_OSX
124         /* Hack to stop hidden text on some versions of OS X */
125         _tip->SetMinSize (wxSize (-1, 256));
126 #endif
127
128         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
129         _move_to_start_of_reel->Bind  (wxEVT_BUTTON, boost::bind (&TimingPanel::move_to_start_of_reel_clicked, this));
130         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
131         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
132         _trim_start_to_playhead->Bind (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
133         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
134         _trim_end_to_playhead->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
135         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
136         _video_frame_rate->Bind       (wxEVT_TEXT, boost::bind (&TimingPanel::video_frame_rate_changed, this));
137         _set_video_frame_rate->Bind   (wxEVT_BUTTON, boost::bind (&TimingPanel::set_video_frame_rate, this));
138
139         shared_ptr<FilmViewer> fv = _viewer.lock ();
140         DCPOMATIC_ASSERT (fv);
141         fv->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
142
143         setup_sensitivity ();
144         add_to_grid ();
145 }
146
147 void
148 TimingPanel::add_to_grid ()
149 {
150         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
151
152         int r = 0;
153
154         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
155         labels->Add (_h_label, 1, wxEXPAND);
156         add_label_to_sizer (labels, _colon[0], false);
157         labels->Add (_m_label, 1, wxEXPAND);
158         add_label_to_sizer (labels, _colon[1], false);
159         labels->Add (_s_label, 1, wxEXPAND);
160         add_label_to_sizer (labels, _colon[2], false);
161         labels->Add (_f_label, 1, wxEXPAND);
162         _grid->Add (labels, wxGBPosition(r, 1));
163         ++r;
164
165         add_label_to_sizer (_grid, _position_label, true, wxGBPosition(r, 0));
166         _grid->Add (_position, wxGBPosition(r, 1));
167         ++r;
168
169         _move_to_start_of_reel->Show (full);
170         _full_length_label->Show (full);
171         _full_length->Show (full);
172         _trim_start_label->Show (full);
173         _trim_start->Show (full);
174         _trim_start_to_playhead->Show (full);
175         _trim_end_label->Show (full);
176         _trim_end->Show (full);
177         _trim_end_to_playhead->Show (full);
178         _play_length_label->Show (full);
179         _play_length->Show (full);
180         _video_frame_rate_label->Show (full);
181         _video_frame_rate->Show (full);
182         _set_video_frame_rate->Show (full);
183         _tip->Show (full);
184
185         if (full) {
186                 _grid->Add (_move_to_start_of_reel, wxGBPosition(r, 1));
187                 ++r;
188
189                 add_label_to_sizer (_grid, _full_length_label, true, wxGBPosition(r, 0));
190                 _grid->Add (_full_length, wxGBPosition(r, 1));
191                 ++r;
192
193                 add_label_to_sizer (_grid, _trim_start_label, true, wxGBPosition(r, 0));
194                 _grid->Add (_trim_start, wxGBPosition(r, 1));
195                 ++r;
196
197                 _grid->Add (_trim_start_to_playhead, wxGBPosition(r, 1));
198                 ++r;
199
200                 add_label_to_sizer (_grid, _trim_end_label, true, wxGBPosition(r, 0));
201                 _grid->Add (_trim_end, wxGBPosition(r, 1));
202                 ++r;
203
204                 _grid->Add (_trim_end_to_playhead, wxGBPosition(r, 1));
205                 ++r;
206
207                 add_label_to_sizer (_grid, _play_length_label, true, wxGBPosition(r, 0));
208                 _grid->Add (_play_length, wxGBPosition(r, 1));
209                 ++r;
210
211                 {
212                         add_label_to_sizer (_grid, _video_frame_rate_label, true, wxGBPosition(r, 0));
213                         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
214                         s->Add (_video_frame_rate, 1, wxEXPAND);
215                         s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
216                         _grid->Add (s, wxGBPosition(r, 1), wxGBSpan(1, 2));
217                 }
218                 ++r;
219
220                 _grid->Add (_tip, wxGBPosition(r, 1), wxGBSpan(1, 2));
221         }
222
223         /* Completely speculative fix for #891 */
224         _grid->Layout ();
225 }
226
227 void
228 TimingPanel::update_full_length ()
229 {
230         set<DCPTime> check;
231         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
232                 check.insert (i->full_length(_parent->film()));
233         }
234
235         if (check.size() == 1) {
236                 _full_length->set (_parent->selected().front()->full_length(_parent->film()), _parent->film()->video_frame_rate());
237         } else {
238                 _full_length->clear ();
239         }
240 }
241
242 void
243 TimingPanel::update_play_length ()
244 {
245         set<DCPTime> check;
246         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
247                 check.insert (i->length_after_trim(_parent->film()));
248         }
249
250         if (check.size() == 1) {
251                 _play_length->set (_parent->selected().front()->length_after_trim(_parent->film()), _parent->film()->video_frame_rate());
252         } else {
253                 _play_length->clear ();
254         }
255 }
256
257 void
258 TimingPanel::film_content_changed (int property)
259 {
260         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
261
262         /* Here we check to see if we have exactly one different value of various
263            properties, and fill the controls with that value if so.
264         */
265
266         if (property == ContentProperty::POSITION) {
267
268                 set<DCPTime> check;
269                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
270                         check.insert (i->position ());
271                 }
272
273                 if (check.size() == 1) {
274                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
275                 } else {
276                         _position->clear ();
277                 }
278
279         } else if (
280                 property == ContentProperty::LENGTH ||
281                 property == ContentProperty::VIDEO_FRAME_RATE ||
282                 property == VideoContentProperty::FRAME_TYPE
283                 ) {
284
285                 update_full_length ();
286
287         } else if (property == ContentProperty::TRIM_START) {
288
289                 set<ContentTime> check;
290                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
291                         check.insert (i->trim_start ());
292                 }
293
294                 if (check.size() == 1) {
295                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
296                 } else {
297                         _trim_start->clear ();
298                 }
299
300         } else if (property == ContentProperty::TRIM_END) {
301
302                 set<ContentTime> check;
303                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
304                         check.insert (i->trim_end ());
305                 }
306
307                 if (check.size() == 1) {
308                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
309                 } else {
310                         _trim_end->clear ();
311                 }
312         }
313
314         if (
315                 property == ContentProperty::LENGTH ||
316                 property == ContentProperty::TRIM_START ||
317                 property == ContentProperty::TRIM_END ||
318                 property == ContentProperty::VIDEO_FRAME_RATE ||
319                 property == VideoContentProperty::FRAME_TYPE
320                 ) {
321
322                 update_play_length ();
323         }
324
325         if (property == ContentProperty::VIDEO_FRAME_RATE) {
326                 set<double> check_vc;
327                 shared_ptr<const Content> content;
328                 int count_ac = 0;
329                 int count_sc = 0;
330                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
331                         if (i->video && i->video_frame_rate()) {
332                                 check_vc.insert (i->video_frame_rate().get());
333                                 content = i;
334                         }
335                         if (i->audio && i->video_frame_rate()) {
336                                 ++count_ac;
337                                 content = i;
338                         }
339                         if (!i->text.empty() && i->video_frame_rate()) {
340                                 ++count_sc;
341                                 content = i;
342                         }
343
344                 }
345
346                 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
347
348                 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
349                         checked_set (_video_frame_rate, locale_convert<string> (content->video_frame_rate().get(), 5));
350                         _video_frame_rate->Enable (true);
351                 } else {
352                         checked_set (_video_frame_rate, wxT (""));
353                         _video_frame_rate->Enable (false);
354                 }
355         }
356
357         bool have_still = false;
358         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
359                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
360                 if (ic && ic->still ()) {
361                         have_still = true;
362                 }
363         }
364
365         _full_length->set_editable (have_still);
366         _play_length->set_editable (!have_still);
367         _set_video_frame_rate->Enable (false);
368         setup_sensitivity ();
369 }
370
371 void
372 TimingPanel::position_changed ()
373 {
374         DCPTime const pos = _position->get (_parent->film()->video_frame_rate ());
375         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
376                 i->set_position (_parent->film(), pos);
377         }
378 }
379
380 void
381 TimingPanel::full_length_changed ()
382 {
383         int const vfr = _parent->film()->video_frame_rate ();
384         Frame const len = _full_length->get (vfr).frames_round (vfr);
385         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
386                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
387                 if (ic && ic->still ()) {
388                         ic->video->set_length (len);
389                 }
390         }
391 }
392
393 void
394 TimingPanel::trim_start_changed ()
395 {
396         shared_ptr<FilmViewer> fv = _viewer.lock ();
397         if (!fv) {
398                 return;
399         }
400
401         DCPTime const ph = fv->position ();
402
403         fv->set_coalesce_player_changes (true);
404
405         shared_ptr<Content> ref;
406         optional<FrameRateChange> ref_frc;
407         optional<DCPTime> ref_ph;
408
409         ContentTime const trim = _trim_start->get (_parent->film()->video_frame_rate ());
410
411         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
412                 if (i->position() <= ph && ph < i->end(_parent->film())) {
413                         /* The playhead is in i.  Use it as a reference to work out
414                            where to put the playhead post-trim; we're trying to keep the playhead
415                            at the same frame of content that we're looking at pre-trim.
416                         */
417                         ref = i;
418                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
419                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
420                 }
421
422                 i->set_trim_start (trim);
423         }
424
425         if (ref) {
426                 fv->seek (max(DCPTime(), ref_ph.get() + ref->position() - DCPTime(ref->trim_start(), ref_frc.get())), true);
427         }
428
429         fv->set_coalesce_player_changes (false);
430 }
431
432 void
433 TimingPanel::trim_end_changed ()
434 {
435         shared_ptr<FilmViewer> fv = _viewer.lock ();
436         if (!fv) {
437                 return;
438         }
439
440         fv->set_coalesce_player_changes (true);
441
442         ContentTime const trim = _trim_end->get (_parent->film()->video_frame_rate ());
443         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
444                 i->set_trim_end (trim);
445         }
446
447         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
448         if (fv->position() >= _parent->film()->length()) {
449                 fv->seek (_parent->film()->length() - DCPTime::from_frames(1, _parent->film()->video_frame_rate()), true);
450         }
451
452         fv->set_coalesce_player_changes (true);
453 }
454
455 void
456 TimingPanel::play_length_changed ()
457 {
458         DCPTime const play_length = _play_length->get (_parent->film()->video_frame_rate());
459         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
460                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
461                 i->set_trim_end (
462                         ContentTime (max(DCPTime(), i->full_length(_parent->film()) - play_length), frc) - i->trim_start()
463                         );
464         }
465 }
466
467 void
468 TimingPanel::video_frame_rate_changed ()
469 {
470         _set_video_frame_rate->Enable (true);
471 }
472
473 void
474 TimingPanel::set_video_frame_rate ()
475 {
476         optional<double> fr;
477         if (_video_frame_rate->GetValue() != wxT("")) {
478                 fr = locale_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
479         }
480         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
481                 if (fr) {
482                         i->set_video_frame_rate (*fr);
483                 } else {
484                         i->unset_video_frame_rate ();
485                 }
486         }
487
488         _set_video_frame_rate->Enable (false);
489 }
490
491 void
492 TimingPanel::content_selection_changed ()
493 {
494         setup_sensitivity ();
495
496         film_content_changed (ContentProperty::POSITION);
497         film_content_changed (ContentProperty::LENGTH);
498         film_content_changed (ContentProperty::TRIM_START);
499         film_content_changed (ContentProperty::TRIM_END);
500         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
501 }
502
503 void
504 TimingPanel::film_changed (Film::Property p)
505 {
506         if (p == Film::VIDEO_FRAME_RATE) {
507                 update_full_length ();
508                 update_play_length ();
509         }
510 }
511
512 void
513 TimingPanel::trim_start_to_playhead_clicked ()
514 {
515         shared_ptr<FilmViewer> fv = _viewer.lock ();
516         if (!fv) {
517                 return;
518         }
519
520         shared_ptr<const Film> film = _parent->film ();
521         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
522         optional<DCPTime> new_ph;
523
524         fv->set_coalesce_player_changes (true);
525
526         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
527                 if (i->position() < ph && ph < i->end(film)) {
528                         FrameRateChange const frc = film->active_frame_rate_change (i->position());
529                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position(), frc));
530                         new_ph = i->position ();
531                 }
532         }
533
534         if (new_ph) {
535                 fv->seek (new_ph.get(), true);
536         }
537
538         fv->set_coalesce_player_changes (false);
539 }
540
541 void
542 TimingPanel::trim_end_to_playhead_clicked ()
543 {
544         shared_ptr<FilmViewer> fv = _viewer.lock ();
545         if (!fv) {
546                 return;
547         }
548
549         shared_ptr<const Film> film = _parent->film ();
550         DCPTime const ph = fv->position().floor (film->video_frame_rate ());
551         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
552                 if (i->position() < ph && ph < i->end(film)) {
553                         FrameRateChange const frc = film->active_frame_rate_change (i->position ());
554                         i->set_trim_end (ContentTime(i->position() + i->full_length(film) - ph - DCPTime::from_frames(1, frc.dcp), frc) - i->trim_start());
555                 }
556         }
557 }
558
559 void
560 TimingPanel::setup_sensitivity ()
561 {
562         bool const e = !_parent->selected().empty ();
563
564         _position->Enable (e);
565         _move_to_start_of_reel->Enable (e);
566         _full_length->Enable (e);
567         _trim_start->Enable (e);
568         _trim_end->Enable (e);
569         _play_length->Enable (e);
570         _video_frame_rate->Enable (e);
571
572         shared_ptr<FilmViewer> fv = _viewer.lock ();
573         DCPOMATIC_ASSERT (fv);
574         DCPTime const ph = fv->position ();
575         bool any_over_ph = false;
576         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
577                 if (i->position() <= ph && ph < i->end(_parent->film())) {
578                         any_over_ph = true;
579                 }
580         }
581
582         _trim_start_to_playhead->Enable (any_over_ph);
583         _trim_end_to_playhead->Enable (any_over_ph);
584 }
585
586 void
587 TimingPanel::move_to_start_of_reel_clicked ()
588 {
589         /* Find common position of all selected content, if it exists */
590
591         optional<DCPTime> position;
592         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
593                 if (!position) {
594                         position = i->position();
595                 } else {
596                         if (position.get() != i->position()) {
597                                 position.reset ();
598                                 break;
599                         }
600                 }
601         }
602
603         MoveToDialog* d = new MoveToDialog (this, position, _parent->film());
604
605         if (d->ShowModal() == wxID_OK) {
606                 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected()) {
607                         i->set_position (_parent->film(), d->position());
608                 }
609         }
610         d->Destroy ();
611 }