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