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"));
54 add_label_to_sizer (grid, this, _("X Offset"), true);
55 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
56 _x_offset = new wxSpinCtrl (this);
58 add_label_to_sizer (s, this, _("%"), false);
63 add_label_to_sizer (grid, this, _("Y Offset"), true);
64 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
65 _y_offset = new wxSpinCtrl (this);
67 add_label_to_sizer (s, this, _("%"), false);
72 add_label_to_sizer (grid, this, _("X Scale"), true);
73 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
74 _x_scale = new wxSpinCtrl (this);
76 add_label_to_sizer (s, this, _("%"), false);
81 add_label_to_sizer (grid, this, _("Y Scale"), true);
82 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
83 _y_scale = new wxSpinCtrl (this);
85 add_label_to_sizer (s, this, _("%"), false);
89 add_label_to_sizer (grid, this, _("Language"), true);
90 _language = new wxTextCtrl (this, wxID_ANY);
91 grid->Add (_language, 1, wxEXPAND);
93 add_label_to_sizer (grid, this, _("Stream"), true);
94 _stream = new wxChoice (this, wxID_ANY);
95 grid->Add (_stream, 1, wxEXPAND);
97 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
98 grid->Add (_subtitle_view_button);
101 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
102 grid->Add (_fonts_dialog_button);
105 _x_offset->SetRange (-100, 100);
106 _y_offset->SetRange (-100, 100);
107 _x_scale->SetRange (10, 1000);
108 _y_scale->SetRange (10, 1000);
110 _use->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
111 _x_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
112 _y_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
113 _x_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
114 _y_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
115 _language->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&SubtitlePanel::language_changed, this));
116 _stream->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&SubtitlePanel::stream_changed, this));
117 _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
118 _fonts_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
122 SubtitlePanel::film_changed (Film::Property property)
124 if (property == Film::CONTENT) {
125 setup_sensitivity ();
130 SubtitlePanel::film_content_changed (int property)
132 FFmpegContentList fc = _parent->selected_ffmpeg ();
133 SubtitleContentList sc = _parent->selected_subtitle ();
135 shared_ptr<FFmpegContent> fcs;
136 if (fc.size() == 1) {
140 shared_ptr<SubtitleContent> scs;
141 if (sc.size() == 1) {
145 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
148 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
149 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
150 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
153 if (fcs->subtitle_stream()) {
154 checked_set (_stream, fcs->subtitle_stream()->identifier ());
156 _stream->SetSelection (wxNOT_FOUND);
159 setup_sensitivity ();
160 } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
161 checked_set (_use, scs ? scs->use_subtitles() : false);
162 setup_sensitivity ();
163 } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
164 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
165 } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
166 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
167 } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
168 checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
169 } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
170 checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
171 } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
172 checked_set (_language, scs ? scs->subtitle_language() : "");
177 SubtitlePanel::use_toggled ()
179 SubtitleContentList c = _parent->selected_subtitle ();
180 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
181 (*i)->set_use_subtitles (_use->GetValue());
186 SubtitlePanel::setup_sensitivity ()
190 int subrip_or_dcp_subs = 0;
191 SubtitleContentList c = _parent->selected_subtitle ();
192 for (SubtitleContentList::const_iterator i = c.begin(); i != c.end(); ++i) {
193 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (*i);
194 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (*i);
195 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (*i);
197 if (fc->has_subtitles ()) {
201 } else if (sc || dsc) {
202 ++subrip_or_dcp_subs;
209 _use->Enable (any_subs > 0);
210 bool const use = _use->GetValue ();
212 _x_offset->Enable (any_subs > 0 && use);
213 _y_offset->Enable (any_subs > 0 && use);
214 _x_scale->Enable (any_subs > 0 && use);
215 _y_scale->Enable (any_subs > 0 && use);
216 _language->Enable (any_subs > 0 && use);
217 _stream->Enable (ffmpeg_subs == 1);
218 _subtitle_view_button->Enable (subrip_or_dcp_subs == 1);
219 _fonts_dialog_button->Enable (subrip_or_dcp_subs == 1);
223 SubtitlePanel::stream_changed ()
225 FFmpegContentList fc = _parent->selected_ffmpeg ();
226 if (fc.size() != 1) {
230 shared_ptr<FFmpegContent> fcs = fc.front ();
232 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
233 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
234 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
235 while (i != a.end() && (*i)->identifier () != s) {
240 fcs->set_subtitle_stream (*i);
245 SubtitlePanel::x_offset_changed ()
247 SubtitleContentList c = _parent->selected_subtitle ();
248 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
249 (*i)->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
254 SubtitlePanel::y_offset_changed ()
256 SubtitleContentList c = _parent->selected_subtitle ();
257 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
258 (*i)->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
263 SubtitlePanel::x_scale_changed ()
265 SubtitleContentList c = _parent->selected_subtitle ();
267 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
272 SubtitlePanel::y_scale_changed ()
274 SubtitleContentList c = _parent->selected_subtitle ();
275 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
276 (*i)->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
281 SubtitlePanel::language_changed ()
283 SubtitleContentList c = _parent->selected_subtitle ();
284 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
285 (*i)->set_subtitle_language (wx_to_std (_language->GetValue()));
290 SubtitlePanel::content_selection_changed ()
292 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
293 film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
294 film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
295 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
296 film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
297 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
298 film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
299 film_content_changed (SubtitleContentProperty::FONTS);
303 SubtitlePanel::subtitle_view_clicked ()
305 if (_subtitle_view) {
306 _subtitle_view->Destroy ();
310 SubtitleContentList c = _parent->selected_subtitle ();
311 DCPOMATIC_ASSERT (c.size() == 1);
313 shared_ptr<SubtitleDecoder> decoder;
315 shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
317 decoder.reset (new SubRipDecoder (sr));
320 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
322 decoder.reset (new DCPSubtitleDecoder (dc));
326 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
327 _subtitle_view->Show ();
332 SubtitlePanel::fonts_dialog_clicked ()
335 _fonts_dialog->Destroy ();
339 SubtitleContentList c = _parent->selected_subtitle ();
340 DCPOMATIC_ASSERT (c.size() == 1);
342 _fonts_dialog = new FontsDialog (this, c.front ());
343 _fonts_dialog->Show ();