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