Dolby is apparently recreating all their certificates (as they will soon expire)...
[dcpomatic.git] / src / wx / closed_captions_dialog.cc
1 /*
2     Copyright (C) 2018-2019 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
22 #include "closed_captions_dialog.h"
23 #include "film_viewer.h"
24 #include "wx_util.h"
25 #include "lib/butler.h"
26 #include "lib/compose.hpp"
27 #include "lib/constants.h"
28 #include "lib/string_text.h"
29 #include "lib/text_content.h"
30 #include <boost/bind/bind.hpp>
31
32
33 using std::cout;
34 using std::list;
35 using std::make_pair;
36 using std::max;
37 using std::pair;
38 using std::shared_ptr;
39 using std::weak_ptr;
40 using boost::optional;
41 #if BOOST_VERSION >= 106100
42 using namespace boost::placeholders;
43 #endif
44 using namespace dcpomatic;
45
46
47 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
48         : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
49 #ifdef DCPOMATIC_OSX
50                     /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
51                        the window above all others (and not just our own) it's better than nothing for now.
52                     */
53                     wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
54 #else
55                     wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
56 #endif
57                 )
58         , _viewer (viewer)
59           /* XXX: empirical and probably unhelpful default size here; needs to be related to font metrics */
60         , _display (new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(640, (640 / 10) + 64)))
61         , _track (new wxChoice(this, wxID_ANY))
62         , _current_in_lines (false)
63         , _timer (this)
64 {
65         _lines.resize (MAX_CLOSED_CAPTION_LINES);
66
67         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
68
69         wxBoxSizer* track_sizer = new wxBoxSizer (wxHORIZONTAL);
70         add_label_to_sizer (track_sizer, this, _("Track"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
71         track_sizer->Add (_track, 0, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
72
73         sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
74         sizer->Add (_display, 1, wxEXPAND);
75
76         Bind (wxEVT_SHOW, boost::bind(&ClosedCaptionsDialog::shown, this, _1));
77         Bind (wxEVT_TIMER, boost::bind(&ClosedCaptionsDialog::update, this));
78         _display->Bind (wxEVT_PAINT, boost::bind(&ClosedCaptionsDialog::paint, this));
79         _track->Bind (wxEVT_CHOICE, boost::bind(&ClosedCaptionsDialog::track_selected, this));
80
81         SetSizerAndFit (sizer);
82 }
83
84 void
85 ClosedCaptionsDialog::shown (wxShowEvent ev)
86 {
87         if (ev.IsShown ()) {
88                 _timer.Start (40);
89         } else {
90                 _timer.Stop ();
91         }
92 }
93
94 void
95 ClosedCaptionsDialog::track_selected ()
96 {
97         _current = optional<TextRingBuffers::Data> ();
98         _viewer->slow_refresh ();
99         update ();
100 }
101
102 void
103 ClosedCaptionsDialog::paint ()
104 {
105         wxPaintDC dc (_display);
106         dc.SetBackground (*wxBLACK_BRUSH);
107         dc.Clear ();
108         dc.SetTextForeground (*wxWHITE);
109
110         /* Choose a font which fits vertically */
111         int const line_height = max (8, dc.GetSize().GetHeight() / MAX_CLOSED_CAPTION_LINES);
112         wxFont font (*wxNORMAL_FONT);
113         font.SetPixelSize (wxSize (0, line_height * 0.8));
114         dc.SetFont (font);
115
116         for (int i = 0; i < MAX_CLOSED_CAPTION_LINES; ++i) {
117                 wxString const good = _lines[i].Left (MAX_CLOSED_CAPTION_LENGTH);
118                 dc.DrawText (good, 8, line_height * i);
119                 if (_lines[i].Length() > MAX_CLOSED_CAPTION_LENGTH) {
120                         wxString const bad = _lines[i].Right (_lines[i].Length() - MAX_CLOSED_CAPTION_LENGTH);
121                         wxSize size = dc.GetTextExtent (good);
122                         dc.SetTextForeground (*wxRED);
123                         dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
124                         dc.SetTextForeground (*wxWHITE);
125                 }
126         }
127 }
128
129 class ClosedCaptionSorter
130 {
131 public:
132         bool operator() (StringText const & a, StringText const & b)
133         {
134                 return from_top(a) < from_top(b);
135         }
136
137 private:
138         float from_top (StringText const & c) const
139         {
140                 switch (c.v_align()) {
141                 case dcp::VAlign::TOP:
142                         return c.v_position();
143                 case dcp::VAlign::CENTER:
144                         return c.v_position() + 0.5;
145                 case dcp::VAlign::BOTTOM:
146                         return 1.0 - c.v_position();
147                 }
148                 DCPOMATIC_ASSERT (false);
149                 return 0;
150         }
151 };
152
153 void
154 ClosedCaptionsDialog::update ()
155 {
156         auto const time = _viewer->time ();
157
158         if (_current_in_lines && _current && _current->period.to > time) {
159                 /* Current one is fine */
160                 return;
161         }
162
163         if (_current && _current->period.to < time) {
164                 /* Current one has finished; clear out */
165                 for (int j = 0; j < MAX_CLOSED_CAPTION_LINES; ++j) {
166                         _lines[j] = "";
167                 }
168                 Refresh ();
169                 _current = optional<TextRingBuffers::Data>();
170         }
171
172         if (!_current && !_tracks.empty()) {
173                 /* We have no current one: get another */
174                 auto butler = _butler.lock ();
175                 DCPOMATIC_ASSERT (_track->GetSelection() >= 0);
176                 DCPOMATIC_ASSERT (_track->GetSelection() < int(_tracks.size()));
177                 auto track = _tracks[_track->GetSelection()];
178                 if (butler) {
179                         while (true) {
180                                 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
181                                 if (!d) {
182                                         break;
183                                 }
184                                 if (d->track == track) {
185                                         _current = d;
186                                         break;
187                                 }
188                         }
189
190                         _current_in_lines = false;
191                 }
192         }
193
194         if (_current && _current->period.contains(time)) {
195                 /* We need to set this new one up */
196
197                 auto to_show = _current->text.string;
198
199                 for (int j = 0; j < MAX_CLOSED_CAPTION_LINES; ++j) {
200                         _lines[j] = "";
201                 }
202
203                 to_show.sort (ClosedCaptionSorter());
204
205                 auto j = to_show.begin();
206                 int k = 0;
207                 while (j != to_show.end() && k < MAX_CLOSED_CAPTION_LINES) {
208                         _lines[k] = std_to_wx (j->text());
209                         ++j;
210                         ++k;
211                 }
212
213                 Refresh ();
214                 _current_in_lines = true;
215         }
216
217         if (!_current && _tracks.empty()) {
218                 for (int i = 0; i < MAX_CLOSED_CAPTION_LINES; ++i) {
219                         _lines[i] = wxString();
220                 }
221         }
222 }
223
224 void
225 ClosedCaptionsDialog::clear ()
226 {
227         _current = optional<TextRingBuffers::Data>();
228         _current_in_lines = false;
229         Refresh ();
230 }
231
232
233 void
234 ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
235 {
236         _butler = butler;
237 }
238
239 void
240 ClosedCaptionsDialog::update_tracks (shared_ptr<const Film> film)
241 {
242         _tracks.clear ();
243
244         for (auto i: film->content()) {
245                 for (auto j: i->text) {
246                         if (j->use() && j->type() == TextType::CLOSED_CAPTION && j->dcp_track()) {
247                                 if (find(_tracks.begin(), _tracks.end(), j->dcp_track()) == _tracks.end()) {
248                                         _tracks.push_back (*j->dcp_track());
249                                 }
250                         }
251                 }
252         }
253
254         _track->Clear ();
255         for (auto const& i: _tracks) {
256                 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language ? i.language->to_string() : wx_to_std(_("Unknown")))));
257         }
258
259         if (_track->GetCount() > 0) {
260                 _track->SetSelection (0);
261         }
262
263         track_selected ();
264 }