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