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