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