Basic SPL support.
[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         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
77
78         _dcp_directory = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
79         _dcp_directory->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
80         e_sizer->Add (_dcp_directory, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
81
82         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
83         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
84         e_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
85
86         wxBoxSizer* buttons_sizer = new wxBoxSizer (wxVERTICAL);
87         _add_button = new wxButton(this, wxID_ANY, _("Add"));
88         buttons_sizer->Add (_add_button);
89         e_sizer->Add (buttons_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
90
91         _v_sizer->Add (e_sizer, 1, wxEXPAND);
92
93         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
94         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
95
96         _dcp_directory->Show (false);
97         _spl_view->Show (false);
98         _add_button->Show (false);
99         _log->Show (false);
100
101         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
102
103         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
104         time_sizer->Add (_frame_number, 0, wxEXPAND);
105         time_sizer->Add (_timecode, 0, wxEXPAND);
106
107         h_sizer->Add (_rewind_button, 0, wxALL, 2);
108         h_sizer->Add (_back_button, 0, wxALL, 2);
109         h_sizer->Add (time_sizer, 0, wxEXPAND);
110         h_sizer->Add (_forward_button, 0, wxALL, 2);
111         h_sizer->Add (_play_button, 0, wxEXPAND);
112 #ifdef DCPOMATIC_VARIANT_SWAROOP
113         h_sizer->Add (_pause_button, 0, wxEXPAND);
114         h_sizer->Add (_stop_button, 0, wxEXPAND);
115 #endif
116         h_sizer->Add (_slider, 1, wxEXPAND);
117
118         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
119
120         _frame_number->SetMinSize (wxSize (84, -1));
121         _rewind_button->SetMinSize (wxSize (32, -1));
122         _back_button->SetMinSize (wxSize (32, -1));
123         _forward_button->SetMinSize (wxSize (32, -1));
124
125         if (_eye) {
126                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
127         }
128         if (_outline_content) {
129                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
130         }
131
132         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
133         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
134         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
135         _slider->Bind           (wxEVT_SCROLL_CHANGED,       boost::bind(&Controls::slider_released, this));
136 #ifdef DCPOMATIC_VARIANT_SWAROOP
137         _play_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::play_clicked,    this));
138         _pause_button->Bind     (wxEVT_BUTTON,               boost::bind(&Controls::pause_clicked,   this));
139         _stop_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::stop_clicked,    this));
140 #else
141         _play_button->Bind      (wxEVT_TOGGLEBUTTON,         boost::bind(&Controls::play_clicked,    this));
142 #endif
143         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
144         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
145         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
146         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
147         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
148         _dcp_directory->Bind    (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&Controls::setup_sensitivity, this));
149         _dcp_directory->Bind    (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&Controls::setup_sensitivity, this));
150         if (_jump_to_selected) {
151                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
152                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
153         }
154         _add_button->Bind       (wxEVT_BUTTON,              boost::bind(&Controls::add_clicked, this));
155
156         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
157         _viewer->Started.connect (boost::bind(&Controls::started, this));
158         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
159         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
160
161         film_changed ();
162
163         setup_sensitivity ();
164         update_dcp_directory ();
165
166         JobManager::instance()->ActiveJobsChanged.connect (
167                 bind (&Controls::active_jobs_changed, this, _2)
168                 );
169
170         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
171 }
172
173 void
174 Controls::add_clicked ()
175 {
176         optional<boost::filesystem::path> sel = selected_dcp ();
177         DCPOMATIC_ASSERT (sel);
178         _spl.push_back (SPLEntry(*sel));
179         _spl_view->InsertItem(_spl_view->GetItemCount(), std_to_wx(sel->filename().string()));
180         SPLChanged (_spl);
181         setup_sensitivity ();
182 }
183
184 void
185 Controls::config_changed (int property)
186 {
187         if (property == Config::PLAYER_DCP_DIRECTORY) {
188                 update_dcp_directory ();
189         }
190 }
191
192 void
193 Controls::started ()
194 {
195 #ifdef DCPOMATIC_VARIANT_SWAROOP
196         _play_button->Enable (false);
197         _pause_button->Enable (true);
198 #else
199         _play_button->SetValue (true);
200 #endif
201         setup_sensitivity ();
202 }
203
204 void
205 Controls::stopped ()
206 {
207 #ifdef DCPOMATIC_VARIANT_SWAROOP
208         _play_button->Enable (true);
209         _pause_button->Enable (false);
210 #else
211         _play_button->SetValue (false);
212 #endif
213         setup_sensitivity ();
214 }
215
216 void
217 Controls::position_changed ()
218 {
219         if (!_slider_being_moved) {
220                 update_position_label ();
221                 update_position_slider ();
222         }
223 }
224
225 void
226 Controls::eye_changed ()
227 {
228         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
229 }
230
231 void
232 Controls::outline_content_changed ()
233 {
234         _viewer->set_outline_content (_outline_content->GetValue());
235 }
236
237 void
238 Controls::film_change (ChangeType type, Film::Property p)
239 {
240         if (type != CHANGE_TYPE_DONE) {
241                 return;
242         }
243
244         if (p == Film::CONTENT || p == Film::THREE_D) {
245                 setup_sensitivity ();
246         }
247 }
248
249 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
250 void
251 Controls::slider_moved (bool page)
252 {
253         if (!_film) {
254                 return;
255         }
256
257         if (!page && !_slider_being_moved) {
258                 /* This is the first event of a drag; stop playback for the duration of the drag */
259                 _was_running_before_slider = _viewer->stop ();
260                 _slider_being_moved = true;
261         }
262
263         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
264         t = t.round (_film->video_frame_rate());
265         /* Ensure that we hit the end of the film at the end of the slider */
266         if (t >= _film->length ()) {
267                 t = _film->length() - _viewer->one_video_frame();
268         }
269         _viewer->seek (t, false);
270         update_position_label ();
271 }
272
273 void
274 Controls::slider_released ()
275 {
276         if (_was_running_before_slider) {
277                 /* Restart after a drag */
278                 _viewer->start ();
279         }
280         _slider_being_moved = false;
281 }
282
283 void
284 Controls::play_clicked ()
285 {
286 #ifdef DCPOMATIC_VARIANT_SWAROOP
287         _viewer->start ();
288 #else
289         check_play_state ();
290 #endif
291 }
292
293
294 #ifndef DCPOMATIC_VARIANT_SWAROOP
295 void
296 Controls::check_play_state ()
297 {
298         if (!_film || _film->video_frame_rate() == 0) {
299                 return;
300         }
301
302         if (_play_button->GetValue()) {
303                 _viewer->start ();
304         } else {
305                 _viewer->stop ();
306         }
307 }
308 #endif
309
310 void
311 Controls::update_position_slider ()
312 {
313         if (!_film) {
314                 _slider->SetValue (0);
315                 return;
316         }
317
318         DCPTime const len = _film->length ();
319
320         if (len.get ()) {
321                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
322                 if (new_slider_position != _slider->GetValue()) {
323                         _slider->SetValue (new_slider_position);
324                 }
325         }
326 }
327
328 void
329 Controls::update_position_label ()
330 {
331         if (!_film) {
332                 _frame_number->SetLabel ("0");
333                 _timecode->SetLabel ("0:0:0.0");
334                 return;
335         }
336
337         double const fps = _film->video_frame_rate ();
338         /* Count frame number from 1 ... not sure if this is the best idea */
339         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
340         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
341 }
342
343 void
344 Controls::active_jobs_changed (optional<string> j)
345 {
346         _active_job = j;
347         setup_sensitivity ();
348 }
349
350 DCPTime
351 Controls::nudge_amount (wxKeyboardState& ev)
352 {
353         DCPTime amount = _viewer->one_video_frame ();
354
355         if (ev.ShiftDown() && !ev.ControlDown()) {
356                 amount = DCPTime::from_seconds (1);
357         } else if (!ev.ShiftDown() && ev.ControlDown()) {
358                 amount = DCPTime::from_seconds (10);
359         } else if (ev.ShiftDown() && ev.ControlDown()) {
360                 amount = DCPTime::from_seconds (60);
361         }
362
363         return amount;
364 }
365
366 void
367 Controls::rewind_clicked (wxMouseEvent& ev)
368 {
369         _viewer->seek (DCPTime(), true);
370         ev.Skip();
371 }
372
373 void
374 Controls::back_frame ()
375 {
376         _viewer->seek_by (-_viewer->one_video_frame(), true);
377 }
378
379 void
380 Controls::forward_frame ()
381 {
382         _viewer->seek_by (_viewer->one_video_frame(), true);
383 }
384
385 void
386 Controls::back_clicked (wxKeyboardState& ev)
387 {
388         _viewer->seek_by (-nudge_amount(ev), true);
389 }
390
391 void
392 Controls::forward_clicked (wxKeyboardState& ev)
393 {
394         _viewer->seek_by (nudge_amount(ev), true);
395 }
396
397 void
398 Controls::setup_sensitivity ()
399 {
400         /* examine content is the only job which stops the viewer working */
401         bool const active_job = _active_job && *_active_job != "examine_content";
402         bool const c = ((_film && !_film->content().empty()) || !_spl.empty()) && !active_job;
403
404         _slider->Enable (c);
405         _rewind_button->Enable (c);
406         _back_button->Enable (c);
407         _forward_button->Enable (c);
408 #ifdef DCPOMATIC_VARIANT_SWAROOP
409         _play_button->Enable (c && !_viewer->playing());
410         _pause_button->Enable (c && _viewer->playing());
411         _stop_button->Enable (c);
412 #else
413         _play_button->Enable (c);
414 #endif
415         if (_outline_content) {
416                 _outline_content->Enable (c);
417         }
418         _frame_number->Enable (c);
419         _timecode->Enable (c);
420         if (_jump_to_selected) {
421                 _jump_to_selected->Enable (c);
422         }
423
424         if (_eye) {
425                 _eye->Enable (c && _film->three_d ());
426         }
427
428         _add_button->Enable (static_cast<bool>(selected_dcp()));
429 }
430
431 optional<boost::filesystem::path>
432 Controls::selected_dcp () const
433 {
434         long int s = _dcp_directory->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
435         if (s == -1) {
436                 return optional<boost::filesystem::path>();
437         }
438
439         DCPOMATIC_ASSERT (s < int(_dcp_directories.size()));
440         return _dcp_directories[s];
441 }
442
443 void
444 Controls::timecode_clicked ()
445 {
446         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
447         if (dialog->ShowModal() == wxID_OK) {
448                 _viewer->seek (dialog->get(), true);
449         }
450         dialog->Destroy ();
451 }
452
453 void
454 Controls::frame_number_clicked ()
455 {
456         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
457         if (dialog->ShowModal() == wxID_OK) {
458                 _viewer->seek (dialog->get(), true);
459         }
460         dialog->Destroy ();
461 }
462
463 void
464 Controls::jump_to_selected_clicked ()
465 {
466         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
467 }
468
469 void
470 Controls::film_changed ()
471 {
472         shared_ptr<Film> film = _viewer->film ();
473
474         if (_film == film) {
475                 return;
476         }
477
478         _film = film;
479
480         setup_sensitivity ();
481
482         update_position_slider ();
483         update_position_label ();
484
485         if (_film) {
486                 _film->Change.connect (boost::bind (&Controls::film_change, this, _1, _2));
487         }
488 }
489
490 shared_ptr<Film>
491 Controls::film () const
492 {
493         return _film;
494 }
495
496 void
497 Controls::show_extended_player_controls (bool s)
498 {
499         _dcp_directory->Show (s);
500         _spl_view->Show (s);
501         _log->Show (s);
502         _add_button->Show (s);
503         _v_sizer->Layout ();
504 }
505
506 void
507 Controls::update_dcp_directory ()
508 {
509         using namespace boost::filesystem;
510
511         _dcp_directory->DeleteAllItems ();
512         _dcp_directories.clear ();
513         optional<path> dir = Config::instance()->player_dcp_directory();
514         if (!dir) {
515                 return;
516         }
517
518         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
519                 try {
520                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
521                                 string const x = i->path().string().substr(dir->string().length() + 1);
522                                 _dcp_directory->InsertItem(_dcp_directory->GetItemCount(), std_to_wx(x));
523                                 _dcp_directories.push_back(*i);
524                         }
525                 } catch (boost::filesystem::filesystem_error& e) {
526                         /* Never mind */
527                 }
528         }
529 }
530
531 #ifdef DCPOMATIC_VARIANT_SWAROOP
532 void
533 Controls::pause_clicked ()
534 {
535         _viewer->stop ();
536 }
537
538 void
539 Controls::stop_clicked ()
540 {
541         _viewer->stop ();
542         _viewer->seek (DCPTime(), true);
543 }
544 #endif
545
546 void
547 Controls::log (wxString s)
548 {
549         struct timeval time;
550         gettimeofday (&time, 0);
551         char buffer[64];
552         time_t const sec = time.tv_sec;
553         struct tm* t = localtime (&sec);
554         strftime (buffer, 64, "%c", t);
555         wxString ts = std_to_wx(string(buffer)) + N_(": ");
556         _log->SetValue(_log->GetValue() + ts + s + "\n");
557 }