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 content_selection_changed ();
175 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
176 _use->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
177 _type->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::type_changed, this));
178 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
179 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
180 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
181 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
182 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
183 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
184 _language->Bind (wxEVT_TEXT, boost::bind (&TextPanel::language_changed, this));
185 _stream->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::stream_changed, this));
186 _text_view_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::text_view_clicked, this));
187 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::fonts_dialog_clicked, this));
188 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::appearance_dialog_clicked, this));
192 TextPanel::film_changed (Film::Property property)
194 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
195 setup_sensitivity ();
200 TextPanel::film_content_changed (int property)
202 FFmpegContentList fc = _parent->selected_ffmpeg ();
203 ContentList sc = _parent->selected_text ();
205 shared_ptr<FFmpegContent> fcs;
206 if (fc.size() == 1) {
210 shared_ptr<Content> scs;
211 if (sc.size() == 1) {
215 shared_ptr<TextContent> text;
217 text = scs->text_of_original_type(_original_type);
220 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
223 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
224 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
225 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
228 if (fcs->subtitle_stream()) {
229 checked_set (_stream, fcs->subtitle_stream()->identifier ());
231 _stream->SetSelection (wxNOT_FOUND);
234 setup_sensitivity ();
235 } else if (property == TextContentProperty::USE) {
236 checked_set (_use, text ? text->use() : false);
237 setup_sensitivity ();
238 } else if (property == TextContentProperty::TYPE) {
240 switch (text->type()) {
241 case TEXT_OPEN_SUBTITLE:
242 _type->SetSelection (0);
244 case TEXT_CLOSED_CAPTION:
245 _type->SetSelection (1);
248 DCPOMATIC_ASSERT (false);
251 _type->SetSelection (0);
253 setup_sensitivity ();
254 } else if (property == TextContentProperty::BURN) {
255 checked_set (_burn, text ? text->burn() : false);
256 } else if (property == TextContentProperty::X_OFFSET) {
257 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
258 } else if (property == TextContentProperty::Y_OFFSET) {
259 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
260 } else if (property == TextContentProperty::X_SCALE) {
261 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
262 } else if (property == TextContentProperty::Y_SCALE) {
263 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
264 } else if (property == TextContentProperty::LINE_SPACING) {
265 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
266 } else if (property == TextContentProperty::LANGUAGE) {
267 checked_set (_language, text ? text->language() : "");
268 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
270 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
271 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
273 checked_set (_reference, false);
276 setup_sensitivity ();
277 } else if (property == DCPContentProperty::TEXTS) {
278 setup_sensitivity ();
283 TextPanel::use_toggled ()
285 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
286 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
290 /** @return the text type that is currently selected in the drop-down */
292 TextPanel::current_type () const
294 switch (_type->GetSelection()) {
296 return TEXT_OPEN_SUBTITLE;
298 return TEXT_CLOSED_CAPTION;
305 TextPanel::type_changed ()
307 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
308 i->text_of_original_type(_original_type)->set_type (current_type ());
313 TextPanel::burn_toggled ()
315 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
316 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
321 TextPanel::setup_sensitivity ()
325 ContentList sel = _parent->selected_text ();
326 BOOST_FOREACH (shared_ptr<Content> i, sel) {
327 /* These are the content types that could include subtitles */
328 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
329 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
330 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
331 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
333 if (!fc->text.empty()) {
337 } else if (sc || dc || dsc) {
338 /* XXX: in the future there could be bitmap subs from DCPs */
343 /* Decide whether we can reference these subs */
345 shared_ptr<DCPContent> dcp;
346 if (sel.size() == 1) {
347 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
351 bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
352 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
354 bool const reference = _reference->GetValue ();
356 TextType const type = current_type ();
358 /* Set up sensitivity */
359 _use->Enable (!reference && any_subs > 0);
360 bool const use = _use->GetValue ();
361 _type->Enable (!reference && any_subs > 0 && use);
362 _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
363 _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
364 _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
365 _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
366 _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
367 _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
368 _language->Enable (!reference && any_subs > 0 && use);
369 _stream->Enable (!reference && ffmpeg_subs == 1);
370 _text_view_button->Enable (!reference);
371 _fonts_dialog_button->Enable (!reference && type == TEXT_OPEN_SUBTITLE);
372 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
376 TextPanel::stream_changed ()
378 FFmpegContentList fc = _parent->selected_ffmpeg ();
379 if (fc.size() != 1) {
383 shared_ptr<FFmpegContent> fcs = fc.front ();
385 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
386 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
387 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
388 while (i != a.end() && (*i)->identifier () != s) {
393 fcs->set_subtitle_stream (*i);
398 TextPanel::x_offset_changed ()
400 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
401 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
406 TextPanel::y_offset_changed ()
408 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
409 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
414 TextPanel::x_scale_changed ()
416 ContentList c = _parent->selected_text ();
418 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
423 TextPanel::y_scale_changed ()
425 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
426 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
431 TextPanel::line_spacing_changed ()
433 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
434 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
439 TextPanel::language_changed ()
441 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
442 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
447 TextPanel::content_selection_changed ()
449 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
450 film_content_changed (TextContentProperty::USE);
451 film_content_changed (TextContentProperty::BURN);
452 film_content_changed (TextContentProperty::X_OFFSET);
453 film_content_changed (TextContentProperty::Y_OFFSET);
454 film_content_changed (TextContentProperty::X_SCALE);
455 film_content_changed (TextContentProperty::Y_SCALE);
456 film_content_changed (TextContentProperty::LINE_SPACING);
457 film_content_changed (TextContentProperty::LANGUAGE);
458 film_content_changed (TextContentProperty::FONTS);
459 film_content_changed (TextContentProperty::TYPE);
460 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
464 TextPanel::text_view_clicked ()
467 _text_view->Destroy ();
471 ContentList c = _parent->selected_text ();
472 DCPOMATIC_ASSERT (c.size() == 1);
474 shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
477 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
483 TextPanel::fonts_dialog_clicked ()
486 _fonts_dialog->Destroy ();
490 ContentList c = _parent->selected_text ();
491 DCPOMATIC_ASSERT (c.size() == 1);
493 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
494 _fonts_dialog->Show ();
498 TextPanel::reference_clicked ()
500 ContentList c = _parent->selected ();
505 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
510 d->set_reference_text (_original_type, _reference->GetValue ());
514 TextPanel::appearance_dialog_clicked ()
516 ContentList c = _parent->selected_text ();
517 DCPOMATIC_ASSERT (c.size() == 1);
519 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
520 if (d->ShowModal () == wxID_OK) {