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