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