Supporters update.
[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, 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(&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(&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         if (!_slider_being_moved && !_viewer.pending_idle_get()) {
200                 update_position_label ();
201                 update_position_slider ();
202         }
203 }
204
205
206 void
207 Controls::eye_changed ()
208 {
209         _viewer.set_eyes(_eye->GetSelection() == 0 ? Eyes::LEFT : Eyes::RIGHT);
210 }
211
212
213 void
214 Controls::outline_content_changed ()
215 {
216         _viewer.set_outline_content(_outline_content->GetValue());
217 }
218
219
220 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
221 void
222 Controls::slider_moved (bool page)
223 {
224         if (!_film) {
225                 return;
226         }
227
228         if (!page && !_slider_being_moved) {
229                 /* This is the first event of a drag; stop playback for the duration of the drag */
230                 _viewer.suspend();
231                 _slider_being_moved = true;
232         }
233
234         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
235         t = t.round (_film->video_frame_rate());
236         /* Ensure that we hit the end of the film at the end of the slider.  In particular, we
237            need to do an accurate seek in case there isn't a keyframe near the end.
238         */
239         bool accurate = false;
240         if (t >= _film->length ()) {
241                 t = _film->length() - _viewer.one_video_frame();
242                 accurate = true;
243         }
244         _viewer.seek(t, accurate);
245         update_position_label ();
246 }
247
248
249 void
250 Controls::slider_released ()
251 {
252         /* Restart after a drag */
253         _viewer.resume();
254         _slider_being_moved = false;
255 }
256
257
258 void
259 Controls::update_position_slider ()
260 {
261         if (!_film) {
262                 _slider->SetValue (0);
263                 return;
264         }
265
266         auto const len = _film->length ();
267
268         if (len.get ()) {
269                 int const new_slider_position = 4096 * _viewer.position().get() / len.get();
270                 if (new_slider_position != _slider->GetValue()) {
271                         _slider->SetValue (new_slider_position);
272                 }
273         }
274 }
275
276
277 void
278 Controls::update_position_label ()
279 {
280         if (!_film) {
281                 checked_set (_frame_number, wxT("0"));
282                 checked_set (_timecode, wxT("0:0:0.0"));
283                 return;
284         }
285
286         double const fps = _film->video_frame_rate ();
287         /* Count frame number from 1 ... not sure if this is the best idea */
288         checked_set(_frame_number, wxString::Format (wxT("%ld"), lrint(_viewer.position().seconds() * fps) + 1));
289         checked_set(_timecode, time_to_timecode(_viewer.position(), fps));
290 }
291
292
293 void
294 Controls::active_jobs_changed (optional<string> j)
295 {
296         _active_job = j;
297         setup_sensitivity ();
298 }
299
300
301 DCPTime
302 Controls::nudge_amount (wxKeyboardState& ev)
303 {
304         auto amount = _viewer.one_video_frame();
305
306         if (ev.ShiftDown() && !ev.ControlDown()) {
307                 amount = DCPTime::from_seconds (1);
308         } else if (!ev.ShiftDown() && ev.ControlDown()) {
309                 amount = DCPTime::from_seconds (10);
310         } else if (ev.ShiftDown() && ev.ControlDown()) {
311                 amount = DCPTime::from_seconds (60);
312         }
313
314         return amount;
315 }
316
317
318 void
319 Controls::rewind_clicked (wxMouseEvent& ev)
320 {
321         _viewer.seek(DCPTime(), true);
322         ev.Skip();
323 }
324
325
326 void
327 Controls::back_frame ()
328 {
329         _viewer.seek_by(-_viewer.one_video_frame(), true);
330 }
331
332
333 void
334 Controls::forward_frame ()
335 {
336         _viewer.seek_by(_viewer.one_video_frame(), true);
337 }
338
339
340 void
341 Controls::back_clicked (wxKeyboardState& ev)
342 {
343         _viewer.seek_by(-nudge_amount(ev), true);
344 }
345
346
347 void
348 Controls::forward_clicked (wxKeyboardState& ev)
349 {
350         _viewer.seek_by(nudge_amount(ev), true);
351 }
352
353
354 void
355 Controls::setup_sensitivity ()
356 {
357         /* examine content is the only job which stops the viewer working */
358         bool const active_job = _active_job && *_active_job != "examine_content";
359         bool const c = _film && !_film->content().empty() && !active_job;
360
361         _slider->Enable (c);
362         _rewind_button->Enable (c);
363         _back_button->Enable (c);
364         _forward_button->Enable (c);
365         if (_outline_content) {
366                 _outline_content->Enable (c);
367         }
368         _frame_number->Enable (c);
369         _timecode->Enable (c);
370         if (_jump_to_selected) {
371                 _jump_to_selected->Enable (c);
372         }
373
374         if (_eye) {
375                 _eye->Enable (c && _film->three_d ());
376         }
377 }
378
379
380 void
381 Controls::timecode_clicked ()
382 {
383         auto dialog = new PlayheadToTimecodeDialog(this, _viewer.position(), _film->video_frame_rate());
384         if (dialog->ShowModal() == wxID_OK) {
385                 _viewer.seek(dialog->get(), true);
386         }
387         dialog->Destroy ();
388 }
389
390
391 void
392 Controls::frame_number_clicked ()
393 {
394         auto dialog = new PlayheadToFrameDialog(this, _viewer.position(), _film->video_frame_rate());
395         if (dialog->ShowModal() == wxID_OK) {
396                 _viewer.seek(dialog->get(), true);
397         }
398         dialog->Destroy ();
399 }
400
401
402 void
403 Controls::jump_to_selected_clicked ()
404 {
405         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
406 }
407
408
409 void
410 Controls::set_film (shared_ptr<Film> film)
411 {
412         if (_film == film) {
413                 return;
414         }
415
416         _film = film;
417
418         _markers->set_film (_film);
419
420         if (_film) {
421                 _film_change_connection = _film->Change.connect (boost::bind(&Controls::film_change, this, _1, _2));
422         }
423
424         setup_sensitivity ();
425
426         update_position_slider ();
427         update_position_label ();
428 }
429
430
431 shared_ptr<Film>
432 Controls::film () const
433 {
434         return _film;
435 }
436
437
438 void
439 Controls::film_change (ChangeType type, Film::Property p)
440 {
441         if (type == ChangeType::DONE) {
442                 if (p == Film::Property::CONTENT) {
443                         setup_sensitivity ();
444                         update_position_label ();
445                         update_position_slider ();
446                 } else if (p == Film::Property::THREE_D) {
447                         setup_sensitivity ();
448                 }
449         }
450 }
451
452
453 void
454 Controls::seek (int slider)
455 {
456         _slider->SetValue (slider);
457         slider_moved (false);
458         slider_released ();
459 }