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