2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "subtitle_panel.h"
21 #include "film_editor.h"
23 #include "subtitle_view.h"
24 #include "content_panel.h"
25 #include "fonts_dialog.h"
26 #include "lib/ffmpeg_content.h"
27 #include "lib/subrip_content.h"
28 #include "lib/ffmpeg_subtitle_stream.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/subrip_decoder.h"
31 #include "lib/dcp_subtitle_decoder.h"
32 #include "lib/dcp_content.h"
33 #include <wx/spinctrl.h>
34 #include <boost/lexical_cast.hpp>
35 #include <boost/foreach.hpp>
39 using boost::shared_ptr;
40 using boost::lexical_cast;
41 using boost::dynamic_pointer_cast;
43 SubtitlePanel::SubtitlePanel (ContentPanel* p)
44 : ContentSubPanel (p, _("Subtitles"))
48 wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
49 _sizer->Add (grid, 0, wxALL, 8);
51 _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
52 grid->Add (_reference);
55 _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
59 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
64 add_label_to_sizer (grid, this, _("X Offset"), true);
65 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
66 _x_offset = new wxSpinCtrl (this);
68 add_label_to_sizer (s, this, _("%"), false);
73 add_label_to_sizer (grid, this, _("Y Offset"), true);
74 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
75 _y_offset = new wxSpinCtrl (this);
77 add_label_to_sizer (s, this, _("%"), false);
82 add_label_to_sizer (grid, this, _("X Scale"), true);
83 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
84 _x_scale = new wxSpinCtrl (this);
86 add_label_to_sizer (s, this, _("%"), false);
91 add_label_to_sizer (grid, this, _("Y Scale"), true);
92 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
93 _y_scale = new wxSpinCtrl (this);
95 add_label_to_sizer (s, this, _("%"), false);
99 add_label_to_sizer (grid, this, _("Language"), true);
100 _language = new wxTextCtrl (this, wxID_ANY);
101 grid->Add (_language, 1, wxEXPAND);
103 add_label_to_sizer (grid, this, _("Stream"), true);
104 _stream = new wxChoice (this, wxID_ANY);
105 grid->Add (_stream, 1, wxEXPAND);
107 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
108 grid->Add (_subtitle_view_button);
111 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
112 grid->Add (_fonts_dialog_button);
115 _x_offset->SetRange (-100, 100);
116 _y_offset->SetRange (-100, 100);
117 _x_scale->SetRange (10, 1000);
118 _y_scale->SetRange (10, 1000);
120 _reference->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::reference_clicked, this));
121 _use->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
122 _burn->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
123 _x_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
124 _y_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
125 _x_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
126 _y_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
127 _language->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&SubtitlePanel::language_changed, this));
128 _stream->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&SubtitlePanel::stream_changed, this));
129 _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
130 _fonts_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
134 SubtitlePanel::film_changed (Film::Property property)
136 if (property == Film::CONTENT) {
137 setup_sensitivity ();
142 SubtitlePanel::film_content_changed (int property)
144 FFmpegContentList fc = _parent->selected_ffmpeg ();
145 SubtitleContentList sc = _parent->selected_subtitle ();
147 shared_ptr<FFmpegContent> fcs;
148 if (fc.size() == 1) {
152 shared_ptr<SubtitleContent> scs;
153 if (sc.size() == 1) {
157 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
160 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
161 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
162 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
165 if (fcs->subtitle_stream()) {
166 checked_set (_stream, fcs->subtitle_stream()->identifier ());
168 _stream->SetSelection (wxNOT_FOUND);
171 setup_sensitivity ();
172 } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
173 checked_set (_use, scs ? scs->use_subtitles() : false);
174 setup_sensitivity ();
175 } else if (property == SubtitleContentProperty::BURN_SUBTITLES) {
176 checked_set (_burn, scs ? scs->burn_subtitles() : false);
177 } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
178 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
179 } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
180 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
181 } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
182 checked_set (_x_scale, scs ? lrint (scs->subtitle_x_scale() * 100) : 100);
183 } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
184 checked_set (_y_scale, scs ? lrint (scs->subtitle_y_scale() * 100) : 100);
185 } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
186 checked_set (_language, scs ? scs->subtitle_language() : "");
187 } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
189 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
190 checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
192 checked_set (_reference, false);
195 setup_sensitivity ();
200 SubtitlePanel::use_toggled ()
202 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
203 i->set_use_subtitles (_use->GetValue());
208 SubtitlePanel::burn_toggled ()
210 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
211 i->set_burn_subtitles (_burn->GetValue());
216 SubtitlePanel::setup_sensitivity ()
220 int subrip_or_dcp_subs = 0;
222 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
223 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
224 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (i);
225 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
227 if (fc->has_subtitles ()) {
231 } else if (sc || dsc) {
232 ++subrip_or_dcp_subs;
238 if (i->has_image_subtitles ()) {
240 /* We must burn image subtitles at the moment */
241 i->set_burn_subtitles (true);
245 bool const reference = _reference->GetValue ();
247 _use->Enable (!reference && any_subs > 0);
248 bool const use = _use->GetValue ();
249 _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
250 _x_offset->Enable (!reference && any_subs > 0 && use);
251 _y_offset->Enable (!reference && any_subs > 0 && use);
252 _x_scale->Enable (!reference && any_subs > 0 && use);
253 _y_scale->Enable (!reference && any_subs > 0 && use);
254 _language->Enable (!reference && any_subs > 0 && use);
255 _stream->Enable (!reference && ffmpeg_subs == 1);
256 _subtitle_view_button->Enable (!reference && subrip_or_dcp_subs == 1);
257 _fonts_dialog_button->Enable (!reference && subrip_or_dcp_subs == 1);
259 ContentList sel = _parent->selected ();
260 _reference->Enable (sel.size() == 1 && dynamic_pointer_cast<DCPContent> (sel.front ()));
264 SubtitlePanel::stream_changed ()
266 FFmpegContentList fc = _parent->selected_ffmpeg ();
267 if (fc.size() != 1) {
271 shared_ptr<FFmpegContent> fcs = fc.front ();
273 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
274 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
275 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
276 while (i != a.end() && (*i)->identifier () != s) {
281 fcs->set_subtitle_stream (*i);
286 SubtitlePanel::x_offset_changed ()
288 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
289 i->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
294 SubtitlePanel::y_offset_changed ()
296 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
297 i->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
302 SubtitlePanel::x_scale_changed ()
304 SubtitleContentList c = _parent->selected_subtitle ();
306 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
311 SubtitlePanel::y_scale_changed ()
313 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
314 i->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
319 SubtitlePanel::language_changed ()
321 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
322 i->set_subtitle_language (wx_to_std (_language->GetValue()));
327 SubtitlePanel::content_selection_changed ()
329 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
330 film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
331 film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
332 film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
333 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
334 film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
335 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
336 film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
337 film_content_changed (SubtitleContentProperty::FONTS);
338 film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
342 SubtitlePanel::subtitle_view_clicked ()
344 if (_subtitle_view) {
345 _subtitle_view->Destroy ();
349 SubtitleContentList c = _parent->selected_subtitle ();
350 DCPOMATIC_ASSERT (c.size() == 1);
352 shared_ptr<SubtitleDecoder> decoder;
354 shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
356 decoder.reset (new SubRipDecoder (sr));
359 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
361 decoder.reset (new DCPSubtitleDecoder (dc));
365 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
366 _subtitle_view->Show ();
371 SubtitlePanel::fonts_dialog_clicked ()
374 _fonts_dialog->Destroy ();
378 SubtitleContentList c = _parent->selected_subtitle ();
379 DCPOMATIC_ASSERT (c.size() == 1);
381 _fonts_dialog = new FontsDialog (this, c.front ());
382 _fonts_dialog->Show ();
386 SubtitlePanel::reference_clicked ()
388 ContentList c = _parent->selected ();
393 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
398 d->set_reference_subtitle (_reference->GetValue ());