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)
57 _lines.resize (CLOSED_CAPTION_LINES);
59 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
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);
65 sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
66 sizer->Add (_display, 1, wxEXPAND);
68 _display->Bind (wxEVT_PAINT, boost::bind(&ClosedCaptionsDialog::paint, this));
69 _track->Bind (wxEVT_CHOICE, boost::bind(&ClosedCaptionsDialog::track_selected, this));
71 SetSizerAndFit (sizer);
75 ClosedCaptionsDialog::track_selected ()
77 _current = optional<TextRingBuffers::Data> ();
78 _viewer->slow_refresh ();
79 update (_last_update);
83 ClosedCaptionsDialog::paint ()
85 wxPaintDC dc (_display);
86 dc.SetBackground (*wxBLACK_BRUSH);
88 dc.SetTextForeground (*wxWHITE);
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));
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);
109 class ClosedCaptionSorter
112 bool operator() (StringText const & a, StringText const & b)
114 return from_top(a) < from_top(b);
118 float from_top (StringText const & c) const
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();
128 DCPOMATIC_ASSERT (false);
134 ClosedCaptionsDialog::update (DCPTime time)
138 if (_current_in_lines && _current && _current->period.to > time) {
139 /* Current one is fine */
143 if (_current && _current->period.to < time) {
144 /* Current one has finished; clear out */
145 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
149 _current = optional<TextRingBuffers::Data>();
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()];
160 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
164 if (d->track == track) {
170 _current_in_lines = false;
173 if (_current && _current->period.contains(time)) {
174 /* We need to set this new one up */
176 list<StringText> to_show = _current->text.string;
178 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
182 to_show.sort (ClosedCaptionSorter());
184 list<StringText>::const_iterator j = to_show.begin();
186 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
187 _lines[k] = j->text();
193 _current_in_lines = true;
198 ClosedCaptionsDialog::clear ()
200 _current = optional<TextRingBuffers::Data>();
201 _current_in_lines = false;
206 ClosedCaptionsDialog::set_film_and_butler (shared_ptr<Film> film, weak_ptr<Butler> butler)
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());
221 BOOST_FOREACH (DCPTextTrack const & i, _tracks) {
222 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language)));
225 if (_track->GetCount() > 0) {
226 _track->SetSelection (0);