Basic video fade support.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2013 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 <dcp/raw_convert.h>
21 #include "lib/content.h"
22 #include "lib/image_content.h"
23 #include "timing_panel.h"
24 #include "wx_util.h"
25 #include "timecode.h"
26 #include "content_panel.h"
27
28 using std::cout;
29 using std::string;
30 using std::set;
31 using boost::shared_ptr;
32 using boost::dynamic_pointer_cast;
33 using dcp::raw_convert;
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         add_label_to_sizer (grid, this, _("Position"), true);
43         _position = new Timecode<DCPTime> (this);
44         grid->Add (_position);
45         add_label_to_sizer (grid, this, _("Full length"), true);
46         _full_length = new Timecode<DCPTime> (this);
47         grid->Add (_full_length);
48         add_label_to_sizer (grid, this, _("Trim from start"), true);
49         _trim_start = new Timecode<DCPTime> (this);
50         grid->Add (_trim_start);
51         add_label_to_sizer (grid, this, _("Trim from end"), true);
52         _trim_end = new Timecode<DCPTime> (this);
53         grid->Add (_trim_end);
54         add_label_to_sizer (grid, this, _("Play length"), true);
55         _play_length = new Timecode<DCPTime> (this);
56         grid->Add (_play_length);
57
58         {
59                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
60                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
61                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
62                 s->Add (_video_frame_rate, 1, wxEXPAND);
63                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
64                 _set_video_frame_rate->Enable (false);
65                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
66                 grid->Add (s, 1, wxEXPAND);
67         }
68
69         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
70         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
71         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
72         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
73         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
74         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
75         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
76 }
77
78 void
79 TimingPanel::film_content_changed (int property)
80 {
81         ContentList cl = _parent->selected ();
82         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
83
84         /* Here we check to see if we have exactly one different value of various
85            properties, and fill the controls with that value if so.
86         */
87         
88         if (property == ContentProperty::POSITION) {
89
90                 set<DCPTime> check;
91                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
92                         check.insert ((*i)->position ());
93                 }
94
95                 if (check.size() == 1) {
96                         _position->set (cl.front()->position(), film_video_frame_rate);
97                 } else {
98                         _position->clear ();
99                 }
100                 
101         } else if (
102                 property == ContentProperty::LENGTH ||
103                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
104                 property == VideoContentProperty::VIDEO_FRAME_TYPE
105                 ) {
106
107                 set<DCPTime> check;
108                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
109                         check.insert ((*i)->full_length ());
110                 }
111                 
112                 if (check.size() == 1) {
113                         _full_length->set (cl.front()->full_length (), film_video_frame_rate);
114                 } else {
115                         _full_length->clear ();
116                 }
117
118         } else if (property == ContentProperty::TRIM_START) {
119
120                 set<DCPTime> check;
121                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
122                         check.insert ((*i)->trim_start ());
123                 }
124                 
125                 if (check.size() == 1) {
126                         _trim_start->set (cl.front()->trim_start (), film_video_frame_rate);
127                 } else {
128                         _trim_start->clear ();
129                 }
130                 
131         } else if (property == ContentProperty::TRIM_END) {
132
133                 set<DCPTime> check;
134                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
135                         check.insert ((*i)->trim_end ());
136                 }
137                 
138                 if (check.size() == 1) {
139                         _trim_end->set (cl.front()->trim_end (), film_video_frame_rate);
140                 } else {
141                         _trim_end->clear ();
142                 }
143         }
144
145         if (
146                 property == ContentProperty::LENGTH ||
147                 property == ContentProperty::TRIM_START ||
148                 property == ContentProperty::TRIM_END ||
149                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
150                 property == VideoContentProperty::VIDEO_FRAME_TYPE
151                 ) {
152
153                 set<DCPTime> check;
154                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
155                         check.insert ((*i)->length_after_trim ());
156                 }
157                 
158                 if (check.size() == 1) {
159                         _play_length->set (cl.front()->length_after_trim (), film_video_frame_rate);
160                 } else {
161                         _play_length->clear ();
162                 }
163         }
164
165         if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
166                 set<float> check;
167                 shared_ptr<VideoContent> vc;
168                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
169                         shared_ptr<VideoContent> t = dynamic_pointer_cast<VideoContent> (*i);
170                         if (t) {
171                                 check.insert (t->video_frame_rate ());
172                                 vc = t;
173                         }
174                 }
175                 if (check.size() == 1) {
176                         _video_frame_rate->SetValue (std_to_wx (raw_convert<string> (vc->video_frame_rate (), 5)));
177                         _video_frame_rate->Enable (true);
178                 } else {
179                         _video_frame_rate->SetValue ("");
180                         _video_frame_rate->Enable (false);
181                 }
182         }
183
184         bool have_still = false;
185         for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
186                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
187                 if (ic && ic->still ()) {
188                         have_still = true;
189                 }
190         }
191
192         _full_length->set_editable (have_still);
193         _play_length->set_editable (!have_still);
194         _set_video_frame_rate->Enable (false);
195 }
196
197 void
198 TimingPanel::position_changed ()
199 {
200         ContentList c = _parent->selected ();
201         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
202                 (*i)->set_position (_position->get (_parent->film()->video_frame_rate ()));
203         }
204 }
205
206 void
207 TimingPanel::full_length_changed ()
208 {
209         ContentList c = _parent->selected ();
210         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
211                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
212                 if (ic && ic->still ()) {
213                         /* XXX: No effective FRC here... is this right? */
214                         ic->set_video_length (ContentTime (_full_length->get (_parent->film()->video_frame_rate()), FrameRateChange (1, 1)));
215                 }
216         }
217 }
218
219 void
220 TimingPanel::trim_start_changed ()
221 {
222         ContentList c = _parent->selected ();
223         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
224                 (*i)->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
225         }
226 }
227
228
229 void
230 TimingPanel::trim_end_changed ()
231 {
232         ContentList c = _parent->selected ();
233         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
234                 (*i)->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
235         }
236 }
237
238 void
239 TimingPanel::play_length_changed ()
240 {
241         ContentList c = _parent->selected ();
242         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
243                 (*i)->set_trim_end ((*i)->full_length() - _play_length->get (_parent->film()->video_frame_rate()) - (*i)->trim_start());
244         }
245 }
246
247 void
248 TimingPanel::video_frame_rate_changed ()
249 {
250         _set_video_frame_rate->Enable (true);
251 }
252
253 void
254 TimingPanel::set_video_frame_rate ()
255 {
256         ContentList c = _parent->selected ();
257         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
258                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
259                 if (vc) {
260                         vc->set_video_frame_rate (raw_convert<float> (wx_to_std (_video_frame_rate->GetValue ())));
261                 }
262                 _set_video_frame_rate->Enable (false);
263         }
264 }
265
266 void
267 TimingPanel::content_selection_changed ()
268 {
269         bool const e = !_parent->selected().empty ();
270
271         _position->Enable (e);
272         _full_length->Enable (e);
273         _trim_start->Enable (e);
274         _trim_end->Enable (e);
275         _play_length->Enable (e);
276         _video_frame_rate->Enable (e);
277         
278         film_content_changed (ContentProperty::POSITION);
279         film_content_changed (ContentProperty::LENGTH);
280         film_content_changed (ContentProperty::TRIM_START);
281         film_content_changed (ContentProperty::TRIM_END);
282         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
283 }