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