Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / wx / subtitle_appearance_dialog.cc
1 /*
2     Copyright (C) 2015-2017 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
37 int const SubtitleAppearanceDialog::NONE = 0;
38 int const SubtitleAppearanceDialog::OUTLINE = 1;
39 int const SubtitleAppearanceDialog::SHADOW = 2;
40
41 SubtitleAppearanceDialog::SubtitleAppearanceDialog (wxWindow* parent, shared_ptr<Content> content)
42         : wxDialog (parent, wxID_ANY, _("Subtitle appearance"))
43         , _content (content)
44 {
45         shared_ptr<FFmpegContent> ff = dynamic_pointer_cast<FFmpegContent> (content);
46         if (ff) {
47                 _stream = ff->subtitle_stream ();
48         }
49
50         wxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
51         SetSizer (overall_sizer);
52
53         _table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
54
55         overall_sizer->Add (_table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
56
57         int r = 0;
58
59         add_label_to_sizer (_table, this, _("Colour"), true, wxGBPosition (r, 0));
60         _colour = new wxColourPickerCtrl (this, wxID_ANY);
61         _table->Add (_colour, wxGBPosition (r, 1));
62         ++r;
63
64         add_label_to_sizer (_table, this, _("Effect"), true, wxGBPosition (r, 0));
65         _effect = new wxChoice (this, wxID_ANY);
66         _table->Add (_effect, wxGBPosition (r, 1));
67         ++r;
68
69         add_label_to_sizer (_table, this, _("Effect colour"), true, wxGBPosition (r, 0));
70         _effect_colour = new wxColourPickerCtrl (this, wxID_ANY);
71         _table->Add (_effect_colour, wxGBPosition (r, 1));
72         ++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         _fade_in = new Timecode<ContentTime> (this);
81         _table->Add (_fade_in, wxGBPosition (r, 1));
82         ++r;
83
84         add_label_to_sizer (_table, this, _("Fade out time"), true, wxGBPosition (r, 0));
85         _fade_out = new Timecode<ContentTime> (this);
86         _table->Add (_fade_out, wxGBPosition (r, 1));
87         ++r;
88
89         wxScrolled<wxPanel>* colours_panel = new wxScrolled<wxPanel> (this);
90         colours_panel->EnableScrolling (false, true);
91         colours_panel->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_ALWAYS);
92         colours_panel->SetScrollRate (0, 16);
93
94         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
95         table->AddGrowableCol (1, 1);
96
97         if (_stream) {
98                 map<RGBA, RGBA> colours = _stream->colours ();
99
100                 wxStaticText* t = new wxStaticText (colours_panel, wxID_ANY, "");
101                 t->SetLabelMarkup (_("<b>Original colour</b>"));
102                 table->Add (t, 1, wxEXPAND);
103                 t = new wxStaticText (colours_panel, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
104                 t->SetLabelMarkup (_("<b>New colour</b>"));
105                 table->Add (t, 1, wxALIGN_CENTER);
106
107                 for (map<RGBA, RGBA>::const_iterator i = colours.begin(); i != colours.end(); ++i) {
108                         wxPanel* from = new wxPanel (colours_panel, wxID_ANY);
109                         from->SetBackgroundColour (wxColour (i->first.r, i->first.g, i->first.b, i->first.a));
110                         table->Add (from, 1, wxEXPAND);
111                         RGBAColourPicker* to = new RGBAColourPicker (colours_panel, i->second);
112                         table->Add (to, 1, wxEXPAND);
113                         _pickers[i->first] = to;
114                 }
115
116                 colours_panel->SetSizer (table);
117
118                 overall_sizer->Add (colours_panel, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
119
120                 wxButton* restore = new wxButton (this, wxID_ANY, _("Restore to original colours"));
121                 restore->Bind (wxEVT_BUTTON, bind (&SubtitleAppearanceDialog::restore, this));
122                 overall_sizer->Add (restore, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
123         }
124
125         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
126         if (buttons) {
127                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
128         }
129
130         overall_sizer->Layout ();
131         overall_sizer->SetSizeHints (this);
132
133         /* Keep these Appends() up to date with NONE/OUTLINE/SHADOW variables */
134         _effect->Append (_("None"));
135         _effect->Append (_("Outline"));
136         _effect->Append (_("Shadow"));;
137
138         _colour->SetColour (wxColour (_content->subtitle->colour().r, _content->subtitle->colour().g, _content->subtitle->colour().b));
139         if (_content->subtitle->outline()) {
140                 _effect->SetSelection (OUTLINE);
141         } else if (_content->subtitle->shadow()) {
142                 _effect->SetSelection (SHADOW);
143         } else {
144                 _effect->SetSelection (NONE);
145         }
146         _effect_colour->SetColour (
147                 wxColour (_content->subtitle->effect_colour().r, _content->subtitle->effect_colour().g, _content->subtitle->effect_colour().b)
148                 );
149         _fade_in->set (_content->subtitle->fade_in(), _content->active_video_frame_rate ());
150         _fade_out->set (_content->subtitle->fade_out(), _content->active_video_frame_rate ());
151         _outline_width->SetValue (_content->subtitle->outline_width ());
152
153         _effect->Bind (wxEVT_CHOICE, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
154         _content_connection = _content->Changed.connect (bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
155
156         setup_sensitivity ();
157 }
158
159 void
160 SubtitleAppearanceDialog::apply ()
161 {
162         wxColour const c = _colour->GetColour ();
163         _content->subtitle->set_colour (dcp::Colour (c.Red(), c.Green(), c.Blue()));
164         _content->subtitle->set_outline (_effect->GetSelection() == OUTLINE);
165         _content->subtitle->set_shadow (_effect->GetSelection() == SHADOW);
166         wxColour const ec = _effect_colour->GetColour ();
167         _content->subtitle->set_effect_colour (dcp::Colour (ec.Red(), ec.Green(), ec.Blue()));
168         _content->subtitle->set_fade_in (_fade_in->get (_content->active_video_frame_rate ()));
169         _content->subtitle->set_fade_out (_fade_out->get (_content->active_video_frame_rate ()));
170         _content->subtitle->set_outline_width (_outline_width->GetValue ());
171
172         if (_stream) {
173                 for (map<RGBA, RGBAColourPicker*>::const_iterator i = _pickers.begin(); i != _pickers.end(); ++i) {
174                         _stream->set_colour (i->first, i->second->colour ());
175                 }
176         }
177
178         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (_content);
179         if (fc) {
180                 fc->signal_subtitle_stream_changed ();
181         }
182 }
183
184 void
185 SubtitleAppearanceDialog::restore ()
186 {
187         for (map<RGBA, RGBAColourPicker*>::const_iterator i = _pickers.begin(); i != _pickers.end(); ++i) {
188                 i->second->set (i->first);
189         }
190 }
191
192 void
193 SubtitleAppearanceDialog::setup_sensitivity ()
194 {
195         _effect_colour->Enable (_effect->GetSelection() != NONE);
196
197         bool const can_outline_width = _effect->GetSelection() == OUTLINE && _content->subtitle->burn ();
198         _outline_width->Enable (can_outline_width);
199         if (can_outline_width) {
200                 _outline_width->UnsetToolTip ();
201         } else {
202                 _outline_width->SetToolTip (_("Outline width cannot be set unless you are burning in subtitles"));
203         }
204 }