Merge master.
[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 boost::shared_ptr;
29 using boost::dynamic_pointer_cast;
30
31 TimingPanel::TimingPanel (FilmEditor* e)
32         : FilmEditorPanel (e, _("Timing"))
33 {
34         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
35         _sizer->Add (grid, 0, wxALL, 8);
36
37         add_label_to_sizer (grid, this, _("Position"), true);
38         _position = new Timecode (this);
39         grid->Add (_position);
40         add_label_to_sizer (grid, this, _("Length"), true);
41         _length = new Timecode (this);
42         grid->Add (_length);
43         add_label_to_sizer (grid, this, _("Trim from start"), true);
44         _trim_start = new Timecode (this);
45         grid->Add (_trim_start);
46         add_label_to_sizer (grid, this, _("Trim from end"), true);
47         _trim_end = new Timecode (this);
48         grid->Add (_trim_end);
49
50         _position->Changed.connect   (boost::bind (&TimingPanel::position_changed, this));
51         _length->Changed.connect     (boost::bind (&TimingPanel::length_changed, this));
52         _trim_start->Changed.connect (boost::bind (&TimingPanel::trim_start_changed, this));
53         _trim_end->Changed.connect   (boost::bind (&TimingPanel::trim_end_changed, this));
54 }
55
56 void
57 TimingPanel::film_content_changed (int property)
58 {
59         ContentList cl = _editor->selected_content ();
60         shared_ptr<Content> content;
61         if (cl.size() == 1) {
62                 content = cl.front ();
63         }
64         
65         if (property == ContentProperty::POSITION) {
66                 if (content) {
67                         _position->set (content->position (), _editor->film()->video_frame_rate ());
68                 } else {
69                         _position->set (0, 24);
70                 }
71         } else if (property == ContentProperty::LENGTH) {
72                 if (content) {
73                         _length->set (content->full_length (), _editor->film()->video_frame_rate ());
74                 } else {
75                         _length->set (0, 24);
76                 }
77         } else if (property == ContentProperty::TRIM_START) {
78                 if (content) {
79                         _trim_start->set (content->trim_start (), _editor->film()->video_frame_rate ());
80                 } else {
81                         _trim_start->set (0, 24);
82                 }
83         } else if (property == ContentProperty::TRIM_END) {
84                 if (content) {
85                         _trim_end->set (content->trim_end (), _editor->film()->video_frame_rate ());
86                 } else {
87                         _trim_end->set (0, 24);
88                 }
89         }       
90
91         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (content);
92         _length->set_editable (ic && ic->still ());
93 }
94
95 void
96 TimingPanel::position_changed ()
97 {
98         ContentList c = _editor->selected_content ();
99         if (c.size() == 1) {
100                 c.front()->set_position (_position->get (_editor->film()->video_frame_rate ()));
101         }
102 }
103
104 void
105 TimingPanel::length_changed ()
106 {
107         ContentList c = _editor->selected_content ();
108         if (c.size() == 1) {
109                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (c.front ());
110                 if (ic && ic->still ()) {
111                         ic->set_video_length (rint (_length->get (_editor->film()->video_frame_rate()) * ic->video_frame_rate() / TIME_HZ));
112                 }
113         }
114 }
115
116 void
117 TimingPanel::trim_start_changed ()
118 {
119         ContentList c = _editor->selected_content ();
120         if (c.size() == 1) {
121                 c.front()->set_trim_start (_trim_start->get (_editor->film()->video_frame_rate ()));
122         }
123 }
124
125
126 void
127 TimingPanel::trim_end_changed ()
128 {
129         ContentList c = _editor->selected_content ();
130         if (c.size() == 1) {
131                 c.front()->set_trim_end (_trim_end->get (_editor->film()->video_frame_rate ()));
132         }
133 }
134
135 void
136 TimingPanel::content_selection_changed ()
137 {
138         VideoContentList sel = _editor->selected_video_content ();
139         bool const single = sel.size() == 1;
140
141         /* Things that are only allowed with single selections */
142         _position->Enable (single);
143         _length->Enable (single);
144         _trim_start->Enable (single);
145         _trim_end->Enable (single);
146         
147         film_content_changed (ContentProperty::POSITION);
148         film_content_changed (ContentProperty::LENGTH);
149         film_content_changed (ContentProperty::TRIM_START);
150         film_content_changed (ContentProperty::TRIM_END);
151 }