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