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