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 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
33 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
35 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
36 the window above all others (and not just our own) it's better than nothing for now.
38 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
40 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
45 _lines.resize (CLOSED_CAPTION_LINES);
46 Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
50 ClosedCaptionsDialog::paint ()
53 dc.SetBackground (*wxBLACK_BRUSH);
55 dc.SetTextForeground (*wxWHITE);
57 /* Choose a font which fits vertically */
58 int const line_height = max (8, dc.GetSize().GetHeight() / CLOSED_CAPTION_LINES);
59 wxFont font (*wxNORMAL_FONT);
60 font.SetPixelSize (wxSize (0, line_height * 0.8));
63 for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
64 wxString const good = _lines[i].Left (CLOSED_CAPTION_LENGTH);
65 dc.DrawText (good, 8, line_height * i);
66 if (_lines[i].Length() > CLOSED_CAPTION_LENGTH) {
67 wxString const bad = _lines[i].Right (_lines[i].Length() - CLOSED_CAPTION_LENGTH);
68 wxSize size = dc.GetTextExtent (good);
69 dc.SetTextForeground (*wxRED);
70 dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
71 dc.SetTextForeground (*wxWHITE);
76 class ClosedCaptionSorter
79 bool operator() (StringText const & a, StringText const & b)
81 return from_top(a) < from_top(b);
85 float from_top (StringText const & c) const
87 switch (c.v_align()) {
89 return c.v_position();
90 case dcp::VALIGN_CENTER:
91 return c.v_position() + 0.5;
92 case dcp::VALIGN_BOTTOM:
93 return 1.0 - c.v_position();
95 DCPOMATIC_ASSERT (false);
101 ClosedCaptionsDialog::update (DCPTime time)
103 shared_ptr<Player> player = _player.lock ();
104 DCPOMATIC_ASSERT (player);
105 list<StringText> to_show;
106 BOOST_FOREACH (PlayerText i, player->closed_captions_for_frame(time)) {
107 BOOST_FOREACH (StringText j, i.text) {
108 to_show.push_back (j);
112 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
116 to_show.sort (ClosedCaptionSorter());
118 list<StringText>::const_iterator j = to_show.begin();
120 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
121 _lines[k] = j->text();
130 ClosedCaptionsDialog::clear ()
136 ClosedCaptionsDialog::set_player (weak_ptr<Player> player)