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