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