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