More Controls API tweaks.
[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 "lib/job_manager.h"
27 #include <wx/wx.h>
28 #include <wx/tglbtn.h>
29 #include <wx/listctrl.h>
30
31 using std::string;
32 using boost::optional;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35
36 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
37         : wxPanel (parent)
38         , _viewer (viewer)
39         , _slider_being_moved (false)
40         , _was_running_before_slider (false)
41         , _outline_content (0)
42         , _eye (0)
43         , _jump_to_selected (0)
44         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
45         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
46         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
47         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
48         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
49         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
50 #ifdef DCPOMATIC_VARIANT_SWAROOP
51         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
52         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
53         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
54 #else
55         , _play_button (new wxToggleButton(this, wxID_ANY, _("Play")))
56 #endif
57 {
58         _v_sizer = new wxBoxSizer (wxVERTICAL);
59         SetSizer (_v_sizer);
60
61         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
62         if (editor_controls) {
63                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
64                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
65                 _eye = new wxChoice (this, wxID_ANY);
66                 _eye->Append (_("Left"));
67                 _eye->Append (_("Right"));
68                 _eye->SetSelection (0);
69                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
70                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
71                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
72         }
73
74         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
75
76         _dcp_directory = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(600, -1), wxLC_REPORT | wxLC_NO_HEADER);
77         _dcp_directory->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
78         _v_sizer->Add (_dcp_directory, 0, wxALL, DCPOMATIC_SIZER_GAP);
79         _dcp_directory->Show (false);
80
81         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
82
83         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
84         time_sizer->Add (_frame_number, 0, wxEXPAND);
85         time_sizer->Add (_timecode, 0, wxEXPAND);
86
87         h_sizer->Add (_rewind_button, 0, wxALL, 2);
88         h_sizer->Add (_back_button, 0, wxALL, 2);
89         h_sizer->Add (time_sizer, 0, wxEXPAND);
90         h_sizer->Add (_forward_button, 0, wxALL, 2);
91         h_sizer->Add (_play_button, 0, wxEXPAND);
92 #ifdef DCPOMATIC_VARIANT_SWAROOP
93         h_sizer->Add (_pause_button, 0, wxEXPAND);
94         h_sizer->Add (_stop_button, 0, wxEXPAND);
95 #endif
96         h_sizer->Add (_slider, 1, wxEXPAND);
97
98         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
99
100         _frame_number->SetMinSize (wxSize (84, -1));
101         _rewind_button->SetMinSize (wxSize (32, -1));
102         _back_button->SetMinSize (wxSize (32, -1));
103         _forward_button->SetMinSize (wxSize (32, -1));
104
105         if (_eye) {
106                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
107         }
108         if (_outline_content) {
109                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
110         }
111
112         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,   boost::bind (&Controls::slider_moved,    this, false));
113         _slider->Bind           (wxEVT_SCROLL_PAGEUP,       boost::bind (&Controls::slider_moved,    this, true));
114         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,     boost::bind (&Controls::slider_moved,    this, true));
115         _slider->Bind           (wxEVT_SCROLL_CHANGED,      boost::bind (&Controls::slider_released, this));
116 #ifdef DCPOMATIC_VARIANT_SWAROOP
117         _play_button->Bind      (wxEVT_BUTTON,              boost::bind (&Controls::play_clicked,    this));
118         _pause_button->Bind     (wxEVT_BUTTON,              boost::bind (&Controls::pause_clicked,   this));
119         _stop_button->Bind      (wxEVT_BUTTON,              boost::bind (&Controls::stop_clicked,    this));
120 #else
121         _play_button->Bind      (wxEVT_TOGGLEBUTTON,        boost::bind (&Controls::play_clicked,    this));
122 #endif
123         _rewind_button->Bind    (wxEVT_LEFT_DOWN,           boost::bind (&Controls::rewind_clicked,  this, _1));
124         _back_button->Bind      (wxEVT_LEFT_DOWN,           boost::bind (&Controls::back_clicked,    this, _1));
125         _forward_button->Bind   (wxEVT_LEFT_DOWN,           boost::bind (&Controls::forward_clicked, this, _1));
126         _frame_number->Bind     (wxEVT_LEFT_DOWN,           boost::bind (&Controls::frame_number_clicked, this));
127         _timecode->Bind         (wxEVT_LEFT_DOWN,           boost::bind (&Controls::timecode_clicked, this));
128         _dcp_directory->Bind    (wxEVT_LIST_ITEM_SELECTED,  boost::bind (&Controls::dcp_directory_selected, this));
129         if (_jump_to_selected) {
130                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
131                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
132         }
133
134         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
135         _viewer->Started.connect (boost::bind(&Controls::started, this));
136         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
137         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
138
139         film_changed ();
140
141         setup_sensitivity ();
142         update_dcp_directory ();
143
144         JobManager::instance()->ActiveJobsChanged.connect (
145                 bind (&Controls::active_jobs_changed, this, _2)
146                 );
147
148         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
149 }
150
151 void
152 Controls::config_changed (int property)
153 {
154         if (property == Config::PLAYER_DCP_DIRECTORY) {
155                 update_dcp_directory ();
156         }
157 }
158
159 void
160 Controls::started ()
161 {
162 #ifdef DCPOMATIC_VARIANT_SWAROOP
163         _play_button->Enable (false);
164         _pause_button->Enable (true);
165 #else
166         _play_button->SetValue (true);
167 #endif
168         setup_sensitivity ();
169 }
170
171 void
172 Controls::stopped ()
173 {
174 #ifdef DCPOMATIC_VARIANT_SWAROOP
175         _play_button->Enable (true);
176         _pause_button->Enable (false);
177 #else
178         _play_button->SetValue (false);
179 #endif
180         setup_sensitivity ();
181 }
182
183 void
184 Controls::position_changed ()
185 {
186         if (!_slider_being_moved) {
187                 update_position_label ();
188                 update_position_slider ();
189         }
190 }
191
192 void
193 Controls::eye_changed ()
194 {
195         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
196 }
197
198 void
199 Controls::outline_content_changed ()
200 {
201         _viewer->set_outline_content (_outline_content->GetValue());
202 }
203
204 void
205 Controls::film_change (ChangeType type, Film::Property p)
206 {
207         if (type != CHANGE_TYPE_DONE) {
208                 return;
209         }
210
211         if (p == Film::CONTENT || p == Film::THREE_D) {
212                 setup_sensitivity ();
213         }
214 }
215
216 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
217 void
218 Controls::slider_moved (bool page)
219 {
220         if (!_film) {
221                 return;
222         }
223
224         if (!page && !_slider_being_moved) {
225                 /* This is the first event of a drag; stop playback for the duration of the drag */
226                 _was_running_before_slider = _viewer->stop ();
227                 _slider_being_moved = true;
228         }
229
230         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
231         t = t.round (_film->video_frame_rate());
232         /* Ensure that we hit the end of the film at the end of the slider */
233         if (t >= _film->length ()) {
234                 t = _film->length() - _viewer->one_video_frame();
235         }
236         _viewer->seek (t, false);
237         update_position_label ();
238 }
239
240 void
241 Controls::slider_released ()
242 {
243         if (_was_running_before_slider) {
244                 /* Restart after a drag */
245                 _viewer->start ();
246         }
247         _slider_being_moved = false;
248 }
249
250 void
251 Controls::play_clicked ()
252 {
253 #ifdef DCPOMATIC_VARIANT_SWAROOP
254         _viewer->start ();
255 #else
256         check_play_state ();
257 #endif
258 }
259
260
261 #ifndef DCPOMATIC_VARIANT_SWAROOP
262 void
263 Controls::check_play_state ()
264 {
265         if (!_film || _film->video_frame_rate() == 0) {
266                 return;
267         }
268
269         if (_play_button->GetValue()) {
270                 _viewer->start ();
271         } else {
272                 _viewer->stop ();
273         }
274 }
275 #endif
276
277 void
278 Controls::update_position_slider ()
279 {
280         if (!_film) {
281                 _slider->SetValue (0);
282                 return;
283         }
284
285         DCPTime const len = _film->length ();
286
287         if (len.get ()) {
288                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
289                 if (new_slider_position != _slider->GetValue()) {
290                         _slider->SetValue (new_slider_position);
291                 }
292         }
293 }
294
295 void
296 Controls::update_position_label ()
297 {
298         if (!_film) {
299                 _frame_number->SetLabel ("0");
300                 _timecode->SetLabel ("0:0:0.0");
301                 return;
302         }
303
304         double const fps = _film->video_frame_rate ();
305         /* Count frame number from 1 ... not sure if this is the best idea */
306         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
307         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
308 }
309
310 void
311 Controls::active_jobs_changed (optional<string> j)
312 {
313         _active_job = j;
314         setup_sensitivity ();
315 }
316
317 DCPTime
318 Controls::nudge_amount (wxKeyboardState& ev)
319 {
320         DCPTime amount = _viewer->one_video_frame ();
321
322         if (ev.ShiftDown() && !ev.ControlDown()) {
323                 amount = DCPTime::from_seconds (1);
324         } else if (!ev.ShiftDown() && ev.ControlDown()) {
325                 amount = DCPTime::from_seconds (10);
326         } else if (ev.ShiftDown() && ev.ControlDown()) {
327                 amount = DCPTime::from_seconds (60);
328         }
329
330         return amount;
331 }
332
333 void
334 Controls::rewind_clicked (wxMouseEvent& ev)
335 {
336         _viewer->seek (DCPTime(), true);
337         ev.Skip();
338 }
339
340 void
341 Controls::back_frame ()
342 {
343         _viewer->seek_by (-_viewer->one_video_frame(), true);
344 }
345
346 void
347 Controls::forward_frame ()
348 {
349         _viewer->seek_by (_viewer->one_video_frame(), true);
350 }
351
352 void
353 Controls::back_clicked (wxKeyboardState& ev)
354 {
355         _viewer->seek_by (-nudge_amount(ev), true);
356 }
357
358 void
359 Controls::forward_clicked (wxKeyboardState& ev)
360 {
361         _viewer->seek_by (nudge_amount(ev), true);
362 }
363
364 void
365 Controls::setup_sensitivity ()
366 {
367         /* examine content is the only job which stops the viewer working */
368         bool const c = _film && !_film->content().empty() && (!_active_job || *_active_job != "examine_content");
369
370         _slider->Enable (c);
371         _rewind_button->Enable (c);
372         _back_button->Enable (c);
373         _forward_button->Enable (c);
374 #ifdef DCPOMATIC_VARIANT_SWAROOP
375         _play_button->Enable (c && !_viewer->playing());
376         _pause_button->Enable (c && _viewer->playing());
377         _stop_button->Enable (c);
378 #else
379         _play_button->Enable (c);
380 #endif
381         if (_outline_content) {
382                 _outline_content->Enable (c);
383         }
384         _frame_number->Enable (c);
385         _timecode->Enable (c);
386         if (_jump_to_selected) {
387                 _jump_to_selected->Enable (c);
388         }
389
390         if (_eye) {
391                 _eye->Enable (c && _film->three_d ());
392         }
393 }
394
395 void
396 Controls::timecode_clicked ()
397 {
398         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
399         if (dialog->ShowModal() == wxID_OK) {
400                 _viewer->seek (dialog->get(), true);
401         }
402         dialog->Destroy ();
403 }
404
405 void
406 Controls::frame_number_clicked ()
407 {
408         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
409         if (dialog->ShowModal() == wxID_OK) {
410                 _viewer->seek (dialog->get(), true);
411         }
412         dialog->Destroy ();
413 }
414
415 void
416 Controls::jump_to_selected_clicked ()
417 {
418         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
419 }
420
421 void
422 Controls::film_changed ()
423 {
424         shared_ptr<Film> film = _viewer->film ();
425
426         if (_film == film) {
427                 return;
428         }
429
430         _film = film;
431
432         setup_sensitivity ();
433
434         update_position_slider ();
435         update_position_label ();
436
437         if (_film) {
438                 _film->Change.connect (boost::bind (&Controls::film_change, this, _1, _2));
439         }
440 }
441
442 shared_ptr<Film>
443 Controls::film () const
444 {
445         return _film;
446 }
447
448 void
449 Controls::show_extended_player_controls (bool s)
450 {
451         _dcp_directory->Show (s);
452 }
453
454 void
455 Controls::update_dcp_directory ()
456 {
457         using namespace boost::filesystem;
458
459         _dcp_directory->DeleteAllItems ();
460         _dcp_directories.clear ();
461         optional<path> dir = Config::instance()->player_dcp_directory();
462         if (!dir) {
463                 return;
464         }
465
466         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
467                 try {
468                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
469                                 string const x = i->path().string().substr(dir->string().length() + 1);
470                                 _dcp_directory->InsertItem(_dcp_directory->GetItemCount(), std_to_wx(x));
471                                 _dcp_directories.push_back(x);
472                         }
473                 } catch (boost::filesystem::filesystem_error& e) {
474                         /* Never mind */
475                 }
476         }
477 }
478
479 void
480 Controls::dcp_directory_selected ()
481 {
482         long int s = _dcp_directory->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
483         if (s == -1) {
484                 return;
485         }
486
487         DCPOMATIC_ASSERT (s < int(_dcp_directories.size()));
488         DCPDirectorySelected (*Config::instance()->player_dcp_directory() / _dcp_directories[s]);
489 }
490
491 #ifdef DCPOMATIC_VARIANT_SWAROOP
492 void
493 Controls::pause_clicked ()
494 {
495         _viewer->stop ();
496 }
497
498 void
499 Controls::stop_clicked ()
500 {
501         _viewer->stop ();
502         DCPEjected ();
503 }
504 #endif