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_view.h"
22 #include <boost/bind.hpp>
28 int const ClosedCaptionsDialog::_num_lines = 3;
29 int const ClosedCaptionsDialog::_num_chars_per_line = 30;
31 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
32 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
34 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
35 the window above all others (and not just our own) it's better than nothing for now.
37 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
39 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
44 _lines.resize (_num_lines);
45 Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
49 ClosedCaptionsDialog::paint ()
52 dc.SetBackground (*wxBLACK_BRUSH);
54 dc.SetTextForeground (*wxWHITE);
56 /* Choose a font which fits vertically */
57 int const line_height = dc.GetSize().GetHeight() / _num_lines;
58 wxFont font (*wxNORMAL_FONT);
59 font.SetPixelSize (wxSize (0, line_height * 0.8));
62 for (int i = 0; i < _num_lines; ++i) {
63 dc.DrawText (_lines[i], 8, line_height * i);
67 class ClosedCaptionSorter
70 bool operator() (TextCaption const & a, TextCaption const & b)
72 return from_top(a) < from_top(b);
76 float from_top (TextCaption const & c) const
78 switch (c.v_align()) {
80 return c.v_position();
81 case dcp::VALIGN_CENTER:
82 return c.v_position() + 0.5;
83 case dcp::VALIGN_BOTTOM:
84 return 1.0 - c.v_position();
86 DCPOMATIC_ASSERT (false);
92 ClosedCaptionsDialog::refresh (DCPTime time)
94 list<TextCaption> to_show;
95 list<Caption>::iterator i = _captions.begin ();
96 while (i != _captions.end ()) {
97 if (time > i->second.to) {
98 list<Caption>::iterator tmp = i;
100 _captions.erase (tmp);
101 } else if (i->second.contains (time)) {
102 BOOST_FOREACH (TextCaption j, i->first.text) {
103 to_show.push_back (j);
111 for (int j = 0; j < _num_lines; ++j) {
115 to_show.sort (ClosedCaptionSorter());
117 list<TextCaption>::const_iterator j = to_show.begin();
119 while (j != to_show.end() && k < _num_lines) {
120 _lines[k] = j->text();
129 ClosedCaptionsDialog::caption (PlayerCaption caption, DCPTimePeriod period)
131 _captions.push_back (make_pair (caption, period));
135 ClosedCaptionsDialog::clear ()