Lots of #include <iostream>s for Arch.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/content.h"
21 #include "lib/image_content.h"
22 #include "lib/raw_convert.h"
23 #include "timing_panel.h"
24 #include "wx_util.h"
25 #include "film_viewer.h"
26 #include "timecode.h"
27 #include "content_panel.h"
28 #include <boost/foreach.hpp>
29 #include <set>
30 #include <iostream>
31
32 using std::cout;
33 using std::string;
34 using std::set;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37
38 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
39         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
40         : ContentSubPanel (p, S_("Timing|Timing"))
41         , _viewer (viewer)
42 {
43         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
44         _sizer->Add (grid, 0, wxALL, 8);
45
46         wxSize size = TimecodeBase::size (this);
47
48         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
49         //// TRANSLATORS: this is an abbreviation for "hours"
50         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
51 #ifdef DCPOMATIC_LINUX
52         /* Hack to work around failure to centre text on GTK */
53         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
54 #endif
55         labels->Add (t, 1, wxEXPAND);
56         add_label_to_sizer (labels, this, wxT (":"), false);
57         //// TRANSLATORS: this is an abbreviation for "minutes"
58         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
59 #ifdef DCPOMATIC_LINUX
60         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
61 #endif
62         labels->Add (t, 1, wxEXPAND);
63         add_label_to_sizer (labels, this, wxT (":"), false);
64         //// TRANSLATORS: this is an abbreviation for "seconds"
65         t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
66 #ifdef DCPOMATIC_LINUX
67         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
68 #endif
69         labels->Add (t, 1, wxEXPAND);
70         add_label_to_sizer (labels, this, wxT (":"), false);
71         //// TRANSLATORS: this is an abbreviation for "frames"
72         t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
73 #ifdef DCPOMATIC_LINUX
74         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
75 #endif
76         labels->Add (t, 1, wxEXPAND);
77         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
78         grid->Add (labels);
79
80         add_label_to_sizer (grid, this, _("Position"), true);
81         _position = new Timecode<DCPTime> (this);
82         grid->Add (_position);
83         add_label_to_sizer (grid, this, _("Full length"), true);
84         _full_length = new Timecode<DCPTime> (this);
85         grid->Add (_full_length);
86         add_label_to_sizer (grid, this, _("Trim from start"), true);
87         _trim_start = new Timecode<ContentTime> (this);
88         grid->Add (_trim_start);
89         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
90         grid->AddSpacer (0);
91         grid->Add (_trim_start_to_playhead);
92         add_label_to_sizer (grid, this, _("Trim from end"), true);
93         _trim_end = new Timecode<ContentTime> (this);
94         grid->Add (_trim_end);
95         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
96         grid->AddSpacer (0);
97         grid->Add (_trim_end_to_playhead);
98         add_label_to_sizer (grid, this, _("Play length"), true);
99         _play_length = new Timecode<DCPTime> (this);
100         grid->Add (_play_length);
101
102         {
103                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
104                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
105                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
106                 s->Add (_video_frame_rate, 1, wxEXPAND);
107                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
108                 _set_video_frame_rate->Enable (false);
109                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
110                 grid->Add (s, 1, wxEXPAND);
111         }
112
113         grid->AddSpacer (0);
114
115         /* We can't use Wrap() here as it doesn't work with markup:
116          * http://trac.wxwidgets.org/ticket/13389
117          */
118
119         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
120         wxString out;
121         int const width = 20;
122         int current = 0;
123         for (size_t i = 0; i < in.Length(); ++i) {
124                 if (in[i] == ' ' && current >= width) {
125                         out += '\n';
126                         current = 0;
127                 } else {
128                         out += in[i];
129                         ++current;
130                 }
131         }
132
133         t = new wxStaticText (this, wxID_ANY, wxT (""));
134         t->SetLabelMarkup (out);
135         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
136
137         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
138         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
139         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
140         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
141         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
142         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
143         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
144         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
145         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
146 }
147
148 void
149 TimingPanel::update_full_length ()
150 {
151         set<DCPTime> check;
152         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
153                 check.insert (i->full_length ());
154         }
155
156         if (check.size() == 1) {
157                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
158         } else {
159                 _full_length->clear ();
160         }
161 }
162
163 void
164 TimingPanel::update_play_length ()
165 {
166         set<DCPTime> check;
167         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
168                 check.insert (i->length_after_trim ());
169         }
170
171         if (check.size() == 1) {
172                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
173         } else {
174                 _play_length->clear ();
175         }
176 }
177
178 void
179 TimingPanel::film_content_changed (int property)
180 {
181         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
182
183         /* Here we check to see if we have exactly one different value of various
184            properties, and fill the controls with that value if so.
185         */
186
187         if (property == ContentProperty::POSITION) {
188
189                 set<DCPTime> check;
190                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
191                         check.insert (i->position ());
192                 }
193
194                 if (check.size() == 1) {
195                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
196                 } else {
197                         _position->clear ();
198                 }
199
200         } else if (
201                 property == ContentProperty::LENGTH ||
202                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
203                 property == VideoContentProperty::VIDEO_FRAME_TYPE
204                 ) {
205
206                 update_full_length ();
207
208         } else if (property == ContentProperty::TRIM_START) {
209
210                 set<ContentTime> check;
211                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
212                         check.insert (i->trim_start ());
213                 }
214
215                 if (check.size() == 1) {
216                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
217                 } else {
218                         _trim_start->clear ();
219                 }
220
221         } else if (property == ContentProperty::TRIM_END) {
222
223                 set<ContentTime> check;
224                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
225                         check.insert (i->trim_end ());
226                 }
227
228                 if (check.size() == 1) {
229                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
230                 } else {
231                         _trim_end->clear ();
232                 }
233         }
234
235         if (
236                 property == ContentProperty::LENGTH ||
237                 property == ContentProperty::TRIM_START ||
238                 property == ContentProperty::TRIM_END ||
239                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
240                 property == VideoContentProperty::VIDEO_FRAME_TYPE
241                 ) {
242
243                 update_play_length ();
244         }
245
246         if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
247                 set<double> check;
248                 shared_ptr<const VideoContent> vc;
249                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
250                         shared_ptr<const VideoContent> t = dynamic_pointer_cast<const VideoContent> (i);
251                         if (t) {
252                                 check.insert (t->video_frame_rate ());
253                                 vc = t;
254                         }
255                 }
256                 if (check.size() == 1) {
257                         checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
258                         _video_frame_rate->Enable (true);
259                 } else {
260                         checked_set (_video_frame_rate, wxT (""));
261                         _video_frame_rate->Enable (false);
262                 }
263         }
264
265         bool have_still = false;
266         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
267                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
268                 if (ic && ic->still ()) {
269                         have_still = true;
270                 }
271         }
272
273         _full_length->set_editable (have_still);
274         _play_length->set_editable (!have_still);
275         _set_video_frame_rate->Enable (false);
276 }
277
278 void
279 TimingPanel::position_changed ()
280 {
281         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
282                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
283         }
284 }
285
286 void
287 TimingPanel::full_length_changed ()
288 {
289         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
290                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
291                 if (ic && ic->still ()) {
292                         int const vfr = _parent->film()->video_frame_rate ();
293                         ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
294                 }
295         }
296 }
297
298 void
299 TimingPanel::trim_start_changed ()
300 {
301         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
302                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
303         }
304 }
305
306
307 void
308 TimingPanel::trim_end_changed ()
309 {
310         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
311                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
312         }
313 }
314
315 void
316 TimingPanel::play_length_changed ()
317 {
318         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
319                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
320                 i->set_trim_end (
321                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
322                         - i->trim_start ()
323                         );
324         }
325 }
326
327 void
328 TimingPanel::video_frame_rate_changed ()
329 {
330         _set_video_frame_rate->Enable (true);
331 }
332
333 void
334 TimingPanel::set_video_frame_rate ()
335 {
336         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
337                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
338                 if (vc) {
339                         vc->set_video_frame_rate (raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ())));
340                 }
341                 _set_video_frame_rate->Enable (false);
342         }
343 }
344
345 void
346 TimingPanel::content_selection_changed ()
347 {
348         bool const e = !_parent->selected().empty ();
349
350         _position->Enable (e);
351         _full_length->Enable (e);
352         _trim_start->Enable (e);
353         _trim_end->Enable (e);
354         _play_length->Enable (e);
355         _video_frame_rate->Enable (e);
356
357         film_content_changed (ContentProperty::POSITION);
358         film_content_changed (ContentProperty::LENGTH);
359         film_content_changed (ContentProperty::TRIM_START);
360         film_content_changed (ContentProperty::TRIM_END);
361         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
362 }
363
364 void
365 TimingPanel::film_changed (Film::Property p)
366 {
367         if (p == Film::VIDEO_FRAME_RATE) {
368                 update_full_length ();
369                 update_play_length ();
370         }
371 }
372
373 void
374 TimingPanel::trim_start_to_playhead_clicked ()
375 {
376         DCPTime const ph = _viewer->position ();
377         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
378                 if (i->position() < ph && ph < i->end ()) {
379                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
380                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
381                 }
382         }
383 }
384
385 void
386 TimingPanel::trim_end_to_playhead_clicked ()
387 {
388         DCPTime const ph = _viewer->position ();
389         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
390                 if (i->position() < ph && ph < i->end ()) {
391                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
392                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph, frc) - i->trim_start());
393                 }
394
395         }
396 }