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