No-op: remove all trailing whitespace.
[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 "lib/subrip_content.h"
24 #include "lib/ffmpeg_subtitle_stream.h"
25 #include "lib/dcp_subtitle_content.h"
26 #include "lib/subrip_decoder.h"
27 #include "lib/dcp_subtitle_decoder.h"
28 #include "subtitle_panel.h"
29 #include "film_editor.h"
30 #include "wx_util.h"
31 #include "subtitle_view.h"
32 #include "content_panel.h"
33 #include "fonts_dialog.h"
34
35 using std::vector;
36 using std::string;
37 using boost::shared_ptr;
38 using boost::lexical_cast;
39 using boost::dynamic_pointer_cast;
40
41 SubtitlePanel::SubtitlePanel (ContentPanel* p)
42         : ContentSubPanel (p, _("Subtitles"))
43         , _subtitle_view (0)
44         , _fonts_dialog (0)
45 {
46         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
47         _sizer->Add (grid, 0, wxALL, 8);
48
49         _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
50         grid->Add (_use);
51         grid->AddSpacer (0);
52
53         {
54                 add_label_to_sizer (grid, this, _("X Offset"), true);
55                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
56                 _x_offset = new wxSpinCtrl (this);
57                 s->Add (_x_offset);
58                 add_label_to_sizer (s, this, _("%"), false);
59                 grid->Add (s);
60         }
61
62         {
63                 add_label_to_sizer (grid, this, _("Y Offset"), true);
64                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
65                 _y_offset = new wxSpinCtrl (this);
66                 s->Add (_y_offset);
67                 add_label_to_sizer (s, this, _("%"), false);
68                 grid->Add (s);
69         }
70
71         {
72                 add_label_to_sizer (grid, this, _("X Scale"), true);
73                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
74                 _x_scale = new wxSpinCtrl (this);
75                 s->Add (_x_scale);
76                 add_label_to_sizer (s, this, _("%"), false);
77                 grid->Add (s);
78         }
79
80         {
81                 add_label_to_sizer (grid, this, _("Y Scale"), true);
82                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
83                 _y_scale = new wxSpinCtrl (this);
84                 s->Add (_y_scale);
85                 add_label_to_sizer (s, this, _("%"), false);
86                 grid->Add (s);
87         }
88
89         add_label_to_sizer (grid, this, _("Language"), true);
90         _language = new wxTextCtrl (this, wxID_ANY);
91         grid->Add (_language, 1, wxEXPAND);
92
93         add_label_to_sizer (grid, this, _("Stream"), true);
94         _stream = new wxChoice (this, wxID_ANY);
95         grid->Add (_stream, 1, wxEXPAND);
96
97         _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
98         grid->Add (_subtitle_view_button);
99         grid->AddSpacer (0);
100
101         _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
102         grid->Add (_fonts_dialog_button);
103         grid->AddSpacer (0);
104
105         _x_offset->SetRange (-100, 100);
106         _y_offset->SetRange (-100, 100);
107         _x_scale->SetRange (10, 1000);
108         _y_scale->SetRange (10, 1000);
109
110         _use->Bind                  (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
111         _x_offset->Bind             (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
112         _y_offset->Bind             (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
113         _x_scale->Bind              (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
114         _y_scale->Bind              (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
115         _language->Bind             (wxEVT_COMMAND_TEXT_UPDATED,     boost::bind (&SubtitlePanel::language_changed, this));
116         _stream->Bind               (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
117         _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
118         _fonts_dialog_button->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
119 }
120
121 void
122 SubtitlePanel::film_changed (Film::Property property)
123 {
124         if (property == Film::CONTENT) {
125                 setup_sensitivity ();
126         }
127 }
128
129 void
130 SubtitlePanel::film_content_changed (int property)
131 {
132         FFmpegContentList fc = _parent->selected_ffmpeg ();
133         SubtitleContentList sc = _parent->selected_subtitle ();
134
135         shared_ptr<FFmpegContent> fcs;
136         if (fc.size() == 1) {
137                 fcs = fc.front ();
138         }
139
140         shared_ptr<SubtitleContent> scs;
141         if (sc.size() == 1) {
142                 scs = sc.front ();
143         }
144
145         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
146                 _stream->Clear ();
147                 if (fcs) {
148                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
149                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
150                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
151                         }
152
153                         if (fcs->subtitle_stream()) {
154                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
155                         } else {
156                                 _stream->SetSelection (wxNOT_FOUND);
157                         }
158                 }
159                 setup_sensitivity ();
160         } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
161                 checked_set (_use, scs ? scs->use_subtitles() : false);
162                 setup_sensitivity ();
163         } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
164                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
165         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
166                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
167         } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
168                 checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
169         } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
170                 checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
171         } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
172                 checked_set (_language, scs ? scs->subtitle_language() : "");
173         }
174 }
175
176 void
177 SubtitlePanel::use_toggled ()
178 {
179         SubtitleContentList c = _parent->selected_subtitle ();
180         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
181                 (*i)->set_use_subtitles (_use->GetValue());
182         }
183 }
184
185 void
186 SubtitlePanel::setup_sensitivity ()
187 {
188         int any_subs = 0;
189         int ffmpeg_subs = 0;
190         int subrip_or_dcp_subs = 0;
191         SubtitleContentList c = _parent->selected_subtitle ();
192         for (SubtitleContentList::const_iterator i = c.begin(); i != c.end(); ++i) {
193                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (*i);
194                 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (*i);
195                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (*i);
196                 if (fc) {
197                         if (fc->has_subtitles ()) {
198                                 ++ffmpeg_subs;
199                                 ++any_subs;
200                         }
201                 } else if (sc || dsc) {
202                         ++subrip_or_dcp_subs;
203                         ++any_subs;
204                 } else {
205                         ++any_subs;
206                 }
207         }
208
209         _use->Enable (any_subs > 0);
210         bool const use = _use->GetValue ();
211
212         _x_offset->Enable (any_subs > 0 && use);
213         _y_offset->Enable (any_subs > 0 && use);
214         _x_scale->Enable (any_subs > 0 && use);
215         _y_scale->Enable (any_subs > 0 && use);
216         _language->Enable (any_subs > 0 && use);
217         _stream->Enable (ffmpeg_subs == 1);
218         _subtitle_view_button->Enable (subrip_or_dcp_subs == 1);
219         _fonts_dialog_button->Enable (subrip_or_dcp_subs == 1);
220 }
221
222 void
223 SubtitlePanel::stream_changed ()
224 {
225         FFmpegContentList fc = _parent->selected_ffmpeg ();
226         if (fc.size() != 1) {
227                 return;
228         }
229
230         shared_ptr<FFmpegContent> fcs = fc.front ();
231
232         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
233         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
234         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
235         while (i != a.end() && (*i)->identifier () != s) {
236                 ++i;
237         }
238
239         if (i != a.end ()) {
240                 fcs->set_subtitle_stream (*i);
241         }
242 }
243
244 void
245 SubtitlePanel::x_offset_changed ()
246 {
247         SubtitleContentList c = _parent->selected_subtitle ();
248         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
249                 (*i)->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
250         }
251 }
252
253 void
254 SubtitlePanel::y_offset_changed ()
255 {
256         SubtitleContentList c = _parent->selected_subtitle ();
257         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
258                 (*i)->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
259         }
260 }
261
262 void
263 SubtitlePanel::x_scale_changed ()
264 {
265         SubtitleContentList c = _parent->selected_subtitle ();
266         if (c.size() == 1) {
267                 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
268         }
269 }
270
271 void
272 SubtitlePanel::y_scale_changed ()
273 {
274         SubtitleContentList c = _parent->selected_subtitle ();
275         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
276                 (*i)->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
277         }
278 }
279
280 void
281 SubtitlePanel::language_changed ()
282 {
283         SubtitleContentList c = _parent->selected_subtitle ();
284         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
285                 (*i)->set_subtitle_language (wx_to_std (_language->GetValue()));
286         }
287 }
288
289 void
290 SubtitlePanel::content_selection_changed ()
291 {
292         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
293         film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
294         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
295         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
296         film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
297         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
298         film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
299         film_content_changed (SubtitleContentProperty::FONTS);
300 }
301
302 void
303 SubtitlePanel::subtitle_view_clicked ()
304 {
305         if (_subtitle_view) {
306                 _subtitle_view->Destroy ();
307                 _subtitle_view = 0;
308         }
309
310         SubtitleContentList c = _parent->selected_subtitle ();
311         DCPOMATIC_ASSERT (c.size() == 1);
312
313         shared_ptr<SubtitleDecoder> decoder;
314
315         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
316         if (sr) {
317                 decoder.reset (new SubRipDecoder (sr));
318         }
319
320         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
321         if (dc) {
322                 decoder.reset (new DCPSubtitleDecoder (dc));
323         }
324
325         if (decoder) {
326                 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
327                 _subtitle_view->Show ();
328         }
329 }
330
331 void
332 SubtitlePanel::fonts_dialog_clicked ()
333 {
334         if (_fonts_dialog) {
335                 _fonts_dialog->Destroy ();
336                 _fonts_dialog = 0;
337         }
338
339         SubtitleContentList c = _parent->selected_subtitle ();
340         DCPOMATIC_ASSERT (c.size() == 1);
341
342         _fonts_dialog = new FontsDialog (this, c.front ());
343         _fonts_dialog->Show ();
344 }