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"
36 using boost::shared_ptr;
37 using boost::lexical_cast;
38 using boost::dynamic_pointer_cast;
40 SubtitlePanel::SubtitlePanel (ContentPanel* p)
41 : ContentSubPanel (p, _("Subtitles"))
44 wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45 _sizer->Add (grid, 0, wxALL, 8);
47 _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
52 add_label_to_sizer (grid, this, _("X Offset"), true);
53 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
54 _x_offset = new wxSpinCtrl (this);
56 add_label_to_sizer (s, this, _("%"), false);
61 add_label_to_sizer (grid, this, _("Y Offset"), true);
62 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
63 _y_offset = new wxSpinCtrl (this);
65 add_label_to_sizer (s, this, _("%"), false);
70 add_label_to_sizer (grid, this, _("X Scale"), true);
71 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
72 _x_scale = new wxSpinCtrl (this);
74 add_label_to_sizer (s, this, _("%"), false);
79 add_label_to_sizer (grid, this, _("Y Scale"), true);
80 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
81 _y_scale = new wxSpinCtrl (this);
83 add_label_to_sizer (s, this, _("%"), false);
87 add_label_to_sizer (grid, this, _("Language"), true);
88 _language = new wxTextCtrl (this, wxID_ANY);
89 grid->Add (_language, 1, wxEXPAND);
91 add_label_to_sizer (grid, this, _("Stream"), true);
92 _stream = new wxChoice (this, wxID_ANY);
93 grid->Add (_stream, 1, wxEXPAND);
95 _view_button = new wxButton (this, wxID_ANY, _("View..."));
96 grid->Add (_view_button);
98 _x_offset->SetRange (-100, 100);
99 _y_offset->SetRange (-100, 100);
100 _x_scale->SetRange (10, 1000);
101 _y_scale->SetRange (10, 1000);
103 _use->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
104 _x_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
105 _y_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
106 _x_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
107 _y_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
108 _language->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&SubtitlePanel::language_changed, this));
109 _stream->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&SubtitlePanel::stream_changed, this));
110 _view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::view_clicked, this));
114 SubtitlePanel::film_changed (Film::Property property)
116 if (property == Film::CONTENT) {
117 setup_sensitivity ();
122 SubtitlePanel::film_content_changed (int property)
124 FFmpegContentList fc = _parent->selected_ffmpeg ();
125 SubtitleContentList sc = _parent->selected_subtitle ();
127 shared_ptr<FFmpegContent> fcs;
128 if (fc.size() == 1) {
132 shared_ptr<SubtitleContent> scs;
133 if (sc.size() == 1) {
137 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
140 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
141 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
142 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
145 if (fcs->subtitle_stream()) {
146 checked_set (_stream, fcs->subtitle_stream()->identifier ());
148 _stream->SetSelection (wxNOT_FOUND);
151 setup_sensitivity ();
152 } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
153 checked_set (_use, scs ? scs->use_subtitles() : false);
154 setup_sensitivity ();
155 } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
156 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
157 } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
158 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
159 } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
160 checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
161 } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
162 checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
163 } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
164 checked_set (_language, scs ? scs->subtitle_language() : "");
169 SubtitlePanel::use_toggled ()
171 SubtitleContentList c = _parent->selected_subtitle ();
172 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
173 (*i)->set_use_subtitles (_use->GetValue());
178 SubtitlePanel::setup_sensitivity ()
182 int subrip_or_dcp_subs = 0;
183 SubtitleContentList c = _parent->selected_subtitle ();
184 for (SubtitleContentList::const_iterator i = c.begin(); i != c.end(); ++i) {
185 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (*i);
186 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (*i);
187 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (*i);
189 if (fc->has_subtitles ()) {
193 } else if (sc || dsc) {
194 ++subrip_or_dcp_subs;
201 _use->Enable (any_subs > 0);
202 bool const use = _use->GetValue ();
204 _x_offset->Enable (any_subs > 0 && use);
205 _y_offset->Enable (any_subs > 0 && use);
206 _x_scale->Enable (any_subs > 0 && use);
207 _y_scale->Enable (any_subs > 0 && use);
208 _language->Enable (any_subs > 0 && use);
209 _stream->Enable (ffmpeg_subs == 1);
210 _view_button->Enable (subrip_or_dcp_subs == 1);
214 SubtitlePanel::stream_changed ()
216 FFmpegContentList fc = _parent->selected_ffmpeg ();
217 if (fc.size() != 1) {
221 shared_ptr<FFmpegContent> fcs = fc.front ();
223 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
224 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
225 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
226 while (i != a.end() && (*i)->identifier () != s) {
231 fcs->set_subtitle_stream (*i);
236 SubtitlePanel::x_offset_changed ()
238 SubtitleContentList c = _parent->selected_subtitle ();
239 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
240 (*i)->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
245 SubtitlePanel::y_offset_changed ()
247 SubtitleContentList c = _parent->selected_subtitle ();
248 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
249 (*i)->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
254 SubtitlePanel::x_scale_changed ()
256 SubtitleContentList c = _parent->selected_subtitle ();
258 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
263 SubtitlePanel::y_scale_changed ()
265 SubtitleContentList c = _parent->selected_subtitle ();
266 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
267 (*i)->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
272 SubtitlePanel::language_changed ()
274 SubtitleContentList c = _parent->selected_subtitle ();
275 for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
276 (*i)->set_subtitle_language (wx_to_std (_language->GetValue()));
281 SubtitlePanel::content_selection_changed ()
283 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
284 film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
285 film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
286 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
287 film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
288 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
289 film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
293 SubtitlePanel::view_clicked ()
300 SubtitleContentList c = _parent->selected_subtitle ();
301 assert (c.size() == 1);
303 shared_ptr<SubtitleDecoder> decoder;
305 shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
307 decoder.reset (new SubRipDecoder (sr));
310 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
312 decoder.reset (new DCPSubtitleDecoder (dc));
316 _view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());