Fix the alignment of the current position in the playback controls.
[dcpomatic.git] / src / wx / controls.cc
1 /*
2     Copyright (C) 2018-2021 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 "check_box.h"
23 #include "content_view.h"
24 #include "controls.h"
25 #include "dcpomatic_button.h"
26 #include "film_viewer.h"
27 #include "markers_panel.h"
28 #include "playhead_to_frame_dialog.h"
29 #include "playhead_to_timecode_dialog.h"
30 #include "static_text.h"
31 #include "wx_util.h"
32 #include "lib/content_factory.h"
33 #include "lib/cross.h"
34 #include "lib/dcp_content.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/job.h"
37 #include "lib/job_manager.h"
38 #include "lib/player_video.h"
39 #include <dcp/cpl.h>
40 #include <dcp/dcp.h>
41 #include <dcp/reel.h>
42 #include <dcp/reel_picture_asset.h>
43 #include <dcp/warnings.h>
44 LIBDCP_DISABLE_WARNINGS
45 #include <wx/listctrl.h>
46 #include <wx/progdlg.h>
47 #include <wx/tglbtn.h>
48 #include <wx/wx.h>
49 LIBDCP_ENABLE_WARNINGS
50
51
52 using std::cout;
53 using std::dynamic_pointer_cast;
54 using std::exception;
55 using std::list;
56 using std::make_pair;
57 using std::shared_ptr;
58 using std::string;
59 using std::weak_ptr;
60 using boost::optional;
61 #if BOOST_VERSION >= 106100
62 using namespace boost::placeholders;
63 #endif
64 using namespace dcpomatic;
65
66
67 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
68         : wxPanel (parent)
69         , _markers (new MarkersPanel(this, viewer))
70         , _slider (new wxSlider(this, wxID_ANY, 0, 0, 4096))
71         , _viewer (viewer)
72         , _slider_being_moved (false)
73         , _outline_content (0)
74         , _eye (0)
75         , _jump_to_selected (0)
76         , _rewind_button (new Button(this, wxT("|<")))
77         , _back_button (new Button(this, wxT("<")))
78         , _forward_button (new Button(this, wxT(">")))
79         , _frame_number (new StaticText(this, wxT("")))
80         , _timecode (new StaticText(this, wxT("")))
81         , _timer (this)
82 {
83         _v_sizer = new wxBoxSizer (wxVERTICAL);
84         SetSizer (_v_sizer);
85
86         auto view_options = new wxBoxSizer (wxHORIZONTAL);
87         if (editor_controls) {
88                 _outline_content = new CheckBox (this, _("Outline content"));
89                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
90                 _eye = new wxChoice (this, wxID_ANY);
91                 _eye->Append (_("Left"));
92                 _eye->Append (_("Right"));
93                 _eye->SetSelection (0);
94                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
95                 _jump_to_selected = new CheckBox (this, _("Jump to selected content"));
96                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
97         }
98
99         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
100
101         auto h_sizer = new wxBoxSizer (wxHORIZONTAL);
102
103         auto time_sizer = new wxBoxSizer (wxVERTICAL);
104         time_sizer->Add (_frame_number, 0, wxEXPAND);
105         time_sizer->Add (_timecode, 0, wxEXPAND);
106
107         h_sizer->Add (_rewind_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
108         h_sizer->Add (time_sizer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
109         h_sizer->Add (_back_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
110         h_sizer->Add (_forward_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
111
112         _button_sizer = new wxBoxSizer (wxHORIZONTAL);
113         h_sizer->Add (_button_sizer, 0, wxEXPAND);
114
115         {
116                 auto box = new wxBoxSizer (wxVERTICAL);
117                 box->Add (_markers, 0, wxEXPAND);
118                 box->Add (_slider, 0, wxEXPAND);
119                 h_sizer->Add (box, 1, wxEXPAND);
120         }
121
122         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
123
124 #ifdef __WXGTK3__
125         _frame_number->SetMinSize (wxSize(100, -1));
126         _rewind_button->SetMinSize (wxSize(48, -1));
127         _back_button->SetMinSize (wxSize(48, -1));
128         _forward_button->SetMinSize (wxSize(48, -1));
129 #else
130         _frame_number->SetMinSize (wxSize (84, -1));
131         _rewind_button->SetMinSize (wxSize (32, -1));
132         _back_button->SetMinSize (wxSize (32, -1));
133         _forward_button->SetMinSize (wxSize (32, -1));
134 #endif
135
136         if (_eye) {
137                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
138         }
139         if (_outline_content) {
140                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
141         }
142
143         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
144         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
145         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
146         _slider->Bind           (wxEVT_SCROLL_THUMBRELEASE,  boost::bind(&Controls::slider_released, this));
147         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
148         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
149         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
150         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
151         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
152         if (_jump_to_selected) {
153                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
154                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
155         }
156
157         viewer->Started.connect (boost::bind(&Controls::started, this));
158         viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
159
160         Bind (wxEVT_TIMER, boost::bind(&Controls::update_position, this));
161         _timer.Start (80, wxTIMER_CONTINUOUS);
162
163         set_film (viewer->film());
164
165         setup_sensitivity ();
166
167         JobManager::instance()->ActiveJobsChanged.connect (
168                 bind (&Controls::active_jobs_changed, this, _2)
169                 );
170
171         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
172         config_changed (Config::OTHER);
173 }
174
175 void
176 Controls::config_changed (int)
177 {
178         setup_sensitivity ();
179 }
180
181
182 void
183 Controls::started ()
184 {
185         setup_sensitivity ();
186 }
187
188
189 void
190 Controls::stopped ()
191 {
192         setup_sensitivity ();
193 }
194
195
196 void
197 Controls::update_position ()
198 {
199         auto viewer = _viewer.lock ();
200         if (!viewer) {
201                 return;
202         }
203
204         if (!_slider_being_moved && !viewer->pending_idle_get()) {
205                 update_position_label ();
206                 update_position_slider ();
207         }
208 }
209
210
211 void
212 Controls::eye_changed ()
213 {
214         auto viewer = _viewer.lock ();
215         if (!viewer) {
216                 return;
217         }
218
219         viewer->set_eyes (_eye->GetSelection() == 0 ? Eyes::LEFT : Eyes::RIGHT);
220 }
221
222
223 void
224 Controls::outline_content_changed ()
225 {
226         auto viewer = _viewer.lock ();
227         if (!viewer) {
228                 return;
229         }
230
231         viewer->set_outline_content (_outline_content->GetValue());
232 }
233
234
235 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
236 void
237 Controls::slider_moved (bool page)
238 {
239         auto viewer = _viewer.lock ();
240         if (!_film || !viewer) {
241                 return;
242         }
243
244         if (!page && !_slider_being_moved) {
245                 /* This is the first event of a drag; stop playback for the duration of the drag */
246                 viewer->suspend ();
247                 _slider_being_moved = true;
248         }
249
250         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
251         t = t.round (_film->video_frame_rate());
252         /* Ensure that we hit the end of the film at the end of the slider.  In particular, we
253            need to do an accurate seek in case there isn't a keyframe near the end.
254         */
255         bool accurate = false;
256         if (t >= _film->length ()) {
257                 t = _film->length() - viewer->one_video_frame();
258                 accurate = true;
259         }
260         viewer->seek (t, accurate);
261         update_position_label ();
262 }
263
264
265 void
266 Controls::slider_released ()
267 {
268         auto viewer = _viewer.lock ();
269         if (!viewer) {
270                 return;
271         }
272
273         /* Restart after a drag */
274         viewer->resume ();
275         _slider_being_moved = false;
276 }
277
278
279 void
280 Controls::update_position_slider ()
281 {
282         if (!_film) {
283                 _slider->SetValue (0);
284                 return;
285         }
286
287         auto viewer = _viewer.lock ();
288         if (!viewer) {
289                 return;
290         }
291
292         auto const len = _film->length ();
293
294         if (len.get ()) {
295                 int const new_slider_position = 4096 * viewer->position().get() / len.get();
296                 if (new_slider_position != _slider->GetValue()) {
297                         _slider->SetValue (new_slider_position);
298                 }
299         }
300 }
301
302
303 void
304 Controls::update_position_label ()
305 {
306         if (!_film) {
307                 checked_set (_frame_number, wxT("0"));
308                 checked_set (_timecode, wxT("0:0:0.0"));
309                 return;
310         }
311
312         auto viewer = _viewer.lock ();
313         if (!viewer) {
314                 return;
315         }
316
317         double const fps = _film->video_frame_rate ();
318         /* Count frame number from 1 ... not sure if this is the best idea */
319         checked_set (_frame_number, wxString::Format (wxT("%ld"), lrint (viewer->position().seconds() * fps) + 1));
320         checked_set (_timecode, time_to_timecode (viewer->position(), fps));
321 }
322
323
324 void
325 Controls::active_jobs_changed (optional<string> j)
326 {
327         _active_job = j;
328         setup_sensitivity ();
329 }
330
331
332 DCPTime
333 Controls::nudge_amount (wxKeyboardState& ev)
334 {
335         auto viewer = _viewer.lock ();
336         if (!viewer) {
337                 return {};
338         }
339
340         auto amount = viewer->one_video_frame ();
341
342         if (ev.ShiftDown() && !ev.ControlDown()) {
343                 amount = DCPTime::from_seconds (1);
344         } else if (!ev.ShiftDown() && ev.ControlDown()) {
345                 amount = DCPTime::from_seconds (10);
346         } else if (ev.ShiftDown() && ev.ControlDown()) {
347                 amount = DCPTime::from_seconds (60);
348         }
349
350         return amount;
351 }
352
353
354 void
355 Controls::rewind_clicked (wxMouseEvent& ev)
356 {
357         auto viewer = _viewer.lock ();
358         if (viewer) {
359                 viewer->seek (DCPTime(), true);
360         }
361         ev.Skip();
362 }
363
364
365 void
366 Controls::back_frame ()
367 {
368         auto viewer = _viewer.lock ();
369         if (viewer) {
370                 viewer->seek_by (-viewer->one_video_frame(), true);
371         }
372 }
373
374
375 void
376 Controls::forward_frame ()
377 {
378         auto viewer = _viewer.lock ();
379         if (viewer) {
380                 viewer->seek_by (viewer->one_video_frame(), true);
381         }
382 }
383
384
385 void
386 Controls::back_clicked (wxKeyboardState& ev)
387 {
388         auto viewer = _viewer.lock ();
389         if (viewer) {
390                 viewer->seek_by (-nudge_amount(ev), true);
391         }
392 }
393
394
395 void
396 Controls::forward_clicked (wxKeyboardState& ev)
397 {
398         auto viewer = _viewer.lock ();
399         if (viewer) {
400                 viewer->seek_by (nudge_amount(ev), true);
401         }
402 }
403
404
405 void
406 Controls::setup_sensitivity ()
407 {
408         /* examine content is the only job which stops the viewer working */
409         bool const active_job = _active_job && *_active_job != "examine_content";
410         bool const c = _film && !_film->content().empty() && !active_job;
411
412         _slider->Enable (c);
413         _rewind_button->Enable (c);
414         _back_button->Enable (c);
415         _forward_button->Enable (c);
416         if (_outline_content) {
417                 _outline_content->Enable (c);
418         }
419         _frame_number->Enable (c);
420         _timecode->Enable (c);
421         if (_jump_to_selected) {
422                 _jump_to_selected->Enable (c);
423         }
424
425         if (_eye) {
426                 _eye->Enable (c && _film->three_d ());
427         }
428 }
429
430
431 void
432 Controls::timecode_clicked ()
433 {
434         auto viewer = _viewer.lock ();
435         if (!viewer) {
436                 return;
437         }
438
439         auto dialog = new PlayheadToTimecodeDialog (this, viewer->position(), _film->video_frame_rate());
440         if (dialog->ShowModal() == wxID_OK) {
441                 viewer->seek (dialog->get(), true);
442         }
443         dialog->Destroy ();
444 }
445
446
447 void
448 Controls::frame_number_clicked ()
449 {
450         auto viewer = _viewer.lock ();
451         if (!viewer) {
452                 return;
453         }
454
455         auto dialog = new PlayheadToFrameDialog (this, viewer->position(), _film->video_frame_rate());
456         if (dialog->ShowModal() == wxID_OK) {
457                 viewer->seek (dialog->get(), true);
458         }
459         dialog->Destroy ();
460 }
461
462
463 void
464 Controls::jump_to_selected_clicked ()
465 {
466         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
467 }
468
469
470 void
471 Controls::set_film (shared_ptr<Film> film)
472 {
473         if (_film == film) {
474                 return;
475         }
476
477         _film = film;
478
479         _markers->set_film (_film);
480
481         if (_film) {
482                 _film_change_connection = _film->Change.connect (boost::bind(&Controls::film_change, this, _1, _2));
483         }
484
485         setup_sensitivity ();
486
487         update_position_slider ();
488         update_position_label ();
489 }
490
491
492 shared_ptr<Film>
493 Controls::film () const
494 {
495         return _film;
496 }
497
498
499 void
500 Controls::film_change (ChangeType type, Film::Property p)
501 {
502         if (type == ChangeType::DONE) {
503                 if (p == Film::Property::CONTENT) {
504                         setup_sensitivity ();
505                         update_position_label ();
506                         update_position_slider ();
507                 } else if (p == Film::Property::THREE_D) {
508                         setup_sensitivity ();
509                 }
510         }
511 }
512
513
514 void
515 Controls::seek (int slider)
516 {
517         _slider->SetValue (slider);
518         slider_moved (false);
519         slider_released ();
520 }