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