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