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