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