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