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