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 "dcp_text_track_dialog.h"
28 #include "subtitle_appearance_dialog.h"
29 #include "static_text.h"
30 #include "check_box.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/string_text_file_content.h"
33 #include "lib/ffmpeg_subtitle_stream.h"
34 #include "lib/dcp_subtitle_content.h"
35 #include "lib/string_text_file_decoder.h"
36 #include "lib/dcp_subtitle_decoder.h"
37 #include "lib/dcp_content.h"
38 #include "lib/text_content.h"
39 #include "lib/decoder_factory.h"
40 #include <wx/spinctrl.h>
41 #include <boost/foreach.hpp>
47 using boost::shared_ptr;
48 using boost::optional;
49 using boost::dynamic_pointer_cast;
51 /** @param t Original text type of the content, if known */
52 TextPanel::TextPanel (ContentPanel* p, TextType t)
53 : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
58 wxString refer = _("Use this DCP's subtitle as OV and make VF");
59 if (t == TEXT_CLOSED_CAPTION) {
60 refer = _("Use this DCP's closed caption as OV and make VF");
63 _reference = new CheckBox (this, refer);
64 _reference_note = new StaticText (this, wxT(""));
65 _reference_note->Wrap (200);
66 wxFont font = _reference_note->GetFont();
67 font.SetStyle(wxFONTSTYLE_ITALIC);
68 font.SetPointSize(font.GetPointSize() - 1);
69 _reference_note->SetFont(font);
71 _use = new CheckBox (this, _("Use as"));
72 _type = new wxChoice (this, wxID_ANY);
73 _type->Append (_("open subtitles"));
74 _type->Append (_("closed captions"));
76 _burn = new CheckBox (this, _("Burn subtitles into image"));
78 _offset_label = create_label (this, _("Offset"), true);
79 _x_offset_label = create_label (this, _("X"), true);
80 _x_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
81 _x_offset_pc_label = new StaticText (this, _("%"));
82 _y_offset_label = create_label (this, _("Y"), true);
83 _y_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
84 _y_offset_pc_label = new StaticText (this, _("%"));
86 _scale_label = create_label (this, _("Scale"), true);
87 _x_scale_label = create_label (this, _("X"), true);
88 _x_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
89 _x_scale_pc_label = new StaticText (this, _("%"));
90 _y_scale_label = create_label (this, _("Y"), true);
91 _y_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
92 _y_scale_pc_label = new StaticText (this, _("%"));
94 _line_spacing_label = create_label (this, _("Line spacing"), true);
95 _line_spacing = new wxSpinCtrl (this);
96 _line_spacing_pc_label = new StaticText (this, _("%"));
98 _dcp_track_label = create_label (this, _("DCP track"), true);
99 _dcp_track = new wxChoice (this, wxID_ANY);
101 _language_label = create_label (this, _("Language"), true);
102 _language = new wxTextCtrl (this, wxID_ANY);
104 _stream_label = create_label (this, _("Stream"), true);
105 _stream = new wxChoice (this, wxID_ANY);
107 _text_view_button = new wxButton (this, wxID_ANY, _("View..."));
108 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
109 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
111 _x_offset->SetRange (-100, 100);
112 _y_offset->SetRange (-100, 100);
113 _x_scale->SetRange (10, 1000);
114 _y_scale->SetRange (10, 1000);
115 _line_spacing->SetRange (10, 1000);
117 update_dcp_tracks ();
119 content_selection_changed ();
121 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
122 _use->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
123 _type->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::type_changed, this));
124 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
125 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
126 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
127 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
128 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
129 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
130 _dcp_track->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::dcp_track_changed, this));
131 _language->Bind (wxEVT_TEXT, boost::bind (&TextPanel::language_changed, this));
132 _stream->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::stream_changed, this));
133 _text_view_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::text_view_clicked, this));
134 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::fonts_dialog_clicked, this));
135 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::appearance_dialog_clicked, this));
141 TextPanel::add_to_grid ()
143 Config::Interface const interface = Config::instance()->interface_complexity();
147 _reference->Show (interface == Config::INTERFACE_FULL);
148 _reference_note->Show (interface == Config::INTERFACE_FULL);
150 if (interface == Config::INTERFACE_FULL) {
151 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
152 reference_sizer->Add (_reference, 0);
153 reference_sizer->Add (_reference_note, 0);
154 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
158 wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
159 use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
160 use->Add (_type, 1, wxEXPAND, 0);
161 _grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
164 _grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
167 add_label_to_sizer (_grid, _offset_label, true, wxGBPosition (r, 0));
168 wxBoxSizer* offset = new wxBoxSizer (wxHORIZONTAL);
169 add_label_to_sizer (offset, _x_offset_label, true);
170 offset->Add (_x_offset, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
171 offset->Add (_x_offset_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
172 add_label_to_sizer (offset, _y_offset_label, true);
173 offset->Add (_y_offset, 0);
174 add_label_to_sizer (offset, _y_offset_pc_label, false);
175 _grid->Add (offset, wxGBPosition (r, 1));
178 add_label_to_sizer (_grid, _scale_label, true, wxGBPosition (r, 0));
179 wxBoxSizer* scale = new wxBoxSizer (wxHORIZONTAL);
180 add_label_to_sizer (scale, _x_scale_label, true);
181 scale->Add (_x_scale, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
182 scale->Add (_x_scale_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
183 add_label_to_sizer (scale, _y_scale_label, true);
184 scale->Add (_y_scale, 0);
185 add_label_to_sizer (scale, _y_scale_pc_label, false);
186 _grid->Add (scale, wxGBPosition (r, 1));
190 add_label_to_sizer (_grid, _line_spacing_label, true, wxGBPosition (r, 0));
191 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
192 s->Add (_line_spacing);
193 add_label_to_sizer (s, _line_spacing_pc_label, false);
194 _grid->Add (s, wxGBPosition (r, 1));
198 add_label_to_sizer (_grid, _dcp_track_label, true, wxGBPosition(r, 0));
199 _grid->Add (_dcp_track, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
202 add_label_to_sizer (_grid, _language_label, true, wxGBPosition (r, 0));
203 _grid->Add (_language, wxGBPosition (r, 1));
206 add_label_to_sizer (_grid, _stream_label, true, wxGBPosition (r, 0));
207 _grid->Add (_stream, wxGBPosition (r, 1));
211 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
213 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
214 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
215 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
217 _grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
223 TextPanel::update_dcp_track_selection ()
225 optional<DCPTextTrack> selected;
227 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
228 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
230 optional<DCPTextTrack> dt = t->dcp_track();
231 if (dt && selected && *dt != *selected) {
233 } else if (!selected) {
240 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
241 if (!many && selected && *selected == i) {
242 _dcp_track->SetSelection (n);
247 if (!selected || many) {
248 _dcp_track->SetSelection (wxNOT_FOUND);
253 TextPanel::update_dcp_tracks ()
255 _dcp_track->Clear ();
256 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
257 _dcp_track->Append (std_to_wx(i.summary()));
260 if (_parent->film()->closed_caption_tracks().size() < 6) {
261 _dcp_track->Append (_("Add new..."));
264 update_dcp_track_selection ();
268 TextPanel::dcp_track_changed ()
270 optional<DCPTextTrack> track;
272 if (_dcp_track->GetSelection() == int(_dcp_track->GetCount()) - 1) {
273 DCPTextTrackDialog* d = new DCPTextTrackDialog (this);
274 if (d->ShowModal() == wxID_OK) {
279 /* Find the DCPTextTrack that was selected */
280 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
281 if (i.summary() == wx_to_std(_dcp_track->GetStringSelection())) {
288 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
289 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
290 if (t && t->type() == TEXT_CLOSED_CAPTION) {
291 t->set_dcp_track(*track);
296 update_dcp_tracks ();
300 TextPanel::film_changed (Film::Property property)
302 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
303 setup_sensitivity ();
308 TextPanel::film_content_changed (int property)
310 FFmpegContentList fc = _parent->selected_ffmpeg ();
311 ContentList sc = _parent->selected_text ();
313 shared_ptr<FFmpegContent> fcs;
314 if (fc.size() == 1) {
318 shared_ptr<Content> scs;
319 if (sc.size() == 1) {
323 shared_ptr<TextContent> text;
325 text = scs->text_of_original_type(_original_type);
328 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
331 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
332 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
333 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
336 if (fcs->subtitle_stream()) {
337 checked_set (_stream, fcs->subtitle_stream()->identifier ());
339 _stream->SetSelection (wxNOT_FOUND);
342 setup_sensitivity ();
343 } else if (property == TextContentProperty::USE) {
344 checked_set (_use, text ? text->use() : false);
345 setup_sensitivity ();
346 } else if (property == TextContentProperty::TYPE) {
348 switch (text->type()) {
349 case TEXT_OPEN_SUBTITLE:
350 _type->SetSelection (0);
352 case TEXT_CLOSED_CAPTION:
353 _type->SetSelection (1);
356 DCPOMATIC_ASSERT (false);
359 _type->SetSelection (0);
361 setup_sensitivity ();
362 update_dcp_track_selection ();
363 } else if (property == TextContentProperty::BURN) {
364 checked_set (_burn, text ? text->burn() : false);
365 } else if (property == TextContentProperty::X_OFFSET) {
366 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
367 } else if (property == TextContentProperty::Y_OFFSET) {
368 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
369 } else if (property == TextContentProperty::X_SCALE) {
370 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
371 } else if (property == TextContentProperty::Y_SCALE) {
372 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
373 } else if (property == TextContentProperty::LINE_SPACING) {
374 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
375 } else if (property == TextContentProperty::LANGUAGE) {
376 checked_set (_language, text ? text->language() : "");
377 } else if (property == TextContentProperty::DCP_TRACK) {
378 update_dcp_track_selection ();
379 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
381 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
382 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
384 checked_set (_reference, false);
387 setup_sensitivity ();
388 } else if (property == DCPContentProperty::TEXTS) {
389 setup_sensitivity ();
394 TextPanel::use_toggled ()
396 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
397 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
401 /** @return the text type that is currently selected in the drop-down */
403 TextPanel::current_type () const
405 switch (_type->GetSelection()) {
407 return TEXT_OPEN_SUBTITLE;
409 return TEXT_CLOSED_CAPTION;
416 TextPanel::type_changed ()
418 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
419 i->text_of_original_type(_original_type)->set_type (current_type ());
424 TextPanel::burn_toggled ()
426 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
427 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
432 TextPanel::setup_sensitivity ()
436 ContentList sel = _parent->selected_text ();
437 BOOST_FOREACH (shared_ptr<Content> i, sel) {
438 /* These are the content types that could include subtitles */
439 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
440 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
441 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
442 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
444 if (!fc->text.empty()) {
448 } else if (sc || dc || dsc) {
449 /* XXX: in the future there could be bitmap subs from DCPs */
454 /* Decide whether we can reference these subs */
456 shared_ptr<DCPContent> dcp;
457 if (sel.size() == 1) {
458 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
462 bool const can_reference = dcp && dcp->can_reference_text (_parent->film(), _original_type, why_not);
463 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
465 bool const reference = _reference->GetValue ();
467 TextType const type = current_type ();
469 /* Set up sensitivity */
470 _use->Enable (!reference && any_subs > 0);
471 bool const use = _use->GetValue ();
472 _type->Enable (!reference && any_subs > 0 && use);
473 _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
474 _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
475 _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
476 _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
477 _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
478 _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
479 _dcp_track->Enable (!reference && any_subs > 0 && use && type == TEXT_CLOSED_CAPTION);
480 _language->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
481 _stream->Enable (!reference && ffmpeg_subs == 1);
482 _text_view_button->Enable (!reference);
483 _fonts_dialog_button->Enable (!reference && type == TEXT_OPEN_SUBTITLE);
484 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
488 TextPanel::stream_changed ()
490 FFmpegContentList fc = _parent->selected_ffmpeg ();
491 if (fc.size() != 1) {
495 shared_ptr<FFmpegContent> fcs = fc.front ();
497 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
498 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
499 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
500 while (i != a.end() && (*i)->identifier () != s) {
505 fcs->set_subtitle_stream (*i);
510 TextPanel::x_offset_changed ()
512 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
513 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
518 TextPanel::y_offset_changed ()
520 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
521 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
526 TextPanel::x_scale_changed ()
528 ContentList c = _parent->selected_text ();
530 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
535 TextPanel::y_scale_changed ()
537 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
538 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
543 TextPanel::line_spacing_changed ()
545 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
546 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
551 TextPanel::language_changed ()
553 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
554 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
559 TextPanel::content_selection_changed ()
561 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
562 film_content_changed (TextContentProperty::USE);
563 film_content_changed (TextContentProperty::BURN);
564 film_content_changed (TextContentProperty::X_OFFSET);
565 film_content_changed (TextContentProperty::Y_OFFSET);
566 film_content_changed (TextContentProperty::X_SCALE);
567 film_content_changed (TextContentProperty::Y_SCALE);
568 film_content_changed (TextContentProperty::LINE_SPACING);
569 film_content_changed (TextContentProperty::LANGUAGE);
570 film_content_changed (TextContentProperty::FONTS);
571 film_content_changed (TextContentProperty::TYPE);
572 film_content_changed (TextContentProperty::DCP_TRACK);
573 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
577 TextPanel::text_view_clicked ()
580 _text_view->Destroy ();
584 ContentList c = _parent->selected_text ();
585 DCPOMATIC_ASSERT (c.size() == 1);
587 shared_ptr<Decoder> decoder = decoder_factory (_parent->film(), c.front(), false);
590 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
596 TextPanel::fonts_dialog_clicked ()
599 _fonts_dialog->Destroy ();
603 ContentList c = _parent->selected_text ();
604 DCPOMATIC_ASSERT (c.size() == 1);
606 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
607 _fonts_dialog->Show ();
611 TextPanel::reference_clicked ()
613 ContentList c = _parent->selected ();
618 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
623 d->set_reference_text (_original_type, _reference->GetValue ());
627 TextPanel::appearance_dialog_clicked ()
629 ContentList c = _parent->selected_text ();
630 DCPOMATIC_ASSERT (c.size() == 1);
632 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type));
633 if (d->ShowModal () == wxID_OK) {