UI for selecting caption type.
[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                         }
232                 } else {
233                         _type->SetSelection (0);
234                 }
235                 setup_sensitivity ();
236         } else if (property == TextContentProperty::BURN) {
237                 checked_set (_burn, scs ? scs->subtitle->burn() : false);
238         } else if (property == TextContentProperty::X_OFFSET) {
239                 checked_set (_x_offset, scs ? lrint (scs->subtitle->x_offset() * 100) : 0);
240         } else if (property == TextContentProperty::Y_OFFSET) {
241                 checked_set (_y_offset, scs ? lrint (scs->subtitle->y_offset() * 100) : 0);
242         } else if (property == TextContentProperty::X_SCALE) {
243                 checked_set (_x_scale, scs ? lrint (scs->subtitle->x_scale() * 100) : 100);
244         } else if (property == TextContentProperty::Y_SCALE) {
245                 checked_set (_y_scale, scs ? lrint (scs->subtitle->y_scale() * 100) : 100);
246         } else if (property == TextContentProperty::LINE_SPACING) {
247                 checked_set (_line_spacing, scs ? lrint (scs->subtitle->line_spacing() * 100) : 100);
248         } else if (property == TextContentProperty::LANGUAGE) {
249                 checked_set (_language, scs ? scs->subtitle->language() : "");
250         } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
251                 if (scs) {
252                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
253                         checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
254                 } else {
255                         checked_set (_reference, false);
256                 }
257
258                 setup_sensitivity ();
259         } else if (property == DCPContentProperty::HAS_SUBTITLES) {
260                 setup_sensitivity ();
261         }
262 }
263
264 void
265 CaptionPanel::use_toggled ()
266 {
267         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
268                 i->subtitle->set_use (_use->GetValue());
269         }
270 }
271
272 void
273 CaptionPanel::type_changed ()
274 {
275         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle()) {
276                 switch (_type->GetSelection()) {
277                 case 0:
278                         i->subtitle->set_type (TEXT_SUBTITLE);
279                         break;
280                 case 1:
281                         i->subtitle->set_type (TEXT_CLOSED_CAPTION);
282                         break;
283                 }
284         }
285 }
286
287 void
288 CaptionPanel::burn_toggled ()
289 {
290         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
291                 i->subtitle->set_burn (_burn->GetValue());
292         }
293 }
294
295 void
296 CaptionPanel::setup_sensitivity ()
297 {
298         int any_subs = 0;
299         int ffmpeg_subs = 0;
300         ContentList sel = _parent->selected_subtitle ();
301         BOOST_FOREACH (shared_ptr<Content> i, sel) {
302                 /* These are the content types that could include subtitles */
303                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
304                 shared_ptr<const PlainTextFileContent> sc = boost::dynamic_pointer_cast<const PlainTextFileContent> (i);
305                 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
306                 shared_ptr<const DCPTextContent> dsc = boost::dynamic_pointer_cast<const DCPTextContent> (i);
307                 if (fc) {
308                         if (fc->subtitle) {
309                                 ++ffmpeg_subs;
310                                 ++any_subs;
311                         }
312                 } else if (sc || dc || dsc) {
313                         /* XXX: in the future there could be bitmap subs from DCPs */
314                         ++any_subs;
315                 }
316         }
317
318         /* Decide whether we can reference these subs */
319
320         shared_ptr<DCPContent> dcp;
321         if (sel.size() == 1) {
322                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
323         }
324
325         string why_not;
326         bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
327         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
328
329         bool const reference = _reference->GetValue ();
330
331         /* Set up sensitivity */
332         _use->Enable (!reference && any_subs > 0);
333         bool const use = _use->GetValue ();
334         _type->Enable (!reference && any_subs > 0 && use);
335         _burn->Enable (!reference && any_subs > 0 && use && _type->GetSelection() == 0);
336         _x_offset->Enable (!reference && any_subs > 0 && use);
337         _y_offset->Enable (!reference && any_subs > 0 && use);
338         _x_scale->Enable (!reference && any_subs > 0 && use);
339         _y_scale->Enable (!reference && any_subs > 0 && use);
340         _line_spacing->Enable (!reference && use);
341         _language->Enable (!reference && any_subs > 0 && use);
342         _stream->Enable (!reference && ffmpeg_subs == 1);
343         _subtitle_view_button->Enable (!reference);
344         _fonts_dialog_button->Enable (!reference);
345         _appearance_dialog_button->Enable (!reference && any_subs > 0 && use);
346 }
347
348 void
349 CaptionPanel::stream_changed ()
350 {
351         FFmpegContentList fc = _parent->selected_ffmpeg ();
352         if (fc.size() != 1) {
353                 return;
354         }
355
356         shared_ptr<FFmpegContent> fcs = fc.front ();
357
358         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
359         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
360         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
361         while (i != a.end() && (*i)->identifier () != s) {
362                 ++i;
363         }
364
365         if (i != a.end ()) {
366                 fcs->set_subtitle_stream (*i);
367         }
368 }
369
370 void
371 CaptionPanel::x_offset_changed ()
372 {
373         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
374                 i->subtitle->set_x_offset (_x_offset->GetValue() / 100.0);
375         }
376 }
377
378 void
379 CaptionPanel::y_offset_changed ()
380 {
381         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
382                 i->subtitle->set_y_offset (_y_offset->GetValue() / 100.0);
383         }
384 }
385
386 void
387 CaptionPanel::x_scale_changed ()
388 {
389         ContentList c = _parent->selected_subtitle ();
390         if (c.size() == 1) {
391                 c.front()->subtitle->set_x_scale (_x_scale->GetValue() / 100.0);
392         }
393 }
394
395 void
396 CaptionPanel::y_scale_changed ()
397 {
398         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
399                 i->subtitle->set_y_scale (_y_scale->GetValue() / 100.0);
400         }
401 }
402
403 void
404 CaptionPanel::line_spacing_changed ()
405 {
406         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
407                 i->subtitle->set_line_spacing (_line_spacing->GetValue() / 100.0);
408         }
409 }
410
411 void
412 CaptionPanel::language_changed ()
413 {
414         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
415                 i->subtitle->set_language (wx_to_std (_language->GetValue()));
416         }
417 }
418
419 void
420 CaptionPanel::content_selection_changed ()
421 {
422         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
423         film_content_changed (TextContentProperty::USE);
424         film_content_changed (TextContentProperty::BURN);
425         film_content_changed (TextContentProperty::X_OFFSET);
426         film_content_changed (TextContentProperty::Y_OFFSET);
427         film_content_changed (TextContentProperty::X_SCALE);
428         film_content_changed (TextContentProperty::Y_SCALE);
429         film_content_changed (TextContentProperty::LINE_SPACING);
430         film_content_changed (TextContentProperty::LANGUAGE);
431         film_content_changed (TextContentProperty::FONTS);
432         film_content_changed (TextContentProperty::TYPE);
433         film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
434 }
435
436 void
437 CaptionPanel::subtitle_view_clicked ()
438 {
439         if (_subtitle_view) {
440                 _subtitle_view->Destroy ();
441                 _subtitle_view = 0;
442         }
443
444         ContentList c = _parent->selected_subtitle ();
445         DCPOMATIC_ASSERT (c.size() == 1);
446
447         shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
448
449         if (decoder) {
450                 _subtitle_view = new SubtitleView (this, _parent->film(), c.front(), decoder, _parent->film_viewer());
451                 _subtitle_view->Show ();
452         }
453 }
454
455 void
456 CaptionPanel::fonts_dialog_clicked ()
457 {
458         if (_fonts_dialog) {
459                 _fonts_dialog->Destroy ();
460                 _fonts_dialog = 0;
461         }
462
463         ContentList c = _parent->selected_subtitle ();
464         DCPOMATIC_ASSERT (c.size() == 1);
465
466         _fonts_dialog = new FontsDialog (this, c.front ());
467         _fonts_dialog->Show ();
468 }
469
470 void
471 CaptionPanel::reference_clicked ()
472 {
473         ContentList c = _parent->selected ();
474         if (c.size() != 1) {
475                 return;
476         }
477
478         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
479         if (!d) {
480                 return;
481         }
482
483         d->set_reference_subtitle (_reference->GetValue ());
484 }
485
486 void
487 CaptionPanel::appearance_dialog_clicked ()
488 {
489         ContentList c = _parent->selected_subtitle ();
490         DCPOMATIC_ASSERT (c.size() == 1);
491
492         SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front());
493         if (d->ShowModal () == wxID_OK) {
494                 d->apply ();
495         }
496         d->Destroy ();
497 }