Remove debug code.
[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                 dc.DrawText (_lines[i], 8, line_height * i);
64         }
65 }
66
67 class ClosedCaptionSorter
68 {
69 public:
70         bool operator() (TextCaption const & a, TextCaption const & b)
71         {
72                 return from_top(a) < from_top(b);
73         }
74
75 private:
76         float from_top (TextCaption const & c) const
77         {
78                 switch (c.v_align()) {
79                 case dcp::VALIGN_TOP:
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();
85                 }
86                 DCPOMATIC_ASSERT (false);
87                 return 0;
88         }
89 };
90
91 void
92 ClosedCaptionsDialog::refresh (DCPTime time)
93 {
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;
99                         ++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);
104                         }
105                         ++i;
106                 } else {
107                         ++i;
108                 }
109         }
110
111         for (int j = 0; j < _num_lines; ++j) {
112                 _lines[j] = "";
113         }
114
115         to_show.sort (ClosedCaptionSorter());
116
117         list<TextCaption>::const_iterator j = to_show.begin();
118         int k = 0;
119         while (j != to_show.end() && k < _num_lines) {
120                 _lines[k] = j->text();
121                 ++j;
122                 ++k;
123         }
124
125         Refresh ();
126 }
127
128 void
129 ClosedCaptionsDialog::caption (PlayerCaption caption, DCPTimePeriod period)
130 {
131         _captions.push_back (make_pair (caption, period));
132 }
133
134 void
135 ClosedCaptionsDialog::clear ()
136 {
137         _captions.clear ();
138         Refresh ();
139 }