Missing file.
[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         wxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
46         SetSizer (overall_sizer);
47
48         _table = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
49
50         overall_sizer->Add (_table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
51
52         int r = 0;
53
54         add_label_to_sizer (_table, this, _("Colour"), true, wxGBPosition (r, 0));
55         _colour = new wxColourPickerCtrl (this, wxID_ANY);
56         _table->Add (_colour, wxGBPosition (r, 1));
57         ++r;
58
59         add_label_to_sizer (_table, this, _("Effect"), true, wxGBPosition (r, 0));
60         _effect = new wxChoice (this, wxID_ANY);
61         _table->Add (_effect, wxGBPosition (r, 1));
62         ++r;
63
64         add_label_to_sizer (_table, this, _("Effect colour"), true, wxGBPosition (r, 0));
65         _effect_colour = new wxColourPickerCtrl (this, wxID_ANY);
66         _table->Add (_effect_colour, wxGBPosition (r, 1));
67         ++r;
68
69         add_label_to_sizer (_table, this, _("Outline width"), true, wxGBPosition (r, 0));
70         _outline_width = new wxSpinCtrl (this, wxID_ANY);
71         _table->Add (_outline_width, wxGBPosition (r, 1));
72         ++r;
73
74         add_label_to_sizer (_table, this, _("Fade in time"), true, wxGBPosition (r, 0));
75         _fade_in = new Timecode<ContentTime> (this);
76         _table->Add (_fade_in, wxGBPosition (r, 1));
77         ++r;
78
79         add_label_to_sizer (_table, this, _("Fade out time"), true, wxGBPosition (r, 0));
80         _fade_out = new Timecode<ContentTime> (this);
81         _table->Add (_fade_out, wxGBPosition (r, 1));
82         ++r;
83
84         wxScrolled<wxPanel>* colours_panel = new wxScrolled<wxPanel> (this);
85         colours_panel->EnableScrolling (false, true);
86         colours_panel->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_ALWAYS);
87         colours_panel->SetScrollRate (0, 16);
88
89         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
90         table->AddGrowableCol (1, 1);
91
92         map<RGBA, RGBA> colours = _stream->colours ();
93
94         wxStaticText* t = new wxStaticText (colours_panel, wxID_ANY, "");
95         t->SetLabelMarkup (_("<b>Original colour</b>"));
96         table->Add (t, 1, wxEXPAND);
97         t = new wxStaticText (colours_panel, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
98         t->SetLabelMarkup (_("<b>New colour</b>"));
99         table->Add (t, 1, wxALIGN_CENTER);
100
101         for (map<RGBA, RGBA>::const_iterator i = colours.begin(); i != colours.end(); ++i) {
102                 wxPanel* from = new wxPanel (colours_panel, wxID_ANY);
103                 from->SetBackgroundColour (wxColour (i->first.r, i->first.g, i->first.b, i->first.a));
104                 table->Add (from, 1, wxEXPAND);
105                 RGBAColourPicker* to = new RGBAColourPicker (colours_panel, i->second);
106                 table->Add (to, 1, wxEXPAND);
107                 _pickers[i->first] = to;
108         }
109
110         colours_panel->SetSizer (table);
111
112         overall_sizer->Add (colours_panel, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
113
114         wxButton* restore = new wxButton (this, wxID_ANY, _("Restore to original colours"));
115         restore->Bind (wxEVT_BUTTON, bind (&SubtitleAppearanceDialog::restore, this));
116         overall_sizer->Add (restore, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
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         _colour->SetColour (wxColour (_content->subtitle->colour().r, _content->subtitle->colour().g, _content->subtitle->colour().b));
132         if (_content->subtitle->outline()) {
133                 _effect->SetSelection (OUTLINE);
134         } else if (_content->subtitle->shadow()) {
135                 _effect->SetSelection (SHADOW);
136         } else {
137                 _effect->SetSelection (NONE);
138         }
139         _effect_colour->SetColour (
140                 wxColour (_content->subtitle->effect_colour().r, _content->subtitle->effect_colour().g, _content->subtitle->effect_colour().b)
141                 );
142         _fade_in->set (_content->subtitle->fade_in(), _content->active_video_frame_rate ());
143         _fade_out->set (_content->subtitle->fade_out(), _content->active_video_frame_rate ());
144         _outline_width->SetValue (_content->subtitle->outline_width ());
145
146         _effect->Bind (wxEVT_CHOICE, bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
147         _content_connection = _content->Changed.connect (bind (&SubtitleAppearanceDialog::setup_sensitivity, this));
148
149         setup_sensitivity ();
150 }
151
152 void
153 SubtitleAppearanceDialog::apply ()
154 {
155         wxColour const c = _colour->GetColour ();
156         _content->subtitle->set_colour (dcp::Colour (c.Red(), c.Green(), c.Blue()));
157         _content->subtitle->set_outline (_effect->GetSelection() == OUTLINE);
158         _content->subtitle->set_shadow (_effect->GetSelection() == SHADOW);
159         wxColour const ec = _effect_colour->GetColour ();
160         _content->subtitle->set_effect_colour (dcp::Colour (ec.Red(), ec.Green(), ec.Blue()));
161         _content->subtitle->set_fade_in (_fade_in->get (_content->active_video_frame_rate ()));
162         _content->subtitle->set_fade_out (_fade_out->get (_content->active_video_frame_rate ()));
163         _content->subtitle->set_outline_width (_outline_width->GetValue ());
164
165         for (map<RGBA, RGBAColourPicker*>::const_iterator i = _pickers.begin(); i != _pickers.end(); ++i) {
166                 _stream->set_colour (i->first, i->second->colour ());
167         }
168
169         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (_content);
170         if (fc) {
171                 fc->signal_subtitle_stream_changed ();
172         }
173 }
174
175 void
176 SubtitleAppearanceDialog::restore ()
177 {
178         for (map<RGBA, RGBAColourPicker*>::const_iterator i = _pickers.begin(); i != _pickers.end(); ++i) {
179                 i->second->set (i->first);
180         }
181 }
182
183 void
184 SubtitleAppearanceDialog::setup_sensitivity ()
185 {
186         _effect_colour->Enable (_effect->GetSelection() != NONE);
187
188         bool const can_outline_width = _effect->GetSelection() == OUTLINE && _content->subtitle->burn ();
189         _outline_width->Enable (can_outline_width);
190         if (can_outline_width) {
191                 _outline_width->UnsetToolTip ();
192         } else {
193                 _outline_width->SetToolTip (_("Outline width cannot be set unless you are burning in subtitles"));
194         }
195 }