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