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