Merge.
[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 "subtitle_panel.h"
24 #include "film_editor.h"
25 #include "wx_util.h"
26
27 using std::vector;
28 using std::string;
29 using boost::shared_ptr;
30 using boost::lexical_cast;
31 using boost::dynamic_pointer_cast;
32
33 SubtitlePanel::SubtitlePanel (FilmEditor* e)
34         : FilmEditorPanel (e, _("Subtitles"))
35 {
36         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
37         _sizer->Add (grid, 0, wxALL, 8);
38
39         _with_subtitles = new wxCheckBox (this, wxID_ANY, _("With Subtitles"));
40         grid->Add (_with_subtitles, 1);
41         grid->AddSpacer (0);
42         
43         {
44                 add_label_to_sizer (grid, this, _("X Offset"), true);
45                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
46                 _x_offset = new wxSpinCtrl (this);
47                 s->Add (_x_offset);
48                 add_label_to_sizer (s, this, _("%"), false);
49                 grid->Add (s);
50         }
51
52         {
53                 add_label_to_sizer (grid, this, _("Y Offset"), true);
54                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
55                 _y_offset = new wxSpinCtrl (this);
56                 s->Add (_y_offset);
57                 add_label_to_sizer (s, this, _("%"), false);
58                 grid->Add (s);
59         }
60         
61         {
62                 add_label_to_sizer (grid, this, _("Scale"), true);
63                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
64                 _scale = new wxSpinCtrl (this);
65                 s->Add (_scale);
66                 add_label_to_sizer (s, this, _("%"), false);
67                 grid->Add (s);
68         }
69
70         add_label_to_sizer (grid, this, _("Stream"), true);
71         _stream = new wxChoice (this, wxID_ANY);
72         grid->Add (_stream, 1, wxEXPAND);
73         
74         _x_offset->SetRange (-100, 100);
75         _y_offset->SetRange (-100, 100);
76         _scale->SetRange (1, 1000);
77         _scale->SetValue (100);
78
79         _with_subtitles->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::with_subtitles_toggled, this));
80         _x_offset->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
81         _y_offset->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
82         _scale->Bind          (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::scale_changed, this));
83         _stream->Bind         (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
84 }
85
86 void
87 SubtitlePanel::film_changed (Film::Property property)
88 {
89         switch (property) {
90         case Film::CONTENT:
91                 setup_sensitivity ();
92                 break;
93         case Film::WITH_SUBTITLES:
94                 checked_set (_with_subtitles, _editor->film()->with_subtitles ());
95                 setup_sensitivity ();
96                 break;
97         default:
98                 break;
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_X_OFFSET) {
134                 checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
135         } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
136                 checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
137         } else if (property == SubtitleContentProperty::SUBTITLE_SCALE) {
138                 checked_set (_scale, scs ? (scs->subtitle_scale() * 100) : 100);
139         }
140 }
141
142 void
143 SubtitlePanel::with_subtitles_toggled ()
144 {
145         if (!_editor->film()) {
146                 return;
147         }
148
149         _editor->film()->set_with_subtitles (_with_subtitles->GetValue ());
150 }
151
152 void
153 SubtitlePanel::setup_sensitivity ()
154 {
155         bool h = false;
156         bool j = false;
157         if (_editor->film()) {
158                 h = _editor->film()->has_subtitles ();
159                 j = _editor->film()->with_subtitles ();
160         }
161         
162         _with_subtitles->Enable (h);
163         _x_offset->Enable (j);
164         _y_offset->Enable (j);
165         _scale->Enable (j);
166         _stream->Enable (j);
167 }
168
169 void
170 SubtitlePanel::stream_changed ()
171 {
172         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
173         if (fc.size() != 1) {
174                 return;
175         }
176
177         shared_ptr<FFmpegContent> fcs = fc.front ();
178         
179         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
180         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
181         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
182         while (i != a.end() && (*i)->identifier () != s) {
183                 ++i;
184         }
185
186         if (i != a.end ()) {
187                 fcs->set_subtitle_stream (*i);
188         }
189 }
190
191 void
192 SubtitlePanel::x_offset_changed ()
193 {
194         SubtitleContentList c = _editor->selected_subtitle_content ();
195         if (c.size() == 1) {
196                 c.front()->set_subtitle_x_offset (_x_offset->GetValue() / 100.0);
197         }
198 }
199
200 void
201 SubtitlePanel::y_offset_changed ()
202 {
203         SubtitleContentList c = _editor->selected_subtitle_content ();
204         if (c.size() == 1) {
205                 c.front()->set_subtitle_y_offset (_y_offset->GetValue() / 100.0);
206         }
207 }
208
209 void
210 SubtitlePanel::scale_changed ()
211 {
212         SubtitleContentList c = _editor->selected_subtitle_content ();
213         if (c.size() == 1) {
214                 c.front()->set_subtitle_scale (_scale->GetValue() / 100.0);
215         }
216 }
217
218 void
219 SubtitlePanel::content_selection_changed ()
220 {
221         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
222         film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
223         film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
224         film_content_changed (SubtitleContentProperty::SUBTITLE_SCALE);
225 }