ef3f20e7a8ab106c663b96726a8835041da7cd02
[dcpomatic.git] / src / wx / subtitle_appearance_dialog.cc
1 /*
2     Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "subtitle_appearance_dialog.h"
22 #include "rgba_colour_picker.h"
23 #include "static_text.h"
24 #include "check_box.h"
25 #include "dcpomatic_button.h"
26 #include "lib/string_text_file_content.h"
27 #include "lib/text_content.h"
28 #include "lib/ffmpeg_subtitle_stream.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/examine_ffmpeg_subtitles_job.h"
31 #include "lib/job_manager.h"
32 #include "lib/warnings.h"
33 DCPOMATIC_DISABLE_WARNINGS
34 #include <wx/wx.h>
35 #include <wx/clrpicker.h>
36 #include <wx/spinctrl.h>
37 #include <wx/gbsizer.h>
38 DCPOMATIC_ENABLE_WARNINGS
39
40 using std::map;
41 using std::string;
42 using std::shared_ptr;
43 using boost::bind;
44 using std::dynamic_pointer_cast;
45 using boost::optional;
46 using namespace dcpomatic;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
49 #endif
50
51 int const SubtitleAppearanceDialog::NONE = 0;
52 int const SubtitleAppearanceDialog::OUTLINE = 1;
53 int const SubtitleAppearanceDialog::SHADOW = 2;
54
55 SubtitleAppearanceDialog::SubtitleAppearanceDialog (wxWindow* parent, shared_ptr<const Film> film, shared_ptr<Content> content, shared_ptr<TextContent> caption)
56         : wxDialog (parent, wxID_ANY, _("Caption appearance"))
57         , _film (film)
58         , _finding (0)
59         , _content (content)
60         , _caption (caption)
61 {
62         auto ff = dynamic_pointer_cast<FFmpegContent> (content);
63         if (ff) {
64                 _stream = ff->subtitle_stream ();
65                 /* XXX: assuming that all FFmpeg streams have bitmap subs */
66                 if (_stream->colours().empty()) {
67                         _job_manager_connection = JobManager::instance()->ActiveJobsChanged.connect(boost::bind(&SubtitleAppearanceDialog::active_jobs_changed, this, _1));
68                         _job = JobManager::instance()->add(shared_ptr<Job>(new ExamineFFmpegSubtitlesJob(film, ff)));
69                 }
70         }
71
72         _overall_sizer = new wxBoxSizer (wxVERTICAL);
73         SetSizer (_overall_sizer);
74
75         _table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
76
77         _overall_sizer->Add (_table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
78
79         int r = 0;
80
81         add_label_to_sizer (_table, this, _("Colour"), true, wxGBPosition (r, 0));
82         _force_colour = set_to (_colour = new wxColourPickerCtrl (this, wxID_ANY), r);
83
84         add_label_to_sizer (_table, this, _("Effect"), true, wxGBPosition (r, 0));
85         _force_effect = set_to (_effect = new wxChoice (this, wxID_ANY), r);
86
87         add_label_to_sizer (_table, this, _("Effect colour"), true, wxGBPosition (r, 0));
88         _force_effect_colour = set_to (_effect_colour = new wxColourPickerCtrl (this, wxID_ANY), r);
89
90         add_label_to_sizer (_table, this, _("Outline width"), true, wxGBPosition (r, 0));
91         _outline_width = new wxSpinCtrl (this, wxID_ANY);
92         _table->Add (_outline_width, wxGBPosition (r, 1));
93         ++r;
94
95         add_label_to_sizer (_table, this, _("Fade in time"), true, wxGBPosition (r, 0));
96         _force_fade_in = set_to (_fade_in = new Timecode<ContentTime> (this), r);
97
98         add_label_to_sizer (_table, this, _("Fade out time"), true, wxGBPosition (r, 0));
99         _force_fade_out = set_to (_fade_out = new Timecode<ContentTime> (this), r);
100
101         if (_stream) {
102                 _colours_panel = new wxScrolled<wxPanel> (this);
103                 _colours_panel->EnableScrolling (false, true);
104                 _colours_panel->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_ALWAYS);
105                 _colours_panel->SetScrollRate (0, 16);
106
107                 _colour_table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
108                 _colour_table->AddGrowableCol (1, 1);
109
110                 wxStaticText* t = new StaticText (_colours_panel, "");
111                 t->SetLabelMarkup (_("<b>Original colour</b>"));
112                 _colour_table->Add (t, 1, wxEXPAND);
113                 t = new StaticText (_colours_panel, "", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
114                 t->SetLabelMarkup (_("<b>New colour</b>"));
115                 _colour_table->Add (t, 1, wxALIGN_CENTER);
116
117                 shared_ptr<Job> job = _job.lock ();
118                 if (!job || job->finished()) {
119                         add_colours ();
120                 }
121
122                 _colours_panel->SetSizer (_colour_table);
123
124                 /* XXX: still assuming that all FFmpeg streams have bitmap subs */
125                 if (_stream->colours().empty()) {
126                         _finding = new wxStaticText(this, wxID_ANY, _("Finding the colours in these subtitles..."));
127                         _overall_sizer->Add (_finding, 0, wxALL, DCPOMATIC_DIALOG_BORDER);
128                         _colours_panel->Show (false);
129                 }
130
131                 _overall_sizer->Add (_colours_panel, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
132
133                 wxButton* restore = new Button (this, _("Restore to original colours"));
134                 restore->Bind (wxEVT_BUTTON, bind (&SubtitleAppearanceDialog::restore, this));
135                 _overall_sizer->Add (restore, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
136         }
137
138         auto buttons = CreateSeparatedButtonSizer (wxOK);
139         if (buttons) {
140                 _overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
141         }
142
143         _overall_sizer->Layout ();
144         _overall_sizer->SetSizeHints (this);
145
146         /* Keep these Appends() up to date with NONE/OUTLINE/SHADOW variables */
147         _effect->Append (_("None"));
148         _effect->Append (_("Outline"));
149         _effect->Append (_("Shadow"));;
150
151         optional<dcp::Colour> colour = _caption->colour();
152         _force_colour->SetValue (static_cast<bool>(colour));
153         if (colour) {
154                 _colour->SetColour (wxColour (colour->r, colour->g, colour->b));
155         } else {
156                 _colour->SetColour (wxColour (255, 255, 255));
157         }
158
159         optional<dcp::Effect> effect = _caption->effect();
160         _force_effect->SetValue (static_cast<bool>(effect));
161         if (effect) {
162                 switch (*effect) {
163                 case dcp::Effect::NONE:
164                         _effect->SetSelection (NONE);
165                         break;
166                 case dcp::Effect::BORDER:
167                         _effect->SetSelection (OUTLINE);
168                         break;
169                 case dcp::Effect::SHADOW:
170                         _effect->SetSelection (SHADOW);
171                         break;
172                 }
173         } else {
174                 _effect->SetSelection (NONE);
175         }
176
177         auto effect_colour = _caption->effect_colour();
178         _force_effect_colour->SetValue (static_cast<bool>(effect_colour));
179         if (effect_colour) {
180                 _effect_colour->SetColour (wxColour (effect_colour->r, effect_colour->g, effect_colour->b));
181         } else {
182                 _effect_colour->SetColour (wxColour (0, 0, 0));
183         }
184
185         auto fade_in = _caption->fade_in();
186         _force_fade_in->SetValue (static_cast<bool>(fade_in));
187         if (fade_in) {
188                 _fade_in->set (*fade_in, _content->active_video_frame_rate(film));
189         } else {
190                 _fade_in->set (ContentTime(), _content->active_video_frame_rate(film));
191         }
192
193         auto fade_out = _caption->fade_out();
194         _force_fade_out->SetValue (static_cast<bool>(fade_out));
195         if (fade_out) {
196                 _fade_out->set (*fade_out, _content->active_video_frame_rate(film));
197         } else {
198                 _fade_out->set (ContentTime(), _content->active_video_frame_rate(film));
199         }
200
201         _outline_width->SetValue (_caption->outline_width ());
202
203         _force_colour->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
204         _force_effect_colour->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
205         _force_effect->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
206         _force_fade_in->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
207         _force_fade_out->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
208         _effect->Bind (wxEVT_CHOICE, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
209         _content_connection = _content->Change.connect (bind (&SubtitleAppearanceDialog::content_change, this, _1));
210
211         setup_sensitivity ();
212 }
213
214 void
215 SubtitleAppearanceDialog::content_change (ChangeType type)
216 {
217         if (type == ChangeType::DONE) {
218                 setup_sensitivity ();
219         }
220 }
221
222 wxCheckBox*
223 SubtitleAppearanceDialog::set_to (wxWindow* w, int& r)
224 {
225         auto s = new wxBoxSizer (wxHORIZONTAL);
226         auto set_to = new CheckBox (this, _("Set to"));
227         s->Add (set_to, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 8);
228         s->Add (w, 0, wxALIGN_CENTER_VERTICAL);
229         _table->Add (s, wxGBPosition (r, 1));
230         ++r;
231         return set_to;
232 }
233
234 void
235 SubtitleAppearanceDialog::apply ()
236 {
237         auto film = _film.lock ();
238
239         if (_force_colour->GetValue ()) {
240                 auto const c = _colour->GetColour ();
241                 _caption->set_colour (dcp::Colour (c.Red(), c.Green(), c.Blue()));
242         } else {
243                 _caption->unset_colour ();
244         }
245         if (_force_effect->GetValue()) {
246                 switch (_effect->GetSelection()) {
247                 case NONE:
248                         _caption->set_effect (dcp::Effect::NONE);
249                         break;
250                 case OUTLINE:
251                         _caption->set_effect (dcp::Effect::BORDER);
252                         break;
253                 case SHADOW:
254                         _caption->set_effect (dcp::Effect::SHADOW);
255                         break;
256                 }
257         } else {
258                 _caption->unset_effect ();
259         }
260         if (_force_effect_colour->GetValue ()) {
261                 auto const ec = _effect_colour->GetColour ();
262                 _caption->set_effect_colour (dcp::Colour (ec.Red(), ec.Green(), ec.Blue()));
263         } else {
264                 _caption->unset_effect_colour ();
265         }
266         if (_force_fade_in->GetValue ()) {
267                 _caption->set_fade_in (_fade_in->get(_content->active_video_frame_rate(film)));
268         } else {
269                 _caption->unset_fade_in ();
270         }
271         if (_force_fade_out->GetValue ()) {
272                 _caption->set_fade_out (_fade_out->get(_content->active_video_frame_rate(film)));
273         } else {
274                 _caption->unset_fade_out ();
275         }
276         _caption->set_outline_width (_outline_width->GetValue ());
277
278         if (_stream) {
279                 for (auto const& i: _pickers) {
280                         _stream->set_colour (i.first, i.second->colour());
281                 }
282         }
283
284         auto fc = dynamic_pointer_cast<FFmpegContent> (_content);
285         if (fc) {
286                 fc->signal_subtitle_stream_changed ();
287         }
288 }
289
290 void
291 SubtitleAppearanceDialog::restore ()
292 {
293         for (auto const& i: _pickers) {
294                 i.second->set (i.first);
295         }
296 }
297
298 void
299 SubtitleAppearanceDialog::setup_sensitivity ()
300 {
301         _colour->Enable (_force_colour->GetValue ());
302         _effect_colour->Enable (_force_effect_colour->GetValue ());
303         _effect->Enable (_force_effect->GetValue ());
304         _fade_in->Enable (_force_fade_in->GetValue ());
305         _fade_out->Enable (_force_fade_out->GetValue ());
306
307         bool const can_outline_width = _effect->GetSelection() == OUTLINE && _caption->burn ();
308         _outline_width->Enable (can_outline_width);
309         if (can_outline_width) {
310                 _outline_width->UnsetToolTip ();
311         } else {
312                 _outline_width->SetToolTip (_("Outline width cannot be set unless you are burning in captions"));
313         }
314 }
315
316 void
317 SubtitleAppearanceDialog::active_jobs_changed (optional<string> last)
318 {
319         if (last && *last == "examine_subtitles") {
320                 _colours_panel->Show (true);
321                 if (_finding) {
322                         _finding->Show (false);
323                 }
324                 add_colours ();
325                 _overall_sizer->Layout ();
326                 _overall_sizer->SetSizeHints (this);
327         }
328 }
329
330 void
331 SubtitleAppearanceDialog::add_colours ()
332 {
333         auto colours = _stream->colours ();
334         for (auto const& i: _stream->colours()) {
335                 auto from = new wxPanel(_colours_panel, wxID_ANY);
336                 from->SetBackgroundColour(wxColour(i.first.r, i.first.g, i.first.b, i.first.a));
337                 _colour_table->Add (from, 1, wxEXPAND);
338                 auto to = new RGBAColourPicker(_colours_panel, i.second);
339                 _colour_table->Add (to, 1, wxEXPAND);
340                 _pickers[i.first] = to;
341         }
342 }