Fix insensitive subtitle controls after adding a VF.
[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         {
109                 add_label_to_sizer (grid, this, _("Line spacing"), true, wxGBPosition (r, 0));
110                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
111                 _line_spacing = new wxSpinCtrl (this);
112                 s->Add (_line_spacing);
113                 add_label_to_sizer (s, this, _("%"), false);
114                 grid->Add (s, wxGBPosition (r, 1));
115                 ++r;
116         }
117
118         add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
119         _language = new wxTextCtrl (this, wxID_ANY);
120         grid->Add (_language, wxGBPosition (r, 1));
121         ++r;
122
123         add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
124         _stream = new wxChoice (this, wxID_ANY);
125         grid->Add (_stream, wxGBPosition (r, 1));
126         ++r;
127
128         {
129                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
130
131                 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
132                 s->Add (_subtitle_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
133                 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
134                 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
135                 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
136                 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
137
138                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
139                 ++r;
140         }
141
142         _x_offset->SetRange (-100, 100);
143         _y_offset->SetRange (-100, 100);
144         _x_scale->SetRange (10, 1000);
145         _y_scale->SetRange (10, 1000);
146         _line_spacing->SetRange (10, 1000);
147
148         _reference->Bind                (wxEVT_CHECKBOX, boost::bind (&SubtitlePanel::reference_clicked, this));
149         _use->Bind                      (wxEVT_CHECKBOX, boost::bind (&SubtitlePanel::use_toggled, this));
150         _burn->Bind                     (wxEVT_CHECKBOX, boost::bind (&SubtitlePanel::burn_toggled, this));
151         _x_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&SubtitlePanel::x_offset_changed, this));
152         _y_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&SubtitlePanel::y_offset_changed, this));
153         _x_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&SubtitlePanel::x_scale_changed, this));
154         _y_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&SubtitlePanel::y_scale_changed, this));
155         _line_spacing->Bind             (wxEVT_SPINCTRL, boost::bind (&SubtitlePanel::line_spacing_changed, this));
156         _language->Bind                 (wxEVT_TEXT,     boost::bind (&SubtitlePanel::language_changed, this));
157         _stream->Bind                   (wxEVT_CHOICE,   boost::bind (&SubtitlePanel::stream_changed, this));
158         _subtitle_view_button->Bind     (wxEVT_BUTTON,   boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
159         _fonts_dialog_button->Bind      (wxEVT_BUTTON,   boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
160         _appearance_dialog_button->Bind (wxEVT_BUTTON,   boost::bind (&SubtitlePanel::appearance_dialog_clicked, this));
161 }
162
163 void
164 SubtitlePanel::film_changed (Film::Property property)
165 {
166         if (property == Film::CONTENT || property == Film::REEL_TYPE) {
167                 setup_sensitivity ();
168         }
169 }
170
171 void
172 SubtitlePanel::film_content_changed (int property)
173 {
174         FFmpegContentList fc = _parent->selected_ffmpeg ();
175         ContentList sc = _parent->selected_subtitle ();
176
177         shared_ptr<FFmpegContent> fcs;
178         if (fc.size() == 1) {
179                 fcs = fc.front ();
180         }
181
182         shared_ptr<Content> scs;
183         if (sc.size() == 1) {
184                 scs = sc.front ();
185         }
186
187         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
188                 _stream->Clear ();
189                 if (fcs) {
190                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
191                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
192                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
193                         }
194
195                         if (fcs->subtitle_stream()) {
196                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
197                         } else {
198                                 _stream->SetSelection (wxNOT_FOUND);
199                         }
200                 }
201                 setup_sensitivity ();
202         } else if (property == SubtitleContentProperty::USE) {
203                 checked_set (_use, scs ? scs->subtitle->use() : false);
204                 setup_sensitivity ();
205         } else if (property == SubtitleContentProperty::BURN) {
206                 checked_set (_burn, scs ? scs->subtitle->burn() : false);
207         } else if (property == SubtitleContentProperty::X_OFFSET) {
208                 checked_set (_x_offset, scs ? lrint (scs->subtitle->x_offset() * 100) : 0);
209         } else if (property == SubtitleContentProperty::Y_OFFSET) {
210                 checked_set (_y_offset, scs ? lrint (scs->subtitle->y_offset() * 100) : 0);
211         } else if (property == SubtitleContentProperty::X_SCALE) {
212                 checked_set (_x_scale, scs ? lrint (scs->subtitle->x_scale() * 100) : 100);
213         } else if (property == SubtitleContentProperty::Y_SCALE) {
214                 checked_set (_y_scale, scs ? lrint (scs->subtitle->y_scale() * 100) : 100);
215         } else if (property == SubtitleContentProperty::LINE_SPACING) {
216                 checked_set (_line_spacing, scs ? lrint (scs->subtitle->line_spacing() * 100) : 100);
217         } else if (property == SubtitleContentProperty::LANGUAGE) {
218                 checked_set (_language, scs ? scs->subtitle->language() : "");
219         } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
220                 if (scs) {
221                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
222                         checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
223                 } else {
224                         checked_set (_reference, false);
225                 }
226
227                 setup_sensitivity ();
228         } else if (property == DCPContentProperty::HAS_SUBTITLES) {
229                 setup_sensitivity ();
230         }
231 }
232
233 void
234 SubtitlePanel::use_toggled ()
235 {
236         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
237                 i->subtitle->set_use (_use->GetValue());
238         }
239 }
240
241 void
242 SubtitlePanel::burn_toggled ()
243 {
244         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
245                 i->subtitle->set_burn (_burn->GetValue());
246         }
247 }
248
249 void
250 SubtitlePanel::setup_sensitivity ()
251 {
252         int any_subs = 0;
253         int ffmpeg_subs = 0;
254         int text_subs = 0;
255         int image_subs = 0;
256         ContentList sel = _parent->selected_subtitle ();
257         BOOST_FOREACH (shared_ptr<Content> i, sel) {
258                 /* These are the content types that could include subtitles */
259                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
260                 shared_ptr<const TextSubtitleContent> sc = boost::dynamic_pointer_cast<const TextSubtitleContent> (i);
261                 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
262                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
263                 if (fc) {
264                         if (fc->subtitle) {
265                                 DCPOMATIC_ASSERT (fc->subtitle_stream());
266                                 /* This content has some subtitles; check the selected stream to decide what type */
267                                 if (fc->subtitle_stream()->has_text()) {
268                                         ++text_subs;
269                                 } else if (fc->subtitle_stream()->has_image()) {
270                                         ++image_subs;
271                                 }
272                                 ++ffmpeg_subs;
273                                 ++any_subs;
274                         }
275                 } else if (sc || dc || dsc) {
276                         /* XXX: in the future there could be bitmap subs from DCPs */
277                         ++text_subs;
278                         ++any_subs;
279                 }
280         }
281
282         if (image_subs) {
283                 BOOST_FOREACH (shared_ptr<Content> i, sel) {
284                         /* We must burn image subtitles at the moment */
285                         i->subtitle->set_burn (true);
286                 }
287         }
288
289         /* Decide whether we can reference these subs */
290
291         shared_ptr<DCPContent> dcp;
292         if (sel.size() == 1) {
293                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
294         }
295
296         list<string> why_not;
297         bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
298         setup_refer_button (_reference, dcp, can_reference, why_not);
299
300         bool const reference = _reference->GetValue ();
301
302         /* Set up sensitivity */
303         _use->Enable (!reference && any_subs > 0);
304         bool const use = _use->GetValue ();
305         _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
306         _x_offset->Enable (!reference && any_subs > 0 && use);
307         _y_offset->Enable (!reference && any_subs > 0 && use);
308         _x_scale->Enable (!reference && any_subs > 0 && use);
309         _y_scale->Enable (!reference && any_subs > 0 && use);
310         _line_spacing->Enable (!reference && text_subs > 0 && use);
311         _language->Enable (!reference && any_subs > 0 && use);
312         _stream->Enable (!reference && ffmpeg_subs == 1);
313         _subtitle_view_button->Enable (!reference && text_subs == 1);
314         _fonts_dialog_button->Enable (!reference && text_subs == 1);
315         _appearance_dialog_button->Enable (!reference && any_subs > 0 && use);
316 }
317
318 void
319 SubtitlePanel::stream_changed ()
320 {
321         FFmpegContentList fc = _parent->selected_ffmpeg ();
322         if (fc.size() != 1) {
323                 return;
324         }
325
326         shared_ptr<FFmpegContent> fcs = fc.front ();
327
328         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
329         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
330         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
331         while (i != a.end() && (*i)->identifier () != s) {
332                 ++i;
333         }
334
335         if (i != a.end ()) {
336                 fcs->set_subtitle_stream (*i);
337         }
338 }
339
340 void
341 SubtitlePanel::x_offset_changed ()
342 {
343         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
344                 i->subtitle->set_x_offset (_x_offset->GetValue() / 100.0);
345         }
346 }
347
348 void
349 SubtitlePanel::y_offset_changed ()
350 {
351         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
352                 i->subtitle->set_y_offset (_y_offset->GetValue() / 100.0);
353         }
354 }
355
356 void
357 SubtitlePanel::x_scale_changed ()
358 {
359         ContentList c = _parent->selected_subtitle ();
360         if (c.size() == 1) {
361                 c.front()->subtitle->set_x_scale (_x_scale->GetValue() / 100.0);
362         }
363 }
364
365 void
366 SubtitlePanel::y_scale_changed ()
367 {
368         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
369                 i->subtitle->set_y_scale (_y_scale->GetValue() / 100.0);
370         }
371 }
372
373 void
374 SubtitlePanel::line_spacing_changed ()
375 {
376         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
377                 i->subtitle->set_line_spacing (_line_spacing->GetValue() / 100.0);
378         }
379 }
380
381 void
382 SubtitlePanel::language_changed ()
383 {
384         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_subtitle ()) {
385                 i->subtitle->set_language (wx_to_std (_language->GetValue()));
386         }
387 }
388
389 void
390 SubtitlePanel::content_selection_changed ()
391 {
392         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
393         film_content_changed (SubtitleContentProperty::USE);
394         film_content_changed (SubtitleContentProperty::BURN);
395         film_content_changed (SubtitleContentProperty::X_OFFSET);
396         film_content_changed (SubtitleContentProperty::Y_OFFSET);
397         film_content_changed (SubtitleContentProperty::X_SCALE);
398         film_content_changed (SubtitleContentProperty::Y_SCALE);
399         film_content_changed (SubtitleContentProperty::LINE_SPACING);
400         film_content_changed (SubtitleContentProperty::LANGUAGE);
401         film_content_changed (SubtitleContentProperty::FONTS);
402         film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
403 }
404
405 void
406 SubtitlePanel::subtitle_view_clicked ()
407 {
408         if (_subtitle_view) {
409                 _subtitle_view->Destroy ();
410                 _subtitle_view = 0;
411         }
412
413         ContentList c = _parent->selected_subtitle ();
414         DCPOMATIC_ASSERT (c.size() == 1);
415
416         shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log());
417
418         if (decoder) {
419                 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
420                 _subtitle_view->Show ();
421         }
422 }
423
424 void
425 SubtitlePanel::fonts_dialog_clicked ()
426 {
427         if (_fonts_dialog) {
428                 _fonts_dialog->Destroy ();
429                 _fonts_dialog = 0;
430         }
431
432         ContentList c = _parent->selected_subtitle ();
433         DCPOMATIC_ASSERT (c.size() == 1);
434
435         _fonts_dialog = new FontsDialog (this, c.front ());
436         _fonts_dialog->Show ();
437 }
438
439 void
440 SubtitlePanel::reference_clicked ()
441 {
442         ContentList c = _parent->selected ();
443         if (c.size() != 1) {
444                 return;
445         }
446
447         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
448         if (!d) {
449                 return;
450         }
451
452         d->set_reference_subtitle (_reference->GetValue ());
453 }
454
455 void
456 SubtitlePanel::appearance_dialog_clicked ()
457 {
458         ContentList c = _parent->selected_subtitle ();
459         DCPOMATIC_ASSERT (c.size() == 1);
460
461         bool text = false;
462         bool image = false;
463
464         if (
465                 dynamic_pointer_cast<TextSubtitleContent> (c.front()) ||
466                 dynamic_pointer_cast<DCPContent> (c.front()) ||
467                 dynamic_pointer_cast<DCPSubtitleContent> (c.front())) {
468
469                 text = true;
470         }
471
472         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c.front());
473         if (fc) {
474                 if (fc->subtitle_stream()->has_text()) {
475                         text = true;
476                 } else if (fc->subtitle_stream()->has_image()) {
477                         image = true;
478                 }
479         }
480
481         if (text) {
482                 TextSubtitleAppearanceDialog* d = new TextSubtitleAppearanceDialog (this, c.front());
483                 if (d->ShowModal () == wxID_OK) {
484                         d->apply ();
485                 }
486                 d->Destroy ();
487         } else if (image) {
488                 DCPOMATIC_ASSERT (fc);
489                 ImageSubtitleColourDialog* d = new ImageSubtitleColourDialog (this, fc, fc->subtitle_stream ());
490                 if (d->ShowModal() == wxID_OK) {
491                         d->apply ();
492                 }
493                 d->Destroy ();
494         }
495 }