Allow moving-still-image sources to have their frame rate specified.
[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 "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 "film_editor.h"
26
27 using std::cout;
28 using std::string;
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
31 using boost::lexical_cast;
32
33 TimingPanel::TimingPanel (FilmEditor* e)
34         : FilmEditorPanel (e, _("Timing"))
35 {
36         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
37         _sizer->Add (grid, 0, wxALL, 8);
38
39         add_label_to_sizer (grid, this, _("Position"), true);
40         _position = new Timecode (this);
41         grid->Add (_position);
42         add_label_to_sizer (grid, this, _("Full length"), true);
43         _full_length = new Timecode (this);
44         grid->Add (_full_length);
45         add_label_to_sizer (grid, this, _("Trim from start"), true);
46         _trim_start = new Timecode (this);
47         grid->Add (_trim_start);
48         add_label_to_sizer (grid, this, _("Trim from end"), true);
49         _trim_end = new Timecode (this);
50         grid->Add (_trim_end);
51         add_label_to_sizer (grid, this, _("Play length"), true);
52         _play_length = new Timecode (this);
53         grid->Add (_play_length);
54
55         {
56                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
57                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
58                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
59                 s->Add (_video_frame_rate, 1, wxEXPAND);
60                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
61                 _set_video_frame_rate->Enable (false);
62                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
63                 grid->Add (s, 1, wxEXPAND);
64         }
65
66         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
67         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
68         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
69         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
70         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
71         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
72         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
73 }
74
75 void
76 TimingPanel::film_content_changed (int property)
77 {
78         ContentList cl = _editor->selected_content ();
79         shared_ptr<Content> content;
80         if (cl.size() == 1) {
81                 content = cl.front ();
82         }
83         
84         if (property == ContentProperty::POSITION) {
85                 if (content) {
86                         _position->set (content->position (), _editor->film()->video_frame_rate ());
87                 } else {
88                         _position->set (0, 24);
89                 }
90         } else if (property == ContentProperty::LENGTH) {
91                 if (content) {
92                         _full_length->set (content->full_length (), _editor->film()->video_frame_rate ());
93                         _play_length->set (content->length_after_trim (), _editor->film()->video_frame_rate ());
94                 } else {
95                         _full_length->set (0, 24);
96                         _play_length->set (0, 24);
97                 }
98         } else if (property == ContentProperty::TRIM_START) {
99                 if (content) {
100                         _trim_start->set (content->trim_start (), _editor->film()->video_frame_rate ());
101                         _play_length->set (content->length_after_trim (), _editor->film()->video_frame_rate ());
102                 } else {
103                         _trim_start->set (0, 24);
104                         _play_length->set (0, 24);
105                 }
106         } else if (property == ContentProperty::TRIM_END) {
107                 if (content) {
108                         _trim_end->set (content->trim_end (), _editor->film()->video_frame_rate ());
109                         _play_length->set (content->length_after_trim (), _editor->film()->video_frame_rate ());
110                 } else {
111                         _trim_end->set (0, 24);
112                         _play_length->set (0, 24);
113                 }
114         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
115                 if (content) {
116                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (content);
117                         if (vc) {
118                                 _video_frame_rate->SetValue (std_to_wx (lexical_cast<string> (vc->video_frame_rate ())));
119                         } else {
120                                 _video_frame_rate->SetValue ("24");
121                         }
122                 } else {
123                         _video_frame_rate->SetValue ("24");
124                 }
125         }
126
127         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (content);
128         _full_length->set_editable (ic && ic->still ());
129         _play_length->set_editable (!ic || !ic->still ());
130         _video_frame_rate->Enable (ic && !ic->still ());
131         _set_video_frame_rate->Enable (false);
132 }
133
134 void
135 TimingPanel::position_changed ()
136 {
137         ContentList c = _editor->selected_content ();
138         if (c.size() == 1) {
139                 c.front()->set_position (_position->get (_editor->film()->video_frame_rate ()));
140         }
141 }
142
143 void
144 TimingPanel::full_length_changed ()
145 {
146         ContentList c = _editor->selected_content ();
147         if (c.size() == 1) {
148                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (c.front ());
149                 if (ic && ic->still ()) {
150                         ic->set_video_length (rint (_full_length->get (_editor->film()->video_frame_rate()) * ic->video_frame_rate() / TIME_HZ));
151                 }
152         }
153 }
154
155 void
156 TimingPanel::trim_start_changed ()
157 {
158         ContentList c = _editor->selected_content ();
159         if (c.size() == 1) {
160                 c.front()->set_trim_start (_trim_start->get (_editor->film()->video_frame_rate ()));
161         }
162 }
163
164
165 void
166 TimingPanel::trim_end_changed ()
167 {
168         ContentList c = _editor->selected_content ();
169         if (c.size() == 1) {
170                 c.front()->set_trim_end (_trim_end->get (_editor->film()->video_frame_rate ()));
171         }
172 }
173
174 void
175 TimingPanel::play_length_changed ()
176 {
177         ContentList c = _editor->selected_content ();
178         if (c.size() == 1) {
179                 c.front()->set_trim_end (c.front()->full_length() - _play_length->get (_editor->film()->video_frame_rate()) - c.front()->trim_start());
180         }
181 }
182
183 void
184 TimingPanel::video_frame_rate_changed ()
185 {
186         _set_video_frame_rate->Enable (true);
187 }
188
189 void
190 TimingPanel::set_video_frame_rate ()
191 {
192         ContentList c = _editor->selected_content ();
193         if (c.size() == 1) {
194                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (c.front ());
195                 if (ic) {
196                         ic->set_video_frame_rate (lexical_cast<float> (wx_to_std (_video_frame_rate->GetValue ())));
197                         _set_video_frame_rate->Enable (false);
198                 }
199         }
200 }
201
202 void
203 TimingPanel::content_selection_changed ()
204 {
205         ContentList sel = _editor->selected_content ();
206         bool const single = sel.size() == 1;
207
208         /* Things that are only allowed with single selections */
209         _position->Enable (single);
210         _full_length->Enable (single);
211         _trim_start->Enable (single);
212         _trim_end->Enable (single);
213         _play_length->Enable (single);
214         _video_frame_rate->Enable (single);
215         
216         film_content_changed (ContentProperty::POSITION);
217         film_content_changed (ContentProperty::LENGTH);
218         film_content_changed (ContentProperty::TRIM_START);
219         film_content_changed (ContentProperty::TRIM_END);
220         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
221 }