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