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