No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2016 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 "lib/content.h"
27 #include "lib/image_content.h"
28 #include "lib/raw_convert.h"
29 #include "lib/subtitle_content.h"
30 #include "lib/dcp_subtitle_content.h"
31 #include "lib/audio_content.h"
32 #include "lib/text_subtitle_content.h"
33 #include "lib/video_content.h"
34 #include <boost/foreach.hpp>
35 #include <set>
36 #include <iostream>
37
38 using std::cout;
39 using std::string;
40 using std::set;
41 using boost::shared_ptr;
42 using boost::dynamic_pointer_cast;
43 using boost::optional;
44
45 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
46         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
47         /// TRANSLATORS: translate the word "Timing" here; do not include the "Timing|" prefix
48         : ContentSubPanel (p, S_("Timing|Timing"))
49         , _viewer (viewer)
50 {
51         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
52         _sizer->Add (grid, 0, wxALL, 8);
53
54         wxSize size = TimecodeBase::size (this);
55
56         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
57         //// TRANSLATORS: this is an abbreviation for "hours"
58         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
59 #ifdef DCPOMATIC_LINUX
60         /* Hack to work around failure to centre text on GTK */
61         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
62 #endif
63         labels->Add (t, 1, wxEXPAND);
64         add_label_to_sizer (labels, this, wxT (":"), false);
65         //// TRANSLATORS: this is an abbreviation for "minutes"
66         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
67 #ifdef DCPOMATIC_LINUX
68         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
69 #endif
70         labels->Add (t, 1, wxEXPAND);
71         add_label_to_sizer (labels, this, wxT (":"), false);
72         //// TRANSLATORS: this is an abbreviation for "seconds"
73         t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
74 #ifdef DCPOMATIC_LINUX
75         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
76 #endif
77         labels->Add (t, 1, wxEXPAND);
78         add_label_to_sizer (labels, this, wxT (":"), false);
79         //// TRANSLATORS: this is an abbreviation for "frames"
80         t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
81 #ifdef DCPOMATIC_LINUX
82         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
83 #endif
84         labels->Add (t, 1, wxEXPAND);
85         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
86         grid->Add (labels);
87
88         add_label_to_sizer (grid, this, _("Position"), true);
89         _position = new Timecode<DCPTime> (this);
90         grid->Add (_position);
91         add_label_to_sizer (grid, this, _("Full length"), true);
92         _full_length = new Timecode<DCPTime> (this);
93         grid->Add (_full_length);
94         add_label_to_sizer (grid, this, _("Trim from start"), true);
95         _trim_start = new Timecode<ContentTime> (this);
96         grid->Add (_trim_start);
97         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
98         grid->AddSpacer (0);
99         grid->Add (_trim_start_to_playhead);
100         add_label_to_sizer (grid, this, _("Trim from end"), true);
101         _trim_end = new Timecode<ContentTime> (this);
102         grid->Add (_trim_end);
103         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
104         grid->AddSpacer (0);
105         grid->Add (_trim_end_to_playhead);
106         add_label_to_sizer (grid, this, _("Play length"), true);
107         _play_length = new Timecode<DCPTime> (this);
108         grid->Add (_play_length);
109
110         {
111                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
112                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
113                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
114                 s->Add (_video_frame_rate, 1, wxEXPAND);
115                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
116                 _set_video_frame_rate->Enable (false);
117                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
118                 grid->Add (s, 1, wxEXPAND);
119         }
120
121         grid->AddSpacer (0);
122
123         /* We can't use Wrap() here as it doesn't work with markup:
124          * http://trac.wxwidgets.org/ticket/13389
125          */
126
127         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
128         wxString out;
129         int const width = 20;
130         int current = 0;
131         for (size_t i = 0; i < in.Length(); ++i) {
132                 if (in[i] == ' ' && current >= width) {
133                         out += '\n';
134                         current = 0;
135                 } else {
136                         out += in[i];
137                         ++current;
138                 }
139         }
140
141         t = new wxStaticText (this, wxID_ANY, wxT (""));
142         t->SetLabelMarkup (out);
143         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
144
145         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
146         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
147         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
148         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
149         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
150         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
151         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
152         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
153         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
154
155         _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
156
157         setup_sensitivity ();
158 }
159
160 void
161 TimingPanel::update_full_length ()
162 {
163         set<DCPTime> check;
164         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
165                 check.insert (i->full_length ());
166         }
167
168         if (check.size() == 1) {
169                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
170         } else {
171                 _full_length->clear ();
172         }
173 }
174
175 void
176 TimingPanel::update_play_length ()
177 {
178         set<DCPTime> check;
179         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
180                 check.insert (i->length_after_trim ());
181         }
182
183         if (check.size() == 1) {
184                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
185         } else {
186                 _play_length->clear ();
187         }
188 }
189
190 void
191 TimingPanel::film_content_changed (int property)
192 {
193         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
194
195         /* Here we check to see if we have exactly one different value of various
196            properties, and fill the controls with that value if so.
197         */
198
199         if (property == ContentProperty::POSITION) {
200
201                 set<DCPTime> check;
202                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
203                         check.insert (i->position ());
204                 }
205
206                 if (check.size() == 1) {
207                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
208                 } else {
209                         _position->clear ();
210                 }
211
212         } else if (
213                 property == ContentProperty::LENGTH ||
214                 property == ContentProperty::VIDEO_FRAME_RATE ||
215                 property == VideoContentProperty::FRAME_TYPE
216                 ) {
217
218                 update_full_length ();
219
220         } else if (property == ContentProperty::TRIM_START) {
221
222                 set<ContentTime> check;
223                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
224                         check.insert (i->trim_start ());
225                 }
226
227                 if (check.size() == 1) {
228                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
229                 } else {
230                         _trim_start->clear ();
231                 }
232
233         } else if (property == ContentProperty::TRIM_END) {
234
235                 set<ContentTime> check;
236                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
237                         check.insert (i->trim_end ());
238                 }
239
240                 if (check.size() == 1) {
241                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
242                 } else {
243                         _trim_end->clear ();
244                 }
245         }
246
247         if (
248                 property == ContentProperty::LENGTH ||
249                 property == ContentProperty::TRIM_START ||
250                 property == ContentProperty::TRIM_END ||
251                 property == ContentProperty::VIDEO_FRAME_RATE ||
252                 property == VideoContentProperty::FRAME_TYPE
253                 ) {
254
255                 update_play_length ();
256         }
257
258         if (property == ContentProperty::VIDEO_FRAME_RATE) {
259                 set<double> check_vc;
260                 shared_ptr<const Content> content;
261                 int count_ac = 0;
262                 int count_sc = 0;
263                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
264                         if (i->video && i->video_frame_rate()) {
265                                 check_vc.insert (i->video_frame_rate().get());
266                                 content = i;
267                         }
268                         if (i->audio && i->video_frame_rate()) {
269                                 ++count_ac;
270                                 content = i;
271                         }
272                         if (i->subtitle && i->video_frame_rate()) {
273                                 ++count_sc;
274                                 content = i;
275                         }
276
277                 }
278
279                 bool const single_frame_image_content = content && dynamic_pointer_cast<const ImageContent> (content) && content->number_of_paths() == 1;
280
281                 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
282                         checked_set (_video_frame_rate, raw_convert<string> (content->video_frame_rate().get(), 5));
283                         _video_frame_rate->Enable (true);
284                 } else {
285                         checked_set (_video_frame_rate, wxT (""));
286                         _video_frame_rate->Enable (false);
287                 }
288         }
289
290         bool have_still = false;
291         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
292                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
293                 if (ic && ic->still ()) {
294                         have_still = true;
295                 }
296         }
297
298         _full_length->set_editable (have_still);
299         _play_length->set_editable (!have_still);
300         _set_video_frame_rate->Enable (false);
301         setup_sensitivity ();
302 }
303
304 void
305 TimingPanel::position_changed ()
306 {
307         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
308                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
309         }
310 }
311
312 void
313 TimingPanel::full_length_changed ()
314 {
315         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
316                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
317                 if (ic && ic->still ()) {
318                         int const vfr = _parent->film()->video_frame_rate ();
319                         ic->video->set_length (_full_length->get (vfr).frames_round (vfr));
320                 }
321         }
322 }
323
324 void
325 TimingPanel::trim_start_changed ()
326 {
327         DCPTime const ph = _viewer->position ();
328
329         _viewer->set_coalesce_player_changes (true);
330
331         shared_ptr<Content> ref;
332         optional<FrameRateChange> ref_frc;
333         optional<DCPTime> ref_ph;
334         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
335                 if (i->position() <= ph && ph < i->end()) {
336                         /* The playhead is in i.  Use it as a reference to work out
337                            where to put the playhead post-trim; we're trying to keep the playhead
338                            at the same frame of content that we're looking at pre-trim.
339                         */
340                         ref = i;
341                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
342                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
343                 }
344
345                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
346         }
347
348         if (ref) {
349                 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
350         }
351
352         _viewer->set_coalesce_player_changes (false);
353 }
354
355 void
356 TimingPanel::trim_end_changed ()
357 {
358         _viewer->set_coalesce_player_changes (true);
359
360         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
361                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
362         }
363
364         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
365         if (_viewer->position() >= _parent->film()->length()) {
366                 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
367         }
368
369         _viewer->set_coalesce_player_changes (true);
370 }
371
372 void
373 TimingPanel::play_length_changed ()
374 {
375         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
376                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
377                 i->set_trim_end (
378                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
379                         - i->trim_start ()
380                         );
381         }
382 }
383
384 void
385 TimingPanel::video_frame_rate_changed ()
386 {
387         _set_video_frame_rate->Enable (true);
388 }
389
390 void
391 TimingPanel::set_video_frame_rate ()
392 {
393         double const fr = raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
394         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
395                 i->set_video_frame_rate (fr);
396         }
397
398         _set_video_frame_rate->Enable (false);
399 }
400
401 void
402 TimingPanel::content_selection_changed ()
403 {
404         setup_sensitivity ();
405
406         film_content_changed (ContentProperty::POSITION);
407         film_content_changed (ContentProperty::LENGTH);
408         film_content_changed (ContentProperty::TRIM_START);
409         film_content_changed (ContentProperty::TRIM_END);
410         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
411 }
412
413 void
414 TimingPanel::film_changed (Film::Property p)
415 {
416         if (p == Film::VIDEO_FRAME_RATE) {
417                 update_full_length ();
418                 update_play_length ();
419         }
420 }
421
422 void
423 TimingPanel::trim_start_to_playhead_clicked ()
424 {
425         DCPTime const ph = _viewer->position ();
426         optional<DCPTime> new_ph;
427
428         _viewer->set_coalesce_player_changes (true);
429
430         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
431                 if (i->position() < ph && ph < i->end ()) {
432                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
433                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
434                         new_ph = i->position ();
435                 }
436         }
437
438         if (new_ph) {
439                 _viewer->set_position (new_ph.get());
440         }
441
442         _viewer->set_coalesce_player_changes (false);
443 }
444
445 void
446 TimingPanel::trim_end_to_playhead_clicked ()
447 {
448         DCPTime const ph = _viewer->position ();
449         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
450                 if (i->position() < ph && ph < i->end ()) {
451                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
452                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
453                 }
454         }
455 }
456
457 void
458 TimingPanel::setup_sensitivity ()
459 {
460         bool const e = !_parent->selected().empty ();
461
462         _position->Enable (e);
463         _full_length->Enable (e);
464         _trim_start->Enable (e);
465         _trim_end->Enable (e);
466         _play_length->Enable (e);
467         _video_frame_rate->Enable (e);
468
469         DCPTime const ph = _viewer->position ();
470         bool any_over_ph = false;
471         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
472                 if (i->position() <= ph && ph < i->end()) {
473                         any_over_ph = true;
474                 }
475         }
476
477         _trim_start_to_playhead->Enable (any_over_ph);
478         _trim_end_to_playhead->Enable (any_over_ph);
479 }