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