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 "dcpomatic_button.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/string_text_file_content.h"
34 #include "lib/ffmpeg_subtitle_stream.h"
35 #include "lib/dcp_subtitle_content.h"
36 #include "lib/string_text_file_decoder.h"
37 #include "lib/dcp_subtitle_decoder.h"
38 #include "lib/dcp_content.h"
39 #include "lib/text_content.h"
40 #include "lib/decoder_factory.h"
41 #include <wx/spinctrl.h>
42 #include <boost/foreach.hpp>
48 using boost::shared_ptr;
49 using boost::optional;
50 using boost::dynamic_pointer_cast;
52 /** @param t Original text type of the content, if known */
53 TextPanel::TextPanel (ContentPanel* p, TextType t)
54 : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
59 wxString refer = _("Use this DCP's subtitle as OV and make VF");
60 if (t == TEXT_CLOSED_CAPTION) {
61 refer = _("Use this DCP's closed caption as OV and make VF");
64 _reference = new CheckBox (this, refer);
65 _reference_note = new StaticText (this, wxT(""));
66 _reference_note->Wrap (200);
67 wxFont font = _reference_note->GetFont();
68 font.SetStyle(wxFONTSTYLE_ITALIC);
69 font.SetPointSize(font.GetPointSize() - 1);
70 _reference_note->SetFont(font);
72 _use = new CheckBox (this, _("Use as"));
73 _type = new wxChoice (this, wxID_ANY);
74 _type->Append (_("open subtitles"));
75 _type->Append (_("closed captions"));
77 _burn = new CheckBox (this, _("Burn subtitles into image"));
79 _offset_label = create_label (this, _("Offset"), true);
80 _x_offset_label = create_label (this, _("X"), true);
81 _x_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
82 _x_offset_pc_label = new StaticText (this, _("%"));
83 _y_offset_label = create_label (this, _("Y"), true);
84 _y_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
85 _y_offset_pc_label = new StaticText (this, _("%"));
87 _scale_label = create_label (this, _("Scale"), true);
88 _x_scale_label = create_label (this, _("X"), true);
89 _x_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
90 _x_scale_pc_label = new StaticText (this, _("%"));
91 _y_scale_label = create_label (this, S_("Coord|Y"), true);
92 _y_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
93 _y_scale_pc_label = new StaticText (this, _("%"));
95 _line_spacing_label = create_label (this, _("Line spacing"), true);
96 _line_spacing = new wxSpinCtrl (this);
97 _line_spacing_pc_label = new StaticText (this, _("%"));
99 _dcp_track_label = create_label (this, _("DCP track"), true);
100 _dcp_track = new wxChoice (this, wxID_ANY);
102 _language_label = create_label (this, _("Language"), true);
103 _language = new wxTextCtrl (this, wxID_ANY);
105 _stream_label = create_label (this, _("Stream"), true);
106 _stream = new wxChoice (this, wxID_ANY);
108 _text_view_button = new Button (this, _("View..."));
109 _fonts_dialog_button = new Button (this, _("Fonts..."));
110 _appearance_dialog_button = new Button (this, _("Appearance..."));
112 _x_offset->SetRange (-100, 100);
113 _y_offset->SetRange (-100, 100);
114 _x_scale->SetRange (10, 1000);
115 _y_scale->SetRange (10, 1000);
116 _line_spacing->SetRange (10, 1000);
118 update_dcp_tracks ();
120 content_selection_changed ();
122 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
123 _use->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
124 _type->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::type_changed, this));
125 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
126 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
127 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
128 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
129 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
130 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
131 _dcp_track->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::dcp_track_changed, this));
132 _language->Bind (wxEVT_TEXT, boost::bind (&TextPanel::language_changed, this));
133 _stream->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::stream_changed, this));
134 _text_view_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::text_view_clicked, this));
135 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::fonts_dialog_clicked, this));
136 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::appearance_dialog_clicked, this));
142 TextPanel::add_to_grid ()
144 Config::Interface const interface = Config::instance()->interface_complexity();
148 _reference->Show (interface == Config::INTERFACE_FULL);
149 _reference_note->Show (interface == Config::INTERFACE_FULL);
151 if (interface == Config::INTERFACE_FULL) {
152 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
153 reference_sizer->Add (_reference, 0);
154 reference_sizer->Add (_reference_note, 0);
155 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
159 wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
160 use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
161 use->Add (_type, 1, wxEXPAND, 0);
162 _grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
165 _grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
168 add_label_to_sizer (_grid, _offset_label, true, wxGBPosition (r, 0));
169 wxBoxSizer* offset = new wxBoxSizer (wxHORIZONTAL);
170 add_label_to_sizer (offset, _x_offset_label, true);
171 offset->Add (_x_offset, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
172 offset->Add (_x_offset_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
173 add_label_to_sizer (offset, _y_offset_label, true);
174 offset->Add (_y_offset, 0);
175 add_label_to_sizer (offset, _y_offset_pc_label, false);
176 _grid->Add (offset, wxGBPosition (r, 1));
179 add_label_to_sizer (_grid, _scale_label, true, wxGBPosition (r, 0));
180 wxBoxSizer* scale = new wxBoxSizer (wxHORIZONTAL);
181 add_label_to_sizer (scale, _x_scale_label, true);
182 scale->Add (_x_scale, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
183 scale->Add (_x_scale_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
184 add_label_to_sizer (scale, _y_scale_label, true);
185 scale->Add (_y_scale, 0);
186 add_label_to_sizer (scale, _y_scale_pc_label, false);
187 _grid->Add (scale, wxGBPosition (r, 1));
191 add_label_to_sizer (_grid, _line_spacing_label, true, wxGBPosition (r, 0));
192 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
193 s->Add (_line_spacing);
194 add_label_to_sizer (s, _line_spacing_pc_label, false);
195 _grid->Add (s, wxGBPosition (r, 1));
199 add_label_to_sizer (_grid, _dcp_track_label, true, wxGBPosition(r, 0));
200 _grid->Add (_dcp_track, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
203 add_label_to_sizer (_grid, _language_label, true, wxGBPosition (r, 0));
204 _grid->Add (_language, wxGBPosition (r, 1));
207 add_label_to_sizer (_grid, _stream_label, true, wxGBPosition (r, 0));
208 _grid->Add (_stream, wxGBPosition (r, 1));
212 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
214 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
215 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
216 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
218 _grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
224 TextPanel::update_dcp_track_selection ()
226 optional<DCPTextTrack> selected;
228 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
229 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
231 optional<DCPTextTrack> dt = t->dcp_track();
232 if (dt && selected && *dt != *selected) {
234 } else if (!selected) {
241 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
242 if (!many && selected && *selected == i) {
243 _dcp_track->SetSelection (n);
248 if (!selected || many) {
249 _dcp_track->SetSelection (wxNOT_FOUND);
254 TextPanel::update_dcp_tracks ()
256 _dcp_track->Clear ();
257 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
258 /* XXX: don't display the "magic" track which has empty name and language;
259 this is a nasty hack (see also Film::closed_caption_tracks)
261 if (!i.name.empty() || !i.language.empty()) {
262 _dcp_track->Append (std_to_wx(i.summary()));
266 if (_parent->film()->closed_caption_tracks().size() < 6) {
267 _dcp_track->Append (_("Add new..."));
270 update_dcp_track_selection ();
274 TextPanel::dcp_track_changed ()
276 optional<DCPTextTrack> track;
278 if (_dcp_track->GetSelection() == int(_dcp_track->GetCount()) - 1) {
279 DCPTextTrackDialog* d = new DCPTextTrackDialog (this);
280 if (d->ShowModal() == wxID_OK) {
285 /* Find the DCPTextTrack that was selected */
286 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
287 if (i.summary() == wx_to_std(_dcp_track->GetStringSelection())) {
294 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
295 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
296 if (t && t->type() == TEXT_CLOSED_CAPTION) {
297 t->set_dcp_track(*track);
302 update_dcp_tracks ();
306 TextPanel::film_changed (Film::Property property)
308 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
309 setup_sensitivity ();
314 TextPanel::film_content_changed (int property)
316 FFmpegContentList fc = _parent->selected_ffmpeg ();
317 ContentList sc = _parent->selected_text ();
319 shared_ptr<FFmpegContent> fcs;
320 if (fc.size() == 1) {
324 shared_ptr<Content> scs;
325 if (sc.size() == 1) {
329 shared_ptr<TextContent> text;
331 text = scs->text_of_original_type(_original_type);
334 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
337 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
338 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
339 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
342 if (fcs->subtitle_stream()) {
343 checked_set (_stream, fcs->subtitle_stream()->identifier ());
345 _stream->SetSelection (wxNOT_FOUND);
348 setup_sensitivity ();
349 } else if (property == TextContentProperty::USE) {
350 checked_set (_use, text ? text->use() : false);
351 setup_sensitivity ();
352 } else if (property == TextContentProperty::TYPE) {
354 switch (text->type()) {
355 case TEXT_OPEN_SUBTITLE:
356 _type->SetSelection (0);
358 case TEXT_CLOSED_CAPTION:
359 _type->SetSelection (1);
362 DCPOMATIC_ASSERT (false);
365 _type->SetSelection (0);
367 setup_sensitivity ();
368 update_dcp_track_selection ();
369 } else if (property == TextContentProperty::BURN) {
370 checked_set (_burn, text ? text->burn() : false);
371 } else if (property == TextContentProperty::X_OFFSET) {
372 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
373 } else if (property == TextContentProperty::Y_OFFSET) {
374 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
375 } else if (property == TextContentProperty::X_SCALE) {
376 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
377 } else if (property == TextContentProperty::Y_SCALE) {
378 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
379 } else if (property == TextContentProperty::LINE_SPACING) {
380 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
381 } else if (property == TextContentProperty::LANGUAGE) {
382 checked_set (_language, text ? text->language() : "");
383 } else if (property == TextContentProperty::DCP_TRACK) {
384 update_dcp_track_selection ();
385 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
387 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
388 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
390 checked_set (_reference, false);
393 setup_sensitivity ();
394 } else if (property == DCPContentProperty::TEXTS) {
395 setup_sensitivity ();
400 TextPanel::use_toggled ()
402 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
403 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
407 /** @return the text type that is currently selected in the drop-down */
409 TextPanel::current_type () const
411 switch (_type->GetSelection()) {
413 return TEXT_OPEN_SUBTITLE;
415 return TEXT_CLOSED_CAPTION;
422 TextPanel::type_changed ()
424 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
425 i->text_of_original_type(_original_type)->set_type (current_type ());
430 TextPanel::burn_toggled ()
432 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
433 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
438 TextPanel::setup_sensitivity ()
441 /* We currently assume that FFmpeg subtitles are bitmapped */
443 /* DCP subs can't have their line spacing changed */
445 ContentList sel = _parent->selected_text ();
446 BOOST_FOREACH (shared_ptr<Content> i, sel) {
447 /* These are the content types that could include subtitles */
448 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
449 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
450 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
451 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
453 if (!fc->text.empty()) {
457 } else if (dc || dsc) {
461 /* XXX: in the future there could be bitmap subs from DCPs */
466 /* Decide whether we can reference these subs */
468 shared_ptr<DCPContent> dcp;
469 if (sel.size() == 1) {
470 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
474 bool const can_reference = dcp && dcp->can_reference_text (_parent->film(), _original_type, why_not);
475 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
477 bool const reference = _reference->GetValue ();
479 TextType const type = current_type ();
483 _type->Append (_("open subtitles"));
484 if (ffmpeg_subs == 0) {
485 _type->Append (_("closed captions"));
489 case TEXT_OPEN_SUBTITLE:
490 _type->SetSelection (0);
492 case TEXT_CLOSED_CAPTION:
493 if (_type->GetCount() > 1) {
494 _type->SetSelection (1);
501 /* Set up sensitivity */
502 _use->Enable (!reference && any_subs > 0);
503 bool const use = _use->GetValue ();
504 _type->Enable (!reference && any_subs > 0 && use);
505 _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
506 _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
507 _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
508 _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
509 _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
510 /* DCP subs ignore the line spacing setting */
511 _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE && dcp_subs < any_subs);
512 _dcp_track->Enable (!reference && any_subs > 0 && use && type == TEXT_CLOSED_CAPTION);
513 _language->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
514 _stream->Enable (!reference && ffmpeg_subs == 1);
515 /* Ideally we would check here to see if the FFmpeg content has "string" subs (i.e. not bitmaps) */
516 _text_view_button->Enable (!reference && any_subs > 0 && ffmpeg_subs == 0);
517 _fonts_dialog_button->Enable (!reference && any_subs > 0 && ffmpeg_subs == 0 && type == TEXT_OPEN_SUBTITLE);
518 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
522 TextPanel::stream_changed ()
524 FFmpegContentList fc = _parent->selected_ffmpeg ();
525 if (fc.size() != 1) {
529 shared_ptr<FFmpegContent> fcs = fc.front ();
531 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
532 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
533 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
534 while (i != a.end() && (*i)->identifier () != s) {
539 fcs->set_subtitle_stream (*i);
544 TextPanel::x_offset_changed ()
546 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
547 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
552 TextPanel::y_offset_changed ()
554 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
555 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
560 TextPanel::x_scale_changed ()
562 ContentList c = _parent->selected_text ();
564 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
569 TextPanel::y_scale_changed ()
571 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
572 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
577 TextPanel::line_spacing_changed ()
579 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
580 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
585 TextPanel::language_changed ()
587 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
588 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
593 TextPanel::content_selection_changed ()
595 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
596 film_content_changed (TextContentProperty::USE);
597 film_content_changed (TextContentProperty::BURN);
598 film_content_changed (TextContentProperty::X_OFFSET);
599 film_content_changed (TextContentProperty::Y_OFFSET);
600 film_content_changed (TextContentProperty::X_SCALE);
601 film_content_changed (TextContentProperty::Y_SCALE);
602 film_content_changed (TextContentProperty::LINE_SPACING);
603 film_content_changed (TextContentProperty::LANGUAGE);
604 film_content_changed (TextContentProperty::FONTS);
605 film_content_changed (TextContentProperty::TYPE);
606 film_content_changed (TextContentProperty::DCP_TRACK);
607 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
611 TextPanel::text_view_clicked ()
614 _text_view->Destroy ();
618 ContentList c = _parent->selected_text ();
619 DCPOMATIC_ASSERT (c.size() == 1);
621 shared_ptr<Decoder> decoder = decoder_factory (_parent->film(), c.front(), false);
624 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
630 TextPanel::fonts_dialog_clicked ()
633 _fonts_dialog->Destroy ();
637 ContentList c = _parent->selected_text ();
638 DCPOMATIC_ASSERT (c.size() == 1);
640 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
641 _fonts_dialog->Show ();
645 TextPanel::reference_clicked ()
647 ContentList c = _parent->selected ();
652 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
657 d->set_reference_text (_original_type, _reference->GetValue ());
661 TextPanel::appearance_dialog_clicked ()
663 ContentList c = _parent->selected_text ();
664 DCPOMATIC_ASSERT (c.size() == 1);
666 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type));
667 if (d->ShowModal () == wxID_OK) {