Update subtitle / timing panels properly.
[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 "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, _("Subtitle Offset"), true);
45                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
46                 _offset = new wxSpinCtrl (this);
47                 s->Add (_offset);
48                 add_label_to_sizer (s, this, _("%"), false);
49                 grid->Add (s);
50         }
51
52         {
53                 add_label_to_sizer (grid, this, _("Subtitle Scale"), true);
54                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
55                 _scale = new wxSpinCtrl (this);
56                 s->Add (_scale);
57                 add_label_to_sizer (s, this, _("%"), false);
58                 grid->Add (s);
59         }
60
61         add_label_to_sizer (grid, this, _("Subtitle Stream"), true);
62         _stream = new wxChoice (this, wxID_ANY);
63         grid->Add (_stream, 1, wxEXPAND);
64         
65         _offset->SetRange (-100, 100);
66         _scale->SetRange (1, 1000);
67         _scale->SetValue (100);
68
69         _with_subtitles->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::with_subtitles_toggled, this));
70         _offset->Bind         (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::offset_changed, this));
71         _scale->Bind          (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::scale_changed, this));
72         _stream->Bind         (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
73 }
74
75 void
76 SubtitlePanel::film_changed (Film::Property property)
77 {
78         switch (property) {
79         case Film::CONTENT:
80                 setup_sensitivity ();
81                 break;
82         case Film::WITH_SUBTITLES:
83                 checked_set (_with_subtitles, _editor->film()->with_subtitles ());
84                 setup_sensitivity ();
85                 break;
86         default:
87                 break;
88         }
89 }
90
91 void
92 SubtitlePanel::film_content_changed (int property)
93 {
94         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
95         SubtitleContentList sc = _editor->selected_subtitle_content ();
96
97         shared_ptr<FFmpegContent> fcs;
98         if (fc.size() == 1) {
99                 fcs = fc.front ();
100         }
101
102         shared_ptr<SubtitleContent> scs;
103         if (sc.size() == 1) {
104                 scs = sc.front ();
105         }
106         
107         if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
108                 _stream->Clear ();
109                 if (fcs) {
110                         vector<shared_ptr<FFmpegSubtitleStream> > s = fcs->subtitle_streams ();
111                         for (vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = s.begin(); i != s.end(); ++i) {
112                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
113                         }
114                         
115                         if (fcs->subtitle_stream()) {
116                                 checked_set (_stream, lexical_cast<string> (fcs->subtitle_stream()->id));
117                         } else {
118                                 _stream->SetSelection (wxNOT_FOUND);
119                         }
120                 }
121                 setup_sensitivity ();
122         } else if (property == SubtitleContentProperty::SUBTITLE_OFFSET) {
123                 checked_set (_offset, scs ? (scs->subtitle_offset() * 100) : 0);
124         } else if (property == SubtitleContentProperty::SUBTITLE_SCALE) {
125                 checked_set (_scale, scs ? (scs->subtitle_scale() * 100) : 100);
126         }
127 }
128
129 void
130 SubtitlePanel::with_subtitles_toggled ()
131 {
132         if (!_editor->film()) {
133                 return;
134         }
135
136         _editor->film()->set_with_subtitles (_with_subtitles->GetValue ());
137 }
138
139 void
140 SubtitlePanel::setup_sensitivity ()
141 {
142         bool h = false;
143         bool j = false;
144         if (_editor->film()) {
145                 h = _editor->film()->has_subtitles ();
146                 j = _editor->film()->with_subtitles ();
147         }
148         
149         _with_subtitles->Enable (h);
150         _offset->Enable (j);
151         _scale->Enable (j);
152         _stream->Enable (j);
153 }
154
155 void
156 SubtitlePanel::stream_changed ()
157 {
158         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
159         if (fc.size() != 1) {
160                 return;
161         }
162
163         shared_ptr<FFmpegContent> fcs = fc.front ();
164         
165         vector<shared_ptr<FFmpegSubtitleStream> > a = fcs->subtitle_streams ();
166         vector<shared_ptr<FFmpegSubtitleStream> >::iterator i = a.begin ();
167         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
168         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
169                 ++i;
170         }
171
172         if (i != a.end ()) {
173                 fcs->set_subtitle_stream (*i);
174         }
175 }
176
177 void
178 SubtitlePanel::offset_changed ()
179 {
180         SubtitleContentList c = _editor->selected_subtitle_content ();
181         if (c.size() == 1) {
182                 c.front()->set_subtitle_offset (_offset->GetValue() / 100.0);
183         }
184 }
185
186 void
187 SubtitlePanel::scale_changed ()
188 {
189         SubtitleContentList c = _editor->selected_subtitle_content ();
190         if (c.size() == 1) {
191                 c.front()->set_subtitle_scale (_scale->GetValue() / 100.0);
192         }
193 }
194
195 void
196 SubtitlePanel::content_selection_changed ()
197 {
198         film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
199         film_content_changed (SubtitleContentProperty::SUBTITLE_OFFSET);
200         film_content_changed (SubtitleContentProperty::SUBTITLE_SCALE);
201 }