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