Merge branch 'master' into 12bit
[dcpomatic.git] / src / wx / subtitle_panel.cc
1 /*
2     Copyright (C) 2012-2014 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 <boost/lexical_cast.hpp>
21 #include <wx/spinctrl.h>
22 #include "lib/ffmpeg_content.h"
23 #include "subtitle_panel.h"
24 #include "film_editor.h"
25 #include "wx_util.h"
26
27 using std::vector;
28 using std::string;
29 using boost::shared_ptr;
30 using boost::lexical_cast;
31 using boost::dynamic_pointer_cast;
32
33 SubtitlePanel::SubtitlePanel (FilmEditor* e)
34         : FilmEditorPanel (e, _("Subtitles"))
35 {
36         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
37         _sizer->Add (grid, 0, wxALL, 8);
38
39         _with_subtitles = new wxCheckBox (this, wxID_ANY, _("With Subtitles"));
40         grid->Add (_with_subtitles, 1);
41         grid->AddSpacer (0);
42         
43         {
44                 add_label_to_sizer (grid, this, _("X Offset"), true);
45                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
46                 _x_offset = new wxSpinCtrl (this);
47                 s->Add (_x_offset);
48                 add_label_to_sizer (s, this, _("%"), false);
49                 grid->Add (s);
50         }
51
52         {
53                 add_label_to_sizer (grid, this, _("Y Offset"), true);
54                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
55                 _y_offset = new wxSpinCtrl (this);
56                 s->Add (_y_offset);
57                 add_label_to_sizer (s, this, _("%"), false);
58                 grid->Add (s);
59         }
60         
61         {
62                 add_label_to_sizer (grid, this, _("X Scale"), true);
63                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
64                 _x_scale = new wxSpinCtrl (this);
65                 s->Add (_x_scale);
66                 add_label_to_sizer (s, this, _("%"), false);
67                 grid->Add (s);
68         }
69
70         {
71                 add_label_to_sizer (grid, this, _("Y Scale"), true);
72                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
73                 _y_scale = new wxSpinCtrl (this);
74                 s->Add (_y_scale);
75                 add_label_to_sizer (s, this, _("%"), false);
76                 grid->Add (s);
77         }
78         
79         add_label_to_sizer (grid, this, _("Stream"), true);
80         _stream = new wxChoice (this, wxID_ANY);
81         grid->Add (_stream, 1, wxEXPAND);
82         
83         _x_offset->SetRange (-100, 100);
84         _y_offset->SetRange (-100, 100);
85         _x_scale->SetRange (10, 1000);
86         _y_scale->SetRange (10, 1000);
87
88         _with_subtitles->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::with_subtitles_toggled, this));
89         _x_offset->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
90         _y_offset->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
91         _x_scale->Bind        (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
92         _y_scale->Bind        (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
93         _stream->Bind         (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
94 }
95
96 void
97 SubtitlePanel::film_changed (Film::Property property)
98 {
99         switch (property) {
100         case Film::CONTENT:
101                 setup_sensitivity ();
102                 break;
103         case Film::WITH_SUBTITLES:
104                 checked_set (_with_subtitles, _editor->film()->with_subtitles ());
105                 setup_sensitivity ();
106                 break;
107         default:
108                 break;
109         }
110 }
111
112 void
113 SubtitlePanel::film_content_changed (int property)
114 {
115         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
116         SubtitleContentList sc = _editor->selected_subtitle_content ();
117
118         shared_ptr<FFmpegContent> fcs;
119         if (fc.size() == 1) {
120                 fcs = fc.front ();
121         }
122
123         shared_ptr<SubtitleContent> scs;
124         if (sc.size() == 1) {
125                 scs = sc.front ();
126         }
127         
128         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
129                 _stream->Clear ();
130                 if (fcs) {
131                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
132                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
133                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
134                         }
135                         
136                         if (fcs->subtitle_stream()) {
137                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
138                         } else {
139                                 _stream->SetSelection (wxNOT_FOUND);
140                         }
141                 }
142                 setup_sensitivity ();
143         } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
144                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
145         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
146                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
147         } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
148                 checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
149         } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
150                 checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
151         }
152 }
153
154 void
155 SubtitlePanel::with_subtitles_toggled ()
156 {
157         if (!_editor->film()) {
158                 return;
159         }
160
161         _editor->film()->set_with_subtitles (_with_subtitles->GetValue ());
162 }
163
164 void
165 SubtitlePanel::setup_sensitivity ()
166 {
167         bool h = false;
168         bool j = false;
169         if (_editor->film()) {
170                 h = _editor->film()->has_subtitles ();
171                 j = _editor->film()->with_subtitles ();
172         }
173         
174         _with_subtitles->Enable (h);
175         _x_offset->Enable (j);
176         _y_offset->Enable (j);
177         _x_scale->Enable (j);
178         _y_scale->Enable (j);
179         _stream->Enable (j);
180 }
181
182 void
183 SubtitlePanel::stream_changed ()
184 {
185         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
186         if (fc.size() != 1) {
187                 return;
188         }
189
190         shared_ptr<FFmpegContent> fcs = fc.front ();
191         
192         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
193         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
194         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
195         while (i != a.end() && (*i)->identifier () != s) {
196                 ++i;
197         }
198
199         if (i != a.end ()) {
200                 fcs->set_subtitle_stream (*i);
201         }
202 }
203
204 void
205 SubtitlePanel::x_offset_changed ()
206 {
207         SubtitleContentList c = _editor->selected_subtitle_content ();
208         if (c.size() == 1) {
209                 c.front()->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
210         }
211 }
212
213 void
214 SubtitlePanel::y_offset_changed ()
215 {
216         SubtitleContentList c = _editor->selected_subtitle_content ();
217         if (c.size() == 1) {
218                 c.front()->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
219         }
220 }
221
222 void
223 SubtitlePanel::x_scale_changed ()
224 {
225         SubtitleContentList c = _editor->selected_subtitle_content ();
226         if (c.size() == 1) {
227                 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
228         }
229 }
230
231 void
232 SubtitlePanel::y_scale_changed ()
233 {
234         SubtitleContentList c = _editor->selected_subtitle_content ();
235         if (c.size() == 1) {
236                 c.front()->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
237         }
238 }
239
240 void
241 SubtitlePanel::content_selection_changed ()
242 {
243         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
244         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
245         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
246         film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
247         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
248 }