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, _("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 _dcp_track->Append (std_to_wx(i.summary()));
261 if (_parent->film()->closed_caption_tracks().size() < 6) {
262 _dcp_track->Append (_("Add new..."));
265 update_dcp_track_selection ();
269 TextPanel::dcp_track_changed ()
271 optional<DCPTextTrack> track;
273 if (_dcp_track->GetSelection() == int(_dcp_track->GetCount()) - 1) {
274 DCPTextTrackDialog* d = new DCPTextTrackDialog (this);
275 if (d->ShowModal() == wxID_OK) {
280 /* Find the DCPTextTrack that was selected */
281 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
282 if (i.summary() == wx_to_std(_dcp_track->GetStringSelection())) {
289 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
290 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
291 if (t && t->type() == TEXT_CLOSED_CAPTION) {
292 t->set_dcp_track(*track);
297 update_dcp_tracks ();
301 TextPanel::film_changed (Film::Property property)
303 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
304 setup_sensitivity ();
309 TextPanel::film_content_changed (int property)
311 FFmpegContentList fc = _parent->selected_ffmpeg ();
312 ContentList sc = _parent->selected_text ();
314 shared_ptr<FFmpegContent> fcs;
315 if (fc.size() == 1) {
319 shared_ptr<Content> scs;
320 if (sc.size() == 1) {
324 shared_ptr<TextContent> text;
326 text = scs->text_of_original_type(_original_type);
329 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
332 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
333 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
334 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
337 if (fcs->subtitle_stream()) {
338 checked_set (_stream, fcs->subtitle_stream()->identifier ());
340 _stream->SetSelection (wxNOT_FOUND);
343 setup_sensitivity ();
344 } else if (property == TextContentProperty::USE) {
345 checked_set (_use, text ? text->use() : false);
346 setup_sensitivity ();
347 } else if (property == TextContentProperty::TYPE) {
349 switch (text->type()) {
350 case TEXT_OPEN_SUBTITLE:
351 _type->SetSelection (0);
353 case TEXT_CLOSED_CAPTION:
354 _type->SetSelection (1);
357 DCPOMATIC_ASSERT (false);
360 _type->SetSelection (0);
362 setup_sensitivity ();
363 update_dcp_track_selection ();
364 } else if (property == TextContentProperty::BURN) {
365 checked_set (_burn, text ? text->burn() : false);
366 } else if (property == TextContentProperty::X_OFFSET) {
367 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
368 } else if (property == TextContentProperty::Y_OFFSET) {
369 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
370 } else if (property == TextContentProperty::X_SCALE) {
371 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
372 } else if (property == TextContentProperty::Y_SCALE) {
373 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
374 } else if (property == TextContentProperty::LINE_SPACING) {
375 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
376 } else if (property == TextContentProperty::LANGUAGE) {
377 checked_set (_language, text ? text->language() : "");
378 } else if (property == TextContentProperty::DCP_TRACK) {
379 update_dcp_track_selection ();
380 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
382 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
383 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
385 checked_set (_reference, false);
388 setup_sensitivity ();
389 } else if (property == DCPContentProperty::TEXTS) {
390 setup_sensitivity ();
395 TextPanel::use_toggled ()
397 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
398 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
402 /** @return the text type that is currently selected in the drop-down */
404 TextPanel::current_type () const
406 switch (_type->GetSelection()) {
408 return TEXT_OPEN_SUBTITLE;
410 return TEXT_CLOSED_CAPTION;
417 TextPanel::type_changed ()
419 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
420 i->text_of_original_type(_original_type)->set_type (current_type ());
425 TextPanel::burn_toggled ()
427 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
428 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
433 TextPanel::setup_sensitivity ()
436 /* we currently assume that FFmpeg subtitles are bitmapped */
438 ContentList sel = _parent->selected_text ();
439 BOOST_FOREACH (shared_ptr<Content> i, sel) {
440 /* These are the content types that could include subtitles */
441 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
442 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
443 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
444 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
446 if (!fc->text.empty()) {
450 } else if (sc || dc || dsc) {
451 /* XXX: in the future there could be bitmap subs from DCPs */
456 /* Decide whether we can reference these subs */
458 shared_ptr<DCPContent> dcp;
459 if (sel.size() == 1) {
460 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
464 bool const can_reference = dcp && dcp->can_reference_text (_parent->film(), _original_type, why_not);
465 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
467 bool const reference = _reference->GetValue ();
469 TextType const type = current_type ();
473 _type->Append (_("open subtitles"));
474 if (ffmpeg_subs == 0) {
475 _type->Append (_("closed captions"));
479 case TEXT_OPEN_SUBTITLE:
480 _type->SetSelection (0);
482 case TEXT_CLOSED_CAPTION:
483 if (_type->GetCount() > 1) {
484 _type->SetSelection (1);
491 /* Set up sensitivity */
492 _use->Enable (!reference && any_subs > 0);
493 bool const use = _use->GetValue ();
494 _type->Enable (!reference && any_subs > 0 && use);
495 _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
496 _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
497 _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
498 _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
499 _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
500 _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
501 _dcp_track->Enable (!reference && any_subs > 0 && use && type == TEXT_CLOSED_CAPTION);
502 _language->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
503 _stream->Enable (!reference && ffmpeg_subs == 1);
504 /* Ideally we would check here to see if the FFmpeg content has "string" subs (i.e. not bitmaps) */
505 _text_view_button->Enable (!reference && any_subs > 0 && ffmpeg_subs == 0);
506 _fonts_dialog_button->Enable (!reference && any_subs > 0 && ffmpeg_subs == 0 && type == TEXT_OPEN_SUBTITLE);
507 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
511 TextPanel::stream_changed ()
513 FFmpegContentList fc = _parent->selected_ffmpeg ();
514 if (fc.size() != 1) {
518 shared_ptr<FFmpegContent> fcs = fc.front ();
520 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
521 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
522 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
523 while (i != a.end() && (*i)->identifier () != s) {
528 fcs->set_subtitle_stream (*i);
533 TextPanel::x_offset_changed ()
535 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
536 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
541 TextPanel::y_offset_changed ()
543 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
544 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
549 TextPanel::x_scale_changed ()
551 ContentList c = _parent->selected_text ();
553 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
558 TextPanel::y_scale_changed ()
560 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
561 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
566 TextPanel::line_spacing_changed ()
568 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
569 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
574 TextPanel::language_changed ()
576 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
577 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
582 TextPanel::content_selection_changed ()
584 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
585 film_content_changed (TextContentProperty::USE);
586 film_content_changed (TextContentProperty::BURN);
587 film_content_changed (TextContentProperty::X_OFFSET);
588 film_content_changed (TextContentProperty::Y_OFFSET);
589 film_content_changed (TextContentProperty::X_SCALE);
590 film_content_changed (TextContentProperty::Y_SCALE);
591 film_content_changed (TextContentProperty::LINE_SPACING);
592 film_content_changed (TextContentProperty::LANGUAGE);
593 film_content_changed (TextContentProperty::FONTS);
594 film_content_changed (TextContentProperty::TYPE);
595 film_content_changed (TextContentProperty::DCP_TRACK);
596 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
600 TextPanel::text_view_clicked ()
603 _text_view->Destroy ();
607 ContentList c = _parent->selected_text ();
608 DCPOMATIC_ASSERT (c.size() == 1);
610 shared_ptr<Decoder> decoder = decoder_factory (_parent->film(), c.front(), false);
613 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
619 TextPanel::fonts_dialog_clicked ()
622 _fonts_dialog->Destroy ();
626 ContentList c = _parent->selected_text ();
627 DCPOMATIC_ASSERT (c.size() == 1);
629 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
630 _fonts_dialog->Show ();
634 TextPanel::reference_clicked ()
636 ContentList c = _parent->selected ();
641 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
646 d->set_reference_text (_original_type, _reference->GetValue ());
650 TextPanel::appearance_dialog_clicked ()
652 ContentList c = _parent->selected_text ();
653 DCPOMATIC_ASSERT (c.size() == 1);
655 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type));
656 if (d->ShowModal () == wxID_OK) {