2 Copyright (C) 2018 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"
22 #include "lib/string_text.h"
23 #include <boost/bind.hpp>
29 using boost::shared_ptr;
30 using boost::weak_ptr;
32 int const ClosedCaptionsDialog::_num_lines = 3;
33 int const ClosedCaptionsDialog::_num_chars_per_line = 30;
35 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
36 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
38 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
39 the window above all others (and not just our own) it's better than nothing for now.
41 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
43 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
48 _lines.resize (_num_lines);
49 Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
53 ClosedCaptionsDialog::paint ()
56 dc.SetBackground (*wxBLACK_BRUSH);
58 dc.SetTextForeground (*wxWHITE);
60 /* Choose a font which fits vertically */
61 int const line_height = max (8, dc.GetSize().GetHeight() / _num_lines);
62 wxFont font (*wxNORMAL_FONT);
63 font.SetPixelSize (wxSize (0, line_height * 0.8));
66 for (int i = 0; i < _num_lines; ++i) {
67 wxString const good = _lines[i].Left (_num_chars_per_line);
68 dc.DrawText (good, 8, line_height * i);
69 if (_lines[i].Length() > _num_chars_per_line) {
70 wxString const bad = _lines[i].Right (_lines[i].Length() - _num_chars_per_line);
71 wxSize size = dc.GetTextExtent (good);
72 dc.SetTextForeground (*wxRED);
73 dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
74 dc.SetTextForeground (*wxWHITE);
79 class ClosedCaptionSorter
82 bool operator() (StringText const & a, StringText const & b)
84 return from_top(a) < from_top(b);
88 float from_top (StringText const & c) const
90 switch (c.v_align()) {
92 return c.v_position();
93 case dcp::VALIGN_CENTER:
94 return c.v_position() + 0.5;
95 case dcp::VALIGN_BOTTOM:
96 return 1.0 - c.v_position();
98 DCPOMATIC_ASSERT (false);
104 ClosedCaptionsDialog::update (DCPTime time)
106 shared_ptr<Player> player = _player.lock ();
107 DCPOMATIC_ASSERT (player);
108 list<StringText> to_show;
109 BOOST_FOREACH (PlayerText i, player->closed_captions_for_frame(time)) {
110 BOOST_FOREACH (StringText j, i.text) {
111 to_show.push_back (j);
115 for (int j = 0; j < _num_lines; ++j) {
119 to_show.sort (ClosedCaptionSorter());
121 list<StringText>::const_iterator j = to_show.begin();
123 while (j != to_show.end() && k < _num_lines) {
124 _lines[k] = j->text();
133 ClosedCaptionsDialog::clear ()
139 ClosedCaptionsDialog::set_player (weak_ptr<Player> player)