Very basic closed caption viewer.
[dcpomatic.git] / src / wx / closed_captions_dialog.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "closed_captions_view.h"
22 #include <boost/bind.hpp>
23
24 using std::list;
25 using std::cout;
26 using std::make_pair;
27
28 int const ClosedCaptionsDialog::_num_lines = 3;
29 int const ClosedCaptionsDialog::_num_chars_per_line = 30;
30
31 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
32         : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
33 #ifdef DCPOMATIC_OSX
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.
36                 */
37                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
38 #else
39                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
40 #endif
41                 )
42
43 {
44         _lines.resize (_num_lines);
45         Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
46 }
47
48 void
49 ClosedCaptionsDialog::paint ()
50 {
51         wxPaintDC dc (this);
52         dc.SetBackground (*wxBLACK_BRUSH);
53         dc.Clear ();
54         dc.SetTextForeground (*wxWHITE);
55
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));
60         dc.SetFont (font);
61
62         for (int i = 0; i < _num_lines; ++i) {
63                 if (_lines[i].IsEmpty()) {
64                         dc.DrawText (wxString::Format("Line %d", i + 1), 8, line_height * i);
65                 } else {
66                         dc.DrawText (_lines[i], 8, line_height * i);
67                 }
68         }
69 }
70
71 class ClosedCaptionSorter
72 {
73 public:
74         bool operator() (TextCaption const & a, TextCaption const & b)
75         {
76                 return from_top(a) < from_top(b);
77         }
78
79 private:
80         float from_top (TextCaption const & c) const
81         {
82                 switch (c.v_align()) {
83                 case dcp::VALIGN_TOP:
84                         return c.v_position();
85                 case dcp::VALIGN_CENTER:
86                         return c.v_position() + 0.5;
87                 case dcp::VALIGN_BOTTOM:
88                         return 1.0 - c.v_position();
89                 }
90                 DCPOMATIC_ASSERT (false);
91                 return 0;
92         }
93 };
94
95 void
96 ClosedCaptionsDialog::refresh (DCPTime time)
97 {
98         list<TextCaption> to_show;
99         list<Caption>::iterator i = _captions.begin ();
100         while (i != _captions.end ()) {
101                 if (time > i->second.to) {
102                         list<Caption>::iterator tmp = i;
103                         ++i;
104                         _captions.erase (tmp);
105                 } else if (i->second.contains (time)) {
106                         BOOST_FOREACH (TextCaption j, i->first.text) {
107                                 to_show.push_back (j);
108                         }
109                         ++i;
110                 } else {
111                         ++i;
112                 }
113         }
114
115         for (int j = 0; j < _num_lines; ++j) {
116                 _lines[j] = "";
117         }
118
119         to_show.sort (ClosedCaptionSorter());
120
121         list<TextCaption>::const_iterator j = to_show.begin();
122         int k = 0;
123         while (j != to_show.end() && k < _num_lines) {
124                 _lines[k] = j->text();
125                 ++j;
126                 ++k;
127         }
128
129         Refresh ();
130 }
131
132 void
133 ClosedCaptionsDialog::caption (PlayerCaption caption, DCPTimePeriod period)
134 {
135         _captions.push_back (make_pair (caption, period));
136 }
137
138 void
139 ClosedCaptionsDialog::clear ()
140 {
141         _captions.clear ();
142         Refresh ();
143 }