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