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>
40 using boost::shared_ptr;
41 using boost::lexical_cast;
42 using boost::dynamic_pointer_cast;
44 SubtitlePanel::SubtitlePanel (ContentPanel* p)
45 : ContentSubPanel (p, _("Subtitles"))
49 wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
50 _sizer->Add (grid, 0, wxALL, 8);
52 _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
53 grid->Add (_reference);
56 _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
60 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
65 add_label_to_sizer (grid, this, _("X Offset"), true);
66 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
67 _x_offset = new wxSpinCtrl (this);
69 add_label_to_sizer (s, this, _("%"), false);
74 add_label_to_sizer (grid, this, _("Y Offset"), true);
75 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
76 _y_offset = new wxSpinCtrl (this);
78 add_label_to_sizer (s, this, _("%"), false);
83 add_label_to_sizer (grid, this, _("X Scale"), true);
84 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
85 _x_scale = new wxSpinCtrl (this);
87 add_label_to_sizer (s, this, _("%"), false);
92 add_label_to_sizer (grid, this, _("Y Scale"), true);
93 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
94 _y_scale = new wxSpinCtrl (this);
96 add_label_to_sizer (s, this, _("%"), false);
100 add_label_to_sizer (grid, this, _("Language"), true);
101 _language = new wxTextCtrl (this, wxID_ANY);
102 grid->Add (_language, 1, wxEXPAND);
104 add_label_to_sizer (grid, this, _("Stream"), true);
105 _stream = new wxChoice (this, wxID_ANY);
106 grid->Add (_stream, 1, wxEXPAND);
108 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
109 grid->Add (_subtitle_view_button);
112 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
113 grid->Add (_fonts_dialog_button);
116 _x_offset->SetRange (-100, 100);
117 _y_offset->SetRange (-100, 100);
118 _x_scale->SetRange (10, 1000);
119 _y_scale->SetRange (10, 1000);
121 _reference->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::reference_clicked, this));
122 _use->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
123 _burn->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
124 _x_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
125 _y_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
126 _x_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
127 _y_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
128 _language->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&SubtitlePanel::language_changed, this));
129 _stream->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&SubtitlePanel::stream_changed, this));
130 _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
131 _fonts_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
135 SubtitlePanel::film_changed (Film::Property property)
137 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
138 setup_sensitivity ();
143 SubtitlePanel::film_content_changed (int property)
145 FFmpegContentList fc = _parent->selected_ffmpeg ();
146 SubtitleContentList sc = _parent->selected_subtitle ();
148 shared_ptr<FFmpegContent> fcs;
149 if (fc.size() == 1) {
153 shared_ptr<SubtitleContent> scs;
154 if (sc.size() == 1) {
158 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
161 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
162 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
163 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
166 if (fcs->subtitle_stream()) {
167 checked_set (_stream, fcs->subtitle_stream()->identifier ());
169 _stream->SetSelection (wxNOT_FOUND);
172 setup_sensitivity ();
173 } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
174 checked_set (_use, scs ? scs->use_subtitles() : false);
175 setup_sensitivity ();
176 } else if (property == SubtitleContentProperty::BURN_SUBTITLES) {
177 checked_set (_burn, scs ? scs->burn_subtitles() : false);
178 } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
179 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
180 } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
181 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
182 } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
183 checked_set (_x_scale, scs ? lrint (scs->subtitle_x_scale() * 100) : 100);
184 } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
185 checked_set (_y_scale, scs ? lrint (scs->subtitle_y_scale() * 100) : 100);
186 } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
187 checked_set (_language, scs ? scs->subtitle_language() : "");
188 } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
190 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
191 checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
193 checked_set (_reference, false);
196 setup_sensitivity ();
201 SubtitlePanel::use_toggled ()
203 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
204 i->set_use_subtitles (_use->GetValue());
209 SubtitlePanel::burn_toggled ()
211 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
212 i->set_burn_subtitles (_burn->GetValue());
217 SubtitlePanel::setup_sensitivity ()
221 int subrip_or_dcp_subs = 0;
223 SubtitleContentList sel = _parent->selected_subtitle ();
224 BOOST_FOREACH (shared_ptr<SubtitleContent> i, sel) {
225 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
226 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (i);
227 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
229 if (fc->has_subtitles ()) {
233 } else if (sc || dsc) {
234 ++subrip_or_dcp_subs;
240 if (i->has_image_subtitles ()) {
242 /* We must burn image subtitles at the moment */
243 i->set_burn_subtitles (true);
247 shared_ptr<DCPContent> dcp;
248 if (sel.size() == 1) {
249 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
252 list<string> why_not;
253 bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
254 _reference->Enable (can_reference);
257 if (!can_reference) {
258 s = _("Cannot reference this DCP. ");
259 BOOST_FOREACH (string i, why_not) {
260 s += std_to_wx(i) + wxT(" ");
263 _reference->SetToolTip (s);
265 bool const reference = _reference->GetValue ();
267 _use->Enable (!reference && any_subs > 0);
268 bool const use = _use->GetValue ();
269 _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
270 _x_offset->Enable (!reference && any_subs > 0 && use);
271 _y_offset->Enable (!reference && any_subs > 0 && use);
272 _x_scale->Enable (!reference && any_subs > 0 && use);
273 _y_scale->Enable (!reference && any_subs > 0 && use);
274 _language->Enable (!reference && any_subs > 0 && use);
275 _stream->Enable (!reference && ffmpeg_subs == 1);
276 _subtitle_view_button->Enable (!reference && subrip_or_dcp_subs == 1);
277 _fonts_dialog_button->Enable (!reference && subrip_or_dcp_subs == 1);
281 SubtitlePanel::stream_changed ()
283 FFmpegContentList fc = _parent->selected_ffmpeg ();
284 if (fc.size() != 1) {
288 shared_ptr<FFmpegContent> fcs = fc.front ();
290 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
291 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
292 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
293 while (i != a.end() && (*i)->identifier () != s) {
298 fcs->set_subtitle_stream (*i);
303 SubtitlePanel::x_offset_changed ()
305 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
306 i->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
311 SubtitlePanel::y_offset_changed ()
313 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
314 i->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
319 SubtitlePanel::x_scale_changed ()
321 SubtitleContentList c = _parent->selected_subtitle ();
323 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
328 SubtitlePanel::y_scale_changed ()
330 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
331 i->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
336 SubtitlePanel::language_changed ()
338 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
339 i->set_subtitle_language (wx_to_std (_language->GetValue()));
344 SubtitlePanel::content_selection_changed ()
346 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
347 film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
348 film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
349 film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
350 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
351 film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
352 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
353 film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
354 film_content_changed (SubtitleContentProperty::FONTS);
355 film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
359 SubtitlePanel::subtitle_view_clicked ()
361 if (_subtitle_view) {
362 _subtitle_view->Destroy ();
366 SubtitleContentList c = _parent->selected_subtitle ();
367 DCPOMATIC_ASSERT (c.size() == 1);
369 shared_ptr<SubtitleDecoder> decoder;
371 shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
373 decoder.reset (new SubRipDecoder (sr));
376 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
378 decoder.reset (new DCPSubtitleDecoder (dc));
382 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
383 _subtitle_view->Show ();
388 SubtitlePanel::fonts_dialog_clicked ()
391 _fonts_dialog->Destroy ();
395 SubtitleContentList c = _parent->selected_subtitle ();
396 DCPOMATIC_ASSERT (c.size() == 1);
398 _fonts_dialog = new FontsDialog (this, c.front ());
399 _fonts_dialog->Show ();
403 SubtitlePanel::reference_clicked ()
405 ContentList c = _parent->selected ();
410 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
415 d->set_reference_subtitle (_reference->GetValue ());