2 Copyright (C) 2012-2014 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 <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"
31 #include "subtitle_view.h"
32 #include "content_panel.h"
33 #include "fonts_dialog.h"
37 using boost::shared_ptr;
38 using boost::lexical_cast;
39 using boost::dynamic_pointer_cast;
41 SubtitlePanel::SubtitlePanel (ContentPanel* p)
42 : ContentSubPanel (p, _("Subtitles"))
46 wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
47 _sizer->Add (grid, 0, wxALL, 8);
49 _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
53 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles"));
58 add_label_to_sizer (grid, this, _("X Offset"), true);
59 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
60 _x_offset = new wxSpinCtrl (this);
62 add_label_to_sizer (s, this, _("%"), false);
67 add_label_to_sizer (grid, this, _("Y Offset"), true);
68 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
69 _y_offset = new wxSpinCtrl (this);
71 add_label_to_sizer (s, this, _("%"), false);
76 add_label_to_sizer (grid, this, _("X Scale"), true);
77 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
78 _x_scale = new wxSpinCtrl (this);
80 add_label_to_sizer (s, this, _("%"), false);
85 add_label_to_sizer (grid, this, _("Y Scale"), true);
86 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
87 _y_scale = new wxSpinCtrl (this);
89 add_label_to_sizer (s, this, _("%"), false);
93 add_label_to_sizer (grid, this, _("Language"), true);
94 _language = new wxTextCtrl (this, wxID_ANY);
95 grid->Add (_language, 1, wxEXPAND);
97 add_label_to_sizer (grid, this, _("Stream"), true);
98 _stream = new wxChoice (this, wxID_ANY);
99 grid->Add (_stream, 1, wxEXPAND);
101 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
102 grid->Add (_subtitle_view_button);
105 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
106 grid->Add (_fonts_dialog_button);
109 _x_offset->SetRange (-100, 100);
110 _y_offset->SetRange (-100, 100);
111 _x_scale->SetRange (10, 1000);
112 _y_scale->SetRange (10, 1000);
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));
127 SubtitlePanel::film_changed (Film::Property property)
129 if (property == Film::CONTENT) {
130 setup_sensitivity ();
135 SubtitlePanel::film_content_changed (int property)
137 FFmpegContentList fc = _parent->selected_ffmpeg ();
138 SubtitleContentList sc = _parent->selected_subtitle ();
140 shared_ptr<FFmpegContent> fcs;
141 if (fc.size() == 1) {
145 shared_ptr<SubtitleContent> scs;
146 if (sc.size() == 1) {
150 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
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 ())));
158 if (fcs->subtitle_stream()) {
159 checked_set (_stream, fcs->subtitle_stream()->identifier ());
161 _stream->SetSelection (wxNOT_FOUND);
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() : "");
184 SubtitlePanel::use_toggled ()
186 SubtitleContentList c = _parent->selected_subtitle ();
187 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
188 (*i)->set_use_subtitles (_use->GetValue());
193 SubtitlePanel::burn_toggled ()
195 SubtitleContentList c = _parent->selected_subtitle ();
196 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
197 (*i)->set_burn_subtitles (_burn->GetValue());
202 SubtitlePanel::setup_sensitivity ()
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);
213 if (fc->has_subtitles ()) {
217 } else if (sc || dsc) {
218 ++subrip_or_dcp_subs;
225 _use->Enable (any_subs > 0);
226 bool const use = _use->GetValue ();
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);
240 SubtitlePanel::stream_changed ()
242 FFmpegContentList fc = _parent->selected_ffmpeg ();
243 if (fc.size() != 1) {
247 shared_ptr<FFmpegContent> fcs = fc.front ();
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) {
257 fcs->set_subtitle_stream (*i);
262 SubtitlePanel::x_offset_changed ()
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);
271 SubtitlePanel::y_offset_changed ()
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);
280 SubtitlePanel::x_scale_changed ()
282 SubtitleContentList c = _parent->selected_subtitle ();
284 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
289 SubtitlePanel::y_scale_changed ()
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);
298 SubtitlePanel::language_changed ()
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()));
307 SubtitlePanel::content_selection_changed ()
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);
321 SubtitlePanel::subtitle_view_clicked ()
323 if (_subtitle_view) {
324 _subtitle_view->Destroy ();
328 SubtitleContentList c = _parent->selected_subtitle ();
329 DCPOMATIC_ASSERT (c.size() == 1);
331 shared_ptr<SubtitleDecoder> decoder;
333 shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
335 decoder.reset (new SubRipDecoder (sr));
338 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
340 decoder.reset (new DCPSubtitleDecoder (dc));
344 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
345 _subtitle_view->Show ();
350 SubtitlePanel::fonts_dialog_clicked ()
353 _fonts_dialog->Destroy ();
357 SubtitleContentList c = _parent->selected_subtitle ();
358 DCPOMATIC_ASSERT (c.size() == 1);
360 _fonts_dialog = new FontsDialog (this, c.front ());
361 _fonts_dialog->Show ();