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