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