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