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