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 "caption_panel.h"
22 #include "film_editor.h"
24 #include "subtitle_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/text_caption_file_content.h"
30 #include "lib/ffmpeg_subtitle_stream.h"
31 #include "lib/dcp_text_content.h"
32 #include "lib/text_caption_file_decoder.h"
33 #include "lib/dcp_text_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>
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
46 CaptionPanel::CaptionPanel (ContentPanel* p)
47 : ContentSubPanel (p, _("Captions"))
51 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
53 _reference = new wxCheckBox (this, wxID_ANY, _("Use this DCP's subtitle as OV and make VF"));
54 reference_sizer->Add (_reference, 0, wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
56 _reference_note = new wxStaticText (this, wxID_ANY, _(""));
57 _reference_note->Wrap (200);
58 reference_sizer->Add (_reference_note, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
59 wxFont font = _reference_note->GetFont();
60 font.SetStyle(wxFONTSTYLE_ITALIC);
61 font.SetPointSize(font.GetPointSize() - 1);
62 _reference_note->SetFont(font);
64 _sizer->Add (reference_sizer);
66 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
67 _sizer->Add (grid, 0, wxALL, 8);
70 wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
71 _use = new wxCheckBox (this, wxID_ANY, _("Use as"));
72 use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
73 _type = new wxChoice (this, wxID_ANY);
74 _type->Append (_("subtitles (open captions)"));
75 _type->Append (_("closed captions"));
76 use->Add (_type, 1, wxEXPAND, 0);
77 grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
80 _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
81 grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
85 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
86 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
87 _x_offset = new wxSpinCtrl (this);
89 add_label_to_sizer (s, this, _("%"), false);
90 grid->Add (s, wxGBPosition (r, 1));
95 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
96 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
97 _y_offset = new wxSpinCtrl (this);
99 add_label_to_sizer (s, this, _("%"), false);
100 grid->Add (s, wxGBPosition (r, 1));
105 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
106 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
107 _x_scale = new wxSpinCtrl (this);
109 add_label_to_sizer (s, this, _("%"), false);
110 grid->Add (s, wxGBPosition (r, 1));
115 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
116 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
117 _y_scale = new wxSpinCtrl (this);
119 add_label_to_sizer (s, this, _("%"), false);
120 grid->Add (s, wxGBPosition (r, 1));
125 add_label_to_sizer (grid, this, _("Line spacing"), true, wxGBPosition (r, 0));
126 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
127 _line_spacing = new wxSpinCtrl (this);
128 s->Add (_line_spacing);
129 add_label_to_sizer (s, this, _("%"), false);
130 grid->Add (s, wxGBPosition (r, 1));
134 add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
135 _language = new wxTextCtrl (this, wxID_ANY);
136 grid->Add (_language, wxGBPosition (r, 1));
139 add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
140 _stream = new wxChoice (this, wxID_ANY);
141 grid->Add (_stream, wxGBPosition (r, 1));
145 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
147 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
148 s->Add (_subtitle_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
149 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
150 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
151 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
152 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
154 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
158 _x_offset->SetRange (-100, 100);
159 _y_offset->SetRange (-100, 100);
160 _x_scale->SetRange (10, 1000);
161 _y_scale->SetRange (10, 1000);
162 _line_spacing->SetRange (10, 1000);
164 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&CaptionPanel::reference_clicked, this));
165 _use->Bind (wxEVT_CHECKBOX, boost::bind (&CaptionPanel::use_toggled, this));
166 _type->Bind (wxEVT_CHOICE, boost::bind (&CaptionPanel::type_changed, this));
167 _burn->Bind (wxEVT_CHECKBOX, boost::bind (&CaptionPanel::burn_toggled, this));
168 _x_offset->Bind (wxEVT_SPINCTRL, boost::bind (&CaptionPanel::x_offset_changed, this));
169 _y_offset->Bind (wxEVT_SPINCTRL, boost::bind (&CaptionPanel::y_offset_changed, this));
170 _x_scale->Bind (wxEVT_SPINCTRL, boost::bind (&CaptionPanel::x_scale_changed, this));
171 _y_scale->Bind (wxEVT_SPINCTRL, boost::bind (&CaptionPanel::y_scale_changed, this));
172 _line_spacing->Bind (wxEVT_SPINCTRL, boost::bind (&CaptionPanel::line_spacing_changed, this));
173 _language->Bind (wxEVT_TEXT, boost::bind (&CaptionPanel::language_changed, this));
174 _stream->Bind (wxEVT_CHOICE, boost::bind (&CaptionPanel::stream_changed, this));
175 _subtitle_view_button->Bind (wxEVT_BUTTON, boost::bind (&CaptionPanel::subtitle_view_clicked, this));
176 _fonts_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&CaptionPanel::fonts_dialog_clicked, this));
177 _appearance_dialog_button->Bind (wxEVT_BUTTON, boost::bind (&CaptionPanel::appearance_dialog_clicked, this));
181 CaptionPanel::film_changed (Film::Property property)
183 if (property == Film::CONTENT || property == Film::REEL_TYPE) {
184 setup_sensitivity ();
189 CaptionPanel::film_content_changed (int property)
191 FFmpegContentList fc = _parent->selected_ffmpeg ();
192 ContentList sc = _parent->selected_subtitle ();
194 shared_ptr<FFmpegContent> fcs;
195 if (fc.size() == 1) {
199 shared_ptr<Content> scs;
200 if (sc.size() == 1) {
204 if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
207 vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
208 for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
209 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
212 if (fcs->subtitle_stream()) {
213 checked_set (_stream, fcs->subtitle_stream()->identifier ());
215 _stream->SetSelection (wxNOT_FOUND);
218 setup_sensitivity ();
219 } else if (property == TextContentProperty::USE) {
220 checked_set (_use, scs ? scs->subtitle->use() : false);
221 setup_sensitivity ();
222 } else if (property == TextContentProperty::TYPE) {
224 switch (scs->subtitle->type()) {
226 _type->SetSelection (0);
228 case TEXT_CLOSED_CAPTION:
229 _type->SetSelection (1);
232 DCPOMATIC_ASSERT (false);
235 _type->SetSelection (0);
237 setup_sensitivity ();
238 } else if (property == TextContentProperty::BURN) {
239 checked_set (_burn, scs ? scs->subtitle->burn() : false);
240 } else if (property == TextContentProperty::X_OFFSET) {
241 checked_set (_x_offset, scs ? lrint (scs->subtitle->x_offset() * 100) : 0);
242 } else if (property == TextContentProperty::Y_OFFSET) {
243 checked_set (_y_offset, scs ? lrint (scs->subtitle->y_offset() * 100) : 0);
244 } else if (property == TextContentProperty::X_SCALE) {
245 checked_set (_x_scale, scs ? lrint (scs->subtitle->x_scale() * 100) : 100);
246 } else if (property == TextContentProperty::Y_SCALE) {
247 checked_set (_y_scale, scs ? lrint (scs->subtitle->y_scale() * 100) : 100);
248 } else if (property == TextContentProperty::LINE_SPACING) {
249 checked_set (_line_spacing, scs ? lrint (scs->subtitle->line_spacing() * 100) : 100);
250 } else if (property == TextContentProperty::LANGUAGE) {
251 checked_set (_language, scs ? scs->subtitle->language() : "");
252 } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
254 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
255 checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
257 checked_set (_reference, false);
260 setup_sensitivity ();
261 } else if (property == DCPContentProperty::HAS_SUBTITLES) {
262 setup_sensitivity ();
267 CaptionPanel::use_toggled ()
269 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
270 i->subtitle->set_use (_use->GetValue());
275 CaptionPanel::type_changed ()
277 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle()) {
278 switch (_type->GetSelection()) {
280 i->subtitle->set_type (TEXT_SUBTITLE);
283 i->subtitle->set_type (TEXT_CLOSED_CAPTION);
290 CaptionPanel::burn_toggled ()
292 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
293 i->subtitle->set_burn (_burn->GetValue());
298 CaptionPanel::setup_sensitivity ()
302 ContentList sel = _parent->selected_subtitle ();
303 BOOST_FOREACH (shared_ptr<Content> i, sel) {
304 /* These are the content types that could include subtitles */
305 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
306 shared_ptr<const TextCaptionFileContent> sc = boost::dynamic_pointer_cast<const TextCaptionFileContent> (i);
307 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
308 shared_ptr<const DCPTextContent> dsc = boost::dynamic_pointer_cast<const DCPTextContent> (i);
314 } else if (sc || dc || dsc) {
315 /* XXX: in the future there could be bitmap subs from DCPs */
320 /* Decide whether we can reference these subs */
322 shared_ptr<DCPContent> dcp;
323 if (sel.size() == 1) {
324 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
328 bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
329 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
331 bool const reference = _reference->GetValue ();
333 /* Set up sensitivity */
334 _use->Enable (!reference && any_subs > 0);
335 bool const use = _use->GetValue ();
336 _type->Enable (!reference && any_subs > 0 && use);
337 _burn->Enable (!reference && any_subs > 0 && use && _type->GetSelection() == 0);
338 _x_offset->Enable (!reference && any_subs > 0 && use);
339 _y_offset->Enable (!reference && any_subs > 0 && use);
340 _x_scale->Enable (!reference && any_subs > 0 && use);
341 _y_scale->Enable (!reference && any_subs > 0 && use);
342 _line_spacing->Enable (!reference && use);
343 _language->Enable (!reference && any_subs > 0 && use);
344 _stream->Enable (!reference && ffmpeg_subs == 1);
345 _subtitle_view_button->Enable (!reference);
346 _fonts_dialog_button->Enable (!reference);
347 _appearance_dialog_button->Enable (!reference && any_subs > 0 && use);
351 CaptionPanel::stream_changed ()
353 FFmpegContentList fc = _parent->selected_ffmpeg ();
354 if (fc.size() != 1) {
358 shared_ptr<FFmpegContent> fcs = fc.front ();
360 vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
361 vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
362 string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
363 while (i != a.end() && (*i)->identifier () != s) {
368 fcs->set_subtitle_stream (*i);
373 CaptionPanel::x_offset_changed ()
375 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
376 i->subtitle->set_x_offset (_x_offset->GetValue() / 100.0);
381 CaptionPanel::y_offset_changed ()
383 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
384 i->subtitle->set_y_offset (_y_offset->GetValue() / 100.0);
389 CaptionPanel::x_scale_changed ()
391 ContentList c = _parent->selected_subtitle ();
393 c.front()->subtitle->set_x_scale (_x_scale->GetValue() / 100.0);
398 CaptionPanel::y_scale_changed ()
400 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
401 i->subtitle->set_y_scale (_y_scale->GetValue() / 100.0);
406 CaptionPanel::line_spacing_changed ()
408 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
409 i->subtitle->set_line_spacing (_line_spacing->GetValue() / 100.0);
414 CaptionPanel::language_changed ()
416 BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
417 i->subtitle->set_language (wx_to_std (_language->GetValue()));
422 CaptionPanel::content_selection_changed ()
424 film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
425 film_content_changed (TextContentProperty::USE);
426 film_content_changed (TextContentProperty::BURN);
427 film_content_changed (TextContentProperty::X_OFFSET);
428 film_content_changed (TextContentProperty::Y_OFFSET);
429 film_content_changed (TextContentProperty::X_SCALE);
430 film_content_changed (TextContentProperty::Y_SCALE);
431 film_content_changed (TextContentProperty::LINE_SPACING);
432 film_content_changed (TextContentProperty::LANGUAGE);
433 film_content_changed (TextContentProperty::FONTS);
434 film_content_changed (TextContentProperty::TYPE);
435 film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
439 CaptionPanel::subtitle_view_clicked ()
441 if (_subtitle_view) {
442 _subtitle_view->Destroy ();
446 ContentList c = _parent->selected_subtitle ();
447 DCPOMATIC_ASSERT (c.size() == 1);
449 shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
452 _subtitle_view = new SubtitleView (this, _parent->film(), c.front(), decoder, _parent->film_viewer());
453 _subtitle_view->Show ();
458 CaptionPanel::fonts_dialog_clicked ()
461 _fonts_dialog->Destroy ();
465 ContentList c = _parent->selected_subtitle ();
466 DCPOMATIC_ASSERT (c.size() == 1);
468 _fonts_dialog = new FontsDialog (this, c.front ());
469 _fonts_dialog->Show ();
473 CaptionPanel::reference_clicked ()
475 ContentList c = _parent->selected ();
480 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
485 d->set_reference_subtitle (_reference->GetValue ());
489 CaptionPanel::appearance_dialog_clicked ()
491 ContentList c = _parent->selected_subtitle ();
492 DCPOMATIC_ASSERT (c.size() == 1);
494 SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front());
495 if (d->ShowModal () == wxID_OK) {