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