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