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