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