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