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;
39 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
40 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
42 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
43 the window above all others (and not just our own) it's better than nothing for now.
45 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
47 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
51 /* XXX: empirical and probably unhelpful default size here; needs to be related to font metrics */
52 , _display (new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(640, (640 / 10) + 64)))
53 , _track (new wxChoice(this, wxID_ANY))
54 , _current_in_lines (false)
56 _lines.resize (CLOSED_CAPTION_LINES);
58 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
60 wxBoxSizer* track_sizer = new wxBoxSizer (wxHORIZONTAL);
61 add_label_to_sizer (track_sizer, this, _("Track"), true);
62 track_sizer->Add (_track, 0, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
64 sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
65 sizer->Add (_display, 1, wxEXPAND);
67 _display->Bind (wxEVT_PAINT, boost::bind(&ClosedCaptionsDialog::paint, this));
68 _track->Bind (wxEVT_CHOICE, boost::bind(&ClosedCaptionsDialog::track_selected, this));
70 SetSizerAndFit (sizer);
74 ClosedCaptionsDialog::track_selected ()
76 _current = optional<TextRingBuffers::Data> ();
77 _viewer->slow_refresh ();
78 update (_last_update);
82 ClosedCaptionsDialog::paint ()
84 wxPaintDC dc (_display);
85 dc.SetBackground (*wxBLACK_BRUSH);
87 dc.SetTextForeground (*wxWHITE);
89 /* Choose a font which fits vertically */
90 int const line_height = max (8, dc.GetSize().GetHeight() / CLOSED_CAPTION_LINES);
91 wxFont font (*wxNORMAL_FONT);
92 font.SetPixelSize (wxSize (0, line_height * 0.8));
95 for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
96 wxString const good = _lines[i].Left (CLOSED_CAPTION_LENGTH);
97 dc.DrawText (good, 8, line_height * i);
98 if (_lines[i].Length() > CLOSED_CAPTION_LENGTH) {
99 wxString const bad = _lines[i].Right (_lines[i].Length() - CLOSED_CAPTION_LENGTH);
100 wxSize size = dc.GetTextExtent (good);
101 dc.SetTextForeground (*wxRED);
102 dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
103 dc.SetTextForeground (*wxWHITE);
108 class ClosedCaptionSorter
111 bool operator() (StringText const & a, StringText const & b)
113 return from_top(a) < from_top(b);
117 float from_top (StringText const & c) const
119 switch (c.v_align()) {
120 case dcp::VALIGN_TOP:
121 return c.v_position();
122 case dcp::VALIGN_CENTER:
123 return c.v_position() + 0.5;
124 case dcp::VALIGN_BOTTOM:
125 return 1.0 - c.v_position();
127 DCPOMATIC_ASSERT (false);
133 ClosedCaptionsDialog::update (DCPTime time)
137 if (_current_in_lines && _current && _current->period.to > time) {
138 /* Current one is fine */
142 if (_current && _current->period.to < time) {
143 /* Current one has finished; clear out */
144 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
148 _current = optional<TextRingBuffers::Data>();
152 /* We have no current one: get another */
153 shared_ptr<Butler> butler = _butler.lock ();
154 DCPOMATIC_ASSERT (butler);
155 DCPOMATIC_ASSERT (_track->GetSelection() < int(_tracks.size()));
156 DCPTextTrack track = _tracks[_track->GetSelection()];
158 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
162 if (d->track == track) {
168 _current_in_lines = false;
171 if (_current && _current->period.contains(time)) {
172 /* We need to set this new one up */
174 list<StringText> to_show = _current->text.string;
176 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
180 to_show.sort (ClosedCaptionSorter());
182 list<StringText>::const_iterator j = to_show.begin();
184 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
185 _lines[k] = j->text();
191 _current_in_lines = true;
196 ClosedCaptionsDialog::clear ()
198 _current = optional<TextRingBuffers::Data>();
199 _current_in_lines = false;
204 ClosedCaptionsDialog::set_film_and_butler (shared_ptr<Film> film, weak_ptr<Butler> butler)
208 BOOST_FOREACH (shared_ptr<Content> i, film->content()) {
209 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
210 if (j->use() && j->type() == TEXT_CLOSED_CAPTION && j->dcp_track()) {
211 if (find(_tracks.begin(), _tracks.end(), j->dcp_track()) == _tracks.end()) {
212 _tracks.push_back (*j->dcp_track());
219 BOOST_FOREACH (DCPTextTrack const & i, _tracks) {
220 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language)));
223 if (_track->GetCount() > 0) {
224 _track->SetSelection (0);