74a1103499a656a97ef469ed9950ff11297b7f9b
[dcpomatic.git] / src / wx / subtitle_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "subtitle_panel.h"
21 #include "film_editor.h"
22 #include "wx_util.h"
23 #include "subtitle_view.h"
24 #include "content_panel.h"
25 #include "fonts_dialog.h"
26 #include "subtitle_appearance_dialog.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/subrip_content.h"
29 #include "lib/ffmpeg_subtitle_stream.h"
30 #include "lib/dcp_subtitle_content.h"
31 #include "lib/subrip_decoder.h"
32 #include "lib/dcp_subtitle_decoder.h"
33 #include "lib/dcp_content.h"
34 #include <wx/spinctrl.h>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/foreach.hpp>
37
38 using std::vector;
39 using std::string;
40 using std::list;
41 using boost::shared_ptr;
42 using boost::lexical_cast;
43 using boost::dynamic_pointer_cast;
44
45 SubtitlePanel::SubtitlePanel (ContentPanel* p)
46         : ContentSubPanel (p, _("Subtitles"))
47         , _subtitle_view (0)
48         , _fonts_dialog (0)
49 {
50         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
51         _sizer->Add (grid, 0, wxALL, 8);
52         int r = 0;
53
54         _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
55         grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
56         ++r;
57
58         _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
59         grid->Add (_use, wxGBPosition (r, 0), wxGBSpan (1, 2));
60         ++r;
61
62         _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
63         grid->Add (_burn, wxGBPosition (r, 0), wxGBSpan (1, 2));
64         ++r;
65
66         {
67                 add_label_to_sizer (grid, this, _("X Offset"), true, wxGBPosition (r, 0));
68                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
69                 _x_offset = new wxSpinCtrl (this);
70                 s->Add (_x_offset);
71                 add_label_to_sizer (s, this, _("%"), false);
72                 grid->Add (s, wxGBPosition (r, 1));
73                 ++r;
74         }
75
76         {
77                 add_label_to_sizer (grid, this, _("Y Offset"), true, wxGBPosition (r, 0));
78                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
79                 _y_offset = new wxSpinCtrl (this);
80                 s->Add (_y_offset);
81                 add_label_to_sizer (s, this, _("%"), false);
82                 grid->Add (s, wxGBPosition (r, 1));
83                 ++r;
84         }
85
86         {
87                 add_label_to_sizer (grid, this, _("X Scale"), true, wxGBPosition (r, 0));
88                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
89                 _x_scale = new wxSpinCtrl (this);
90                 s->Add (_x_scale);
91                 add_label_to_sizer (s, this, _("%"), false);
92                 grid->Add (s, wxGBPosition (r, 1));
93                 ++r;
94         }
95
96         {
97                 add_label_to_sizer (grid, this, _("Y Scale"), true, wxGBPosition (r, 0));
98                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
99                 _y_scale = new wxSpinCtrl (this);
100                 s->Add (_y_scale);
101                 add_label_to_sizer (s, this, _("%"), false);
102                 grid->Add (s, wxGBPosition (r, 1));
103                 ++r;
104         }
105
106         add_label_to_sizer (grid, this, _("Language"), true, wxGBPosition (r, 0));
107         _language = new wxTextCtrl (this, wxID_ANY);
108         grid->Add (_language, wxGBPosition (r, 1));
109         ++r;
110
111         add_label_to_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
112         _stream = new wxChoice (this, wxID_ANY);
113         grid->Add (_stream, wxGBPosition (r, 1));
114         ++r;
115
116         {
117                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
118
119                 _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
120                 s->Add (_subtitle_view_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
121                 _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
122                 s->Add (_fonts_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
123                 _appearance_dialog_button = new wxButton (this, wxID_ANY, _("Appearance..."));
124                 s->Add (_appearance_dialog_button, 1, wxALL, DCPOMATIC_SIZER_GAP);
125
126                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
127                 ++r;
128         }
129
130         _x_offset->SetRange (-100, 100);
131         _y_offset->SetRange (-100, 100);
132         _x_scale->SetRange (10, 1000);
133         _y_scale->SetRange (10, 1000);
134
135         _reference->Bind                (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::reference_clicked, this));
136         _use->Bind                      (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
137         _burn->Bind                     (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
138         _x_offset->Bind                 (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
139         _y_offset->Bind                 (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
140         _x_scale->Bind                  (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
141         _y_scale->Bind                  (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
142         _language->Bind                 (wxEVT_COMMAND_TEXT_UPDATED,     boost::bind (&SubtitlePanel::language_changed, this));
143         _stream->Bind                   (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
144         _subtitle_view_button->Bind     (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
145         _fonts_dialog_button->Bind      (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
146         _appearance_dialog_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::appearance_dialog_clicked, this));
147 }
148
149 void
150 SubtitlePanel::film_changed (Film::Property property)
151 {
152         if (property == Film::CONTENT || property == Film::REEL_TYPE) {
153                 setup_sensitivity ();
154         }
155 }
156
157 void
158 SubtitlePanel::film_content_changed (int property)
159 {
160         FFmpegContentList fc = _parent->selected_ffmpeg ();
161         SubtitleContentList sc = _parent->selected_subtitle ();
162
163         shared_ptr<FFmpegContent> fcs;
164         if (fc.size() == 1) {
165                 fcs = fc.front ();
166         }
167
168         shared_ptr<SubtitleContent> scs;
169         if (sc.size() == 1) {
170                 scs = sc.front ();
171         }
172
173         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
174                 _stream->Clear ();
175                 if (fcs) {
176                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
177                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
178                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
179                         }
180
181                         if (fcs->subtitle_stream()) {
182                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
183                         } else {
184                                 _stream->SetSelection (wxNOT_FOUND);
185                         }
186                 }
187                 setup_sensitivity ();
188         } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
189                 checked_set (_use, scs ? scs->use_subtitles() : false);
190                 setup_sensitivity ();
191         } else if (property == SubtitleContentProperty::BURN_SUBTITLES) {
192                 checked_set (_burn, scs ? scs->burn_subtitles() : false);
193         } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
194                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
195         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
196                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
197         } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
198                 checked_set (_x_scale, scs ? lrint (scs->subtitle_x_scale() * 100) : 100);
199         } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
200                 checked_set (_y_scale, scs ? lrint (scs->subtitle_y_scale() * 100) : 100);
201         } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
202                 checked_set (_language, scs ? scs->subtitle_language() : "");
203         } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
204                 if (scs) {
205                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
206                         checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
207                 } else {
208                         checked_set (_reference, false);
209                 }
210
211                 setup_sensitivity ();
212         }
213 }
214
215 void
216 SubtitlePanel::use_toggled ()
217 {
218         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
219                 i->set_use_subtitles (_use->GetValue());
220         }
221 }
222
223 void
224 SubtitlePanel::burn_toggled ()
225 {
226         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
227                 i->set_burn_subtitles (_burn->GetValue());
228         }
229 }
230
231 void
232 SubtitlePanel::setup_sensitivity ()
233 {
234         int any_subs = 0;
235         int ffmpeg_subs = 0;
236         int subrip_subs = 0;
237         int dcp_subs = 0;
238         int image_subs = 0;
239         SubtitleContentList sel = _parent->selected_subtitle ();
240         BOOST_FOREACH (shared_ptr<SubtitleContent> i, sel) {
241                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
242                 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (i);
243                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
244                 if (fc) {
245                         if (fc->has_subtitles ()) {
246                                 ++ffmpeg_subs;
247                                 ++any_subs;
248                         }
249                 } else if (sc) {
250                         ++subrip_subs;
251                         ++any_subs;
252                 } else if (dsc) {
253                         ++dcp_subs;
254                         ++any_subs;
255                 } else {
256                         ++any_subs;
257                 }
258
259                 if (i->has_image_subtitles ()) {
260                         ++image_subs;
261                         /* We must burn image subtitles at the moment */
262                         i->set_burn_subtitles (true);
263                 }
264         }
265
266         shared_ptr<DCPContent> dcp;
267         if (sel.size() == 1) {
268                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
269         }
270
271         list<string> why_not;
272         bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
273         _reference->Enable (can_reference);
274
275         wxString s;
276         if (!can_reference) {
277                 s = _("Cannot reference this DCP.  ");
278                 BOOST_FOREACH (string i, why_not) {
279                         s += std_to_wx(i) + wxT("  ");
280                 }
281         }
282         _reference->SetToolTip (s);
283
284         bool const reference = _reference->GetValue ();
285
286         _use->Enable (!reference && any_subs > 0);
287         bool const use = _use->GetValue ();
288         _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
289         _x_offset->Enable (!reference && any_subs > 0 && use);
290         _y_offset->Enable (!reference && any_subs > 0 && use);
291         _x_scale->Enable (!reference && any_subs > 0 && use);
292         _y_scale->Enable (!reference && any_subs > 0 && use);
293         _language->Enable (!reference && any_subs > 0 && use);
294         _stream->Enable (!reference && ffmpeg_subs == 1);
295         _subtitle_view_button->Enable (!reference && (subrip_subs == 1 || dcp_subs == 1));
296         _fonts_dialog_button->Enable (!reference && (subrip_subs == 1 || dcp_subs == 1));
297         _appearance_dialog_button->Enable (!reference && subrip_subs == 1);
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<SubtitleContent> i, _parent->selected_subtitle ()) {
326                 i->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
327         }
328 }
329
330 void
331 SubtitlePanel::y_offset_changed ()
332 {
333         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
334                 i->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
335         }
336 }
337
338 void
339 SubtitlePanel::x_scale_changed ()
340 {
341         SubtitleContentList c = _parent->selected_subtitle ();
342         if (c.size() == 1) {
343                 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
344         }
345 }
346
347 void
348 SubtitlePanel::y_scale_changed ()
349 {
350         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
351                 i->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
352         }
353 }
354
355 void
356 SubtitlePanel::language_changed ()
357 {
358         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
359                 i->set_subtitle_language (wx_to_std (_language->GetValue()));
360         }
361 }
362
363 void
364 SubtitlePanel::content_selection_changed ()
365 {
366         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
367         film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
368         film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
369         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
370         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
371         film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
372         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
373         film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
374         film_content_changed (SubtitleContentProperty::FONTS);
375         film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
376 }
377
378 void
379 SubtitlePanel::subtitle_view_clicked ()
380 {
381         if (_subtitle_view) {
382                 _subtitle_view->Destroy ();
383                 _subtitle_view = 0;
384         }
385
386         SubtitleContentList c = _parent->selected_subtitle ();
387         DCPOMATIC_ASSERT (c.size() == 1);
388
389         shared_ptr<SubtitleDecoder> decoder;
390
391         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
392         if (sr) {
393                 decoder.reset (new SubRipDecoder (sr));
394         }
395
396         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
397         if (dc) {
398                 decoder.reset (new DCPSubtitleDecoder (dc));
399         }
400
401         if (decoder) {
402                 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
403                 _subtitle_view->Show ();
404         }
405 }
406
407 void
408 SubtitlePanel::fonts_dialog_clicked ()
409 {
410         if (_fonts_dialog) {
411                 _fonts_dialog->Destroy ();
412                 _fonts_dialog = 0;
413         }
414
415         SubtitleContentList c = _parent->selected_subtitle ();
416         DCPOMATIC_ASSERT (c.size() == 1);
417
418         _fonts_dialog = new FontsDialog (this, c.front ());
419         _fonts_dialog->Show ();
420 }
421
422 void
423 SubtitlePanel::reference_clicked ()
424 {
425         ContentList c = _parent->selected ();
426         if (c.size() != 1) {
427                 return;
428         }
429
430         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
431         if (!d) {
432                 return;
433         }
434
435         d->set_reference_subtitle (_reference->GetValue ());
436 }
437
438 void
439 SubtitlePanel::appearance_dialog_clicked ()
440 {
441         SubtitleContentList c = _parent->selected_subtitle ();
442         DCPOMATIC_ASSERT (c.size() == 1);
443
444         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
445         DCPOMATIC_ASSERT (sr);
446
447         SubtitleAppearanceDialog* d = new SubtitleAppearanceDialog (this, sr);
448         if (d->ShowModal () == wxID_OK) {
449                 d->apply ();
450         }
451         d->Destroy ();
452 }