Some attempts to block referencing of DCPs when it is not possible.
[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 "lib/ffmpeg_content.h"
27 #include "lib/subrip_content.h"
28 #include "lib/ffmpeg_subtitle_stream.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/subrip_decoder.h"
31 #include "lib/dcp_subtitle_decoder.h"
32 #include "lib/dcp_content.h"
33 #include <wx/spinctrl.h>
34 #include <boost/lexical_cast.hpp>
35 #include <boost/foreach.hpp>
36
37 using std::vector;
38 using std::string;
39 using std::list;
40 using boost::shared_ptr;
41 using boost::lexical_cast;
42 using boost::dynamic_pointer_cast;
43
44 SubtitlePanel::SubtitlePanel (ContentPanel* p)
45         : ContentSubPanel (p, _("Subtitles"))
46         , _subtitle_view (0)
47         , _fonts_dialog (0)
48 {
49         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
50         _sizer->Add (grid, 0, wxALL, 8);
51
52         _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
53         grid->Add (_reference);
54         grid->AddSpacer (0);
55
56         _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
57         grid->Add (_use);
58         grid->AddSpacer (0);
59
60         _burn = new wxCheckBox (this, wxID_ANY, _("Burn subtitles into image"));
61         grid->Add (_burn);
62         grid->AddSpacer (0);
63
64         {
65                 add_label_to_sizer (grid, this, _("X Offset"), true);
66                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
67                 _x_offset = new wxSpinCtrl (this);
68                 s->Add (_x_offset);
69                 add_label_to_sizer (s, this, _("%"), false);
70                 grid->Add (s);
71         }
72
73         {
74                 add_label_to_sizer (grid, this, _("Y Offset"), true);
75                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
76                 _y_offset = new wxSpinCtrl (this);
77                 s->Add (_y_offset);
78                 add_label_to_sizer (s, this, _("%"), false);
79                 grid->Add (s);
80         }
81
82         {
83                 add_label_to_sizer (grid, this, _("X Scale"), true);
84                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
85                 _x_scale = new wxSpinCtrl (this);
86                 s->Add (_x_scale);
87                 add_label_to_sizer (s, this, _("%"), false);
88                 grid->Add (s);
89         }
90
91         {
92                 add_label_to_sizer (grid, this, _("Y Scale"), true);
93                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
94                 _y_scale = new wxSpinCtrl (this);
95                 s->Add (_y_scale);
96                 add_label_to_sizer (s, this, _("%"), false);
97                 grid->Add (s);
98         }
99
100         add_label_to_sizer (grid, this, _("Language"), true);
101         _language = new wxTextCtrl (this, wxID_ANY);
102         grid->Add (_language, 1, wxEXPAND);
103
104         add_label_to_sizer (grid, this, _("Stream"), true);
105         _stream = new wxChoice (this, wxID_ANY);
106         grid->Add (_stream, 1, wxEXPAND);
107
108         _subtitle_view_button = new wxButton (this, wxID_ANY, _("View..."));
109         grid->Add (_subtitle_view_button);
110         grid->AddSpacer (0);
111
112         _fonts_dialog_button = new wxButton (this, wxID_ANY, _("Fonts..."));
113         grid->Add (_fonts_dialog_button);
114         grid->AddSpacer (0);
115
116         _x_offset->SetRange (-100, 100);
117         _y_offset->SetRange (-100, 100);
118         _x_scale->SetRange (10, 1000);
119         _y_scale->SetRange (10, 1000);
120
121         _reference->Bind            (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::reference_clicked, this));
122         _use->Bind                  (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
123         _burn->Bind                 (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::burn_toggled, this));
124         _x_offset->Bind             (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
125         _y_offset->Bind             (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
126         _x_scale->Bind              (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
127         _y_scale->Bind              (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
128         _language->Bind             (wxEVT_COMMAND_TEXT_UPDATED,     boost::bind (&SubtitlePanel::language_changed, this));
129         _stream->Bind               (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
130         _subtitle_view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::subtitle_view_clicked, this));
131         _fonts_dialog_button->Bind  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::fonts_dialog_clicked, this));
132 }
133
134 void
135 SubtitlePanel::film_changed (Film::Property property)
136 {
137         if (property == Film::CONTENT || property == Film::REEL_TYPE) {
138                 setup_sensitivity ();
139         }
140 }
141
142 void
143 SubtitlePanel::film_content_changed (int property)
144 {
145         FFmpegContentList fc = _parent->selected_ffmpeg ();
146         SubtitleContentList sc = _parent->selected_subtitle ();
147
148         shared_ptr<FFmpegContent> fcs;
149         if (fc.size() == 1) {
150                 fcs = fc.front ();
151         }
152
153         shared_ptr<SubtitleContent> scs;
154         if (sc.size() == 1) {
155                 scs = sc.front ();
156         }
157
158         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
159                 _stream->Clear ();
160                 if (fcs) {
161                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
162                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
163                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
164                         }
165
166                         if (fcs->subtitle_stream()) {
167                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
168                         } else {
169                                 _stream->SetSelection (wxNOT_FOUND);
170                         }
171                 }
172                 setup_sensitivity ();
173         } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
174                 checked_set (_use, scs ? scs->use_subtitles() : false);
175                 setup_sensitivity ();
176         } else if (property == SubtitleContentProperty::BURN_SUBTITLES) {
177                 checked_set (_burn, scs ? scs->burn_subtitles() : false);
178         } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
179                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
180         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
181                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
182         } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
183                 checked_set (_x_scale, scs ? lrint (scs->subtitle_x_scale() * 100) : 100);
184         } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
185                 checked_set (_y_scale, scs ? lrint (scs->subtitle_y_scale() * 100) : 100);
186         } else if (property == SubtitleContentProperty::SUBTITLE_LANGUAGE) {
187                 checked_set (_language, scs ? scs->subtitle_language() : "");
188         } else if (property == DCPContentProperty::REFERENCE_SUBTITLE) {
189                 if (scs) {
190                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (scs);
191                         checked_set (_reference, dcp ? dcp->reference_subtitle () : false);
192                 } else {
193                         checked_set (_reference, false);
194                 }
195
196                 setup_sensitivity ();
197         }
198 }
199
200 void
201 SubtitlePanel::use_toggled ()
202 {
203         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
204                 i->set_use_subtitles (_use->GetValue());
205         }
206 }
207
208 void
209 SubtitlePanel::burn_toggled ()
210 {
211         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
212                 i->set_burn_subtitles (_burn->GetValue());
213         }
214 }
215
216 void
217 SubtitlePanel::setup_sensitivity ()
218 {
219         int any_subs = 0;
220         int ffmpeg_subs = 0;
221         int subrip_or_dcp_subs = 0;
222         int image_subs = 0;
223         SubtitleContentList sel = _parent->selected_subtitle ();
224         BOOST_FOREACH (shared_ptr<SubtitleContent> i, sel) {
225                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
226                 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (i);
227                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
228                 if (fc) {
229                         if (fc->has_subtitles ()) {
230                                 ++ffmpeg_subs;
231                                 ++any_subs;
232                         }
233                 } else if (sc || dsc) {
234                         ++subrip_or_dcp_subs;
235                         ++any_subs;
236                 } else {
237                         ++any_subs;
238                 }
239
240                 if (i->has_image_subtitles ()) {
241                         ++image_subs;
242                         /* We must burn image subtitles at the moment */
243                         i->set_burn_subtitles (true);
244                 }
245         }
246
247         shared_ptr<DCPContent> dcp;
248         if (sel.size() == 1) {
249                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
250         }
251
252         list<string> why_not;
253         bool const can_reference = dcp && dcp->can_reference_subtitle (why_not);
254         _reference->Enable (can_reference);
255
256         wxString s;
257         if (!can_reference) {
258                 s = _("Cannot reference this DCP.  ");
259                 BOOST_FOREACH (string i, why_not) {
260                         s += std_to_wx(i) + wxT("  ");
261                 }
262         }
263         _reference->SetToolTip (s);
264
265         bool const reference = _reference->GetValue ();
266
267         _use->Enable (!reference && any_subs > 0);
268         bool const use = _use->GetValue ();
269         _burn->Enable (!reference && any_subs > 0 && use && image_subs == 0);
270         _x_offset->Enable (!reference && any_subs > 0 && use);
271         _y_offset->Enable (!reference && any_subs > 0 && use);
272         _x_scale->Enable (!reference && any_subs > 0 && use);
273         _y_scale->Enable (!reference && any_subs > 0 && use);
274         _language->Enable (!reference && any_subs > 0 && use);
275         _stream->Enable (!reference && ffmpeg_subs == 1);
276         _subtitle_view_button->Enable (!reference && subrip_or_dcp_subs == 1);
277         _fonts_dialog_button->Enable (!reference && subrip_or_dcp_subs == 1);
278 }
279
280 void
281 SubtitlePanel::stream_changed ()
282 {
283         FFmpegContentList fc = _parent->selected_ffmpeg ();
284         if (fc.size() != 1) {
285                 return;
286         }
287
288         shared_ptr<FFmpegContent> fcs = fc.front ();
289
290         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
291         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
292         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
293         while (i != a.end() && (*i)->identifier () != s) {
294                 ++i;
295         }
296
297         if (i != a.end ()) {
298                 fcs->set_subtitle_stream (*i);
299         }
300 }
301
302 void
303 SubtitlePanel::x_offset_changed ()
304 {
305         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
306                 i->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
307         }
308 }
309
310 void
311 SubtitlePanel::y_offset_changed ()
312 {
313         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
314                 i->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
315         }
316 }
317
318 void
319 SubtitlePanel::x_scale_changed ()
320 {
321         SubtitleContentList c = _parent->selected_subtitle ();
322         if (c.size() == 1) {
323                 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
324         }
325 }
326
327 void
328 SubtitlePanel::y_scale_changed ()
329 {
330         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
331                 i->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
332         }
333 }
334
335 void
336 SubtitlePanel::language_changed ()
337 {
338         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
339                 i->set_subtitle_language (wx_to_std (_language->GetValue()));
340         }
341 }
342
343 void
344 SubtitlePanel::content_selection_changed ()
345 {
346         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
347         film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
348         film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
349         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
350         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
351         film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
352         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
353         film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
354         film_content_changed (SubtitleContentProperty::FONTS);
355         film_content_changed (DCPContentProperty::REFERENCE_SUBTITLE);
356 }
357
358 void
359 SubtitlePanel::subtitle_view_clicked ()
360 {
361         if (_subtitle_view) {
362                 _subtitle_view->Destroy ();
363                 _subtitle_view = 0;
364         }
365
366         SubtitleContentList c = _parent->selected_subtitle ();
367         DCPOMATIC_ASSERT (c.size() == 1);
368
369         shared_ptr<SubtitleDecoder> decoder;
370
371         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
372         if (sr) {
373                 decoder.reset (new SubRipDecoder (sr));
374         }
375
376         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
377         if (dc) {
378                 decoder.reset (new DCPSubtitleDecoder (dc));
379         }
380
381         if (decoder) {
382                 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
383                 _subtitle_view->Show ();
384         }
385 }
386
387 void
388 SubtitlePanel::fonts_dialog_clicked ()
389 {
390         if (_fonts_dialog) {
391                 _fonts_dialog->Destroy ();
392                 _fonts_dialog = 0;
393         }
394
395         SubtitleContentList c = _parent->selected_subtitle ();
396         DCPOMATIC_ASSERT (c.size() == 1);
397
398         _fonts_dialog = new FontsDialog (this, c.front ());
399         _fonts_dialog->Show ();
400 }
401
402 void
403 SubtitlePanel::reference_clicked ()
404 {
405         ContentList c = _parent->selected ();
406         if (c.size() != 1) {
407                 return;
408         }
409
410         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
411         if (!d) {
412                 return;
413         }
414
415         d->set_reference_subtitle (_reference->GetValue ());
416 }