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));
93 add_label_to_sizer (grid, this, _("Offset"), true, wxGBPosition (r, 0));
94 wxBoxSizer* offset = new wxBoxSizer (wxHORIZONTAL);
95 add_label_to_sizer (offset, this, _("X"), true);
96 _x_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
97 offset->Add (_x_offset);
98 add_label_to_sizer (offset, this, _("%"), false);
99 add_label_to_sizer (offset, this, _("Y"), true);
100 _y_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
101 offset->Add (_y_offset, 0);
102 add_label_to_sizer (offset, this, _("%"), false);
103 grid->Add (offset, wxGBPosition (r, 1));
106 add_label_to_sizer (grid, this, _("Scale"), true, wxGBPosition (r, 0));
107 wxBoxSizer* scale = new wxBoxSizer (wxHORIZONTAL);
108 add_label_to_sizer (scale, this, _("X"), true);
109 _x_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
110 scale->Add (_x_scale);
111 add_label_to_sizer (scale, this, _("%"), false);
112 add_label_to_sizer (scale, this, _("Y"), true);
113 _y_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
114 scale->Add (_y_scale, 0);
115 add_label_to_sizer (scale, this, _("%"), false);
116 grid->Add (scale, wxGBPosition (r, 1));
120 add_label_to_sizer (grid, this, _("Line spacing"), true, wxGBPosition (r, 0));
121 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
122 _line_spacing = new wxSpinCtrl (this);
123 s->Add (_line_spacing);
124 add_label_to_sizer (s, this, _("%"), false);
125 grid->Add (s, wxGBPosition (r, 1));
129 add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
130 _language = new wxTextCtrl (this, wxID_ANY);
131 grid->Add (_language, wxGBPosition (r, 1));
134 add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
135 _stream = new wxChoice (this, wxID_ANY);
136 grid->Add (_stream, wxGBPosition (r, 1));
140 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
142 _text_view_button = new wxButton (this, wxID_ANY, _("View..."));
143 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
144 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
145 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
146 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
147 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
149 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
153 _x_offset->SetRange (-100, 100);
154 _y_offset->SetRange (-100, 100);
155 _x_scale->SetRange (10, 1000);
156 _y_scale->SetRange (10, 1000);
157 _line_spacing->SetRange (10, 1000);
159 content_selection_changed ();
161 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
162 _use->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
163 _type->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::type_changed, this));
164 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
165 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
166 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
167 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
168 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
169 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
170 _language->Bind (wxEVT_TEXT, boost::bind (&TextPanel::language_changed, this));
171 _stream->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::stream_changed, this));
172 _text_view_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::text_view_clicked, this));
173 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::fonts_dialog_clicked, this));
174 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::appearance_dialog_clicked, this));
178 TextPanel::film_changed (Film::Property property)
180 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
181 setup_sensitivity ();
186 TextPanel::film_content_changed (int property)
188 FFmpegContentList fc = _parent->selected_ffmpeg ();
189 ContentList sc = _parent->selected_text ();
191 shared_ptr<FFmpegContent> fcs;
192 if (fc.size() == 1) {
196 shared_ptr<Content> scs;
197 if (sc.size() == 1) {
201 shared_ptr<TextContent> text;
203 text = scs->text_of_original_type(_original_type);
206 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
209 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
210 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
211 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
214 if (fcs->subtitle_stream()) {
215 checked_set (_stream, fcs->subtitle_stream()->identifier ());
217 _stream->SetSelection (wxNOT_FOUND);
220 setup_sensitivity ();
221 } else if (property == TextContentProperty::USE) {
222 checked_set (_use, text ? text->use() : false);
223 setup_sensitivity ();
224 } else if (property == TextContentProperty::TYPE) {
226 switch (text->type()) {
227 case TEXT_OPEN_SUBTITLE:
228 _type->SetSelection (0);
230 case TEXT_CLOSED_CAPTION:
231 _type->SetSelection (1);
234 DCPOMATIC_ASSERT (false);
237 _type->SetSelection (0);
239 setup_sensitivity ();
240 } else if (property == TextContentProperty::BURN) {
241 checked_set (_burn, text ? text->burn() : false);
242 } else if (property == TextContentProperty::X_OFFSET) {
243 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
244 } else if (property == TextContentProperty::Y_OFFSET) {
245 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
246 } else if (property == TextContentProperty::X_SCALE) {
247 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
248 } else if (property == TextContentProperty::Y_SCALE) {
249 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
250 } else if (property == TextContentProperty::LINE_SPACING) {
251 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
252 } else if (property == TextContentProperty::LANGUAGE) {
253 checked_set (_language, text ? text->language() : "");
254 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
256 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
257 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
259 checked_set (_reference, false);
262 setup_sensitivity ();
263 } else if (property == DCPContentProperty::TEXTS) {
264 setup_sensitivity ();
269 TextPanel::use_toggled ()
271 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
272 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
276 /** @return the text type that is currently selected in the drop-down */
278 TextPanel::current_type () const
280 switch (_type->GetSelection()) {
282 return TEXT_OPEN_SUBTITLE;
284 return TEXT_CLOSED_CAPTION;
291 TextPanel::type_changed ()
293 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
294 i->text_of_original_type(_original_type)->set_type (current_type ());
299 TextPanel::burn_toggled ()
301 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
302 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
307 TextPanel::setup_sensitivity ()
311 ContentList sel = _parent->selected_text ();
312 BOOST_FOREACH (shared_ptr<Content> i, sel) {
313 /* These are the content types that could include subtitles */
314 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
315 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
316 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
317 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
319 if (!fc->text.empty()) {
323 } else if (sc || dc || dsc) {
324 /* XXX: in the future there could be bitmap subs from DCPs */
329 /* Decide whether we can reference these subs */
331 shared_ptr<DCPContent> dcp;
332 if (sel.size() == 1) {
333 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
337 bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
338 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
340 bool const reference = _reference->GetValue ();
342 TextType const type = current_type ();
344 /* Set up sensitivity */
345 _use->Enable (!reference && any_subs > 0);
346 bool const use = _use->GetValue ();
347 _type->Enable (!reference && any_subs > 0 && use);
348 _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
349 _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
350 _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
351 _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
352 _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
353 _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
354 _language->Enable (!reference && any_subs > 0 && use);
355 _stream->Enable (!reference && ffmpeg_subs == 1);
356 _text_view_button->Enable (!reference);
357 _fonts_dialog_button->Enable (!reference && type == TEXT_OPEN_SUBTITLE);
358 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
362 TextPanel::stream_changed ()
364 FFmpegContentList fc = _parent->selected_ffmpeg ();
365 if (fc.size() != 1) {
369 shared_ptr<FFmpegContent> fcs = fc.front ();
371 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
372 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
373 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
374 while (i != a.end() && (*i)->identifier () != s) {
379 fcs->set_subtitle_stream (*i);
384 TextPanel::x_offset_changed ()
386 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
387 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
392 TextPanel::y_offset_changed ()
394 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
395 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
400 TextPanel::x_scale_changed ()
402 ContentList c = _parent->selected_text ();
404 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
409 TextPanel::y_scale_changed ()
411 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
412 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
417 TextPanel::line_spacing_changed ()
419 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
420 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
425 TextPanel::language_changed ()
427 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
428 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
433 TextPanel::content_selection_changed ()
435 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
436 film_content_changed (TextContentProperty::USE);
437 film_content_changed (TextContentProperty::BURN);
438 film_content_changed (TextContentProperty::X_OFFSET);
439 film_content_changed (TextContentProperty::Y_OFFSET);
440 film_content_changed (TextContentProperty::X_SCALE);
441 film_content_changed (TextContentProperty::Y_SCALE);
442 film_content_changed (TextContentProperty::LINE_SPACING);
443 film_content_changed (TextContentProperty::LANGUAGE);
444 film_content_changed (TextContentProperty::FONTS);
445 film_content_changed (TextContentProperty::TYPE);
446 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
450 TextPanel::text_view_clicked ()
453 _text_view->Destroy ();
457 ContentList c = _parent->selected_text ();
458 DCPOMATIC_ASSERT (c.size() == 1);
460 shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
463 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
469 TextPanel::fonts_dialog_clicked ()
472 _fonts_dialog->Destroy ();
476 ContentList c = _parent->selected_text ();
477 DCPOMATIC_ASSERT (c.size() == 1);
479 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
480 _fonts_dialog->Show ();
484 TextPanel::reference_clicked ()
486 ContentList c = _parent->selected ();
491 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
496 d->set_reference_text (_original_type, _reference->GetValue ());
500 TextPanel::appearance_dialog_clicked ()
502 ContentList c = _parent->selected_text ();
503 DCPOMATIC_ASSERT (c.size() == 1);
505 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
506 if (d->ShowModal () == wxID_OK) {