Check for long CCAP lines and too many CCAP lines.
[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_dialog.h"
22 #include "lib/string_text.h"
23 #include <boost/bind.hpp>
24
25 using std::list;
26 using std::max;
27 using std::cout;
28 using std::make_pair;
29 using boost::shared_ptr;
30 using boost::weak_ptr;
31
32 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
33         : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
34 #ifdef DCPOMATIC_OSX
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.
37                 */
38                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
39 #else
40                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
41 #endif
42                 )
43
44 {
45         _lines.resize (CLOSED_CAPTION_LINES);
46         Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
47 }
48
49 void
50 ClosedCaptionsDialog::paint ()
51 {
52         wxPaintDC dc (this);
53         dc.SetBackground (*wxBLACK_BRUSH);
54         dc.Clear ();
55         dc.SetTextForeground (*wxWHITE);
56
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));
61         dc.SetFont (font);
62
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);
72                 }
73         }
74 }
75
76 class ClosedCaptionSorter
77 {
78 public:
79         bool operator() (StringText const & a, StringText const & b)
80         {
81                 return from_top(a) < from_top(b);
82         }
83
84 private:
85         float from_top (StringText const & c) const
86         {
87                 switch (c.v_align()) {
88                 case dcp::VALIGN_TOP:
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();
94                 }
95                 DCPOMATIC_ASSERT (false);
96                 return 0;
97         }
98 };
99
100 void
101 ClosedCaptionsDialog::update (DCPTime time)
102 {
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);
109                 }
110         }
111
112         for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
113                 _lines[j] = "";
114         }
115
116         to_show.sort (ClosedCaptionSorter());
117
118         list<StringText>::const_iterator j = to_show.begin();
119         int k = 0;
120         while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
121                 _lines[k] = j->text();
122                 ++j;
123                 ++k;
124         }
125
126         Refresh ();
127 }
128
129 void
130 ClosedCaptionsDialog::clear ()
131 {
132         Refresh ();
133 }
134
135 void
136 ClosedCaptionsDialog::set_player (weak_ptr<Player> player)
137 {
138         _player = player;
139 }