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