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