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