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