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 "subtitle_appearance_dialog.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/text_subtitle_content.h"
29 #include "lib/ffmpeg_subtitle_stream.h"
30 #include "lib/dcp_subtitle_content.h"
31 #include "lib/text_subtitle_decoder.h"
32 #include "lib/dcp_subtitle_decoder.h"
33 #include "lib/dcp_content.h"
34 #include <wx/spinctrl.h>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/foreach.hpp>
41 using boost::shared_ptr;
42 using boost::lexical_cast;
43 using boost::dynamic_pointer_cast;
45 SubtitlePanel::SubtitlePanel (ContentPanel* p)
46 : ContentSubPanel (p, _("Subtitles"))
50 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
51 _sizer->Add (grid, 0, wxALL, 8);
54 _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
55 grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
58 _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
59 grid->Add (_use, wxGBPosition (r, 0), wxGBSpan (1, 2));
62 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
63 grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
67 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
68 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
69 _x_offset = new wxSpinCtrl (this);
71 add_label_to_sizer (s, this, _("%"), false);
72 grid->Add (s, wxGBPosition (r, 1));
77 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
78 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
79 _y_offset = new wxSpinCtrl (this);
81 add_label_to_sizer (s, this, _("%"), false);
82 grid->Add (s, wxGBPosition (r, 1));
87 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
88 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
89 _x_scale = new wxSpinCtrl (this);
91 add_label_to_sizer (s, this, _("%"), false);
92 grid->Add (s, wxGBPosition (r, 1));
97 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
98 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
99 _y_scale = new wxSpinCtrl (this);
101 add_label_to_sizer (s, this, _("%"), false);
102 grid->Add (s, wxGBPosition (r, 1));
106 add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
107 _language = new wxTextCtrl (this, wxID_ANY);
108 grid->Add (_language, wxGBPosition (r, 1));
111 add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
112 _stream = new wxChoice (this, wxID_ANY);
113 grid->Add (_stream, wxGBPosition (r, 1));
117 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
119 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
120 s->Add (_subtitle_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
121 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
122 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
123 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
124 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
126 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
130 _x_offset->SetRange (-100, 100);
131 _y_offset->SetRange (-100, 100);
132 _x_scale->SetRange (10, 1000);
133 _y_scale->SetRange (10, 1000);
135 _reference->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::reference_clicked, this));
136 _use->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
137 _burn->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
138 _x_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
139 _y_offset->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
140 _x_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
141 _y_scale->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
142 _language->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&SubtitlePanel::language_changed, this));
143 _stream->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&SubtitlePanel::stream_changed, this));
144 _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
145 _fonts_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
146 _appearance_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&SubtitlePanel::appearance_dialog_clicked, this));
150 SubtitlePanel::film_changed (Film::Property property)
152 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
153 setup_sensitivity ();
158 SubtitlePanel::film_content_changed (int property)
160 FFmpegContentList fc = _parent->selected_ffmpeg ();
161 SubtitleContentList sc = _parent->selected_subtitle ();
163 shared_ptr<FFmpegContent> fcs;
164 if (fc.size() == 1) {
168 shared_ptr<SubtitleContent> scs;
169 if (sc.size() == 1) {
173 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
176 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
177 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
178 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
181 if (fcs->subtitle_stream()) {
182 checked_set (_stream, fcs->subtitle_stream()->identifier ());
184 _stream->SetSelection (wxNOT_FOUND);
187 setup_sensitivity ();
188 } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
189 checked_set (_use, scs ? scs->use_subtitles() : false);
190 setup_sensitivity ();
191 } else if (property == SubtitleContentProperty::BURN_SUBTITLES) {
192 checked_set (_burn, scs ? scs->burn_subtitles() : false);
193 } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
194 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
195 } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
196 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
197 } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
198 checked_set (_x_scale, scs ? lrint (scs->subtitle_x_scale() * 100) : 100);
199 } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
200 checked_set (_y_scale, scs ? lrint (scs->subtitle_y_scale() * 100) : 100);
201 } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
202 checked_set (_language, scs ? scs->subtitle_language() : "");
203 } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
205 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
206 checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
208 checked_set (_reference, false);
211 setup_sensitivity ();
216 SubtitlePanel::use_toggled ()
218 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
219 i->set_use_subtitles (_use->GetValue());
224 SubtitlePanel::burn_toggled ()
226 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
227 i->set_burn_subtitles (_burn->GetValue());
232 SubtitlePanel::setup_sensitivity ()
239 SubtitleContentList sel = _parent->selected_subtitle ();
240 BOOST_FOREACH (shared_ptr<SubtitleContent> i, sel) {
241 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
242 shared_ptr<const TextSubtitleContent> sc = boost::dynamic_pointer_cast<const TextSubtitleContent> (i);
243 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
245 if (fc->has_subtitles ()) {
259 if (i->has_image_subtitles ()) {
261 /* We must burn image subtitles at the moment */
262 i->set_burn_subtitles (true);
266 shared_ptr<DCPContent> dcp;
267 if (sel.size() == 1) {
268 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
271 list<string> why_not;
272 bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
273 setup_refer_button (_reference, dcp, can_reference, why_not);
275 bool const reference = _reference->GetValue ();
277 _use->Enable (!reference && any_subs > 0);
278 bool const use = _use->GetValue ();
279 _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
280 _x_offset->Enable (!reference && any_subs > 0 && use);
281 _y_offset->Enable (!reference && any_subs > 0 && use);
282 _x_scale->Enable (!reference && any_subs > 0 && use);
283 _y_scale->Enable (!reference && any_subs > 0 && use);
284 _language->Enable (!reference && any_subs > 0 && use);
285 _stream->Enable (!reference && ffmpeg_subs == 1);
286 _subtitle_view_button->Enable (!reference && (text_subs == 1 || dcp_subs == 1));
287 _fonts_dialog_button->Enable (!reference && (text_subs == 1 || dcp_subs == 1));
288 _appearance_dialog_button->Enable (!reference && text_subs == 1);
292 SubtitlePanel::stream_changed ()
294 FFmpegContentList fc = _parent->selected_ffmpeg ();
295 if (fc.size() != 1) {
299 shared_ptr<FFmpegContent> fcs = fc.front ();
301 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
302 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
303 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
304 while (i != a.end() && (*i)->identifier () != s) {
309 fcs->set_subtitle_stream (*i);
314 SubtitlePanel::x_offset_changed ()
316 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
317 i->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
322 SubtitlePanel::y_offset_changed ()
324 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
325 i->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
330 SubtitlePanel::x_scale_changed ()
332 SubtitleContentList c = _parent->selected_subtitle ();
334 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
339 SubtitlePanel::y_scale_changed ()
341 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
342 i->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
347 SubtitlePanel::language_changed ()
349 BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
350 i->set_subtitle_language (wx_to_std (_language->GetValue()));
355 SubtitlePanel::content_selection_changed ()
357 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
358 film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
359 film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
360 film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
361 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
362 film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
363 film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
364 film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
365 film_content_changed (SubtitleContentProperty::FONTS);
366 film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
370 SubtitlePanel::subtitle_view_clicked ()
372 if (_subtitle_view) {
373 _subtitle_view->Destroy ();
377 SubtitleContentList c = _parent->selected_subtitle ();
378 DCPOMATIC_ASSERT (c.size() == 1);
380 shared_ptr<SubtitleDecoder> decoder;
382 shared_ptr<TextSubtitleContent> sr = dynamic_pointer_cast<TextSubtitleContent> (c.front ());
384 decoder.reset (new TextSubtitleDecoder (sr));
387 shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
389 decoder.reset (new DCPSubtitleDecoder (dc));
393 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
394 _subtitle_view->Show ();
399 SubtitlePanel::fonts_dialog_clicked ()
402 _fonts_dialog->Destroy ();
406 SubtitleContentList c = _parent->selected_subtitle ();
407 DCPOMATIC_ASSERT (c.size() == 1);
409 _fonts_dialog = new FontsDialog (this, c.front ());
410 _fonts_dialog->Show ();
414 SubtitlePanel::reference_clicked ()
416 ContentList c = _parent->selected ();
421 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
426 d->set_reference_subtitle (_reference->GetValue ());
430 SubtitlePanel::appearance_dialog_clicked ()
432 SubtitleContentList c = _parent->selected_subtitle ();
433 DCPOMATIC_ASSERT (c.size() == 1);
435 shared_ptr<TextSubtitleContent> sr = dynamic_pointer_cast<TextSubtitleContent> (c.front ());
436 DCPOMATIC_ASSERT (sr);
438 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, sr);
439 if (d->ShowModal () == wxID_OK) {