Introduce the idea of unknown original subtitle type to clean up the GUI a bit.
[dcpomatic.git] / src / wx / text_panel.cc
1 /*
2     Copyright (C) 2012-2018 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 "subtitle_appearance_dialog.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/string_text_file_content.h"
30 #include "lib/ffmpeg_subtitle_stream.h"
31 #include "lib/dcp_subtitle_content.h"
32 #include "lib/string_text_file_decoder.h"
33 #include "lib/dcp_subtitle_decoder.h"
34 #include "lib/dcp_content.h"
35 #include "lib/text_content.h"
36 #include "lib/decoder_factory.h"
37 #include <wx/spinctrl.h>
38 #include <boost/foreach.hpp>
39
40 using std::vector;
41 using std::string;
42 using std::list;
43 using std::cout;
44 using boost::shared_ptr;
45 using boost::optional;
46 using boost::dynamic_pointer_cast;
47
48 /** @param t Original text type of the content, if known */
49 TextPanel::TextPanel (ContentPanel* p, TextType t)
50         : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
51         , _text_view (0)
52         , _fonts_dialog (0)
53         , _original_type (t)
54 {
55         wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
56
57         wxString refer = _("Use this DCP's subtitle as OV and make VF");
58         if (t == TEXT_CLOSED_CAPTION) {
59                 refer = _("Use this DCP's closed caption as OV and make VF");
60         }
61
62         _reference = new wxCheckBox (this, wxID_ANY, refer);
63         reference_sizer->Add (_reference, 0, wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
64
65         _reference_note = new wxStaticText (this, wxID_ANY, _(""));
66         _reference_note->Wrap (200);
67         reference_sizer->Add (_reference_note, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
68         wxFont font = _reference_note->GetFont();
69         font.SetStyle(wxFONTSTYLE_ITALIC);
70         font.SetPointSize(font.GetPointSize() - 1);
71         _reference_note->SetFont(font);
72
73         _sizer->Add (reference_sizer);
74
75         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
76         _sizer->Add (grid, 0, wxALL, 8);
77         int r = 0;
78
79         wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
80         _use = new wxCheckBox (this, wxID_ANY, _("Use as"));
81         use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
82         _type = new wxChoice (this, wxID_ANY);
83         _type->Append (_("open subtitles"));
84         _type->Append (_("closed captions"));
85         use->Add (_type, 1, wxEXPAND, 0);
86         grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
87         ++r;
88
89         _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
90         grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
91         ++r;
92
93         {
94                 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
95                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
96                 _x_offset = new wxSpinCtrl (this);
97                 s->Add (_x_offset);
98                 add_label_to_sizer (s, this, _("%"), false);
99                 grid->Add (s, wxGBPosition (r, 1));
100                 ++r;
101         }
102
103         {
104                 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
105                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
106                 _y_offset = new wxSpinCtrl (this);
107                 s->Add (_y_offset);
108                 add_label_to_sizer (s, this, _("%"), false);
109                 grid->Add (s, wxGBPosition (r, 1));
110                 ++r;
111         }
112
113         {
114                 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
115                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
116                 _x_scale = new wxSpinCtrl (this);
117                 s->Add (_x_scale);
118                 add_label_to_sizer (s, this, _("%"), false);
119                 grid->Add (s, wxGBPosition (r, 1));
120                 ++r;
121         }
122
123         {
124                 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
125                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
126                 _y_scale = new wxSpinCtrl (this);
127                 s->Add (_y_scale);
128                 add_label_to_sizer (s, this, _("%"), false);
129                 grid->Add (s, wxGBPosition (r, 1));
130                 ++r;
131         }
132
133         {
134                 add_label_to_sizer (grid, this, _("Line spacing"), true, wxGBPosition (r, 0));
135                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
136                 _line_spacing = new wxSpinCtrl (this);
137                 s->Add (_line_spacing);
138                 add_label_to_sizer (s, this, _("%"), false);
139                 grid->Add (s, wxGBPosition (r, 1));
140                 ++r;
141         }
142
143         add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
144         _language = new wxTextCtrl (this, wxID_ANY);
145         grid->Add (_language, wxGBPosition (r, 1));
146         ++r;
147
148         add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
149         _stream = new wxChoice (this, wxID_ANY);
150         grid->Add (_stream, wxGBPosition (r, 1));
151         ++r;
152
153         {
154                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
155
156                 _text_view_button = new wxButton (this, wxID_ANY, _("View..."));
157                 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
158                 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
159                 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
160                 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
161                 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
162
163                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
164                 ++r;
165         }
166
167         _x_offset->SetRange (-100, 100);
168         _y_offset->SetRange (-100, 100);
169         _x_scale->SetRange (10, 1000);
170         _y_scale->SetRange (10, 1000);
171         _line_spacing->SetRange (10, 1000);
172
173         _reference->Bind                (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
174         _use->Bind                      (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
175         _type->Bind                     (wxEVT_CHOICE,   boost::bind (&TextPanel::type_changed, this));
176         _burn->Bind                     (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
177         _x_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
178         _y_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
179         _x_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
180         _y_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
181         _line_spacing->Bind             (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
182         _language->Bind                 (wxEVT_TEXT,     boost::bind (&TextPanel::language_changed, this));
183         _stream->Bind                   (wxEVT_CHOICE,   boost::bind (&TextPanel::stream_changed, this));
184         _text_view_button->Bind         (wxEVT_BUTTON,   boost::bind (&TextPanel::text_view_clicked, this));
185         _fonts_dialog_button->Bind      (wxEVT_BUTTON,   boost::bind (&TextPanel::fonts_dialog_clicked, this));
186         _appearance_dialog_button->Bind (wxEVT_BUTTON,   boost::bind (&TextPanel::appearance_dialog_clicked, this));
187 }
188
189 void
190 TextPanel::film_changed (Film::Property property)
191 {
192         if (property == Film::CONTENT || property == Film::REEL_TYPE) {
193                 setup_sensitivity ();
194         }
195 }
196
197 void
198 TextPanel::film_content_changed (int property)
199 {
200         FFmpegContentList fc = _parent->selected_ffmpeg ();
201         ContentList sc = _parent->selected_text ();
202
203         shared_ptr<FFmpegContent> fcs;
204         if (fc.size() == 1) {
205                 fcs = fc.front ();
206         }
207
208         shared_ptr<Content> scs;
209         if (sc.size() == 1) {
210                 scs = sc.front ();
211         }
212
213         shared_ptr<TextContent> text;
214         if (scs) {
215                 text = scs->text_of_original_type(_original_type);
216         }
217
218         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
219                 _stream->Clear ();
220                 if (fcs) {
221                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
222                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
223                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
224                         }
225
226                         if (fcs->subtitle_stream()) {
227                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
228                         } else {
229                                 _stream->SetSelection (wxNOT_FOUND);
230                         }
231                 }
232                 setup_sensitivity ();
233         } else if (property == TextContentProperty::USE) {
234                 checked_set (_use, text ? text->use() : false);
235                 setup_sensitivity ();
236         } else if (property == TextContentProperty::TYPE) {
237                 if (text) {
238                         switch (text->type()) {
239                         case TEXT_OPEN_SUBTITLE:
240                                 _type->SetSelection (0);
241                                 break;
242                         case TEXT_CLOSED_CAPTION:
243                                 _type->SetSelection (1);
244                                 break;
245                         default:
246                                 DCPOMATIC_ASSERT (false);
247                         }
248                 } else {
249                         _type->SetSelection (0);
250                 }
251                 setup_sensitivity ();
252         } else if (property == TextContentProperty::BURN) {
253                 checked_set (_burn, text ? text->burn() : false);
254         } else if (property == TextContentProperty::X_OFFSET) {
255                 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
256         } else if (property == TextContentProperty::Y_OFFSET) {
257                 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
258         } else if (property == TextContentProperty::X_SCALE) {
259                 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
260         } else if (property == TextContentProperty::Y_SCALE) {
261                 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
262         } else if (property == TextContentProperty::LINE_SPACING) {
263                 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
264         } else if (property == TextContentProperty::LANGUAGE) {
265                 checked_set (_language, text ? text->language() : "");
266         } else if (property == DCPContentProperty::REFERENCE_TEXT) {
267                 if (scs) {
268                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
269                         checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
270                 } else {
271                         checked_set (_reference, false);
272                 }
273
274                 setup_sensitivity ();
275         } else if (property == DCPContentProperty::TEXTS) {
276                 setup_sensitivity ();
277         }
278 }
279
280 void
281 TextPanel::use_toggled ()
282 {
283         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
284                 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
285         }
286 }
287
288 void
289 TextPanel::type_changed ()
290 {
291         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
292                 switch (_type->GetSelection()) {
293                 case 0:
294                         i->text_of_original_type(_original_type)->set_type (TEXT_OPEN_SUBTITLE);
295                         break;
296                 case 1:
297                         i->text_of_original_type(_original_type)->set_type (TEXT_CLOSED_CAPTION);
298                         break;
299                 }
300         }
301 }
302
303 void
304 TextPanel::burn_toggled ()
305 {
306         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
307                 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
308         }
309 }
310
311 void
312 TextPanel::setup_sensitivity ()
313 {
314         int any_subs = 0;
315         int ffmpeg_subs = 0;
316         ContentList sel = _parent->selected_text ();
317         BOOST_FOREACH (shared_ptr<Content> i, sel) {
318                 /* These are the content types that could include subtitles */
319                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
320                 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
321                 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
322                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
323                 if (fc) {
324                         if (!fc->text.empty()) {
325                                 ++ffmpeg_subs;
326                                 ++any_subs;
327                         }
328                 } else if (sc || dc || dsc) {
329                         /* XXX: in the future there could be bitmap subs from DCPs */
330                         ++any_subs;
331                 }
332         }
333
334         /* Decide whether we can reference these subs */
335
336         shared_ptr<DCPContent> dcp;
337         if (sel.size() == 1) {
338                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
339         }
340
341         string why_not;
342         bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
343         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
344
345         bool const reference = _reference->GetValue ();
346
347         /* Set up sensitivity */
348         _use->Enable (!reference && any_subs > 0);
349         bool const use = _use->GetValue ();
350         _type->Enable (!reference && any_subs > 0 && use);
351         _burn->Enable (!reference && any_subs > 0 && use && _type->GetSelection() == 0);
352         _x_offset->Enable (!reference && any_subs > 0 && use);
353         _y_offset->Enable (!reference && any_subs > 0 && use);
354         _x_scale->Enable (!reference && any_subs > 0 && use);
355         _y_scale->Enable (!reference && any_subs > 0 && use);
356         _line_spacing->Enable (!reference && use);
357         _language->Enable (!reference && any_subs > 0 && use);
358         _stream->Enable (!reference && ffmpeg_subs == 1);
359         _text_view_button->Enable (!reference);
360         _fonts_dialog_button->Enable (!reference);
361         _appearance_dialog_button->Enable (!reference && any_subs > 0 && use);
362 }
363
364 void
365 TextPanel::stream_changed ()
366 {
367         FFmpegContentList fc = _parent->selected_ffmpeg ();
368         if (fc.size() != 1) {
369                 return;
370         }
371
372         shared_ptr<FFmpegContent> fcs = fc.front ();
373
374         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
375         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
376         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
377         while (i != a.end() && (*i)->identifier () != s) {
378                 ++i;
379         }
380
381         if (i != a.end ()) {
382                 fcs->set_subtitle_stream (*i);
383         }
384 }
385
386 void
387 TextPanel::x_offset_changed ()
388 {
389         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
390                 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
391         }
392 }
393
394 void
395 TextPanel::y_offset_changed ()
396 {
397         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
398                 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
399         }
400 }
401
402 void
403 TextPanel::x_scale_changed ()
404 {
405         ContentList c = _parent->selected_text ();
406         if (c.size() == 1) {
407                 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
408         }
409 }
410
411 void
412 TextPanel::y_scale_changed ()
413 {
414         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
415                 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
416         }
417 }
418
419 void
420 TextPanel::line_spacing_changed ()
421 {
422         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
423                 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
424         }
425 }
426
427 void
428 TextPanel::language_changed ()
429 {
430         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
431                 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
432         }
433 }
434
435 void
436 TextPanel::content_selection_changed ()
437 {
438         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
439         film_content_changed (TextContentProperty::USE);
440         film_content_changed (TextContentProperty::BURN);
441         film_content_changed (TextContentProperty::X_OFFSET);
442         film_content_changed (TextContentProperty::Y_OFFSET);
443         film_content_changed (TextContentProperty::X_SCALE);
444         film_content_changed (TextContentProperty::Y_SCALE);
445         film_content_changed (TextContentProperty::LINE_SPACING);
446         film_content_changed (TextContentProperty::LANGUAGE);
447         film_content_changed (TextContentProperty::FONTS);
448         film_content_changed (TextContentProperty::TYPE);
449         film_content_changed (DCPContentProperty::REFERENCE_TEXT);
450 }
451
452 void
453 TextPanel::text_view_clicked ()
454 {
455         if (_text_view) {
456                 _text_view->Destroy ();
457                 _text_view = 0;
458         }
459
460         ContentList c = _parent->selected_text ();
461         DCPOMATIC_ASSERT (c.size() == 1);
462
463         shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
464
465         if (decoder) {
466                 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
467                 _text_view->Show ();
468         }
469 }
470
471 void
472 TextPanel::fonts_dialog_clicked ()
473 {
474         if (_fonts_dialog) {
475                 _fonts_dialog->Destroy ();
476                 _fonts_dialog = 0;
477         }
478
479         ContentList c = _parent->selected_text ();
480         DCPOMATIC_ASSERT (c.size() == 1);
481
482         _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
483         _fonts_dialog->Show ();
484 }
485
486 void
487 TextPanel::reference_clicked ()
488 {
489         ContentList c = _parent->selected ();
490         if (c.size() != 1) {
491                 return;
492         }
493
494         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
495         if (!d) {
496                 return;
497         }
498
499         d->set_reference_text (_original_type, _reference->GetValue ());
500 }
501
502 void
503 TextPanel::appearance_dialog_clicked ()
504 {
505         ContentList c = _parent->selected_text ();
506         DCPOMATIC_ASSERT (c.size() == 1);
507
508         SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
509         if (d->ShowModal () == wxID_OK) {
510                 d->apply ();
511         }
512         d->Destroy ();
513 }