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/>.
22 #include "closed_captions_dialog.h"
23 #include "film_viewer.h"
25 #include "lib/butler.h"
26 #include "lib/compose.hpp"
27 #include "lib/constants.h"
29 #include "lib/string_text.h"
30 #include "lib/text_content.h"
31 #include <boost/bind/bind.hpp>
39 using std::shared_ptr;
41 using boost::optional;
42 #if BOOST_VERSION >= 106100
43 using namespace boost::placeholders;
45 using namespace dcpomatic;
48 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
49 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
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.
54 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
56 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
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)
66 _lines.resize (MAX_CLOSED_CAPTION_LINES);
68 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
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);
74 sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
75 sizer->Add (_display, 1, wxEXPAND);
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));
82 SetSizerAndFit (sizer);
86 ClosedCaptionsDialog::shown (wxShowEvent ev)
96 ClosedCaptionsDialog::track_selected ()
98 _current = optional<TextRingBuffers::Data> ();
99 _viewer->slow_refresh ();
104 ClosedCaptionsDialog::paint ()
106 wxPaintDC dc (_display);
107 dc.SetBackground (*wxBLACK_BRUSH);
109 dc.SetTextForeground (*wxWHITE);
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));
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);
130 class ClosedCaptionSorter
133 bool operator() (StringText const & a, StringText const & b)
135 return from_top(a) < from_top(b);
139 float from_top (StringText const & c) const
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();
149 DCPOMATIC_ASSERT (false);
155 ClosedCaptionsDialog::update ()
157 auto const time = _viewer->time ();
159 if (_current_in_lines && _current && _current->period.to > time) {
160 /* Current one is fine */
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) {
170 _current = optional<TextRingBuffers::Data>();
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()];
181 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
185 if (d->track == track) {
191 _current_in_lines = false;
195 if (_current && _current->period.contains(time)) {
196 /* We need to set this new one up */
198 auto to_show = _current->text.string;
200 for (int j = 0; j < MAX_CLOSED_CAPTION_LINES; ++j) {
204 std::sort(to_show.begin(), to_show.end(), ClosedCaptionSorter());
206 auto j = to_show.begin();
208 while (j != to_show.end() && k < MAX_CLOSED_CAPTION_LINES) {
209 _lines[k] = std_to_wx (j->text());
215 _current_in_lines = true;
218 if (!_current && _tracks.empty()) {
219 for (int i = 0; i < MAX_CLOSED_CAPTION_LINES; ++i) {
220 _lines[i] = wxString();
226 ClosedCaptionsDialog::clear ()
228 _current = optional<TextRingBuffers::Data>();
229 _current_in_lines = false;
235 ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
241 ClosedCaptionsDialog::update_tracks (shared_ptr<const Film> film)
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());
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")))));
260 if (_track->GetCount() > 0) {
261 _track->SetSelection (0);