2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "text_panel.h"
22 #include "film_editor.h"
24 #include "text_view.h"
25 #include "content_panel.h"
26 #include "fonts_dialog.h"
27 #include "subtitle_appearance_dialog.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/string_text_file_content.h"
30 #include "lib/ffmpeg_subtitle_stream.h"
31 #include "lib/dcp_subtitle_content.h"
32 #include "lib/string_text_file_decoder.h"
33 #include "lib/dcp_subtitle_decoder.h"
34 #include "lib/dcp_content.h"
35 #include "lib/text_content.h"
36 #include "lib/decoder_factory.h"
37 #include <wx/spinctrl.h>
38 #include <boost/foreach.hpp>
44 using boost::shared_ptr;
45 using boost::optional;
46 using boost::dynamic_pointer_cast;
48 /** @param t Original text type of the content, if known */
49 TextPanel::TextPanel (ContentPanel* p, TextType t)
50 : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
55 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
57 wxString refer = _("Use this DCP's subtitle as OV and make VF");
58 if (t == TEXT_CLOSED_CAPTION) {
59 refer = _("Use this DCP's closed caption as OV and make VF");
62 _reference = new wxCheckBox (this, wxID_ANY, refer);
63 reference_sizer->Add (_reference, 0, wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
65 _reference_note = new wxStaticText (this, wxID_ANY, _(""));
66 _reference_note->Wrap (200);
67 reference_sizer->Add (_reference_note, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
68 wxFont font = _reference_note->GetFont();
69 font.SetStyle(wxFONTSTYLE_ITALIC);
70 font.SetPointSize(font.GetPointSize() - 1);
71 _reference_note->SetFont(font);
73 _sizer->Add (reference_sizer);
75 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
76 _sizer->Add (grid, 0, wxALL, 8);
79 wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
80 _use = new wxCheckBox (this, wxID_ANY, _("Use as"));
81 use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
82 _type = new wxChoice (this, wxID_ANY);
83 _type->Append (_("open subtitles"));
84 _type->Append (_("closed captions"));
85 use->Add (_type, 1, wxEXPAND, 0);
86 grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
89 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
90 grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
94 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
95 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
96 _x_offset = new wxSpinCtrl (this);
98 add_label_to_sizer (s, this, _("%"), false);
99 grid->Add (s, wxGBPosition (r, 1));
104 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
105 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
106 _y_offset = new wxSpinCtrl (this);
108 add_label_to_sizer (s, this, _("%"), false);
109 grid->Add (s, wxGBPosition (r, 1));
114 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
115 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
116 _x_scale = new wxSpinCtrl (this);
118 add_label_to_sizer (s, this, _("%"), false);
119 grid->Add (s, wxGBPosition (r, 1));
124 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
125 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
126 _y_scale = new wxSpinCtrl (this);
128 add_label_to_sizer (s, this, _("%"), false);
129 grid->Add (s, wxGBPosition (r, 1));
134 add_label_to_sizer (grid, this, _("Line spacing"), true, wxGBPosition (r, 0));
135 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
136 _line_spacing = new wxSpinCtrl (this);
137 s->Add (_line_spacing);
138 add_label_to_sizer (s, this, _("%"), false);
139 grid->Add (s, wxGBPosition (r, 1));
143 add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
144 _language = new wxTextCtrl (this, wxID_ANY);
145 grid->Add (_language, wxGBPosition (r, 1));
148 add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
149 _stream = new wxChoice (this, wxID_ANY);
150 grid->Add (_stream, wxGBPosition (r, 1));
154 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
156 _text_view_button = new wxButton (this, wxID_ANY, _("View..."));
157 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
158 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
159 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
160 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
161 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
163 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
167 _x_offset->SetRange (-100, 100);
168 _y_offset->SetRange (-100, 100);
169 _x_scale->SetRange (10, 1000);
170 _y_scale->SetRange (10, 1000);
171 _line_spacing->SetRange (10, 1000);
173 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
174 _use->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
175 _type->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::type_changed, this));
176 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
177 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
178 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
179 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
180 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
181 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
182 _language->Bind (wxEVT_TEXT, boost::bind (&TextPanel::language_changed, this));
183 _stream->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::stream_changed, this));
184 _text_view_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::text_view_clicked, this));
185 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::fonts_dialog_clicked, this));
186 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::appearance_dialog_clicked, this));
190 TextPanel::film_changed (Film::Property property)
192 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
193 setup_sensitivity ();
198 TextPanel::film_content_changed (int property)
200 FFmpegContentList fc = _parent->selected_ffmpeg ();
201 ContentList sc = _parent->selected_text ();
203 shared_ptr<FFmpegContent> fcs;
204 if (fc.size() == 1) {
208 shared_ptr<Content> scs;
209 if (sc.size() == 1) {
213 shared_ptr<TextContent> text;
215 text = scs->text_of_original_type(_original_type);
218 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
221 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
222 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
223 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
226 if (fcs->subtitle_stream()) {
227 checked_set (_stream, fcs->subtitle_stream()->identifier ());
229 _stream->SetSelection (wxNOT_FOUND);
232 setup_sensitivity ();
233 } else if (property == TextContentProperty::USE) {
234 checked_set (_use, text ? text->use() : false);
235 setup_sensitivity ();
236 } else if (property == TextContentProperty::TYPE) {
238 switch (text->type()) {
239 case TEXT_OPEN_SUBTITLE:
240 _type->SetSelection (0);
242 case TEXT_CLOSED_CAPTION:
243 _type->SetSelection (1);
246 DCPOMATIC_ASSERT (false);
249 _type->SetSelection (0);
251 setup_sensitivity ();
252 } else if (property == TextContentProperty::BURN) {
253 checked_set (_burn, text ? text->burn() : false);
254 } else if (property == TextContentProperty::X_OFFSET) {
255 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
256 } else if (property == TextContentProperty::Y_OFFSET) {
257 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
258 } else if (property == TextContentProperty::X_SCALE) {
259 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
260 } else if (property == TextContentProperty::Y_SCALE) {
261 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
262 } else if (property == TextContentProperty::LINE_SPACING) {
263 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
264 } else if (property == TextContentProperty::LANGUAGE) {
265 checked_set (_language, text ? text->language() : "");
266 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
268 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
269 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
271 checked_set (_reference, false);
274 setup_sensitivity ();
275 } else if (property == DCPContentProperty::TEXTS) {
276 setup_sensitivity ();
281 TextPanel::use_toggled ()
283 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
284 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
289 TextPanel::type_changed ()
291 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
292 switch (_type->GetSelection()) {
294 i->text_of_original_type(_original_type)->set_type (TEXT_OPEN_SUBTITLE);
297 i->text_of_original_type(_original_type)->set_type (TEXT_CLOSED_CAPTION);
304 TextPanel::burn_toggled ()
306 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
307 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
312 TextPanel::setup_sensitivity ()
316 ContentList sel = _parent->selected_text ();
317 BOOST_FOREACH (shared_ptr<Content> i, sel) {
318 /* These are the content types that could include subtitles */
319 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
320 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
321 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
322 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
324 if (!fc->text.empty()) {
328 } else if (sc || dc || dsc) {
329 /* XXX: in the future there could be bitmap subs from DCPs */
334 /* Decide whether we can reference these subs */
336 shared_ptr<DCPContent> dcp;
337 if (sel.size() == 1) {
338 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
342 bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
343 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
345 bool const reference = _reference->GetValue ();
347 /* Set up sensitivity */
348 _use->Enable (!reference && any_subs > 0);
349 bool const use = _use->GetValue ();
350 _type->Enable (!reference && any_subs > 0 && use);
351 _burn->Enable (!reference && any_subs > 0 && use && _type->GetSelection() == 0);
352 _x_offset->Enable (!reference && any_subs > 0 && use);
353 _y_offset->Enable (!reference && any_subs > 0 && use);
354 _x_scale->Enable (!reference && any_subs > 0 && use);
355 _y_scale->Enable (!reference && any_subs > 0 && use);
356 _line_spacing->Enable (!reference && use);
357 _language->Enable (!reference && any_subs > 0 && use);
358 _stream->Enable (!reference && ffmpeg_subs == 1);
359 _text_view_button->Enable (!reference);
360 _fonts_dialog_button->Enable (!reference);
361 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use);
365 TextPanel::stream_changed ()
367 FFmpegContentList fc = _parent->selected_ffmpeg ();
368 if (fc.size() != 1) {
372 shared_ptr<FFmpegContent> fcs = fc.front ();
374 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
375 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
376 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
377 while (i != a.end() && (*i)->identifier () != s) {
382 fcs->set_subtitle_stream (*i);
387 TextPanel::x_offset_changed ()
389 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
390 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
395 TextPanel::y_offset_changed ()
397 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
398 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
403 TextPanel::x_scale_changed ()
405 ContentList c = _parent->selected_text ();
407 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
412 TextPanel::y_scale_changed ()
414 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
415 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
420 TextPanel::line_spacing_changed ()
422 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
423 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
428 TextPanel::language_changed ()
430 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
431 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
436 TextPanel::content_selection_changed ()
438 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
439 film_content_changed (TextContentProperty::USE);
440 film_content_changed (TextContentProperty::BURN);
441 film_content_changed (TextContentProperty::X_OFFSET);
442 film_content_changed (TextContentProperty::Y_OFFSET);
443 film_content_changed (TextContentProperty::X_SCALE);
444 film_content_changed (TextContentProperty::Y_SCALE);
445 film_content_changed (TextContentProperty::LINE_SPACING);
446 film_content_changed (TextContentProperty::LANGUAGE);
447 film_content_changed (TextContentProperty::FONTS);
448 film_content_changed (TextContentProperty::TYPE);
449 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
453 TextPanel::text_view_clicked ()
456 _text_view->Destroy ();
460 ContentList c = _parent->selected_text ();
461 DCPOMATIC_ASSERT (c.size() == 1);
463 shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
466 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
472 TextPanel::fonts_dialog_clicked ()
475 _fonts_dialog->Destroy ();
479 ContentList c = _parent->selected_text ();
480 DCPOMATIC_ASSERT (c.size() == 1);
482 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
483 _fonts_dialog->Show ();
487 TextPanel::reference_clicked ()
489 ContentList c = _parent->selected ();
494 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
499 d->set_reference_text (_original_type, _reference->GetValue ());
503 TextPanel::appearance_dialog_clicked ()
505 ContentList c = _parent->selected_text ();
506 DCPOMATIC_ASSERT (c.size() == 1);
508 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
509 if (d->ShowModal () == wxID_OK) {