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