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