Fix controls in main DoM.
[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 "lib/job_manager.h"
28 #include "lib/player_video.h"
29 #include "lib/dcp_content.h"
30 #include "lib/job.h"
31 #include "lib/examine_content_job.h"
32 #include "lib/content_factory.h"
33 #include "lib/cross.h"
34 #include <dcp/dcp.h>
35 #include <dcp/cpl.h>
36 #include <dcp/reel.h>
37 #include <dcp/reel_picture_asset.h>
38 #include <wx/wx.h>
39 #include <wx/tglbtn.h>
40 #include <wx/listctrl.h>
41 #include <wx/progdlg.h>
42
43 using std::string;
44 using std::list;
45 using std::cout;
46 using std::make_pair;
47 using std::exception;
48 using boost::optional;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
52
53 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
54         : wxPanel (parent)
55         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
56         , _viewer (viewer)
57         , _slider_being_moved (false)
58         , _was_running_before_slider (false)
59         , _outline_content (0)
60         , _eye (0)
61         , _jump_to_selected (0)
62         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
63         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
64         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
65         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
66         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
67 {
68         _v_sizer = new wxBoxSizer (wxVERTICAL);
69         SetSizer (_v_sizer);
70
71         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
72         if (editor_controls) {
73                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
74                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
75                 _eye = new wxChoice (this, wxID_ANY);
76                 _eye->Append (_("Left"));
77                 _eye->Append (_("Right"));
78                 _eye->SetSelection (0);
79                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
80                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
81                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
82         }
83
84         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
85
86         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
87
88         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
89         time_sizer->Add (_frame_number, 0, wxEXPAND);
90         time_sizer->Add (_timecode, 0, wxEXPAND);
91
92         h_sizer->Add (_rewind_button, 0, wxALL, 2);
93         h_sizer->Add (_back_button, 0, wxALL, 2);
94         h_sizer->Add (time_sizer, 0, wxEXPAND);
95         h_sizer->Add (_forward_button, 0, wxALL, 2);
96
97         _button_sizer = new wxBoxSizer (wxHORIZONTAL);
98         h_sizer->Add (_button_sizer, 0, wxEXPAND);
99
100         h_sizer->Add (_slider, 1, wxEXPAND);
101
102         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
103
104         _frame_number->SetMinSize (wxSize (84, -1));
105         _rewind_button->SetMinSize (wxSize (32, -1));
106         _back_button->SetMinSize (wxSize (32, -1));
107         _forward_button->SetMinSize (wxSize (32, -1));
108
109         if (_eye) {
110                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
111         }
112         if (_outline_content) {
113                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
114         }
115
116         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
117         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
118         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
119         _slider->Bind           (wxEVT_SCROLL_CHANGED,       boost::bind(&Controls::slider_released, this));
120         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
121         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
122         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
123         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
124         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
125         if (_jump_to_selected) {
126                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
127                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
128         }
129
130         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
131         _viewer->Started.connect (boost::bind(&Controls::started, this));
132         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
133
134         set_film (_viewer->film());
135
136         setup_sensitivity ();
137
138         JobManager::instance()->ActiveJobsChanged.connect (
139                 bind (&Controls::active_jobs_changed, this, _2)
140                 );
141
142         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
143         config_changed (Config::OTHER);
144 }
145
146 void
147 Controls::config_changed (int)
148 {
149         setup_sensitivity ();
150 }
151
152 void
153 Controls::started ()
154 {
155         setup_sensitivity ();
156 }
157
158 void
159 Controls::stopped ()
160 {
161         setup_sensitivity ();
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 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
186 void
187 Controls::slider_moved (bool page)
188 {
189         if (!_film) {
190                 return;
191         }
192
193         if (!page && !_slider_being_moved) {
194                 /* This is the first event of a drag; stop playback for the duration of the drag */
195                 _was_running_before_slider = _viewer->stop ();
196                 _slider_being_moved = true;
197         }
198
199         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
200         t = t.round (_film->video_frame_rate());
201         /* Ensure that we hit the end of the film at the end of the slider */
202         if (t >= _film->length ()) {
203                 t = _film->length() - _viewer->one_video_frame();
204         }
205         _viewer->seek (t, false);
206         update_position_label ();
207 }
208
209 void
210 Controls::slider_released ()
211 {
212         if (_was_running_before_slider) {
213                 /* Restart after a drag */
214                 _viewer->start ();
215         }
216         _slider_being_moved = false;
217 }
218
219 void
220 Controls::update_position_slider ()
221 {
222         if (!_film) {
223                 _slider->SetValue (0);
224                 return;
225         }
226
227         DCPTime const len = _film->length ();
228
229         if (len.get ()) {
230                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
231                 if (new_slider_position != _slider->GetValue()) {
232                         _slider->SetValue (new_slider_position);
233                 }
234         }
235 }
236
237 void
238 Controls::update_position_label ()
239 {
240         if (!_film) {
241                 _frame_number->SetLabel ("0");
242                 _timecode->SetLabel ("0:0:0.0");
243                 return;
244         }
245
246         double const fps = _film->video_frame_rate ();
247         /* Count frame number from 1 ... not sure if this is the best idea */
248         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
249         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
250 }
251
252 void
253 Controls::active_jobs_changed (optional<string> j)
254 {
255         _active_job = j;
256         setup_sensitivity ();
257 }
258
259 DCPTime
260 Controls::nudge_amount (wxKeyboardState& ev)
261 {
262         DCPTime amount = _viewer->one_video_frame ();
263
264         if (ev.ShiftDown() && !ev.ControlDown()) {
265                 amount = DCPTime::from_seconds (1);
266         } else if (!ev.ShiftDown() && ev.ControlDown()) {
267                 amount = DCPTime::from_seconds (10);
268         } else if (ev.ShiftDown() && ev.ControlDown()) {
269                 amount = DCPTime::from_seconds (60);
270         }
271
272         return amount;
273 }
274
275 void
276 Controls::rewind_clicked (wxMouseEvent& ev)
277 {
278         _viewer->seek (DCPTime(), true);
279         ev.Skip();
280 }
281
282 void
283 Controls::back_frame ()
284 {
285         _viewer->seek_by (-_viewer->one_video_frame(), true);
286 }
287
288 void
289 Controls::forward_frame ()
290 {
291         _viewer->seek_by (_viewer->one_video_frame(), true);
292 }
293
294 void
295 Controls::back_clicked (wxKeyboardState& ev)
296 {
297         _viewer->seek_by (-nudge_amount(ev), true);
298 }
299
300 void
301 Controls::forward_clicked (wxKeyboardState& ev)
302 {
303         _viewer->seek_by (nudge_amount(ev), true);
304 }
305
306 void
307 Controls::setup_sensitivity ()
308 {
309         /* examine content is the only job which stops the viewer working */
310         bool const active_job = _active_job && *_active_job != "examine_content";
311         bool const c = _film && !_film->content().empty() && !active_job;
312
313         _slider->Enable (c);
314         _rewind_button->Enable (c);
315         _back_button->Enable (c);
316         _forward_button->Enable (c);
317         if (_outline_content) {
318                 _outline_content->Enable (c);
319         }
320         _frame_number->Enable (c);
321         _timecode->Enable (c);
322         if (_jump_to_selected) {
323                 _jump_to_selected->Enable (c);
324         }
325
326         if (_eye) {
327                 _eye->Enable (c && _film->three_d ());
328         }
329 }
330
331 void
332 Controls::timecode_clicked ()
333 {
334         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
335         if (dialog->ShowModal() == wxID_OK) {
336                 _viewer->seek (dialog->get(), true);
337         }
338         dialog->Destroy ();
339 }
340
341 void
342 Controls::frame_number_clicked ()
343 {
344         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
345         if (dialog->ShowModal() == wxID_OK) {
346                 _viewer->seek (dialog->get(), true);
347         }
348         dialog->Destroy ();
349 }
350
351 void
352 Controls::jump_to_selected_clicked ()
353 {
354         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
355 }
356
357 void
358 Controls::set_film (shared_ptr<Film> film)
359 {
360         if (_film == film) {
361                 return;
362         }
363
364         _film = film;
365         _film_change_connection = _film->Change.connect (boost::bind(&Controls::film_change, this, _1, _2));
366
367         setup_sensitivity ();
368
369         update_position_slider ();
370         update_position_label ();
371 }
372
373 shared_ptr<Film>
374 Controls::film () const
375 {
376         return _film;
377 }
378
379 void
380 Controls::film_change (ChangeType type, Film::Property p)
381 {
382         if (type == CHANGE_TYPE_DONE && p == Film::CONTENT) {
383                 setup_sensitivity ();
384                 update_position_label ();
385                 update_position_slider ();
386         }
387 }