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