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