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