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