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