Split video panel.
[dcpomatic.git] / src / wx / subtitle_panel.cc
1 /*
2     Copyright (C) 2012-2013 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 "subtitle_panel.h"
23 #include "film_editor.h"
24 #include "wx_util.h"
25
26 using std::vector;
27 using std::string;
28 using boost::shared_ptr;
29 using boost::lexical_cast;
30 using boost::dynamic_pointer_cast;
31
32 SubtitlePanel::SubtitlePanel (FilmEditor* e)
33         : FilmEditorPanel (e, _("Subtitles"))
34 {
35         wxFlexGridSizer* grid = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
36         _sizer->Add (grid, 0, wxALL, 8);
37
38         _with_subtitles = new wxCheckBox (this, wxID_ANY, _("With Subtitles"));
39         grid->Add (_with_subtitles, 1);
40         grid->AddSpacer (0);
41         
42         {
43                 add_label_to_sizer (grid, this, _("Subtitle Offset"), true);
44                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
45                 _offset = new wxSpinCtrl (this);
46                 s->Add (_offset);
47                 add_label_to_sizer (s, this, _("%"), false);
48                 grid->Add (s);
49         }
50
51         {
52                 add_label_to_sizer (grid, this, _("Subtitle Scale"), true);
53                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
54                 _scale = new wxSpinCtrl (this);
55                 s->Add (_scale);
56                 add_label_to_sizer (s, this, _("%"), false);
57                 grid->Add (s);
58         }
59
60         add_label_to_sizer (grid, this, _("Subtitle Stream"), true);
61         _stream = new wxChoice (this, wxID_ANY);
62         grid->Add (_stream, 1, wxEXPAND);
63         
64         _offset->SetRange (-100, 100);
65         _scale->SetRange (1, 1000);
66         _scale->SetValue (100);
67
68         _with_subtitles->Connect  (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (SubtitlePanel::with_subtitles_toggled), 0, this);
69         _offset->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (SubtitlePanel::offset_changed), 0, this);
70         _scale->Connect  (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (SubtitlePanel::scale_changed), 0, this);
71         _stream->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED,  wxCommandEventHandler (SubtitlePanel::stream_changed), 0, this);
72 }
73
74 void
75 SubtitlePanel::film_changed (Film::Property property)
76 {
77         switch (property) {
78         case Film::CONTENT:
79                 setup_control_sensitivity ();
80                 break;
81         case Film::WITH_SUBTITLES:
82                 checked_set (_with_subtitles, _editor->film()->with_subtitles ());
83                 setup_control_sensitivity ();
84                 break;
85         default:
86                 break;
87         }
88 }
89
90 void
91 SubtitlePanel::film_content_changed (shared_ptr<Content> c, int property)
92 {
93         shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c);
94         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
95         
96         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
97                 _stream->Clear ();
98                 if (fc) {
99                         vector<shared_ptr<FFmpegSubtitleStream> > s = fc->subtitle_streams ();
100                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
101                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
102                         }
103                         
104                         if (fc->subtitle_stream()) {
105                                 checked_set (_stream, lexical_cast<string> (fc->subtitle_stream()->id));
106                         } else {
107                                 _stream->SetSelection (wxNOT_FOUND);
108                         }
109                 }
110                 setup_control_sensitivity ();
111         } else if (property == SubtitleContentProperty::SUBTITLE_OFFSET) {
112                 checked_set (_offset, sc ? (sc->subtitle_offset() * 100) : 0);
113         } else if (property == SubtitleContentProperty::SUBTITLE_SCALE) {
114                 checked_set (_scale, sc ? (sc->subtitle_scale() * 100) : 100);
115         }
116
117 }
118
119 void
120 SubtitlePanel::with_subtitles_toggled (wxCommandEvent &)
121 {
122         if (!_editor->film()) {
123                 return;
124         }
125
126         _editor->film()->set_with_subtitles (_with_subtitles->GetValue ());
127 }
128
129 void
130 SubtitlePanel::setup_control_sensitivity ()
131 {
132         bool h = false;
133         if (_editor->generally_sensitive() && _editor->film()) {
134                 h = _editor->film()->has_subtitles ();
135         }
136         
137         _with_subtitles->Enable (h);
138
139         bool j = false;
140         if (_editor->film()) {
141                 j = _editor->film()->with_subtitles ();
142         }
143         
144         _offset->Enable (j);
145         _scale->Enable (j);
146         _stream->Enable (j);
147 }
148
149 void
150 SubtitlePanel::stream_changed (wxCommandEvent &)
151 {
152         shared_ptr<Content> c = _editor->selected_content ();
153         if (!c) {
154                 return;
155         }
156         
157         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
158         if (!fc) {
159                 return;
160         }
161         
162         vector<shared_ptr<FFmpegSubtitleStream> > a = fc->subtitle_streams ();
163         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
164         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
165         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
166                 ++i;
167         }
168
169         if (i != a.end ()) {
170                 fc->set_subtitle_stream (*i);
171         }
172 }
173
174 void
175 SubtitlePanel::offset_changed (wxCommandEvent &)
176 {
177         shared_ptr<SubtitleContent> c = _editor->selected_subtitle_content ();
178         if (!c) {
179                 return;
180         }
181
182         c->set_subtitle_offset (_offset->GetValue() / 100.0);
183 }
184
185 void
186 SubtitlePanel::scale_changed (wxCommandEvent &)
187 {
188         shared_ptr<SubtitleContent> c = _editor->selected_subtitle_content ();
189         if (!c) {
190                 return;
191         }
192
193         c->set_subtitle_scale (_scale->GetValue() / 100.0);
194 }
195