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