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