Some use of BOOST_FOREACH.
[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 into image"));
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 ? lrint (scs->subtitle_x_scale() * 100) : 100);
177         } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
178                 checked_set (_y_scale, scs ? lrint (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         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
188                 i->set_use_subtitles (_use->GetValue());
189         }
190 }
191
192 void
193 SubtitlePanel::burn_toggled ()
194 {
195         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
196                 i->set_burn_subtitles (_burn->GetValue());
197         }
198 }
199
200 void
201 SubtitlePanel::setup_sensitivity ()
202 {
203         int any_subs = 0;
204         int ffmpeg_subs = 0;
205         int subrip_or_dcp_subs = 0;
206         int image_subs = 0;
207         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
208                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (i);
209                 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (i);
210                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (i);
211                 if (fc) {
212                         if (fc->has_subtitles ()) {
213                                 ++ffmpeg_subs;
214                                 ++any_subs;
215                         }
216                 } else if (sc || dsc) {
217                         ++subrip_or_dcp_subs;
218                         ++any_subs;
219                 } else {
220                         ++any_subs;
221                 }
222
223                 if (i->has_image_subtitles ()) {
224                         ++image_subs;
225                         /* We must burn image subtitles at the moment */
226                         i->set_burn_subtitles (true);
227                 }
228         }
229
230         _use->Enable (any_subs > 0);
231         bool const use = _use->GetValue ();
232         _burn->Enable (any_subs > 0 && use && image_subs == 0);
233         _x_offset->Enable (any_subs > 0 && use);
234         _y_offset->Enable (any_subs > 0 && use);
235         _x_scale->Enable (any_subs > 0 && use);
236         _y_scale->Enable (any_subs > 0 && use);
237         _language->Enable (any_subs > 0 && use);
238         _stream->Enable (ffmpeg_subs == 1);
239         _subtitle_view_button->Enable (subrip_or_dcp_subs == 1);
240         _fonts_dialog_button->Enable (subrip_or_dcp_subs == 1);
241 }
242
243 void
244 SubtitlePanel::stream_changed ()
245 {
246         FFmpegContentList fc = _parent->selected_ffmpeg ();
247         if (fc.size() != 1) {
248                 return;
249         }
250
251         shared_ptr<FFmpegContent> fcs = fc.front ();
252
253         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
254         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
255         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
256         while (i != a.end() && (*i)->identifier () != s) {
257                 ++i;
258         }
259
260         if (i != a.end ()) {
261                 fcs->set_subtitle_stream (*i);
262         }
263 }
264
265 void
266 SubtitlePanel::x_offset_changed ()
267 {
268         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
269                 i->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
270         }
271 }
272
273 void
274 SubtitlePanel::y_offset_changed ()
275 {
276         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
277                 i->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
278         }
279 }
280
281 void
282 SubtitlePanel::x_scale_changed ()
283 {
284         SubtitleContentList c = _parent->selected_subtitle ();
285         if (c.size() == 1) {
286                 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
287         }
288 }
289
290 void
291 SubtitlePanel::y_scale_changed ()
292 {
293         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
294                 i->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
295         }
296 }
297
298 void
299 SubtitlePanel::language_changed ()
300 {
301         BOOST_FOREACH (shared_ptr<SubtitleContent> i, _parent->selected_subtitle ()) {
302                 i->set_subtitle_language (wx_to_std (_language->GetValue()));
303         }
304 }
305
306 void
307 SubtitlePanel::content_selection_changed ()
308 {
309         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
310         film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
311         film_content_changed (SubtitleContentProperty::BURN_SUBTITLES);
312         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
313         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
314         film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
315         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
316         film_content_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
317         film_content_changed (SubtitleContentProperty::FONTS);
318 }
319
320 void
321 SubtitlePanel::subtitle_view_clicked ()
322 {
323         if (_subtitle_view) {
324                 _subtitle_view->Destroy ();
325                 _subtitle_view = 0;
326         }
327
328         SubtitleContentList c = _parent->selected_subtitle ();
329         DCPOMATIC_ASSERT (c.size() == 1);
330
331         shared_ptr<SubtitleDecoder> decoder;
332
333         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
334         if (sr) {
335                 decoder.reset (new SubRipDecoder (sr));
336         }
337
338         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
339         if (dc) {
340                 decoder.reset (new DCPSubtitleDecoder (dc));
341         }
342
343         if (decoder) {
344                 _subtitle_view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
345                 _subtitle_view->Show ();
346         }
347 }
348
349 void
350 SubtitlePanel::fonts_dialog_clicked ()
351 {
352         if (_fonts_dialog) {
353                 _fonts_dialog->Destroy ();
354                 _fonts_dialog = 0;
355         }
356
357         SubtitleContentList c = _parent->selected_subtitle ();
358         DCPOMATIC_ASSERT (c.size() == 1);
359
360         _fonts_dialog = new FontsDialog (this, c.front ());
361         _fonts_dialog->Show ();
362 }