Update new panels when they are created.
[dcpomatic.git] / src / wx / text_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 "text_panel.h"
22 #include "film_editor.h"
23 #include "wx_util.h"
24 #include "text_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/string_text_file_content.h"
30 #include "lib/ffmpeg_subtitle_stream.h"
31 #include "lib/dcp_subtitle_content.h"
32 #include "lib/string_text_file_decoder.h"
33 #include "lib/dcp_subtitle_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 std::cout;
44 using boost::shared_ptr;
45 using boost::optional;
46 using boost::dynamic_pointer_cast;
47
48 /** @param t Original text type of the content, if known */
49 TextPanel::TextPanel (ContentPanel* p, TextType t)
50         : ContentSubPanel (p, std_to_wx(text_type_to_name(t)))
51         , _text_view (0)
52         , _fonts_dialog (0)
53         , _original_type (t)
54 {
55         wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
56
57         wxString refer = _("Use this DCP's subtitle as OV and make VF");
58         if (t == TEXT_CLOSED_CAPTION) {
59                 refer = _("Use this DCP's closed caption as OV and make VF");
60         }
61
62         _reference = new wxCheckBox (this, wxID_ANY, refer);
63         reference_sizer->Add (_reference, 0, wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
64
65         _reference_note = new wxStaticText (this, wxID_ANY, _(""));
66         _reference_note->Wrap (200);
67         reference_sizer->Add (_reference_note, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
68         wxFont font = _reference_note->GetFont();
69         font.SetStyle(wxFONTSTYLE_ITALIC);
70         font.SetPointSize(font.GetPointSize() - 1);
71         _reference_note->SetFont(font);
72
73         _sizer->Add (reference_sizer);
74
75         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
76         _sizer->Add (grid, 0, wxALL, 8);
77         int r = 0;
78
79         wxBoxSizer* use = new wxBoxSizer (wxHORIZONTAL);
80         _use = new wxCheckBox (this, wxID_ANY, _("Use as"));
81         use->Add (_use, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
82         _type = new wxChoice (this, wxID_ANY);
83         _type->Append (_("open subtitles"));
84         _type->Append (_("closed captions"));
85         use->Add (_type, 1, wxEXPAND, 0);
86         grid->Add (use, wxGBPosition (r, 0), wxGBSpan (1, 2));
87         ++r;
88
89         _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
90         grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
91         ++r;
92
93         {
94                 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
95                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
96                 _x_offset = new wxSpinCtrl (this);
97                 s->Add (_x_offset);
98                 add_label_to_sizer (s, this, _("%"), false);
99                 grid->Add (s, wxGBPosition (r, 1));
100                 ++r;
101         }
102
103         {
104                 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
105                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
106                 _y_offset = new wxSpinCtrl (this);
107                 s->Add (_y_offset);
108                 add_label_to_sizer (s, this, _("%"), false);
109                 grid->Add (s, wxGBPosition (r, 1));
110                 ++r;
111         }
112
113         {
114                 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
115                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
116                 _x_scale = new wxSpinCtrl (this);
117                 s->Add (_x_scale);
118                 add_label_to_sizer (s, this, _("%"), false);
119                 grid->Add (s, wxGBPosition (r, 1));
120                 ++r;
121         }
122
123         {
124                 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
125                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
126                 _y_scale = new wxSpinCtrl (this);
127                 s->Add (_y_scale);
128                 add_label_to_sizer (s, this, _("%"), false);
129                 grid->Add (s, wxGBPosition (r, 1));
130                 ++r;
131         }
132
133         {
134                 add_label_to_sizer (grid, this, _("Line spacing"), true, wxGBPosition (r, 0));
135                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
136                 _line_spacing = new wxSpinCtrl (this);
137                 s->Add (_line_spacing);
138                 add_label_to_sizer (s, this, _("%"), false);
139                 grid->Add (s, wxGBPosition (r, 1));
140                 ++r;
141         }
142
143         add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
144         _language = new wxTextCtrl (this, wxID_ANY);
145         grid->Add (_language, wxGBPosition (r, 1));
146         ++r;
147
148         add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
149         _stream = new wxChoice (this, wxID_ANY);
150         grid->Add (_stream, wxGBPosition (r, 1));
151         ++r;
152
153         {
154                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
155
156                 _text_view_button = new wxButton (this, wxID_ANY, _("View..."));
157                 s->Add (_text_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
158                 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
159                 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
160                 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
161                 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
162
163                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
164                 ++r;
165         }
166
167         _x_offset->SetRange (-100, 100);
168         _y_offset->SetRange (-100, 100);
169         _x_scale->SetRange (10, 1000);
170         _y_scale->SetRange (10, 1000);
171         _line_spacing->SetRange (10, 1000);
172
173         content_selection_changed ();
174
175         _reference->Bind                (wxEVT_CHECKBOX, boost::bind (&TextPanel::reference_clicked, this));
176         _use->Bind                      (wxEVT_CHECKBOX, boost::bind (&TextPanel::use_toggled, this));
177         _type->Bind                     (wxEVT_CHOICE,   boost::bind (&TextPanel::type_changed, this));
178         _burn->Bind                     (wxEVT_CHECKBOX, boost::bind (&TextPanel::burn_toggled, this));
179         _x_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_offset_changed, this));
180         _y_offset->Bind                 (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_offset_changed, this));
181         _x_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&TextPanel::x_scale_changed, this));
182         _y_scale->Bind                  (wxEVT_SPINCTRL, boost::bind (&TextPanel::y_scale_changed, this));
183         _line_spacing->Bind             (wxEVT_SPINCTRL, boost::bind (&TextPanel::line_spacing_changed, this));
184         _language->Bind                 (wxEVT_TEXT,     boost::bind (&TextPanel::language_changed, this));
185         _stream->Bind                   (wxEVT_CHOICE,   boost::bind (&TextPanel::stream_changed, this));
186         _text_view_button->Bind         (wxEVT_BUTTON,   boost::bind (&TextPanel::text_view_clicked, this));
187         _fonts_dialog_button->Bind      (wxEVT_BUTTON,   boost::bind (&TextPanel::fonts_dialog_clicked, this));
188         _appearance_dialog_button->Bind (wxEVT_BUTTON,   boost::bind (&TextPanel::appearance_dialog_clicked, this));
189 }
190
191 void
192 TextPanel::film_changed (Film::Property property)
193 {
194         if (property == Film::CONTENT || property == Film::REEL_TYPE) {
195                 setup_sensitivity ();
196         }
197 }
198
199 void
200 TextPanel::film_content_changed (int property)
201 {
202         FFmpegContentList fc = _parent->selected_ffmpeg ();
203         ContentList sc = _parent->selected_text ();
204
205         shared_ptr<FFmpegContent> fcs;
206         if (fc.size() == 1) {
207                 fcs = fc.front ();
208         }
209
210         shared_ptr<Content> scs;
211         if (sc.size() == 1) {
212                 scs = sc.front ();
213         }
214
215         shared_ptr<TextContent> text;
216         if (scs) {
217                 text = scs->text_of_original_type(_original_type);
218         }
219
220         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
221                 _stream->Clear ();
222                 if (fcs) {
223                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
224                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
225                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
226                         }
227
228                         if (fcs->subtitle_stream()) {
229                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
230                         } else {
231                                 _stream->SetSelection (wxNOT_FOUND);
232                         }
233                 }
234                 setup_sensitivity ();
235         } else if (property == TextContentProperty::USE) {
236                 checked_set (_use, text ? text->use() : false);
237                 setup_sensitivity ();
238         } else if (property == TextContentProperty::TYPE) {
239                 if (text) {
240                         switch (text->type()) {
241                         case TEXT_OPEN_SUBTITLE:
242                                 _type->SetSelection (0);
243                                 break;
244                         case TEXT_CLOSED_CAPTION:
245                                 _type->SetSelection (1);
246                                 break;
247                         default:
248                                 DCPOMATIC_ASSERT (false);
249                         }
250                 } else {
251                         _type->SetSelection (0);
252                 }
253                 setup_sensitivity ();
254         } else if (property == TextContentProperty::BURN) {
255                 checked_set (_burn, text ? text->burn() : false);
256         } else if (property == TextContentProperty::X_OFFSET) {
257                 checked_set (_x_offset, text ? lrint (text->x_offset() * 100) : 0);
258         } else if (property == TextContentProperty::Y_OFFSET) {
259                 checked_set (_y_offset, text ? lrint (text->y_offset() * 100) : 0);
260         } else if (property == TextContentProperty::X_SCALE) {
261                 checked_set (_x_scale, text ? lrint (text->x_scale() * 100) : 100);
262         } else if (property == TextContentProperty::Y_SCALE) {
263                 checked_set (_y_scale, text ? lrint (text->y_scale() * 100) : 100);
264         } else if (property == TextContentProperty::LINE_SPACING) {
265                 checked_set (_line_spacing, text ? lrint (text->line_spacing() * 100) : 100);
266         } else if (property == TextContentProperty::LANGUAGE) {
267                 checked_set (_language, text ? text->language() : "");
268         } else if (property == DCPContentProperty::REFERENCE_TEXT) {
269                 if (scs) {
270                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
271                         checked_set (_reference, dcp ? dcp->reference_text(_original_type) : false);
272                 } else {
273                         checked_set (_reference, false);
274                 }
275
276                 setup_sensitivity ();
277         } else if (property == DCPContentProperty::TEXTS) {
278                 setup_sensitivity ();
279         }
280 }
281
282 void
283 TextPanel::use_toggled ()
284 {
285         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
286                 i->text_of_original_type(_original_type)->set_use (_use->GetValue());
287         }
288 }
289
290 /** @return the text type that is currently selected in the drop-down */
291 TextType
292 TextPanel::current_type () const
293 {
294         switch (_type->GetSelection()) {
295         case 0:
296                 return TEXT_OPEN_SUBTITLE;
297         case 1:
298                 return TEXT_CLOSED_CAPTION;
299         }
300
301         return TEXT_UNKNOWN;
302 }
303
304 void
305 TextPanel::type_changed ()
306 {
307         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text()) {
308                 i->text_of_original_type(_original_type)->set_type (current_type ());
309         }
310 }
311
312 void
313 TextPanel::burn_toggled ()
314 {
315         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
316                 i->text_of_original_type(_original_type)->set_burn (_burn->GetValue());
317         }
318 }
319
320 void
321 TextPanel::setup_sensitivity ()
322 {
323         int any_subs = 0;
324         int ffmpeg_subs = 0;
325         ContentList sel = _parent->selected_text ();
326         BOOST_FOREACH (shared_ptr<Content> i, sel) {
327                 /* These are the content types that could include subtitles */
328                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
329                 shared_ptr<const StringTextFileContent> sc = boost::dynamic_pointer_cast<const StringTextFileContent> (i);
330                 shared_ptr<const DCPContent> dc = boost::dynamic_pointer_cast<const DCPContent> (i);
331                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
332                 if (fc) {
333                         if (!fc->text.empty()) {
334                                 ++ffmpeg_subs;
335                                 ++any_subs;
336                         }
337                 } else if (sc || dc || dsc) {
338                         /* XXX: in the future there could be bitmap subs from DCPs */
339                         ++any_subs;
340                 }
341         }
342
343         /* Decide whether we can reference these subs */
344
345         shared_ptr<DCPContent> dcp;
346         if (sel.size() == 1) {
347                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
348         }
349
350         string why_not;
351         bool const can_reference = dcp && dcp->can_reference_text (_original_type, why_not);
352         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
353
354         bool const reference = _reference->GetValue ();
355
356         TextType const type = current_type ();
357
358         /* Set up sensitivity */
359         _use->Enable (!reference && any_subs > 0);
360         bool const use = _use->GetValue ();
361         _type->Enable (!reference && any_subs > 0 && use);
362         _burn->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
363         _x_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
364         _y_offset->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
365         _x_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
366         _y_scale->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
367         _line_spacing->Enable (!reference && use && type == TEXT_OPEN_SUBTITLE);
368         _language->Enable (!reference && any_subs > 0 && use);
369         _stream->Enable (!reference && ffmpeg_subs == 1);
370         _text_view_button->Enable (!reference);
371         _fonts_dialog_button->Enable (!reference && type == TEXT_OPEN_SUBTITLE);
372         _appearance_dialog_button->Enable (!reference && any_subs > 0 && use && type == TEXT_OPEN_SUBTITLE);
373 }
374
375 void
376 TextPanel::stream_changed ()
377 {
378         FFmpegContentList fc = _parent->selected_ffmpeg ();
379         if (fc.size() != 1) {
380                 return;
381         }
382
383         shared_ptr<FFmpegContent> fcs = fc.front ();
384
385         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
386         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
387         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
388         while (i != a.end() && (*i)->identifier () != s) {
389                 ++i;
390         }
391
392         if (i != a.end ()) {
393                 fcs->set_subtitle_stream (*i);
394         }
395 }
396
397 void
398 TextPanel::x_offset_changed ()
399 {
400         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
401                 i->text_of_original_type(_original_type)->set_x_offset (_x_offset->GetValue() / 100.0);
402         }
403 }
404
405 void
406 TextPanel::y_offset_changed ()
407 {
408         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
409                 i->text_of_original_type(_original_type)->set_y_offset (_y_offset->GetValue() / 100.0);
410         }
411 }
412
413 void
414 TextPanel::x_scale_changed ()
415 {
416         ContentList c = _parent->selected_text ();
417         if (c.size() == 1) {
418                 c.front()->text_of_original_type(_original_type)->set_x_scale (_x_scale->GetValue() / 100.0);
419         }
420 }
421
422 void
423 TextPanel::y_scale_changed ()
424 {
425         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
426                 i->text_of_original_type(_original_type)->set_y_scale (_y_scale->GetValue() / 100.0);
427         }
428 }
429
430 void
431 TextPanel::line_spacing_changed ()
432 {
433         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
434                 i->text_of_original_type(_original_type)->set_line_spacing (_line_spacing->GetValue() / 100.0);
435         }
436 }
437
438 void
439 TextPanel::language_changed ()
440 {
441         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_text ()) {
442                 i->text_of_original_type(_original_type)->set_language (wx_to_std (_language->GetValue()));
443         }
444 }
445
446 void
447 TextPanel::content_selection_changed ()
448 {
449         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
450         film_content_changed (TextContentProperty::USE);
451         film_content_changed (TextContentProperty::BURN);
452         film_content_changed (TextContentProperty::X_OFFSET);
453         film_content_changed (TextContentProperty::Y_OFFSET);
454         film_content_changed (TextContentProperty::X_SCALE);
455         film_content_changed (TextContentProperty::Y_SCALE);
456         film_content_changed (TextContentProperty::LINE_SPACING);
457         film_content_changed (TextContentProperty::LANGUAGE);
458         film_content_changed (TextContentProperty::FONTS);
459         film_content_changed (TextContentProperty::TYPE);
460         film_content_changed (DCPContentProperty::REFERENCE_TEXT);
461 }
462
463 void
464 TextPanel::text_view_clicked ()
465 {
466         if (_text_view) {
467                 _text_view->Destroy ();
468                 _text_view = 0;
469         }
470
471         ContentList c = _parent->selected_text ();
472         DCPOMATIC_ASSERT (c.size() == 1);
473
474         shared_ptr<Decoder> decoder = decoder_factory (c.front(), _parent->film()->log(), false);
475
476         if (decoder) {
477                 _text_view = new TextView (this, _parent->film(), c.front(), c.front()->text_of_original_type(_original_type), decoder, _parent->film_viewer());
478                 _text_view->Show ();
479         }
480 }
481
482 void
483 TextPanel::fonts_dialog_clicked ()
484 {
485         if (_fonts_dialog) {
486                 _fonts_dialog->Destroy ();
487                 _fonts_dialog = 0;
488         }
489
490         ContentList c = _parent->selected_text ();
491         DCPOMATIC_ASSERT (c.size() == 1);
492
493         _fonts_dialog = new FontsDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
494         _fonts_dialog->Show ();
495 }
496
497 void
498 TextPanel::reference_clicked ()
499 {
500         ContentList c = _parent->selected ();
501         if (c.size() != 1) {
502                 return;
503         }
504
505         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
506         if (!d) {
507                 return;
508         }
509
510         d->set_reference_text (_original_type, _reference->GetValue ());
511 }
512
513 void
514 TextPanel::appearance_dialog_clicked ()
515 {
516         ContentList c = _parent->selected_text ();
517         DCPOMATIC_ASSERT (c.size() == 1);
518
519         SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, c.front(), c.front()->text_of_original_type(_original_type));
520         if (d->ShowModal () == wxID_OK) {
521                 d->apply ();
522         }
523         d->Destroy ();
524 }