Rather hacky support for burnt subtitle Z-position in 3D (#1359).
[dcpomatic.git] / src / wx / text_panel.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "check_box.h"
23 #include "content_panel.h"
24 #include "dcp_text_track_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "dcpomatic_spin_ctrl.h"
27 #include "film_editor.h"
28 #include "film_viewer.h"
29 #include "fonts_dialog.h"
30 #include "language_tag_widget.h"
31 #include "static_text.h"
32 #include "subtitle_appearance_dialog.h"
33 #include "text_panel.h"
34 #include "text_view.h"
35 #include "wx_util.h"
36 #include "lib/analyse_subtitles_job.h"
37 #include "lib/dcp_content.h"
38 #include "lib/dcp_subtitle_content.h"
39 #include "lib/dcp_subtitle_decoder.h"
40 #include "lib/decoder_factory.h"
41 #include "lib/ffmpeg_content.h"
42 #include "lib/ffmpeg_subtitle_stream.h"
43 #include "lib/job_manager.h"
44 #include "lib/string_text_file_content.h"
45 #include "lib/string_text_file_decoder.h"
46 #include "lib/subtitle_analysis.h"
47 #include "lib/text_content.h"
48 #include <dcp/warnings.h>
49 LIBDCP_DISABLE_WARNINGS
50 #include <wx/spinctrl.h>
51 LIBDCP_ENABLE_WARNINGS
52
53
54 using std::cout;
55 using std::dynamic_pointer_cast;
56 using std::list;
57 using std::shared_ptr;
58 using std::string;
59 using std::vector;
60 using boost::bind;
61 using boost::optional;
62
63
64 /** @param t Original text type of the content, if known */
65 TextPanel::TextPanel (ContentPanel* p, TextType t)
66         : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
67         , _original_type (t)
68 {
69
70 }
71
72
73 void
74 TextPanel::create ()
75 {
76         wxString refer = _("Use this DCP's subtitle as OV and make VF");
77         if (_original_type == TextType::CLOSED_CAPTION) {
78                 refer = _("Use this DCP's closed caption as OV and make VF");
79         }
80
81         _reference = new CheckBox (this, refer);
82         _reference_note = new StaticText (this, wxT(""));
83         _reference_note->Wrap (200);
84         auto font = _reference_note->GetFont();
85         font.SetStyle(wxFONTSTYLE_ITALIC);
86         font.SetPointSize(font.GetPointSize() - 1);
87         _reference_note->SetFont(font);
88
89         _use = new CheckBox (this, _("Use as"));
90         _type = new wxChoice (this, wxID_ANY);
91         _type->Append (_("open subtitles"));
92         _type->Append (_("closed captions"));
93
94         _burn = new CheckBox (this, _("Burn subtitles into image"));
95
96         _offset_label = create_label (this, _("Offset"), true);
97         _x_offset_label = create_label (this, _("X"), true);
98         _x_offset = new SpinCtrl (this, DCPOMATIC_SPIN_CTRL_WIDTH);
99         _x_offset_pc_label = new StaticText (this, _("%"));
100         _y_offset_label = create_label (this, _("Y"), true);
101         _y_offset = new SpinCtrl (this, DCPOMATIC_SPIN_CTRL_WIDTH);
102         _y_offset_pc_label = new StaticText (this, _("%"));
103
104         _scale_label = create_label (this, _("Scale"), true);
105         _x_scale_label = create_label (this, _("X"), true);
106         _x_scale = new SpinCtrl (this, DCPOMATIC_SPIN_CTRL_WIDTH);
107         _x_scale_pc_label = new StaticText (this, _("%"));
108         _y_scale_label = create_label (this, S_("Coord|Y"), true);
109         _y_scale = new SpinCtrl (this, DCPOMATIC_SPIN_CTRL_WIDTH);
110         _y_scale_pc_label = new StaticText (this, _("%"));
111
112         _line_spacing_label = create_label (this, _("Line spacing"), true);
113         _line_spacing = new SpinCtrl (this, DCPOMATIC_SPIN_CTRL_WIDTH);
114         _line_spacing_pc_label = new StaticText (this, _("%"));
115
116         _z_position_label = create_label (this, _("Z position"), true);
117         _z_position = new SpinCtrl (this, DCPOMATIC_SPIN_CTRL_WIDTH);
118
119         _stream_label = create_label (this, _("Stream"), true);
120         _stream = new wxChoice (this, wxID_ANY);
121
122         _text_view_button = new Button (this, _("View..."));
123         _fonts_dialog_button = new Button (this, _("Fonts..."));
124         _appearance_dialog_button = new Button (this, _("Appearance..."));
125
126         _x_offset->SetRange (-100, 100);
127         _y_offset->SetRange (-100, 100);
128         _x_scale->SetRange (0, 1000);
129         _y_scale->SetRange (0, 1000);
130         _line_spacing->SetRange (0, 1000);
131
132         _reference->Bind                (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
133         _use->Bind                      (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
134         _type->Bind                     (wxEVT_CHOICE,   boost::bind (&TextPanel::type_changed, this));
135         _burn->Bind                     (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
136         _x_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
137         _y_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
138         _x_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
139         _y_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
140         _line_spacing->Bind             (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
141         _z_position->Bind               (wxEVT_SPINCTRL, boost::bind (&TextPanel::z_position_changed, this));
142         _stream->Bind                   (wxEVT_CHOICE,   boost::bind (&TextPanel::stream_changed, this));
143         _text_view_button->Bind         (wxEVT_BUTTON,   boost::bind (&TextPanel::text_view_clicked, this));
144         _fonts_dialog_button->Bind      (wxEVT_BUTTON,   boost::bind (&TextPanel::fonts_dialog_clicked, this));
145         _appearance_dialog_button->Bind (wxEVT_BUTTON,   boost::bind (&TextPanel::appearance_dialog_clicked, this));
146
147         add_to_grid();
148         content_selection_changed ();
149
150         _sizer->Layout ();
151 }
152
153
154 void
155 TextPanel::setup_visibility ()
156 {
157         switch (current_type()) {
158         case TextType::OPEN_SUBTITLE:
159                 if (_dcp_track_label) {
160                         _dcp_track_label->Destroy ();
161                         _dcp_track_label = nullptr;
162                 }
163                 if (_dcp_track) {
164                         _dcp_track->Destroy ();
165                         _dcp_track = nullptr;
166                 }
167                 if (!_outline_subtitles) {
168                         _outline_subtitles = new CheckBox (this, _("Show subtitle area"));
169                         _outline_subtitles->Bind (wxEVT_CHECKBOX, boost::bind (&TextPanel::outline_subtitles_changed, this));
170                         _grid->Add (_outline_subtitles, wxGBPosition(_outline_subtitles_row, 0), wxGBSpan(1, 2));
171                 }
172                 if (!_language) {
173                         _language_label = create_label (this, _("Language"), true);
174                         add_label_to_sizer (_grid, _language_label, true, wxGBPosition(_ccap_track_or_language_row, 0));
175                         _language_sizer = new wxBoxSizer (wxHORIZONTAL);
176                         _language = new LanguageTagWidget (this, _("Language of these subtitles"), boost::none, wxString("en-US-"));
177                         _language->Changed.connect (boost::bind(&TextPanel::language_changed, this));
178                         _language_sizer->Add (_language->sizer(), 1, wxRIGHT, DCPOMATIC_SIZER_GAP);
179                         _language_type = new wxChoice (this, wxID_ANY);
180                         /// TRANSLATORS: Main and Additional here are a choice for whether a set of subtitles is in the "main" language of the
181                         /// film or an "additional" language.
182                         _language_type->Append (_("Main"));
183                         _language_type->Append (_("Additional"));
184                         _language_type->Bind (wxEVT_CHOICE, boost::bind(&TextPanel::language_is_additional_changed, this));
185                         _language_sizer->Add (_language_type, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_CHOICE_TOP_PAD);
186                         _grid->Add (_language_sizer, wxGBPosition(_ccap_track_or_language_row, 1), wxGBSpan(1, 2));
187                         film_content_changed (TextContentProperty::LANGUAGE);
188                         film_content_changed (TextContentProperty::LANGUAGE_IS_ADDITIONAL);
189                 }
190                 break;
191         case TextType::CLOSED_CAPTION:
192                 if (_language_label) {
193                         _language_label->Destroy ();
194                         _language_label = nullptr;
195                         _grid->Remove (_language->sizer());
196                         delete _language;
197                         _grid->Remove (_language_sizer);
198                         _language_sizer = nullptr;
199                         _language = nullptr;
200                         _language_type->Destroy ();
201                         _language_type = nullptr;
202                 }
203                 if (!_dcp_track_label) {
204                         _dcp_track_label = create_label (this, _("CCAP track"), true);
205                         add_label_to_sizer (_grid, _dcp_track_label, true, wxGBPosition(_ccap_track_or_language_row, 0));
206                 }
207                 if (!_dcp_track) {
208                         _dcp_track = new wxChoice (this, wxID_ANY);
209                         _dcp_track->Bind (wxEVT_CHOICE, boost::bind(&TextPanel::dcp_track_changed, this));
210                         _grid->Add (_dcp_track, wxGBPosition(_ccap_track_or_language_row, 1), wxDefaultSpan, wxEXPAND);
211                         update_dcp_tracks ();
212                         film_content_changed (TextContentProperty::DCP_TRACK);
213                 }
214                 if (_outline_subtitles) {
215                         _outline_subtitles->Destroy ();
216                         _outline_subtitles = nullptr;
217                         clear_outline_subtitles ();
218                 }
219                 break;
220         default:
221                 break;
222         }
223
224         _grid->Layout ();
225 }
226
227
228 void
229 TextPanel::add_to_grid ()
230 {
231         int r = 0;
232
233         auto reference_sizer = new wxBoxSizer (wxVERTICAL);
234         reference_sizer->Add (_reference, 0);
235         reference_sizer->Add (_reference_note, 0);
236         _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
237         ++r;
238
239         auto use = new wxBoxSizer (wxHORIZONTAL);
240         use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
241         use->Add (_type, 1, wxEXPAND, 0);
242         _grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
243         ++r;
244
245         _grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
246         ++r;
247
248         _outline_subtitles_row = r;
249         ++r;
250
251         add_label_to_sizer (_grid, _offset_label, true, wxGBPosition (r, 0));
252         auto offset = new wxBoxSizer (wxHORIZONTAL);
253         add_label_to_sizer (offset, _x_offset_label, true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
254         offset->Add (_x_offset, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
255         offset->Add (_x_offset_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
256 #ifdef __WXGTK3__
257         _grid->Add (offset, wxGBPosition(r, 1));
258         ++r;
259         offset = new wxBoxSizer (wxHORIZONTAL);
260 #endif
261         add_label_to_sizer (offset, _y_offset_label, true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
262         offset->Add (_y_offset, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
263         add_label_to_sizer (offset, _y_offset_pc_label, false, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
264         _grid->Add (offset, wxGBPosition (r, 1));
265         ++r;
266
267         add_label_to_sizer (_grid, _scale_label, true, wxGBPosition (r, 0));
268         auto scale = new wxBoxSizer (wxHORIZONTAL);
269         add_label_to_sizer (scale, _x_scale_label, true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
270         scale->Add (_x_scale, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
271         scale->Add (_x_scale_pc_label, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP * 2);
272 #ifdef __WXGTK3__
273         _grid->Add (scale, wxGBPosition(r, 1));
274         ++r;
275         scale = new wxBoxSizer (wxHORIZONTAL);
276 #endif
277         add_label_to_sizer (scale, _y_scale_label, true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
278         scale->Add (_y_scale, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
279         add_label_to_sizer (scale, _y_scale_pc_label, false, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
280         _grid->Add (scale, wxGBPosition (r, 1));
281         ++r;
282
283         {
284                 add_label_to_sizer (_grid, _line_spacing_label, true, wxGBPosition (r, 0));
285                 auto s = new wxBoxSizer (wxHORIZONTAL);
286                 s->Add (_line_spacing);
287                 add_label_to_sizer (s, _line_spacing_pc_label, false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
288                 _grid->Add (s, wxGBPosition (r, 1));
289                 ++r;
290         }
291
292         add_label_to_sizer (_grid, _z_position_label, true, wxGBPosition(r, 0));
293         _grid->Add (_z_position, wxGBPosition(r, 1));
294         ++r;
295
296         _ccap_track_or_language_row = r;
297         ++r;
298
299         add_label_to_sizer (_grid, _stream_label, true, wxGBPosition (r, 0));
300         _grid->Add (_stream, wxGBPosition (r, 1));
301         ++r;
302
303         {
304                 auto s = new wxBoxSizer (wxHORIZONTAL);
305
306                 s->Add (_text_view_button, 0, wxALL, DCPOMATIC_SIZER_GAP);
307                 s->Add (_fonts_dialog_button, 0, wxALL, DCPOMATIC_SIZER_GAP);
308                 s->Add (_appearance_dialog_button, 0, wxALL, DCPOMATIC_SIZER_GAP);
309
310                 _grid->Add (s, wxGBPosition(r, 0), wxGBSpan(1, 2));
311                 ++r;
312         }
313
314         setup_visibility ();
315 }
316
317
318 void
319 TextPanel::update_dcp_track_selection ()
320 {
321         DCPOMATIC_ASSERT (_dcp_track);
322
323         optional<DCPTextTrack> selected;
324         bool many = false;
325         for (auto i: _parent->selected_text()) {
326                 auto t = i->text_of_original_type(_original_type);
327                 if (t) {
328                         auto dt = t->dcp_track();
329                         if (dt && selected && *dt != *selected) {
330                                 many = true;
331                         } else if (!selected) {
332                                 selected = dt;
333                         }
334                 }
335         }
336
337         int n = 0;
338         for (auto i: _parent->film()->closed_caption_tracks()) {
339                 if (!many && selected && *selected == i) {
340                         _dcp_track->SetSelection (n);
341                 }
342                 ++n;
343         }
344
345         if (!selected || many) {
346                 _dcp_track->SetSelection (wxNOT_FOUND);
347         }
348 }
349
350
351 void
352 TextPanel::update_dcp_tracks ()
353 {
354         DCPOMATIC_ASSERT (_dcp_track);
355
356         _dcp_track->Clear ();
357         for (auto i: _parent->film()->closed_caption_tracks()) {
358                 /* XXX: don't display the "magic" track which has empty name and language;
359                    this is a nasty hack (see also Film::closed_caption_tracks)
360                 */
361                 if (!i.name.empty() || i.language) {
362                         _dcp_track->Append (std_to_wx(i.summary()));
363                 }
364         }
365
366         if (_parent->film()->closed_caption_tracks().size() < 6) {
367                 _dcp_track->Append (_("Add new..."));
368         }
369
370         update_dcp_track_selection ();
371 }
372
373
374 void
375 TextPanel::dcp_track_changed ()
376 {
377         optional<DCPTextTrack> track;
378
379         if (_dcp_track->GetSelection() == int(_dcp_track->GetCount()) - 1) {
380                 auto d = new DCPTextTrackDialog (this);
381                 if (d->ShowModal() == wxID_OK) {
382                         track = d->get();
383                 }
384                 d->Destroy ();
385         } else {
386                 /* Find the DCPTextTrack that was selected */
387                 for (auto i: _parent->film()->closed_caption_tracks()) {
388                         if (i.summary() == wx_to_std(_dcp_track->GetStringSelection())) {
389                                 track = i;
390                         }
391                 }
392         }
393
394         if (track) {
395                 for (auto i: _parent->selected_text()) {
396                         auto t = i->text_of_original_type(_original_type);
397                         if (t && t->type() == TextType::CLOSED_CAPTION) {
398                                 t->set_dcp_track(*track);
399                         }
400                 }
401         }
402
403         update_dcp_tracks ();
404 }
405
406
407 void
408 TextPanel::film_changed (Film::Property property)
409 {
410         if (property == Film::Property::CONTENT || property == Film::Property::REEL_TYPE || property == Film::Property::INTEROP) {
411                 setup_sensitivity ();
412         }
413 }
414
415
416 void
417 TextPanel::film_content_changed (int property)
418 {
419         auto fc = _parent->selected_ffmpeg ();
420         auto sc = _parent->selected_text ();
421
422         shared_ptr<FFmpegContent> fcs;
423         if (fc.size() == 1) {
424                 fcs = fc.front ();
425         }
426
427         shared_ptr<Content> scs;
428         if (sc.size() == 1) {
429                 scs = sc.front ();
430         }
431
432         shared_ptr<TextContent> text;
433         if (scs) {
434                 text = scs->text_of_original_type(_original_type);
435         }
436
437         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
438                 _stream->Clear ();
439                 if (fcs) {
440                         for (auto i: fcs->subtitle_streams()) {
441                                 _stream->Append (std_to_wx(i->name), new wxStringClientData(std_to_wx(i->identifier())));
442                         }
443
444                         if (fcs->subtitle_stream()) {
445                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
446                         } else {
447                                 _stream->SetSelection (wxNOT_FOUND);
448                         }
449                 }
450                 setup_sensitivity ();
451                 clear_outline_subtitles ();
452         } else if (property == TextContentProperty::USE) {
453                 checked_set (_use, text ? text->use() : false);
454                 setup_sensitivity ();
455                 clear_outline_subtitles ();
456         } else if (property == TextContentProperty::TYPE) {
457                 if (text) {
458                         switch (text->type()) {
459                         case TextType::OPEN_SUBTITLE:
460                                 _type->SetSelection (0);
461                                 break;
462                         case TextType::CLOSED_CAPTION:
463                                 _type->SetSelection (1);
464                                 break;
465                         default:
466                                 DCPOMATIC_ASSERT (false);
467                         }
468                 } else {
469                         _type->SetSelection (0);
470                 }
471                 setup_sensitivity ();
472                 setup_visibility ();
473         } else if (property == TextContentProperty::BURN) {
474                 checked_set (_burn, text ? text->burn() : false);
475         } else if (property == TextContentProperty::X_OFFSET) {
476                 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
477                 update_outline_subtitles_in_viewer ();
478         } else if (property == TextContentProperty::Y_OFFSET) {
479                 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
480                 update_outline_subtitles_in_viewer ();
481         } else if (property == TextContentProperty::X_SCALE) {
482                 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
483                 clear_outline_subtitles ();
484         } else if (property == TextContentProperty::Y_SCALE) {
485                 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
486                 clear_outline_subtitles ();
487         } else if (property == TextContentProperty::LINE_SPACING) {
488                 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
489                 clear_outline_subtitles ();
490         } else if (property == TextContentProperty::Z_POSITION) {
491                 checked_set (_z_position, text ? text->z_position() : 0);
492         } else if (property == TextContentProperty::DCP_TRACK) {
493                 if (_dcp_track) {
494                         update_dcp_track_selection ();
495                 }
496         } else if (property == TextContentProperty::LANGUAGE) {
497                 if (_language) {
498                         _language->set (text ? text->language() : boost::none);
499                 }
500         } else if (property == TextContentProperty::LANGUAGE_IS_ADDITIONAL) {
501                 if (_language_type) {
502                         _language_type->SetSelection (text ? (text->language_is_additional() ? 1 : 0) : 0);
503                 }
504         } else if (property == DCPContentProperty::REFERENCE_TEXT) {
505                 if (scs) {
506                         auto dcp = dynamic_pointer_cast<DCPContent> (scs);
507                         checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
508                 } else {
509                         checked_set (_reference, false);
510                 }
511
512                 setup_sensitivity ();
513         } else if (property == DCPContentProperty::TEXTS) {
514                 setup_sensitivity ();
515         } else if (property == ContentProperty::TRIM_START) {
516                 setup_sensitivity ();
517         }
518 }
519
520
521 void
522 TextPanel::use_toggled ()
523 {
524         for (auto i: _parent->selected_text()) {
525                 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
526         }
527 }
528
529
530 /** @return the text type that is currently selected in the drop-down */
531 TextType
532 TextPanel::current_type () const
533 {
534         switch (_type->GetSelection()) {
535         case 0:
536                 return TextType::OPEN_SUBTITLE;
537         case 1:
538                 return TextType::CLOSED_CAPTION;
539         }
540
541         return TextType::UNKNOWN;
542 }
543
544
545 void
546 TextPanel::type_changed ()
547 {
548         for (auto i: _parent->selected_text()) {
549                 i->text_of_original_type(_original_type)->set_type (current_type ());
550         }
551
552         setup_visibility ();
553 }
554
555
556 void
557 TextPanel::burn_toggled ()
558 {
559         for (auto i: _parent->selected_text ()) {
560                 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
561         }
562 }
563
564
565 void
566 TextPanel::setup_sensitivity ()
567 {
568         int any_subs = 0;
569         /* We currently assume that FFmpeg subtitles are bitmapped */
570         int ffmpeg_subs = 0;
571         /* DCP subs can't have their line spacing changed */
572         int dcp_subs = 0;
573         auto sel = _parent->selected_text ();
574         for (auto i: sel) {
575                 /* These are the content types that could include subtitles */
576                 auto fc = std::dynamic_pointer_cast<const FFmpegContent>(i);
577                 auto sc = std::dynamic_pointer_cast<const StringTextFileContent>(i);
578                 auto dc = std::dynamic_pointer_cast<const DCPContent>(i);
579                 auto dsc = std::dynamic_pointer_cast<const DCPSubtitleContent>(i);
580                 if (fc) {
581                         if (!fc->text.empty()) {
582                                 ++ffmpeg_subs;
583                                 ++any_subs;
584                         }
585                 } else if (dc || dsc) {
586                         ++dcp_subs;
587                         ++any_subs;
588                 } else if (sc) {
589                         /* XXX: in the future there could be bitmap subs from DCPs */
590                         ++any_subs;
591                 }
592         }
593
594         /* Decide whether we can reference these subs */
595
596         shared_ptr<DCPContent> dcp;
597         if (sel.size() == 1) {
598                 dcp = dynamic_pointer_cast<DCPContent>(sel.front());
599         }
600
601         string why_not;
602         bool const can_reference = dcp && dcp->can_reference_text (_parent->film(), _original_type, why_not);
603         wxString cannot;
604         if (why_not.empty()) {
605                 cannot = _("Cannot reference this DCP's subtitles or captions.");
606         } else {
607                 cannot = _("Cannot reference this DCP's subtitles or captions: ") + std_to_wx(why_not);
608         }
609         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
610
611         bool const reference = _reference->GetValue ();
612
613         auto const type = current_type ();
614
615         /* Set up _type */
616         _type->Clear ();
617         _type->Append (_("open subtitles"));
618         if (ffmpeg_subs == 0) {
619                 _type->Append (_("closed captions"));
620         }
621
622         switch (type) {
623         case TextType::OPEN_SUBTITLE:
624                 _type->SetSelection (0);
625                 break;
626         case TextType::CLOSED_CAPTION:
627                 if (_type->GetCount() > 1) {
628                         _type->SetSelection (1);
629                 }
630                 break;
631         default:
632                 break;
633         }
634
635         /* Set up sensitivity */
636         _use->Enable (!reference && any_subs > 0);
637         bool const use = _use->GetValue ();
638         if (_outline_subtitles) {
639                 _outline_subtitles->Enable (!_loading_analysis && any_subs && use && type == TextType::OPEN_SUBTITLE);
640         }
641         _type->Enable (!reference && any_subs > 0 && use);
642         _burn->Enable (!reference && any_subs > 0 && use && type == TextType::OPEN_SUBTITLE);
643         _x_offset->Enable (!reference && any_subs > 0 && use && type == TextType::OPEN_SUBTITLE);
644         _y_offset->Enable (!reference && any_subs > 0 && use && type == TextType::OPEN_SUBTITLE);
645         _x_scale->Enable (!reference && any_subs > 0 && use && type == TextType::OPEN_SUBTITLE);
646         _y_scale->Enable (!reference && any_subs > 0 && use && type == TextType::OPEN_SUBTITLE);
647         _line_spacing->Enable (!reference && use && type == TextType::OPEN_SUBTITLE && dcp_subs < any_subs);
648         _z_position->Enable (!reference && use && type == TextType::OPEN_SUBTITLE);
649         _stream->Enable (!reference && ffmpeg_subs == 1);
650         /* Ideally we would check here to see if the FFmpeg content has "string" subs (i.e. not bitmaps) */
651         _text_view_button->Enable (!reference && any_subs > 0 && ffmpeg_subs == 0);
652         _fonts_dialog_button->Enable (!reference && any_subs > 0 && ffmpeg_subs == 0 && type == TextType::OPEN_SUBTITLE);
653         _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TextType::OPEN_SUBTITLE);
654 }
655
656
657 void
658 TextPanel::stream_changed ()
659 {
660         auto fc = _parent->selected_ffmpeg ();
661         if (fc.size() != 1) {
662                 return;
663         }
664
665         auto fcs = fc.front ();
666
667         auto a = fcs->subtitle_streams ();
668         auto i = a.begin ();
669         auto const s = string_client_data (_stream->GetClientObject(_stream->GetSelection()));
670         while (i != a.end() && (*i)->identifier () != s) {
671                 ++i;
672         }
673
674         if (i != a.end ()) {
675                 fcs->set_subtitle_stream (*i);
676         }
677 }
678
679
680 void
681 TextPanel::x_offset_changed ()
682 {
683         for (auto i: _parent->selected_text ()) {
684                 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
685         }
686 }
687
688
689 void
690 TextPanel::y_offset_changed ()
691 {
692         for (auto i: _parent->selected_text ()) {
693                 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
694         }
695 }
696
697
698 void
699 TextPanel::x_scale_changed ()
700 {
701         for (auto i: _parent->selected_text ()) {
702                 i->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
703         }
704 }
705
706
707 void
708 TextPanel::y_scale_changed ()
709 {
710         for (auto i: _parent->selected_text ()) {
711                 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
712         }
713 }
714
715
716 void
717 TextPanel::line_spacing_changed ()
718 {
719         for (auto i: _parent->selected_text ()) {
720                 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
721         }
722 }
723
724
725 void
726 TextPanel::z_position_changed ()
727 {
728         for (auto i: _parent->selected_text()) {
729                 i->text_of_original_type(_original_type)->set_z_position (_z_position->GetValue());
730         }
731 }
732
733
734 void
735 TextPanel::content_selection_changed ()
736 {
737         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
738         film_content_changed (TextContentProperty::USE);
739         film_content_changed (TextContentProperty::BURN);
740         film_content_changed (TextContentProperty::X_OFFSET);
741         film_content_changed (TextContentProperty::Y_OFFSET);
742         film_content_changed (TextContentProperty::X_SCALE);
743         film_content_changed (TextContentProperty::Y_SCALE);
744         film_content_changed (TextContentProperty::LINE_SPACING);
745         film_content_changed (TextContentProperty::Z_POSITION);
746         film_content_changed (TextContentProperty::FONTS);
747         film_content_changed (TextContentProperty::TYPE);
748         film_content_changed (TextContentProperty::DCP_TRACK);
749         film_content_changed (TextContentProperty::LANGUAGE);
750         film_content_changed (TextContentProperty::LANGUAGE_IS_ADDITIONAL);
751         film_content_changed (DCPContentProperty::REFERENCE_TEXT);
752 }
753
754
755 void
756 TextPanel::text_view_clicked ()
757 {
758         if (_text_view) {
759                 _text_view->Destroy ();
760                 _text_view = nullptr;
761         }
762
763         auto c = _parent->selected_text ();
764         DCPOMATIC_ASSERT (c.size() == 1);
765
766         auto decoder = decoder_factory (_parent->film(), c.front(), false, false, shared_ptr<Decoder>());
767
768         if (decoder) {
769                 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
770                 _text_view->Show ();
771         }
772 }
773
774
775 void
776 TextPanel::fonts_dialog_clicked ()
777 {
778         if (_fonts_dialog) {
779                 _fonts_dialog->Destroy ();
780                 _fonts_dialog = nullptr;
781         }
782
783         auto c = _parent->selected_text ();
784         DCPOMATIC_ASSERT (c.size() == 1);
785
786         _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
787         _fonts_dialog->Show ();
788 }
789
790
791 void
792 TextPanel::reference_clicked ()
793 {
794         auto c = _parent->selected ();
795         if (c.size() != 1) {
796                 return;
797         }
798
799         auto d = dynamic_pointer_cast<DCPContent> (c.front ());
800         if (!d) {
801                 return;
802         }
803
804         d->set_reference_text (_original_type, _reference->GetValue ());
805 }
806
807
808 void
809 TextPanel::appearance_dialog_clicked ()
810 {
811         auto c = _parent->selected_text ();
812         DCPOMATIC_ASSERT (c.size() == 1);
813
814         auto d = new SubtitleAppearanceDialog (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type));
815         if (d->ShowModal () == wxID_OK) {
816                 d->apply ();
817         }
818         d->Destroy ();
819 }
820
821
822 /** The user has clicked on the outline subtitles check box */
823 void
824 TextPanel::outline_subtitles_changed ()
825 {
826         if (_outline_subtitles->GetValue()) {
827                 _analysis_content = _parent->selected_text().front();
828                 try_to_load_analysis ();
829         } else {
830                 clear_outline_subtitles ();
831         }
832 }
833
834
835 void
836 TextPanel::try_to_load_analysis ()
837 {
838         if (_loading_analysis) {
839                 return;
840         }
841
842         _loading_analysis = true;
843         setup_sensitivity ();
844         _analysis.reset ();
845
846         auto content = _analysis_content.lock ();
847         if (!content) {
848                 _loading_analysis = false;
849                 setup_sensitivity ();
850                 return;
851         }
852
853         auto const path = _parent->film()->subtitle_analysis_path(content);
854
855         if (!boost::filesystem::exists(path)) {
856                 for (auto i: JobManager::instance()->get()) {
857                         if (dynamic_pointer_cast<AnalyseSubtitlesJob>(i)) {
858                                 i->cancel ();
859                         }
860                 }
861
862                 JobManager::instance()->analyse_subtitles (
863                         _parent->film(), content, _analysis_finished_connection, bind(&TextPanel::analysis_finished, this)
864                         );
865                 return;
866         }
867
868         try {
869                 _analysis.reset (new SubtitleAnalysis(path));
870         } catch (OldFormatError& e) {
871                 /* An old analysis file: recreate it */
872                 JobManager::instance()->analyse_subtitles (
873                         _parent->film(), content, _analysis_finished_connection, bind(&TextPanel::analysis_finished, this)
874                         );
875                 return;
876         }
877
878         update_outline_subtitles_in_viewer ();
879         _loading_analysis = false;
880         setup_sensitivity ();
881 }
882
883
884 void
885 TextPanel::update_outline_subtitles_in_viewer ()
886 {
887         auto fv = _parent->film_viewer().lock();
888         if (!fv) {
889                 return;
890         }
891
892         if (_analysis) {
893                 auto rect = _analysis->bounding_box ();
894                 if (rect) {
895                         auto content = _analysis_content.lock ();
896                         DCPOMATIC_ASSERT (content);
897                         rect->x += content->text.front()->x_offset();
898                         rect->y += content->text.front()->y_offset();
899                 }
900                 fv->set_outline_subtitles (rect);
901         } else {
902                 fv->set_outline_subtitles ({});
903         }
904 }
905
906
907 /** Remove any current subtitle outline display */
908 void
909 TextPanel::clear_outline_subtitles ()
910 {
911         _analysis.reset ();
912         update_outline_subtitles_in_viewer ();
913         if (_outline_subtitles) {
914                 _outline_subtitles->SetValue (false);
915         }
916 }
917
918
919 void
920 TextPanel::analysis_finished ()
921 {
922         auto content = _analysis_content.lock ();
923         if (!content) {
924                 _loading_analysis = false;
925                 setup_sensitivity ();
926                 return;
927         }
928
929         if (!boost::filesystem::exists(_parent->film()->subtitle_analysis_path(content))) {
930                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
931                    Give up.
932                 */
933                 error_dialog (_parent->window(), _("Could not analyse subtitles."));
934                 clear_outline_subtitles ();
935                 _loading_analysis = false;
936                 setup_sensitivity ();
937                 return;
938         }
939
940         _loading_analysis = false;
941         try_to_load_analysis ();
942 }
943
944
945 void
946 TextPanel::language_changed ()
947 {
948         for (auto i: _parent->selected_text()) {
949                 auto t = i->text_of_original_type(_original_type);
950                 if (t) {
951                         t->set_language (_language->get());
952                 }
953         }
954 }
955
956
957 void
958 TextPanel::language_is_additional_changed ()
959 {
960         for (auto i: _parent->selected_text()) {
961                 auto t = i->text_of_original_type(_original_type);
962                 if (t) {
963                         t->set_language_is_additional (_language_type->GetSelection() == 1);
964                 }
965         }
966 }
967