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