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