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