Add FilmViewer::time_until_next_frame.
[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 {
57         _lines.resize (CLOSED_CAPTION_LINES);
58
59         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
60
61         wxBoxSizer* track_sizer = new wxBoxSizer (wxHORIZONTAL);
62         add_label_to_sizer (track_sizer, this, _("Track"), true);
63         track_sizer->Add (_track, 0, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
64
65         sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
66         sizer->Add (_display, 1, wxEXPAND);
67
68         _display->Bind (wxEVT_PAINT, boost::bind(&ClosedCaptionsDialog::paint, this));
69         _track->Bind (wxEVT_CHOICE, boost::bind(&ClosedCaptionsDialog::track_selected, this));
70
71         SetSizerAndFit (sizer);
72 }
73
74 void
75 ClosedCaptionsDialog::track_selected ()
76 {
77         _current = optional<TextRingBuffers::Data> ();
78         _viewer->slow_refresh ();
79         update (_last_update);
80 }
81
82 void
83 ClosedCaptionsDialog::paint ()
84 {
85         wxPaintDC dc (_display);
86         dc.SetBackground (*wxBLACK_BRUSH);
87         dc.Clear ();
88         dc.SetTextForeground (*wxWHITE);
89
90         /* Choose a font which fits vertically */
91         int const line_height = max (8, dc.GetSize().GetHeight() / CLOSED_CAPTION_LINES);
92         wxFont font (*wxNORMAL_FONT);
93         font.SetPixelSize (wxSize (0, line_height * 0.8));
94         dc.SetFont (font);
95
96         for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
97                 wxString const good = _lines[i].Left (CLOSED_CAPTION_LENGTH);
98                 dc.DrawText (good, 8, line_height * i);
99                 if (_lines[i].Length() > CLOSED_CAPTION_LENGTH) {
100                         wxString const bad = _lines[i].Right (_lines[i].Length() - CLOSED_CAPTION_LENGTH);
101                         wxSize size = dc.GetTextExtent (good);
102                         dc.SetTextForeground (*wxRED);
103                         dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
104                         dc.SetTextForeground (*wxWHITE);
105                 }
106         }
107 }
108
109 class ClosedCaptionSorter
110 {
111 public:
112         bool operator() (StringText const & a, StringText const & b)
113         {
114                 return from_top(a) < from_top(b);
115         }
116
117 private:
118         float from_top (StringText const & c) const
119         {
120                 switch (c.v_align()) {
121                 case dcp::VALIGN_TOP:
122                         return c.v_position();
123                 case dcp::VALIGN_CENTER:
124                         return c.v_position() + 0.5;
125                 case dcp::VALIGN_BOTTOM:
126                         return 1.0 - c.v_position();
127                 }
128                 DCPOMATIC_ASSERT (false);
129                 return 0;
130         }
131 };
132
133 void
134 ClosedCaptionsDialog::update (DCPTime time)
135 {
136         _last_update = time;
137
138         if (_current_in_lines && _current && _current->period.to > time) {
139                 /* Current one is fine */
140                 return;
141         }
142
143         if (_current && _current->period.to < time) {
144                 /* Current one has finished; clear out */
145                 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
146                         _lines[j] = "";
147                 }
148                 Refresh ();
149                 _current = optional<TextRingBuffers::Data>();
150         }
151
152         if (!_current && !_tracks.empty()) {
153                 /* We have no current one: get another */
154                 shared_ptr<Butler> butler = _butler.lock ();
155                 DCPOMATIC_ASSERT (butler);
156                 DCPOMATIC_ASSERT (_track->GetSelection() >= 0);
157                 DCPOMATIC_ASSERT (_track->GetSelection() < int(_tracks.size()));
158                 DCPTextTrack track = _tracks[_track->GetSelection()];
159                 while (true) {
160                         optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
161                         if (!d) {
162                                 break;
163                         }
164                         if (d->track == track) {
165                                 _current = d;
166                                 break;
167                         }
168                 }
169
170                 _current_in_lines = false;
171         }
172
173         if (_current && _current->period.contains(time)) {
174                 /* We need to set this new one up */
175
176                 list<StringText> to_show = _current->text.string;
177
178                 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
179                         _lines[j] = "";
180                 }
181
182                 to_show.sort (ClosedCaptionSorter());
183
184                 list<StringText>::const_iterator j = to_show.begin();
185                 int k = 0;
186                 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
187                         _lines[k] = j->text();
188                         ++j;
189                         ++k;
190                 }
191
192                 Refresh ();
193                 _current_in_lines = true;
194         }
195 }
196
197 void
198 ClosedCaptionsDialog::clear ()
199 {
200         _current = optional<TextRingBuffers::Data>();
201         _current_in_lines = false;
202         Refresh ();
203 }
204
205 void
206 ClosedCaptionsDialog::set_film_and_butler (shared_ptr<Film> film, weak_ptr<Butler> butler)
207 {
208         _tracks.clear ();
209
210         BOOST_FOREACH (shared_ptr<Content> i, film->content()) {
211                 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
212                         if (j->use() && j->type() == TEXT_CLOSED_CAPTION && j->dcp_track()) {
213                                 if (find(_tracks.begin(), _tracks.end(), j->dcp_track()) == _tracks.end()) {
214                                         _tracks.push_back (*j->dcp_track());
215                                 }
216                         }
217                 }
218         }
219
220         _track->Clear ();
221         BOOST_FOREACH (DCPTextTrack const & i, _tracks) {
222                 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language)));
223         }
224
225         if (_track->GetCount() > 0) {
226                 _track->SetSelection (0);
227         }
228
229         _butler = butler;
230 }