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