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 <wx/spinctrl.h>
33 #include <boost/lexical_cast.hpp>
34 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
39 using boost::lexical_cast;
40 using boost::dynamic_pointer_cast;
42 SubtitlePanel::SubtitlePanel (ContentPanel* p)
43 : ContentSubPanel (p, _("Subtitles"))
47 wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
48 _sizer->Add (grid, 0, wxALL, 8);
50 _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
54 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
59 add_label_to_sizer (grid, this, _("X Offset"), true);
60 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
61 _x_offset = new wxSpinCtrl (this);
63 add_label_to_sizer (s, this, _("%"), false);
68 add_label_to_sizer (grid, this, _("Y Offset"), true);
69 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
70 _y_offset = new wxSpinCtrl (this);
72 add_label_to_sizer (s, this, _("%"), false);
77 add_label_to_sizer (grid, this, _("X Scale"), true);
78 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
79 _x_scale = new wxSpinCtrl (this);
81 add_label_to_sizer (s, this, _("%"), false);
86 add_label_to_sizer (grid, this, _("Y Scale"), true);
87 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
88 _y_scale = new wxSpinCtrl (this);
90 add_label_to_sizer (s, this, _("%"), false);
94 add_label_to_sizer (grid, this, _("Language"), true);
95 _language = new wxTextCtrl (this, wxID_ANY);
96 grid->Add (_language, 1, wxEXPAND);
98 add_label_to_sizer (grid, this, _("Stream"), true);
99 _stream = new wxChoice (this, wxID_ANY);
100 grid->Add (_stream, 1, wxEXPAND);
102 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
103 grid->Add (_subtitle_view_button);
106 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
107 grid->Add (_fonts_dialog_button);
110 _x_offset->SetRange (-100, 100);
111 _y_offset->SetRange (-100, 100);
112 _x_scale->SetRange (10, 1000);
113 _y_scale->SetRange (10, 1000);
115 _use->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
116 _burn->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
117 _x_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
118 _y_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
119 _x_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
120 _y_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
121 _language->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&SubtitlePanel::language_changed, this));
122 _stream->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&SubtitlePanel::stream_changed, this));
123 _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
124 _fonts_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
128 SubtitlePanel::film_changed (Film::Property property)
130 if (property == Film::CONTENT) {
131 setup_sensitivity ();
136 SubtitlePanel::film_content_changed (int property)
138 FFmpegContentList fc = _parent->selected_ffmpeg ();
139 SubtitleContentList sc = _parent->selected_subtitle ();
141 shared_ptr<FFmpegContent> fcs;
142 if (fc.size() == 1) {
146 shared_ptr<SubtitleContent> scs;
147 if (sc.size() == 1) {
151 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
154 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
155 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
156 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
159 if (fcs->subtitle_stream()) {
160 checked_set (_stream, fcs->subtitle_stream()->identifier ());
162 _stream->SetSelection (wxNOT_FOUND);
165 setup_sensitivity ();
166 } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
167 checked_set (_use, scs ? scs->use_subtitles() : false);
168 setup_sensitivity ();
169 } else if (property == SubtitleContentProperty::BURN_SUBTITLES) {
170 checked_set (_burn, scs ? scs->burn_subtitles() : false);
171 } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
172 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
173 } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
174 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
175 } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
176 checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
177 } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
178 checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
179 } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
180 checked_set (_language, scs ? scs->subtitle_language() : "");
185 SubtitlePanel::use_toggled ()
187 SubtitleContentList c = _parent->selected_subtitle ();
188 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
189 (*i)->set_use_subtitles (_use->GetValue());
194 SubtitlePanel::burn_toggled ()
196 SubtitleContentList c = _parent->selected_subtitle ();
197 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
198 (*i)->set_burn_subtitles (_burn->GetValue());
203 SubtitlePanel::setup_sensitivity ()
207 int subrip_or_dcp_subs = 0;
209 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
210 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
211 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (i);
212 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
214 if (fc->has_subtitles ()) {
218 } else if (sc || dsc) {
219 ++subrip_or_dcp_subs;
225 if (i->has_image_subtitles ()) {
227 /* We must burn image subtitles at the moment */
228 i->set_burn_subtitles (true);
232 _use->Enable (any_subs > 0);
233 bool const use = _use->GetValue ();
234 _burn->Enable (any_subs > 0 && use && image_subs == 0);
235 _x_offset->Enable (any_subs > 0 && use);
236 _y_offset->Enable (any_subs > 0 && use);
237 _x_scale->Enable (any_subs > 0 && use);
238 _y_scale->Enable (any_subs > 0 && use);
239 _language->Enable (any_subs > 0 && use);
240 _stream->Enable (ffmpeg_subs == 1);
241 _subtitle_view_button->Enable (subrip_or_dcp_subs == 1);
242 _fonts_dialog_button->Enable (subrip_or_dcp_subs == 1);
246 SubtitlePanel::stream_changed ()
248 FFmpegContentList fc = _parent->selected_ffmpeg ();
249 if (fc.size() != 1) {
253 shared_ptr<FFmpegContent> fcs = fc.front ();
255 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
256 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
257 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
258 while (i != a.end() && (*i)->identifier () != s) {
263 fcs->set_subtitle_stream (*i);
268 SubtitlePanel::x_offset_changed ()
270 SubtitleContentList c = _parent->selected_subtitle ();
271 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
272 (*i)->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
277 SubtitlePanel::y_offset_changed ()
279 SubtitleContentList c = _parent->selected_subtitle ();
280 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
281 (*i)->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
286 SubtitlePanel::x_scale_changed ()
288 SubtitleContentList c = _parent->selected_subtitle ();
290 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
295 SubtitlePanel::y_scale_changed ()
297 SubtitleContentList c = _parent->selected_subtitle ();
298 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
299 (*i)->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
304 SubtitlePanel::language_changed ()
306 SubtitleContentList c = _parent->selected_subtitle ();
307 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
308 (*i)->set_subtitle_language (wx_to_std (_language->GetValue()));
313 SubtitlePanel::content_selection_changed ()
315 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
316 film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
317 film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
318 film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
319 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
320 film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
321 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
322 film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
323 film_content_changed (SubtitleContentProperty::FONTS);
327 SubtitlePanel::subtitle_view_clicked ()
329 if (_subtitle_view) {
330 _subtitle_view->Destroy ();
334 SubtitleContentList c = _parent->selected_subtitle ();
335 DCPOMATIC_ASSERT (c.size() == 1);
337 shared_ptr<SubtitleDecoder> decoder;
339 shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
341 decoder.reset (new SubRipDecoder (sr));
344 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
346 decoder.reset (new DCPSubtitleDecoder (dc));
350 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
351 _subtitle_view->Show ();
356 SubtitlePanel::fonts_dialog_clicked ()
359 _fonts_dialog->Destroy ();
363 SubtitleContentList c = _parent->selected_subtitle ();
364 DCPOMATIC_ASSERT (c.size() == 1);
366 _fonts_dialog = new FontsDialog (this, c.front ());
367 _fonts_dialog->Show ();