Tidy up image/text subtitle distinction with FFmpeg sources.
[dcpomatic.git] / src / wx / subtitle_panel.cc
1 /*
2     Copyright (C) 2012-2016 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 "subtitle_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 "text_subtitle_appearance_dialog.h"
28 #include "image_subtitle_colour_dialog.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/text_subtitle_content.h"
31 #include "lib/ffmpeg_subtitle_stream.h"
32 #include "lib/dcp_subtitle_content.h"
33 #include "lib/text_subtitle_decoder.h"
34 #include "lib/dcp_subtitle_decoder.h"
35 #include "lib/dcp_content.h"
36 #include "lib/subtitle_content.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 SubtitlePanel::SubtitlePanel (ContentPanel* p)
47         : ContentSubPanel (p, _("Subtitles"))
48         , _subtitle_view (0)
49         , _fonts_dialog (0)
50 {
51         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
52         _sizer->Add (grid, 0, wxALL, 8);
53         int r = 0;
54
55         _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
56         grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
57         ++r;
58
59         _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
60         grid->Add (_use, wxGBPosition (r, 0), wxGBSpan (1, 2));
61         ++r;
62
63         _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
64         grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
65         ++r;
66
67         {
68                 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
69                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
70                 _x_offset = new wxSpinCtrl (this);
71                 s->Add (_x_offset);
72                 add_label_to_sizer (s, this, _("%"), false);
73                 grid->Add (s, wxGBPosition (r, 1));
74                 ++r;
75         }
76
77         {
78                 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
79                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
80                 _y_offset = new wxSpinCtrl (this);
81                 s->Add (_y_offset);
82                 add_label_to_sizer (s, this, _("%"), false);
83                 grid->Add (s, wxGBPosition (r, 1));
84                 ++r;
85         }
86
87         {
88                 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
89                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
90                 _x_scale = new wxSpinCtrl (this);
91                 s->Add (_x_scale);
92                 add_label_to_sizer (s, this, _("%"), false);
93                 grid->Add (s, wxGBPosition (r, 1));
94                 ++r;
95         }
96
97         {
98                 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
99                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
100                 _y_scale = new wxSpinCtrl (this);
101                 s->Add (_y_scale);
102                 add_label_to_sizer (s, this, _("%"), false);
103                 grid->Add (s, wxGBPosition (r, 1));
104                 ++r;
105         }
106
107         add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
108         _language = new wxTextCtrl (this, wxID_ANY);
109         grid->Add (_language, wxGBPosition (r, 1));
110         ++r;
111
112         add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
113         _stream = new wxChoice (this, wxID_ANY);
114         grid->Add (_stream, wxGBPosition (r, 1));
115         ++r;
116
117         {
118                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
119
120                 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
121                 s->Add (_subtitle_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
122                 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
123                 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
124                 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
125                 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
126
127                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
128                 ++r;
129         }
130
131         _x_offset->SetRange (-100, 100);
132         _y_offset->SetRange (-100, 100);
133         _x_scale->SetRange (10, 1000);
134         _y_scale->SetRange (10, 1000);
135
136         _reference->Bind                (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::reference_clicked, this));
137         _use->Bind                      (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
138         _burn->Bind                     (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
139         _x_offset->Bind                 (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
140         _y_offset->Bind                 (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
141         _x_scale->Bind                  (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
142         _y_scale->Bind                  (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
143         _language->Bind                 (wxEVT_COMMAND_TEXT_UPDATED,     boost::bind (&SubtitlePanel::language_changed, this));
144         _stream->Bind                   (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
145         _subtitle_view_button->Bind     (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
146         _fonts_dialog_button->Bind      (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
147         _appearance_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::appearance_dialog_clicked, this));
148 }
149
150 void
151 SubtitlePanel::film_changed (Film::Property property)
152 {
153         if (property == Film::CONTENT || property == Film::REEL_TYPE) {
154                 setup_sensitivity ();
155         }
156 }
157
158 void
159 SubtitlePanel::film_content_changed (int property)
160 {
161         FFmpegContentList fc = _parent->selected_ffmpeg ();
162         ContentList sc = _parent->selected_subtitle ();
163
164         shared_ptr<FFmpegContent> fcs;
165         if (fc.size() == 1) {
166                 fcs = fc.front ();
167         }
168
169         shared_ptr<Content> scs;
170         if (sc.size() == 1) {
171                 scs = sc.front ();
172         }
173
174         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
175                 _stream->Clear ();
176                 if (fcs) {
177                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
178                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
179                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
180                         }
181
182                         if (fcs->subtitle_stream()) {
183                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
184                         } else {
185                                 _stream->SetSelection (wxNOT_FOUND);
186                         }
187                 }
188                 setup_sensitivity ();
189         } else if (property == SubtitleContentProperty::USE) {
190                 checked_set (_use, scs ? scs->subtitle->use() : false);
191                 setup_sensitivity ();
192         } else if (property == SubtitleContentProperty::BURN) {
193                 checked_set (_burn, scs ? scs->subtitle->burn() : false);
194         } else if (property == SubtitleContentProperty::X_OFFSET) {
195                 checked_set (_x_offset, scs ? lrint (scs->subtitle->x_offset() * 100) : 0);
196         } else if (property == SubtitleContentProperty::Y_OFFSET) {
197                 checked_set (_y_offset, scs ? lrint (scs->subtitle->y_offset() * 100) : 0);
198         } else if (property == SubtitleContentProperty::X_SCALE) {
199                 checked_set (_x_scale, scs ? lrint (scs->subtitle->x_scale() * 100) : 100);
200         } else if (property == SubtitleContentProperty::Y_SCALE) {
201                 checked_set (_y_scale, scs ? lrint (scs->subtitle->y_scale() * 100) : 100);
202         } else if (property == SubtitleContentProperty::LANGUAGE) {
203                 checked_set (_language, scs ? scs->subtitle->language() : "");
204         } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
205                 if (scs) {
206                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
207                         checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
208                 } else {
209                         checked_set (_reference, false);
210                 }
211
212                 setup_sensitivity ();
213         }
214 }
215
216 void
217 SubtitlePanel::use_toggled ()
218 {
219         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
220                 i->subtitle->set_use (_use->GetValue());
221         }
222 }
223
224 void
225 SubtitlePanel::burn_toggled ()
226 {
227         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
228                 i->subtitle->set_burn (_burn->GetValue());
229         }
230 }
231
232 void
233 SubtitlePanel::setup_sensitivity ()
234 {
235         int any_subs = 0;
236         int ffmpeg_subs = 0;
237         int text_subs = 0;
238         int image_subs = 0;
239         ContentList sel = _parent->selected_subtitle ();
240         BOOST_FOREACH (shared_ptr<Content> i, sel) {
241                 /* These are the content types that could include subtitles */
242                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
243                 shared_ptr<const TextSubtitleContent> sc = boost::dynamic_pointer_cast<const TextSubtitleContent> (i);
244                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
245                 if (fc) {
246                         if (fc->subtitle) {
247                                 /* This content has some subtitles; check the selected stream to decide what type */
248                                 if (fc->subtitle_stream()->has_text()) {
249                                         ++text_subs;
250                                 } else if (fc->subtitle_stream()->has_image()) {
251                                         ++image_subs;
252                                 }
253                                 ++ffmpeg_subs;
254                                 ++any_subs;
255                         }
256                 } else if (sc || dsc) {
257                         /* XXX: in the future there could be bitmap subs from DCPs */
258                         ++text_subs;
259                         ++any_subs;
260                 }
261         }
262
263         if (image_subs) {
264                 BOOST_FOREACH (shared_ptr<Content> i, sel) {
265                         /* We must burn image subtitles at the moment */
266                         i->subtitle->set_burn (true);
267                 }
268         }
269
270         /* Decide whether we can reference these subs */
271
272         shared_ptr<DCPContent> dcp;
273         if (sel.size() == 1) {
274                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
275         }
276
277         list<string> why_not;
278         bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
279         setup_refer_button (_reference, dcp, can_reference, why_not);
280
281         bool const reference = _reference->GetValue ();
282
283         /* Set up sensitivity */
284         _use->Enable (!reference && any_subs > 0);
285         bool const use = _use->GetValue ();
286         _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
287         _x_offset->Enable (!reference && any_subs > 0 && use);
288         _y_offset->Enable (!reference && any_subs > 0 && use);
289         _x_scale->Enable (!reference && any_subs > 0 && use);
290         _y_scale->Enable (!reference && any_subs > 0 && use);
291         _language->Enable (!reference && any_subs > 0 && use);
292         _stream->Enable (!reference && ffmpeg_subs == 1);
293         _subtitle_view_button->Enable (!reference && text_subs == 1);
294         _fonts_dialog_button->Enable (!reference && text_subs == 1);
295         _appearance_dialog_button->Enable (!reference);
296 }
297
298 void
299 SubtitlePanel::stream_changed ()
300 {
301         FFmpegContentList fc = _parent->selected_ffmpeg ();
302         if (fc.size() != 1) {
303                 return;
304         }
305
306         shared_ptr<FFmpegContent> fcs = fc.front ();
307
308         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
309         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
310         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
311         while (i != a.end() && (*i)->identifier () != s) {
312                 ++i;
313         }
314
315         if (i != a.end ()) {
316                 fcs->set_subtitle_stream (*i);
317         }
318 }
319
320 void
321 SubtitlePanel::x_offset_changed ()
322 {
323         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
324                 i->subtitle->set_x_offset (_x_offset->GetValue() / 100.0);
325         }
326 }
327
328 void
329 SubtitlePanel::y_offset_changed ()
330 {
331         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
332                 i->subtitle->set_y_offset (_y_offset->GetValue() / 100.0);
333         }
334 }
335
336 void
337 SubtitlePanel::x_scale_changed ()
338 {
339         ContentList c = _parent->selected_subtitle ();
340         if (c.size() == 1) {
341                 c.front()->subtitle->set_x_scale (_x_scale->GetValue() / 100.0);
342         }
343 }
344
345 void
346 SubtitlePanel::y_scale_changed ()
347 {
348         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
349                 i->subtitle->set_y_scale (_y_scale->GetValue() / 100.0);
350         }
351 }
352
353 void
354 SubtitlePanel::language_changed ()
355 {
356         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
357                 i->subtitle->set_language (wx_to_std (_language->GetValue()));
358         }
359 }
360
361 void
362 SubtitlePanel::content_selection_changed ()
363 {
364         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
365         film_content_changed (SubtitleContentProperty::USE);
366         film_content_changed (SubtitleContentProperty::BURN);
367         film_content_changed (SubtitleContentProperty::X_OFFSET);
368         film_content_changed (SubtitleContentProperty::Y_OFFSET);
369         film_content_changed (SubtitleContentProperty::X_SCALE);
370         film_content_changed (SubtitleContentProperty::Y_SCALE);
371         film_content_changed (SubtitleContentProperty::LANGUAGE);
372         film_content_changed (SubtitleContentProperty::FONTS);
373         film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
374 }
375
376 void
377 SubtitlePanel::subtitle_view_clicked ()
378 {
379         if (_subtitle_view) {
380                 _subtitle_view->Destroy ();
381                 _subtitle_view = 0;
382         }
383
384         ContentList c = _parent->selected_subtitle ();
385         DCPOMATIC_ASSERT (c.size() == 1);
386
387         shared_ptr<Decoder> decoder;
388
389         shared_ptr<TextSubtitleContent> sr = dynamic_pointer_cast<TextSubtitleContent> (c.front ());
390         if (sr) {
391                 decoder.reset (new TextSubtitleDecoder (sr));
392         }
393
394         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
395         if (dc) {
396                 decoder.reset (new DCPSubtitleDecoder (dc));
397         }
398
399         if (decoder) {
400                 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
401                 _subtitle_view->Show ();
402         }
403 }
404
405 void
406 SubtitlePanel::fonts_dialog_clicked ()
407 {
408         if (_fonts_dialog) {
409                 _fonts_dialog->Destroy ();
410                 _fonts_dialog = 0;
411         }
412
413         ContentList c = _parent->selected_subtitle ();
414         DCPOMATIC_ASSERT (c.size() == 1);
415
416         _fonts_dialog = new FontsDialog (this, c.front ());
417         _fonts_dialog->Show ();
418 }
419
420 void
421 SubtitlePanel::reference_clicked ()
422 {
423         ContentList c = _parent->selected ();
424         if (c.size() != 1) {
425                 return;
426         }
427
428         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
429         if (!d) {
430                 return;
431         }
432
433         d->set_reference_subtitle (_reference->GetValue ());
434 }
435
436 void
437 SubtitlePanel::appearance_dialog_clicked ()
438 {
439         ContentList c = _parent->selected_subtitle ();
440         DCPOMATIC_ASSERT (c.size() == 1);
441
442         shared_ptr<TextSubtitleContent> sr = dynamic_pointer_cast<TextSubtitleContent> (c.front ());
443         if (sr) {
444                 TextSubtitleAppearanceDialog* d = new TextSubtitleAppearanceDialog (this, sr);
445                 if (d->ShowModal () == wxID_OK) {
446                         d->apply ();
447                 }
448                 d->Destroy ();
449         } else {
450                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c.front ());
451                 DCPOMATIC_ASSERT (fc);
452                 ImageSubtitleColourDialog* d = new ImageSubtitleColourDialog (this, fc, fc->subtitle_stream ());
453                 if (d->ShowModal() == wxID_OK) {
454                         d->apply ();
455                 }
456                 d->Destroy ();
457         }
458 }