Merge remote-tracking branch 'origin/master' into 2.0
[dcpomatic.git] / src / wx / subtitle_panel.cc
1 /*
2     Copyright (C) 2012-2014 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 <boost/lexical_cast.hpp>
21 #include <wx/spinctrl.h>
22 #include "lib/ffmpeg_content.h"
23 #include "lib/subrip_content.h"
24 #include "lib/ffmpeg_subtitle_stream.h"
25 #include "lib/dcp_subtitle_content.h"
26 #include "lib/subrip_decoder.h"
27 #include "lib/dcp_subtitle_decoder.h"
28 #include "subtitle_panel.h"
29 #include "film_editor.h"
30 #include "wx_util.h"
31 #include "subtitle_view.h"
32 #include "content_panel.h"
33
34 using std::vector;
35 using std::string;
36 using boost::shared_ptr;
37 using boost::lexical_cast;
38 using boost::dynamic_pointer_cast;
39
40 SubtitlePanel::SubtitlePanel (ContentPanel* p)
41         : ContentSubPanel (p, _("Subtitles"))
42         , _view (0)
43 {
44         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45         _sizer->Add (grid, 0, wxALL, 8);
46
47         _use = new wxCheckBox (this, wxID_ANY, _("Use subtitles"));
48         grid->Add (_use);
49         grid->AddSpacer (0);
50
51         {
52                 add_label_to_sizer (grid, this, _("X Offset"), true);
53                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
54                 _x_offset = new wxSpinCtrl (this);
55                 s->Add (_x_offset);
56                 add_label_to_sizer (s, this, _("%"), false);
57                 grid->Add (s);
58         }
59
60         {
61                 add_label_to_sizer (grid, this, _("Y Offset"), true);
62                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
63                 _y_offset = new wxSpinCtrl (this);
64                 s->Add (_y_offset);
65                 add_label_to_sizer (s, this, _("%"), false);
66                 grid->Add (s);
67         }
68         
69         {
70                 add_label_to_sizer (grid, this, _("X Scale"), true);
71                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
72                 _x_scale = new wxSpinCtrl (this);
73                 s->Add (_x_scale);
74                 add_label_to_sizer (s, this, _("%"), false);
75                 grid->Add (s);
76         }
77
78         {
79                 add_label_to_sizer (grid, this, _("Y Scale"), true);
80                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
81                 _y_scale = new wxSpinCtrl (this);
82                 s->Add (_y_scale);
83                 add_label_to_sizer (s, this, _("%"), false);
84                 grid->Add (s);
85         }
86         
87         add_label_to_sizer (grid, this, _("Stream"), true);
88         _stream = new wxChoice (this, wxID_ANY);
89         grid->Add (_stream, 1, wxEXPAND);
90
91         _view_button = new wxButton (this, wxID_ANY, _("View..."));
92         grid->Add (_view_button);
93         
94         _x_offset->SetRange (-100, 100);
95         _y_offset->SetRange (-100, 100);
96         _x_scale->SetRange (10, 1000);
97         _y_scale->SetRange (10, 1000);
98
99         _use->Bind         (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
100         _x_offset->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
101         _y_offset->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
102         _x_scale->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
103         _y_scale->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
104         _stream->Bind      (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
105         _view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::view_clicked, this));
106 }
107
108 void
109 SubtitlePanel::film_changed (Film::Property property)
110 {
111         if (property == Film::CONTENT) {
112                 setup_sensitivity ();
113         }
114 }
115
116 void
117 SubtitlePanel::film_content_changed (int property)
118 {
119         FFmpegContentList fc = _parent->selected_ffmpeg ();
120         SubtitleContentList sc = _parent->selected_subtitle ();
121
122         shared_ptr<FFmpegContent> fcs;
123         if (fc.size() == 1) {
124                 fcs = fc.front ();
125         }
126
127         shared_ptr<SubtitleContent> scs;
128         if (sc.size() == 1) {
129                 scs = sc.front ();
130         }
131
132         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
133                 _stream->Clear ();
134                 if (fcs) {
135                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
136                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
137                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
138                         }
139                         
140                         if (fcs->subtitle_stream()) {
141                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
142                         } else {
143                                 _stream->SetSelection (wxNOT_FOUND);
144                         }
145                 }
146                 setup_sensitivity ();
147         } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
148                 checked_set (_use, scs ? scs->use_subtitles() : false);
149                 setup_sensitivity ();
150         } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
151                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
152         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
153                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
154         } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
155                 checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
156         } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
157                 checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
158         }
159 }
160
161 void
162 SubtitlePanel::use_toggled ()
163 {
164         SubtitleContentList c = _parent->selected_subtitle ();
165         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
166                 (*i)->set_use_subtitles (_use->GetValue());
167         }
168 }
169
170 void
171 SubtitlePanel::setup_sensitivity ()
172 {
173         int any_subs = 0;
174         int ffmpeg_subs = 0;
175         int subrip_or_dcp_subs = 0;
176         SubtitleContentList c = _parent->selected_subtitle ();
177         for (SubtitleContentList::const_iterator i = c.begin(); i != c.end(); ++i) {
178                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (*i);
179                 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (*i);
180                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (*i);
181                 if (fc) {
182                         if (fc->has_subtitles ()) {
183                                 ++ffmpeg_subs;
184                                 ++any_subs;
185                         }
186                 } else if (sc || dsc) {
187                         ++subrip_or_dcp_subs;
188                         ++any_subs;
189                 } else {
190                         ++any_subs;
191                 }
192         }
193                 
194         _use->Enable (any_subs > 0);
195         bool const use = _use->GetValue ();
196         
197         _x_offset->Enable (any_subs > 0 && use);
198         _y_offset->Enable (any_subs > 0 && use);
199         _x_scale->Enable (any_subs > 0 && use);
200         _y_scale->Enable (any_subs > 0 && use);
201         _stream->Enable (ffmpeg_subs == 1);
202         _view_button->Enable (subrip_or_dcp_subs == 1);
203 }
204
205 void
206 SubtitlePanel::stream_changed ()
207 {
208         FFmpegContentList fc = _parent->selected_ffmpeg ();
209         if (fc.size() != 1) {
210                 return;
211         }
212
213         shared_ptr<FFmpegContent> fcs = fc.front ();
214         
215         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
216         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
217         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
218         while (i != a.end() && (*i)->identifier () != s) {
219                 ++i;
220         }
221
222         if (i != a.end ()) {
223                 fcs->set_subtitle_stream (*i);
224         }
225 }
226
227 void
228 SubtitlePanel::x_offset_changed ()
229 {
230         SubtitleContentList c = _parent->selected_subtitle ();
231         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
232                 (*i)->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
233         }
234 }
235
236 void
237 SubtitlePanel::y_offset_changed ()
238 {
239         SubtitleContentList c = _parent->selected_subtitle ();
240         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
241                 (*i)->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
242         }
243 }
244
245 void
246 SubtitlePanel::x_scale_changed ()
247 {
248         SubtitleContentList c = _parent->selected_subtitle ();
249         if (c.size() == 1) {
250                 c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
251         }
252 }
253
254 void
255 SubtitlePanel::y_scale_changed ()
256 {
257         SubtitleContentList c = _parent->selected_subtitle ();
258         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
259                 (*i)->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
260         }
261 }
262
263 void
264 SubtitlePanel::content_selection_changed ()
265 {
266         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
267         film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
268         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
269         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
270         film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
271         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
272 }
273
274 void
275 SubtitlePanel::view_clicked ()
276 {
277         if (_view) {
278                 _view->Destroy ();
279                 _view = 0;
280         }
281
282         SubtitleContentList c = _parent->selected_subtitle ();
283         assert (c.size() == 1);
284
285         shared_ptr<SubtitleDecoder> decoder;
286         
287         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
288         if (sr) {
289                 decoder.reset (new SubRipDecoder (sr));
290         }
291         
292         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
293         if (dc) {
294                 decoder.reset (new DCPSubtitleDecoder (dc));
295         }
296         
297         if (decoder) {
298                 _view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
299                 _view->Show ();
300         }
301 }