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