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