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