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