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