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