Disable bits of TextPanel when we are making closed captions.
[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 /** @return the text type that is currently selected in the drop-down */
289 TextType
290 TextPanel::current_type () const
291 {
292         switch (_type->GetSelection()) {
293         case 0:
294                 return TEXT_OPEN_SUBTITLE;
295         case 1:
296                 return TEXT_CLOSED_CAPTION;
297         }
298
299         return TEXT_UNKNOWN;
300 }
301
302 void
303 TextPanel::type_changed ()
304 {
305         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
306                 i->text_of_original_type(_original_type)->set_type (current_type ());
307         }
308 }
309
310 void
311 TextPanel::burn_toggled ()
312 {
313         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
314                 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
315         }
316 }
317
318 void
319 TextPanel::setup_sensitivity ()
320 {
321         int any_subs = 0;
322         int ffmpeg_subs = 0;
323         ContentList sel = _parent->selected_text ();
324         BOOST_FOREACH (shared_ptr<Content> i, sel) {
325                 /* These are the content types that could include subtitles */
326                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
327                 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
328                 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
329                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
330                 if (fc) {
331                         if (!fc->text.empty()) {
332                                 ++ffmpeg_subs;
333                                 ++any_subs;
334                         }
335                 } else if (sc || dc || dsc) {
336                         /* XXX: in the future there could be bitmap subs from DCPs */
337                         ++any_subs;
338                 }
339         }
340
341         /* Decide whether we can reference these subs */
342
343         shared_ptr<DCPContent> dcp;
344         if (sel.size() == 1) {
345                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
346         }
347
348         string why_not;
349         bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
350         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
351
352         bool const reference = _reference->GetValue ();
353
354         TextType const type = current_type ();
355
356         /* Set up sensitivity */
357         _use->Enable (!reference && any_subs > 0);
358         bool const use = _use->GetValue ();
359         _type->Enable (!reference && any_subs > 0 && use);
360         _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
361         _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
362         _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
363         _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
364         _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
365         _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
366         _language->Enable (!reference && any_subs > 0 && use);
367         _stream->Enable (!reference && ffmpeg_subs == 1);
368         _text_view_button->Enable (!reference);
369         _fonts_dialog_button->Enable (!reference && type == TEXT_OPEN_SUBTITLE);
370         _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
371 }
372
373 void
374 TextPanel::stream_changed ()
375 {
376         FFmpegContentList fc = _parent->selected_ffmpeg ();
377         if (fc.size() != 1) {
378                 return;
379         }
380
381         shared_ptr<FFmpegContent> fcs = fc.front ();
382
383         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
384         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
385         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
386         while (i != a.end() && (*i)->identifier () != s) {
387                 ++i;
388         }
389
390         if (i != a.end ()) {
391                 fcs->set_subtitle_stream (*i);
392         }
393 }
394
395 void
396 TextPanel::x_offset_changed ()
397 {
398         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
399                 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
400         }
401 }
402
403 void
404 TextPanel::y_offset_changed ()
405 {
406         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
407                 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
408         }
409 }
410
411 void
412 TextPanel::x_scale_changed ()
413 {
414         ContentList c = _parent->selected_text ();
415         if (c.size() == 1) {
416                 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
417         }
418 }
419
420 void
421 TextPanel::y_scale_changed ()
422 {
423         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
424                 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
425         }
426 }
427
428 void
429 TextPanel::line_spacing_changed ()
430 {
431         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
432                 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
433         }
434 }
435
436 void
437 TextPanel::language_changed ()
438 {
439         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
440                 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
441         }
442 }
443
444 void
445 TextPanel::content_selection_changed ()
446 {
447         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
448         film_content_changed (TextContentProperty::USE);
449         film_content_changed (TextContentProperty::BURN);
450         film_content_changed (TextContentProperty::X_OFFSET);
451         film_content_changed (TextContentProperty::Y_OFFSET);
452         film_content_changed (TextContentProperty::X_SCALE);
453         film_content_changed (TextContentProperty::Y_SCALE);
454         film_content_changed (TextContentProperty::LINE_SPACING);
455         film_content_changed (TextContentProperty::LANGUAGE);
456         film_content_changed (TextContentProperty::FONTS);
457         film_content_changed (TextContentProperty::TYPE);
458         film_content_changed (DCPContentProperty::REFERENCE_TEXT);
459 }
460
461 void
462 TextPanel::text_view_clicked ()
463 {
464         if (_text_view) {
465                 _text_view->Destroy ();
466                 _text_view = 0;
467         }
468
469         ContentList c = _parent->selected_text ();
470         DCPOMATIC_ASSERT (c.size() == 1);
471
472         shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
473
474         if (decoder) {
475                 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
476                 _text_view->Show ();
477         }
478 }
479
480 void
481 TextPanel::fonts_dialog_clicked ()
482 {
483         if (_fonts_dialog) {
484                 _fonts_dialog->Destroy ();
485                 _fonts_dialog = 0;
486         }
487
488         ContentList c = _parent->selected_text ();
489         DCPOMATIC_ASSERT (c.size() == 1);
490
491         _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
492         _fonts_dialog->Show ();
493 }
494
495 void
496 TextPanel::reference_clicked ()
497 {
498         ContentList c = _parent->selected ();
499         if (c.size() != 1) {
500                 return;
501         }
502
503         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
504         if (!d) {
505                 return;
506         }
507
508         d->set_reference_text (_original_type, _reference->GetValue ());
509 }
510
511 void
512 TextPanel::appearance_dialog_clicked ()
513 {
514         ContentList c = _parent->selected_text ();
515         DCPOMATIC_ASSERT (c.size() == 1);
516
517         SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
518         if (d->ShowModal () == wxID_OK) {
519                 d->apply ();
520         }
521         d->Destroy ();
522 }