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/bind.hpp>
35 using std::shared_ptr;
37 using boost::optional;
38 #if BOOST_VERSION >= 106100
39 using namespace boost::placeholders;
41 using namespace dcpomatic;
43 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
44 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
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.
49 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
51 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
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)
61 _lines.resize (MAX_CLOSED_CAPTION_LINES);
63 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
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);
69 sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
70 sizer->Add (_display, 1, wxEXPAND);
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));
77 SetSizerAndFit (sizer);
81 ClosedCaptionsDialog::shown (wxShowEvent ev)
91 ClosedCaptionsDialog::track_selected ()
93 _current = optional<TextRingBuffers::Data> ();
94 _viewer->slow_refresh ();
99 ClosedCaptionsDialog::paint ()
101 wxPaintDC dc (_display);
102 dc.SetBackground (*wxBLACK_BRUSH);
104 dc.SetTextForeground (*wxWHITE);
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));
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);
125 class ClosedCaptionSorter
128 bool operator() (StringText const & a, StringText const & b)
130 return from_top(a) < from_top(b);
134 float from_top (StringText const & c) const
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();
144 DCPOMATIC_ASSERT (false);
150 ClosedCaptionsDialog::update ()
152 auto const time = _viewer->time ();
154 if (_current_in_lines && _current && _current->period.to > time) {
155 /* Current one is fine */
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) {
165 _current = optional<TextRingBuffers::Data>();
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()];
176 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
180 if (d->track == track) {
186 _current_in_lines = false;
190 if (_current && _current->period.contains(time)) {
191 /* We need to set this new one up */
193 auto to_show = _current->text.string;
195 for (int j = 0; j < MAX_CLOSED_CAPTION_LINES; ++j) {
199 to_show.sort (ClosedCaptionSorter());
201 auto j = to_show.begin();
203 while (j != to_show.end() && k < MAX_CLOSED_CAPTION_LINES) {
204 _lines[k] = std_to_wx (j->text());
210 _current_in_lines = true;
213 if (!_current && _tracks.empty()) {
214 for (int i = 0; i < MAX_CLOSED_CAPTION_LINES; ++i) {
215 _lines[i] = wxString();
221 ClosedCaptionsDialog::clear ()
223 _current = optional<TextRingBuffers::Data>();
224 _current_in_lines = false;
230 ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
236 ClosedCaptionsDialog::update_tracks (shared_ptr<const Film> film)
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());
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")))));
255 if (_track->GetCount() > 0) {
256 _track->SetSelection (0);