2 Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "closed_captions_dialog.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>
35 using boost::shared_ptr;
36 using boost::weak_ptr;
37 using boost::optional;
38 using namespace dcpomatic;
40 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
41 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
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.
46 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
48 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
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)
58 _lines.resize (CLOSED_CAPTION_LINES);
60 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
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);
66 sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
67 sizer->Add (_display, 1, wxEXPAND);
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));
74 SetSizerAndFit (sizer);
78 ClosedCaptionsDialog::shown (wxShowEvent ev)
88 ClosedCaptionsDialog::track_selected ()
90 _current = optional<TextRingBuffers::Data> ();
91 _viewer->slow_refresh ();
96 ClosedCaptionsDialog::paint ()
98 wxPaintDC dc (_display);
99 dc.SetBackground (*wxBLACK_BRUSH);
101 dc.SetTextForeground (*wxWHITE);
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));
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);
122 class ClosedCaptionSorter
125 bool operator() (StringText const & a, StringText const & b)
127 return from_top(a) < from_top(b);
131 float from_top (StringText const & c) const
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();
141 DCPOMATIC_ASSERT (false);
147 ClosedCaptionsDialog::update ()
149 DCPTime const time = _viewer->time ();
151 if (_current_in_lines && _current && _current->period.to > time) {
152 /* Current one is fine */
156 if (_current && _current->period.to < time) {
157 /* Current one has finished; clear out */
158 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
162 _current = optional<TextRingBuffers::Data>();
165 if (!_current && !_tracks.empty()) {
166 /* We have no current one: get another */
167 shared_ptr<Butler> butler = _butler.lock ();
168 DCPOMATIC_ASSERT (butler);
169 DCPOMATIC_ASSERT (_track->GetSelection() >= 0);
170 DCPOMATIC_ASSERT (_track->GetSelection() < int(_tracks.size()));
171 DCPTextTrack track = _tracks[_track->GetSelection()];
173 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
177 if (d->track == track) {
183 _current_in_lines = false;
186 if (_current && _current->period.contains(time)) {
187 /* We need to set this new one up */
189 list<StringText> to_show = _current->text.string;
191 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
195 to_show.sort (ClosedCaptionSorter());
197 list<StringText>::const_iterator j = to_show.begin();
199 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
200 _lines[k] = j->text();
206 _current_in_lines = true;
209 if (!_current && _tracks.empty()) {
210 for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
211 _lines[i] = wxString();
217 ClosedCaptionsDialog::clear ()
219 _current = optional<TextRingBuffers::Data>();
220 _current_in_lines = false;
226 ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
232 ClosedCaptionsDialog::update_tracks (shared_ptr<const Film> film)
236 BOOST_FOREACH (shared_ptr<Content> i, film->content()) {
237 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
238 if (j->use() && j->type() == TEXT_CLOSED_CAPTION && j->dcp_track()) {
239 if (find(_tracks.begin(), _tracks.end(), j->dcp_track()) == _tracks.end()) {
240 _tracks.push_back (*j->dcp_track());
247 BOOST_FOREACH (DCPTextTrack const & i, _tracks) {
248 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language)));
251 if (_track->GetCount() > 0) {
252 _track->SetSelection (0);