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, _("Scale"), true);
71                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
72                 _scale = new wxSpinCtrl (this);
73                 s->Add (_scale);
74                 add_label_to_sizer (s, this, _("%"), false);
75                 grid->Add (s);
76         }
77
78         add_label_to_sizer (grid, this, _("Stream"), true);
79         _stream = new wxChoice (this, wxID_ANY);
80         grid->Add (_stream, 1, wxEXPAND);
81
82         _view_button = new wxButton (this, wxID_ANY, _("View..."));
83         grid->Add (_view_button);
84         
85         _x_offset->SetRange (-100, 100);
86         _y_offset->SetRange (-100, 100);
87         _scale->SetRange (1, 1000);
88         _scale->SetValue (100);
89
90         _use->Bind         (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
91         _x_offset->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
92         _y_offset->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
93         _scale->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::scale_changed, this));
94         _stream->Bind      (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
95         _view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::view_clicked, this));
96 }
97
98 void
99 SubtitlePanel::film_changed (Film::Property property)
100 {
101         if (property == Film::CONTENT) {
102                 setup_sensitivity ();
103         }
104 }
105
106 void
107 SubtitlePanel::film_content_changed (int property)
108 {
109         FFmpegContentList fc = _parent->selected_ffmpeg ();
110         SubtitleContentList sc = _parent->selected_subtitle ();
111
112         shared_ptr<FFmpegContent> fcs;
113         if (fc.size() == 1) {
114                 fcs = fc.front ();
115         }
116
117         shared_ptr<SubtitleContent> scs;
118         if (sc.size() == 1) {
119                 scs = sc.front ();
120         }
121
122         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
123                 _stream->Clear ();
124                 if (fcs) {
125                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
126                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
127                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
128                         }
129                         
130                         if (fcs->subtitle_stream()) {
131                                 checked_set (_stream, fcs->subtitle_stream()->identifier ());
132                         } else {
133                                 _stream->SetSelection (wxNOT_FOUND);
134                         }
135                 }
136                 setup_sensitivity ();
137         } else if (property == SubtitleContentProperty::USE_SUBTITLES) {
138                 checked_set (_use, scs ? scs->use_subtitles() : false);
139                 setup_sensitivity ();
140         } else if (property == SubtitleContentProperty::SUBTITLE_X_OFFSET) {
141                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
142         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
143                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
144         } else if (property == SubtitleContentProperty::SUBTITLE_SCALE) {
145                 checked_set (_scale, scs ? (scs->subtitle_scale() * 100) : 100);
146         }
147 }
148
149 void
150 SubtitlePanel::use_toggled ()
151 {
152         SubtitleContentList c = _parent->selected_subtitle ();
153         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
154                 (*i)->set_use_subtitles (_use->GetValue());
155         }
156 }
157
158 void
159 SubtitlePanel::setup_sensitivity ()
160 {
161         int any_subs = 0;
162         int ffmpeg_subs = 0;
163         int subrip_or_dcp_subs = 0;
164         SubtitleContentList c = _parent->selected_subtitle ();
165         for (SubtitleContentList::const_iterator i = c.begin(); i != c.end(); ++i) {
166                 shared_ptr<const FFmpegContent> fc = boost::dynamic_pointer_cast<const FFmpegContent> (*i);
167                 shared_ptr<const SubRipContent> sc = boost::dynamic_pointer_cast<const SubRipContent> (*i);
168                 shared_ptr<const DCPSubtitleContent> dsc = boost::dynamic_pointer_cast<const DCPSubtitleContent> (*i);
169                 if (fc) {
170                         if (fc->has_subtitles ()) {
171                                 ++ffmpeg_subs;
172                                 ++any_subs;
173                         }
174                 } else if (sc || dsc) {
175                         ++subrip_or_dcp_subs;
176                         ++any_subs;
177                 } else {
178                         ++any_subs;
179                 }
180         }
181                 
182         _use->Enable (any_subs > 0);
183         bool const use = _use->GetValue ();
184         
185         _x_offset->Enable (any_subs > 0 && use);
186         _y_offset->Enable (any_subs > 0 && use);
187         _scale->Enable (any_subs > 0 && use);
188         _stream->Enable (ffmpeg_subs == 1);
189         _view_button->Enable (subrip_or_dcp_subs == 1);
190 }
191
192 void
193 SubtitlePanel::stream_changed ()
194 {
195         FFmpegContentList fc = _parent->selected_ffmpeg ();
196         if (fc.size() != 1) {
197                 return;
198         }
199
200         shared_ptr<FFmpegContent> fcs = fc.front ();
201         
202         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
203         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
204         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
205         while (i != a.end() && (*i)->identifier () != s) {
206                 ++i;
207         }
208
209         if (i != a.end ()) {
210                 fcs->set_subtitle_stream (*i);
211         }
212 }
213
214 void
215 SubtitlePanel::x_offset_changed ()
216 {
217         SubtitleContentList c = _parent->selected_subtitle ();
218         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
219                 (*i)->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
220         }
221 }
222
223 void
224 SubtitlePanel::y_offset_changed ()
225 {
226         SubtitleContentList c = _parent->selected_subtitle ();
227         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
228                 (*i)->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
229         }
230 }
231
232 void
233 SubtitlePanel::scale_changed ()
234 {
235         SubtitleContentList c = _parent->selected_subtitle ();
236         for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
237                 (*i)->set_subtitle_scale (_scale->GetValue() / 100.0);
238         }
239 }
240
241 void
242 SubtitlePanel::content_selection_changed ()
243 {
244         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
245         film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
246         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
247         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
248         film_content_changed (SubtitleContentProperty::SUBTITLE_SCALE);
249 }
250
251 void
252 SubtitlePanel::view_clicked ()
253 {
254         if (_view) {
255                 _view->Destroy ();
256                 _view = 0;
257         }
258
259         SubtitleContentList c = _parent->selected_subtitle ();
260         assert (c.size() == 1);
261
262         shared_ptr<SubtitleDecoder> decoder;
263         
264         shared_ptr<SubRipContent> sr = dynamic_pointer_cast<SubRipContent> (c.front ());
265         if (sr) {
266                 decoder.reset (new SubRipDecoder (sr));
267         }
268         
269         shared_ptr<DCPSubtitleContent> dc = dynamic_pointer_cast<DCPSubtitleContent> (c.front ());
270         if (dc) {
271                 decoder.reset (new DCPSubtitleDecoder (dc));
272         }
273         
274         if (decoder) {
275                 _view = new SubtitleView (this, _parent->film(), decoder, c.front()->position ());
276                 _view->Show ();
277         }
278 }