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