Keep current frame visible when trimming start. Don't trim the current
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "timing_panel.h"
21 #include "wx_util.h"
22 #include "film_viewer.h"
23 #include "timecode.h"
24 #include "content_panel.h"
25 #include "lib/content.h"
26 #include "lib/image_content.h"
27 #include "lib/raw_convert.h"
28 #include "lib/subtitle_content.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/subrip_content.h"
31 #include <boost/foreach.hpp>
32 #include <set>
33 #include <iostream>
34
35 using std::cout;
36 using std::string;
37 using std::set;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
41
42 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
43         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
44         : ContentSubPanel (p, S_("Timing|Timing"))
45         , _viewer (viewer)
46 {
47         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
48         _sizer->Add (grid, 0, wxALL, 8);
49
50         wxSize size = TimecodeBase::size (this);
51
52         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
53         //// TRANSLATORS: this is an abbreviation for "hours"
54         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
55 #ifdef DCPOMATIC_LINUX
56         /* Hack to work around failure to centre text on GTK */
57         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
58 #endif
59         labels->Add (t, 1, wxEXPAND);
60         add_label_to_sizer (labels, this, wxT (":"), false);
61         //// TRANSLATORS: this is an abbreviation for "minutes"
62         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
63 #ifdef DCPOMATIC_LINUX
64         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
65 #endif
66         labels->Add (t, 1, wxEXPAND);
67         add_label_to_sizer (labels, this, wxT (":"), false);
68         //// TRANSLATORS: this is an abbreviation for "seconds"
69         t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
70 #ifdef DCPOMATIC_LINUX
71         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
72 #endif
73         labels->Add (t, 1, wxEXPAND);
74         add_label_to_sizer (labels, this, wxT (":"), false);
75         //// TRANSLATORS: this is an abbreviation for "frames"
76         t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
77 #ifdef DCPOMATIC_LINUX
78         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
79 #endif
80         labels->Add (t, 1, wxEXPAND);
81         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
82         grid->Add (labels);
83
84         add_label_to_sizer (grid, this, _("Position"), true);
85         _position = new Timecode<DCPTime> (this);
86         grid->Add (_position);
87         add_label_to_sizer (grid, this, _("Full length"), true);
88         _full_length = new Timecode<DCPTime> (this);
89         grid->Add (_full_length);
90         add_label_to_sizer (grid, this, _("Trim from start"), true);
91         _trim_start = new Timecode<ContentTime> (this);
92         grid->Add (_trim_start);
93         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
94         grid->AddSpacer (0);
95         grid->Add (_trim_start_to_playhead);
96         add_label_to_sizer (grid, this, _("Trim from end"), true);
97         _trim_end = new Timecode<ContentTime> (this);
98         grid->Add (_trim_end);
99         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
100         grid->AddSpacer (0);
101         grid->Add (_trim_end_to_playhead);
102         add_label_to_sizer (grid, this, _("Play length"), true);
103         _play_length = new Timecode<DCPTime> (this);
104         grid->Add (_play_length);
105
106         {
107                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
108                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
109                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
110                 s->Add (_video_frame_rate, 1, wxEXPAND);
111                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
112                 _set_video_frame_rate->Enable (false);
113                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
114                 grid->Add (s, 1, wxEXPAND);
115         }
116
117         grid->AddSpacer (0);
118
119         /* We can't use Wrap() here as it doesn't work with markup:
120          * http://trac.wxwidgets.org/ticket/13389
121          */
122
123         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
124         wxString out;
125         int const width = 20;
126         int current = 0;
127         for (size_t i = 0; i < in.Length(); ++i) {
128                 if (in[i] == ' ' && current >= width) {
129                         out += '\n';
130                         current = 0;
131                 } else {
132                         out += in[i];
133                         ++current;
134                 }
135         }
136
137         t = new wxStaticText (this, wxID_ANY, wxT (""));
138         t->SetLabelMarkup (out);
139         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
140
141         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
142         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
143         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
144         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
145         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
146         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
147         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
148         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
149         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
150 }
151
152 void
153 TimingPanel::update_full_length ()
154 {
155         set<DCPTime> check;
156         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
157                 check.insert (i->full_length ());
158         }
159
160         if (check.size() == 1) {
161                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
162         } else {
163                 _full_length->clear ();
164         }
165 }
166
167 void
168 TimingPanel::update_play_length ()
169 {
170         set<DCPTime> check;
171         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
172                 check.insert (i->length_after_trim ());
173         }
174
175         if (check.size() == 1) {
176                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
177         } else {
178                 _play_length->clear ();
179         }
180 }
181
182 void
183 TimingPanel::film_content_changed (int property)
184 {
185         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
186
187         /* Here we check to see if we have exactly one different value of various
188            properties, and fill the controls with that value if so.
189         */
190
191         if (property == ContentProperty::POSITION) {
192
193                 set<DCPTime> check;
194                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
195                         check.insert (i->position ());
196                 }
197
198                 if (check.size() == 1) {
199                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
200                 } else {
201                         _position->clear ();
202                 }
203
204         } else if (
205                 property == ContentProperty::LENGTH ||
206                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
207                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
208                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
209                 ) {
210
211                 update_full_length ();
212
213         } else if (property == ContentProperty::TRIM_START) {
214
215                 set<ContentTime> check;
216                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
217                         check.insert (i->trim_start ());
218                 }
219
220                 if (check.size() == 1) {
221                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
222                 } else {
223                         _trim_start->clear ();
224                 }
225
226         } else if (property == ContentProperty::TRIM_END) {
227
228                 set<ContentTime> check;
229                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
230                         check.insert (i->trim_end ());
231                 }
232
233                 if (check.size() == 1) {
234                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
235                 } else {
236                         _trim_end->clear ();
237                 }
238         }
239
240         if (
241                 property == ContentProperty::LENGTH ||
242                 property == ContentProperty::TRIM_START ||
243                 property == ContentProperty::TRIM_END ||
244                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
245                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
246                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
247                 ) {
248
249                 update_play_length ();
250         }
251
252         if (property == VideoContentProperty::VIDEO_FRAME_RATE || property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
253                 set<double> check_vc;
254                 shared_ptr<const VideoContent> vc;
255                 int count_sc = 0;
256                 shared_ptr<const SubtitleContent> sc;
257                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
258                         shared_ptr<const VideoContent> vt = dynamic_pointer_cast<const VideoContent> (i);
259                         if (vt) {
260                                 check_vc.insert (vt->video_frame_rate ());
261                                 vc = vt;
262                         }
263                         shared_ptr<const SubtitleContent> st = dynamic_pointer_cast<const SubtitleContent> (i);
264                         if (st) {
265                                 ++count_sc;
266                                 sc = st;
267                         }
268
269                 }
270
271                 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
272
273                 if ((check_vc.size() == 1 || count_sc == 1) && !single_frame_image_content) {
274                         if (vc) {
275                                 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
276                         } else if (sc) {
277                                 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle_video_frame_rate (), 5));
278                         }
279                         _video_frame_rate->Enable (true);
280                 } else {
281                         checked_set (_video_frame_rate, wxT (""));
282                         _video_frame_rate->Enable (false);
283                 }
284         }
285
286         bool have_still = false;
287         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
288                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
289                 if (ic && ic->still ()) {
290                         have_still = true;
291                 }
292         }
293
294         _full_length->set_editable (have_still);
295         _play_length->set_editable (!have_still);
296         _set_video_frame_rate->Enable (false);
297 }
298
299 void
300 TimingPanel::position_changed ()
301 {
302         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
303                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
304         }
305 }
306
307 void
308 TimingPanel::full_length_changed ()
309 {
310         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
311                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
312                 if (ic && ic->still ()) {
313                         int const vfr = _parent->film()->video_frame_rate ();
314                         ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
315                 }
316         }
317 }
318
319 void
320 TimingPanel::trim_start_changed ()
321 {
322         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
323                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
324         }
325 }
326
327
328 void
329 TimingPanel::trim_end_changed ()
330 {
331         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
332                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
333         }
334 }
335
336 void
337 TimingPanel::play_length_changed ()
338 {
339         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
340                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
341                 i->set_trim_end (
342                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
343                         - i->trim_start ()
344                         );
345         }
346 }
347
348 void
349 TimingPanel::video_frame_rate_changed ()
350 {
351         _set_video_frame_rate->Enable (true);
352 }
353
354 void
355 TimingPanel::set_video_frame_rate ()
356 {
357         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
358                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
359                 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
360                 shared_ptr<SubRipContent> ssc = dynamic_pointer_cast<SubRipContent> (i);
361                 if (vc) {
362                         vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
363                 } else if (dsc) {
364                         dsc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
365                 } else if (ssc) {
366                         ssc->set_subtitle_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
367                 }
368                 _set_video_frame_rate->Enable (false);
369         }
370 }
371
372 void
373 TimingPanel::content_selection_changed ()
374 {
375         bool const e = !_parent->selected().empty ();
376
377         _position->Enable (e);
378         _full_length->Enable (e);
379         _trim_start->Enable (e);
380         _trim_end->Enable (e);
381         _play_length->Enable (e);
382         _video_frame_rate->Enable (e);
383
384         film_content_changed (ContentProperty::POSITION);
385         film_content_changed (ContentProperty::LENGTH);
386         film_content_changed (ContentProperty::TRIM_START);
387         film_content_changed (ContentProperty::TRIM_END);
388         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
389         film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
390 }
391
392 void
393 TimingPanel::film_changed (Film::Property p)
394 {
395         if (p == Film::VIDEO_FRAME_RATE) {
396                 update_full_length ();
397                 update_play_length ();
398         }
399 }
400
401 void
402 TimingPanel::trim_start_to_playhead_clicked ()
403 {
404         DCPTime const ph = _viewer->position ();
405         optional<DCPTime> new_ph;
406
407         _viewer->set_ignore_player_changes (true);
408
409         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
410                 if (i->position() < ph && ph < i->end ()) {
411                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
412                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
413                         new_ph = i->position ();
414                 }
415         }
416
417         _viewer->set_ignore_player_changes (false);
418
419         if (new_ph) {
420                 _viewer->set_position (new_ph.get());
421         }
422 }
423
424 void
425 TimingPanel::trim_end_to_playhead_clicked ()
426 {
427         DCPTime const ph = _viewer->position ();
428         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
429                 if (i->position() < ph && ph < i->end ()) {
430                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
431                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
432                 }
433
434         }
435 }