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