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