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