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