2 Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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
35 #include <wx/clrpicker.h>
36 #include <wx/spinctrl.h>
37 #include <wx/gbsizer.h>
38 DCPOMATIC_ENABLE_WARNINGS
42 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::optional;
46 using namespace dcpomatic;
48 int const SubtitleAppearanceDialog::NONE = 0;
49 int const SubtitleAppearanceDialog::OUTLINE = 1;
50 int const SubtitleAppearanceDialog::SHADOW = 2;
52 SubtitleAppearanceDialog::SubtitleAppearanceDialog (wxWindow* parent, shared_ptr<const Film> film, shared_ptr<Content> content, shared_ptr<TextContent> caption)
53 : wxDialog (parent, wxID_ANY, _("Caption appearance"))
59 shared_ptr<FFmpegContent> ff = dynamic_pointer_cast<FFmpegContent> (content);
61 _stream = ff->subtitle_stream ();
62 /* XXX: assuming that all FFmpeg streams have bitmap subs */
63 if (_stream->colours().empty()) {
64 _job_manager_connection = JobManager::instance()->ActiveJobsChanged.connect(boost::bind(&SubtitleAppearanceDialog::active_jobs_changed, this, _1));
65 _job = JobManager::instance()->add(shared_ptr<Job>(new ExamineFFmpegSubtitlesJob(film, ff)));
69 _overall_sizer = new wxBoxSizer (wxVERTICAL);
70 SetSizer (_overall_sizer);
72 _table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
74 _overall_sizer->Add (_table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
78 add_label_to_sizer (_table, this, _("Colour"), true, wxGBPosition (r, 0));
79 _force_colour = set_to (_colour = new wxColourPickerCtrl (this, wxID_ANY), r);
81 add_label_to_sizer (_table, this, _("Effect"), true, wxGBPosition (r, 0));
82 _force_effect = set_to (_effect = new wxChoice (this, wxID_ANY), r);
84 add_label_to_sizer (_table, this, _("Effect colour"), true, wxGBPosition (r, 0));
85 _force_effect_colour = set_to (_effect_colour = new wxColourPickerCtrl (this, wxID_ANY), r);
87 add_label_to_sizer (_table, this, _("Outline width"), true, wxGBPosition (r, 0));
88 _outline_width = new wxSpinCtrl (this, wxID_ANY);
89 _table->Add (_outline_width, wxGBPosition (r, 1));
92 add_label_to_sizer (_table, this, _("Fade in time"), true, wxGBPosition (r, 0));
93 _force_fade_in = set_to (_fade_in = new Timecode<ContentTime> (this), r);
95 add_label_to_sizer (_table, this, _("Fade out time"), true, wxGBPosition (r, 0));
96 _force_fade_out = set_to (_fade_out = new Timecode<ContentTime> (this), r);
99 _colours_panel = new wxScrolled<wxPanel> (this);
100 _colours_panel->EnableScrolling (false, true);
101 _colours_panel->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_ALWAYS);
102 _colours_panel->SetScrollRate (0, 16);
104 _colour_table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
105 _colour_table->AddGrowableCol (1, 1);
107 wxStaticText* t = new StaticText (_colours_panel, "");
108 t->SetLabelMarkup (_("<b>Original colour</b>"));
109 _colour_table->Add (t, 1, wxEXPAND);
110 t = new StaticText (_colours_panel, "", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
111 t->SetLabelMarkup (_("<b>New colour</b>"));
112 _colour_table->Add (t, 1, wxALIGN_CENTER);
114 shared_ptr<Job> job = _job.lock ();
115 if (!job || job->finished()) {
119 _colours_panel->SetSizer (_colour_table);
121 /* XXX: still assuming that all FFmpeg streams have bitmap subs */
122 if (_stream->colours().empty()) {
123 _finding = new wxStaticText(this, wxID_ANY, _("Finding the colours in these subtitles..."));
124 _overall_sizer->Add (_finding, 0, wxALL, DCPOMATIC_DIALOG_BORDER);
125 _colours_panel->Show (false);
128 _overall_sizer->Add (_colours_panel, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
130 wxButton* restore = new Button (this, _("Restore to original colours"));
131 restore->Bind (wxEVT_BUTTON, bind (&SubtitleAppearanceDialog::restore, this));
132 _overall_sizer->Add (restore, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
135 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
137 _overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
140 _overall_sizer->Layout ();
141 _overall_sizer->SetSizeHints (this);
143 /* Keep these Appends() up to date with NONE/OUTLINE/SHADOW variables */
144 _effect->Append (_("None"));
145 _effect->Append (_("Outline"));
146 _effect->Append (_("Shadow"));;
148 optional<dcp::Colour> colour = _caption->colour();
149 _force_colour->SetValue (static_cast<bool>(colour));
151 _colour->SetColour (wxColour (colour->r, colour->g, colour->b));
153 _colour->SetColour (wxColour (255, 255, 255));
156 optional<dcp::Effect> effect = _caption->effect();
157 _force_effect->SetValue (static_cast<bool>(effect));
161 _effect->SetSelection (NONE);
164 _effect->SetSelection (OUTLINE);
167 _effect->SetSelection (SHADOW);
171 _effect->SetSelection (NONE);
174 optional<dcp::Colour> effect_colour = _caption->effect_colour();
175 _force_effect_colour->SetValue (static_cast<bool>(effect_colour));
177 _effect_colour->SetColour (wxColour (effect_colour->r, effect_colour->g, effect_colour->b));
179 _effect_colour->SetColour (wxColour (0, 0, 0));
182 optional<ContentTime> fade_in = _caption->fade_in();
183 _force_fade_in->SetValue (static_cast<bool>(fade_in));
185 _fade_in->set (*fade_in, _content->active_video_frame_rate(film));
187 _fade_in->set (ContentTime(), _content->active_video_frame_rate(film));
190 optional<ContentTime> fade_out = _caption->fade_out();
191 _force_fade_out->SetValue (static_cast<bool>(fade_out));
193 _fade_out->set (*fade_out, _content->active_video_frame_rate(film));
195 _fade_out->set (ContentTime(), _content->active_video_frame_rate(film));
198 _outline_width->SetValue (_caption->outline_width ());
200 _force_colour->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
201 _force_effect_colour->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
202 _force_effect->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
203 _force_fade_in->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
204 _force_fade_out->Bind (wxEVT_CHECKBOX, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
205 _effect->Bind (wxEVT_CHOICE, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
206 _content_connection = _content->Change.connect (bind (&SubtitleAppearanceDialog::content_change, this, _1));
208 setup_sensitivity ();
212 SubtitleAppearanceDialog::content_change (ChangeType type)
214 if (type == CHANGE_TYPE_DONE) {
215 setup_sensitivity ();
220 SubtitleAppearanceDialog::set_to (wxWindow* w, int& r)
222 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
223 wxCheckBox* set_to = new CheckBox (this, _("Set to"));
224 s->Add (set_to, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 8);
225 s->Add (w, 0, wxALIGN_CENTER_VERTICAL);
226 _table->Add (s, wxGBPosition (r, 1));
232 SubtitleAppearanceDialog::apply ()
234 shared_ptr<const Film> film = _film.lock ();
236 if (_force_colour->GetValue ()) {
237 wxColour const c = _colour->GetColour ();
238 _caption->set_colour (dcp::Colour (c.Red(), c.Green(), c.Blue()));
240 _caption->unset_colour ();
242 if (_force_effect->GetValue()) {
243 switch (_effect->GetSelection()) {
245 _caption->set_effect (dcp::NONE);
248 _caption->set_effect (dcp::BORDER);
251 _caption->set_effect (dcp::SHADOW);
255 _caption->unset_effect ();
257 if (_force_effect_colour->GetValue ()) {
258 wxColour const ec = _effect_colour->GetColour ();
259 _caption->set_effect_colour (dcp::Colour (ec.Red(), ec.Green(), ec.Blue()));
261 _caption->unset_effect_colour ();
263 if (_force_fade_in->GetValue ()) {
264 _caption->set_fade_in (_fade_in->get(_content->active_video_frame_rate(film)));
266 _caption->unset_fade_in ();
268 if (_force_fade_out->GetValue ()) {
269 _caption->set_fade_out (_fade_out->get(_content->active_video_frame_rate(film)));
271 _caption->unset_fade_out ();
273 _caption->set_outline_width (_outline_width->GetValue ());
276 for (map<RGBA, RGBAColourPicker*>::const_iterator i = _pickers.begin(); i != _pickers.end(); ++i) {
277 _stream->set_colour (i->first, i->second->colour ());
281 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (_content);
283 fc->signal_subtitle_stream_changed ();
288 SubtitleAppearanceDialog::restore ()
290 for (map<RGBA, RGBAColourPicker*>::const_iterator i = _pickers.begin(); i != _pickers.end(); ++i) {
291 i->second->set (i->first);
296 SubtitleAppearanceDialog::setup_sensitivity ()
298 _colour->Enable (_force_colour->GetValue ());
299 _effect_colour->Enable (_force_effect_colour->GetValue ());
300 _effect->Enable (_force_effect->GetValue ());
301 _fade_in->Enable (_force_fade_in->GetValue ());
302 _fade_out->Enable (_force_fade_out->GetValue ());
304 bool const can_outline_width = _effect->GetSelection() == OUTLINE && _caption->burn ();
305 _outline_width->Enable (can_outline_width);
306 if (can_outline_width) {
307 _outline_width->UnsetToolTip ();
309 _outline_width->SetToolTip (_("Outline width cannot be set unless you are burning in captions"));
314 SubtitleAppearanceDialog::active_jobs_changed (optional<string> last)
316 if (last && *last == "examine_subtitles") {
317 _colours_panel->Show (true);
319 _finding->Show (false);
322 _overall_sizer->Layout ();
323 _overall_sizer->SetSizeHints (this);
328 SubtitleAppearanceDialog::add_colours ()
330 map<RGBA, RGBA> colours = _stream->colours ();
331 for (map<RGBA, RGBA>::const_iterator i = colours.begin(); i != colours.end(); ++i) {
332 wxPanel* from = new wxPanel (_colours_panel, wxID_ANY);
333 from->SetBackgroundColour (wxColour (i->first.r, i->first.g, i->first.b, i->first.a));
334 _colour_table->Add (from, 1, wxEXPAND);
335 RGBAColourPicker* to = new RGBAColourPicker (_colours_panel, i->second);
336 _colour_table->Add (to, 1, wxEXPAND);
337 _pickers[i->first] = to;