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 "lib/ffmpeg_content.h"
30 #include "lib/string_text_file_content.h"
31 #include "lib/ffmpeg_subtitle_stream.h"
32 #include "lib/dcp_subtitle_content.h"
33 #include "lib/string_text_file_decoder.h"
34 #include "lib/dcp_subtitle_decoder.h"
35 #include "lib/dcp_content.h"
36 #include "lib/text_content.h"
37 #include "lib/decoder_factory.h"
38 #include <wx/spinctrl.h>
39 #include <boost/foreach.hpp>
45 using boost::shared_ptr;
46 using boost::optional;
47 using boost::dynamic_pointer_cast;
49 /** @param t Original text type of the content, if known */
50 TextPanel::TextPanel (ContentPanel* p, TextType t)
51 : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
56 wxString refer = _("Use this DCP's subtitle as OV and make VF");
57 if (t == TEXT_CLOSED_CAPTION) {
58 refer = _("Use this DCP's closed caption as OV and make VF");
61 _reference = new wxCheckBox (this, wxID_ANY, refer);
62 _reference_note = new wxStaticText (this, wxID_ANY, _(""));
63 _reference_note->Wrap (200);
64 wxFont font = _reference_note->GetFont();
65 font.SetStyle(wxFONTSTYLE_ITALIC);
66 font.SetPointSize(font.GetPointSize() - 1);
67 _reference_note->SetFont(font);
69 _use = new wxCheckBox (this, wxID_ANY, _("Use as"));
70 _type = new wxChoice (this, wxID_ANY);
71 _type->Append (_("open subtitles"));
72 _type->Append (_("closed captions"));
74 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
76 _offset_label = create_label (this, _("Offset"), true);
77 _x_offset_label = create_label (this, _("X"), true);
78 _x_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
79 _x_offset_pc_label = new wxStaticText (this, wxID_ANY, _("%"));
80 _y_offset_label = create_label (this, _("Y"), true);
81 _y_offset = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
82 _y_offset_pc_label = new wxStaticText (this, wxID_ANY, _("%"));
84 _scale_label = create_label (this, _("Scale"), true);
85 _x_scale_label = create_label (this, _("X"), true);
86 _x_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
87 _x_scale_pc_label = new wxStaticText (this, wxID_ANY, _("%"));
88 _y_scale_label = create_label (this, _("Y"), true);
89 _y_scale = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(64, -1));
90 _y_scale_pc_label = new wxStaticText (this, wxID_ANY, _("%"));
92 _line_spacing_label = create_label (this, _("Line spacing"), true);
93 _line_spacing = new wxSpinCtrl (this);
94 _line_spacing_pc_label = new wxStaticText (this, wxID_ANY, _("%"));
96 _dcp_track_label = create_label (this, _("DCP track"), true);
97 _dcp_track = new wxChoice (this, wxID_ANY);
99 _language_label = create_label (this, _("Language"), true);
100 _language = new wxTextCtrl (this, wxID_ANY);
102 _stream_label = create_label (this, _("Stream"), true);
103 _stream = new wxChoice (this, wxID_ANY);
105 _text_view_button = new wxButton (this, wxID_ANY, _("View..."));
106 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
107 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
109 _x_offset->SetRange (-100, 100);
110 _y_offset->SetRange (-100, 100);
111 _x_scale->SetRange (10, 1000);
112 _y_scale->SetRange (10, 1000);
113 _line_spacing->SetRange (10, 1000);
115 update_dcp_tracks ();
117 content_selection_changed ();
119 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
120 _use->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
121 _type->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::type_changed, this));
122 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
123 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
124 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
125 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
126 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
127 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
128 _dcp_track->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::dcp_track_changed, this));
129 _language->Bind (wxEVT_TEXT, boost::bind (&TextPanel::language_changed, this));
130 _stream->Bind (wxEVT_CHOICE, boost::bind (&TextPanel::stream_changed, this));
131 _text_view_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::text_view_clicked, this));
132 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::fonts_dialog_clicked, this));
133 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&TextPanel::appearance_dialog_clicked, this));
139 TextPanel::add_to_grid ()
141 Config::Interface const interface = Config::instance()->interface_complexity();
145 _reference->Show (interface == Config::INTERFACE_FULL);
146 _reference_note->Show (interface == Config::INTERFACE_FULL);
148 if (interface == Config::INTERFACE_FULL) {
149 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
150 reference_sizer->Add (_reference, 0);
151 reference_sizer->Add (_reference_note, 0);
152 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
156 wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
157 use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
158 use->Add (_type, 1, wxEXPAND, 0);
159 _grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
162 _grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
165 add_label_to_sizer (_grid, _offset_label, true, wxGBPosition (r, 0));
166 wxBoxSizer* offset = new wxBoxSizer (wxHORIZONTAL);
167 add_label_to_sizer (offset, _x_offset_label, true);
168 offset->Add (_x_offset, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
169 offset->Add (_x_offset_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
170 add_label_to_sizer (offset, _y_offset_label, true);
171 offset->Add (_y_offset, 0);
172 add_label_to_sizer (offset, _y_offset_pc_label, false);
173 _grid->Add (offset, wxGBPosition (r, 1));
176 add_label_to_sizer (_grid, _scale_label, true, wxGBPosition (r, 0));
177 wxBoxSizer* scale = new wxBoxSizer (wxHORIZONTAL);
178 add_label_to_sizer (scale, _x_scale_label, true);
179 scale->Add (_x_scale, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
180 scale->Add (_x_scale_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
181 add_label_to_sizer (scale, _y_scale_label, true);
182 scale->Add (_y_scale, 0);
183 add_label_to_sizer (scale, _y_scale_pc_label, false);
184 _grid->Add (scale, wxGBPosition (r, 1));
188 add_label_to_sizer (_grid, _line_spacing_label, true, wxGBPosition (r, 0));
189 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
190 s->Add (_line_spacing);
191 add_label_to_sizer (s, _line_spacing_pc_label, false);
192 _grid->Add (s, wxGBPosition (r, 1));
196 add_label_to_sizer (_grid, _dcp_track_label, true, wxGBPosition(r, 0));
197 _grid->Add (_dcp_track, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
200 add_label_to_sizer (_grid, _language_label, true, wxGBPosition (r, 0));
201 _grid->Add (_language, wxGBPosition (r, 1));
204 add_label_to_sizer (_grid, _stream_label, true, wxGBPosition (r, 0));
205 _grid->Add (_stream, wxGBPosition (r, 1));
209 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
211 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
212 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
213 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
215 _grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
221 TextPanel::update_dcp_track_selection ()
223 optional<DCPTextTrack> selected;
225 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
226 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
228 optional<DCPTextTrack> dt = t->dcp_track();
229 if (dt && selected && *dt != *selected) {
231 } else if (!selected) {
238 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
239 if (!many && selected && *selected == i) {
240 _dcp_track->SetSelection (n);
245 if (!selected || many) {
246 _dcp_track->SetSelection (wxNOT_FOUND);
251 TextPanel::update_dcp_tracks ()
253 _dcp_track->Clear ();
254 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
255 _dcp_track->Append (std_to_wx(i.summary()));
258 if (_parent->film()->closed_caption_tracks().size() < 6) {
259 _dcp_track->Append (_("Add new..."));
262 update_dcp_track_selection ();
266 TextPanel::dcp_track_changed ()
268 optional<DCPTextTrack> track;
270 if (_dcp_track->GetSelection() == int(_dcp_track->GetCount()) - 1) {
271 DCPTextTrackDialog* d = new DCPTextTrackDialog (this);
272 if (d->ShowModal() == wxID_OK) {
277 /* Find the DCPTextTrack that was selected */
278 BOOST_FOREACH (DCPTextTrack i, _parent->film()->closed_caption_tracks()) {
279 if (i.summary() == wx_to_std(_dcp_track->GetStringSelection())) {
286 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
287 shared_ptr<TextContent> t = i->text_of_original_type(_original_type);
288 if (t && t->type() == TEXT_CLOSED_CAPTION) {
289 t->set_dcp_track(*track);
294 update_dcp_tracks ();
298 TextPanel::film_changed (Film::Property property)
300 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
301 setup_sensitivity ();
306 TextPanel::film_content_changed (int property)
308 FFmpegContentList fc = _parent->selected_ffmpeg ();
309 ContentList sc = _parent->selected_text ();
311 shared_ptr<FFmpegContent> fcs;
312 if (fc.size() == 1) {
316 shared_ptr<Content> scs;
317 if (sc.size() == 1) {
321 shared_ptr<TextContent> text;
323 text = scs->text_of_original_type(_original_type);
326 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
329 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
330 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
331 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
334 if (fcs->subtitle_stream()) {
335 checked_set (_stream, fcs->subtitle_stream()->identifier ());
337 _stream->SetSelection (wxNOT_FOUND);
340 setup_sensitivity ();
341 } else if (property == TextContentProperty::USE) {
342 checked_set (_use, text ? text->use() : false);
343 setup_sensitivity ();
344 } else if (property == TextContentProperty::TYPE) {
346 switch (text->type()) {
347 case TEXT_OPEN_SUBTITLE:
348 _type->SetSelection (0);
350 case TEXT_CLOSED_CAPTION:
351 _type->SetSelection (1);
354 DCPOMATIC_ASSERT (false);
357 _type->SetSelection (0);
359 setup_sensitivity ();
360 update_dcp_track_selection ();
361 } else if (property == TextContentProperty::BURN) {
362 checked_set (_burn, text ? text->burn() : false);
363 } else if (property == TextContentProperty::X_OFFSET) {
364 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
365 } else if (property == TextContentProperty::Y_OFFSET) {
366 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
367 } else if (property == TextContentProperty::X_SCALE) {
368 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
369 } else if (property == TextContentProperty::Y_SCALE) {
370 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
371 } else if (property == TextContentProperty::LINE_SPACING) {
372 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
373 } else if (property == TextContentProperty::LANGUAGE) {
374 checked_set (_language, text ? text->language() : "");
375 } else if (property == TextContentProperty::DCP_TRACK) {
376 update_dcp_track_selection ();
377 } else if (property == DCPContentProperty::REFERENCE_TEXT) {
379 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
380 checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
382 checked_set (_reference, false);
385 setup_sensitivity ();
386 } else if (property == DCPContentProperty::TEXTS) {
387 setup_sensitivity ();
392 TextPanel::use_toggled ()
394 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
395 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
399 /** @return the text type that is currently selected in the drop-down */
401 TextPanel::current_type () const
403 switch (_type->GetSelection()) {
405 return TEXT_OPEN_SUBTITLE;
407 return TEXT_CLOSED_CAPTION;
414 TextPanel::type_changed ()
416 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
417 i->text_of_original_type(_original_type)->set_type (current_type ());
422 TextPanel::burn_toggled ()
424 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
425 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
430 TextPanel::setup_sensitivity ()
434 ContentList sel = _parent->selected_text ();
435 BOOST_FOREACH (shared_ptr<Content> i, sel) {
436 /* These are the content types that could include subtitles */
437 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
438 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
439 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
440 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
442 if (!fc->text.empty()) {
446 } else if (sc || dc || dsc) {
447 /* XXX: in the future there could be bitmap subs from DCPs */
452 /* Decide whether we can reference these subs */
454 shared_ptr<DCPContent> dcp;
455 if (sel.size() == 1) {
456 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
460 bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
461 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
463 bool const reference = _reference->GetValue ();
465 TextType const type = current_type ();
467 /* Set up sensitivity */
468 _use->Enable (!reference && any_subs > 0);
469 bool const use = _use->GetValue ();
470 _type->Enable (!reference && any_subs > 0 && use);
471 _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
472 _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
473 _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
474 _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
475 _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
476 _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
477 _dcp_track->Enable (!reference && any_subs > 0 && use && type == TEXT_CLOSED_CAPTION);
478 _language->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
479 _stream->Enable (!reference && ffmpeg_subs == 1);
480 _text_view_button->Enable (!reference);
481 _fonts_dialog_button->Enable (!reference && type == TEXT_OPEN_SUBTITLE);
482 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
486 TextPanel::stream_changed ()
488 FFmpegContentList fc = _parent->selected_ffmpeg ();
489 if (fc.size() != 1) {
493 shared_ptr<FFmpegContent> fcs = fc.front ();
495 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
496 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
497 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
498 while (i != a.end() && (*i)->identifier () != s) {
503 fcs->set_subtitle_stream (*i);
508 TextPanel::x_offset_changed ()
510 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
511 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
516 TextPanel::y_offset_changed ()
518 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
519 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
524 TextPanel::x_scale_changed ()
526 ContentList c = _parent->selected_text ();
528 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
533 TextPanel::y_scale_changed ()
535 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
536 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
541 TextPanel::line_spacing_changed ()
543 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
544 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
549 TextPanel::language_changed ()
551 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
552 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
557 TextPanel::content_selection_changed ()
559 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
560 film_content_changed (TextContentProperty::USE);
561 film_content_changed (TextContentProperty::BURN);
562 film_content_changed (TextContentProperty::X_OFFSET);
563 film_content_changed (TextContentProperty::Y_OFFSET);
564 film_content_changed (TextContentProperty::X_SCALE);
565 film_content_changed (TextContentProperty::Y_SCALE);
566 film_content_changed (TextContentProperty::LINE_SPACING);
567 film_content_changed (TextContentProperty::LANGUAGE);
568 film_content_changed (TextContentProperty::FONTS);
569 film_content_changed (TextContentProperty::TYPE);
570 film_content_changed (TextContentProperty::DCP_TRACK);
571 film_content_changed (DCPContentProperty::REFERENCE_TEXT);
575 TextPanel::text_view_clicked ()
578 _text_view->Destroy ();
582 ContentList c = _parent->selected_text ();
583 DCPOMATIC_ASSERT (c.size() == 1);
585 shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
588 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
594 TextPanel::fonts_dialog_clicked ()
597 _fonts_dialog->Destroy ();
601 ContentList c = _parent->selected_text ();
602 DCPOMATIC_ASSERT (c.size() == 1);
604 _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
605 _fonts_dialog->Show ();
609 TextPanel::reference_clicked ()
611 ContentList c = _parent->selected ();
616 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
621 d->set_reference_text (_original_type, _reference->GetValue ());
625 TextPanel::appearance_dialog_clicked ()
627 ContentList c = _parent->selected_text ();
628 DCPOMATIC_ASSERT (c.size() == 1);
630 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
631 if (d->ShowModal () == wxID_OK) {