Get closed caption view data from the butler, rather than the player.
[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 "lib/butler.h"
24 #include <boost/bind.hpp>
25
26 using std::list;
27 using std::max;
28 using std::cout;
29 using std::pair;
30 using std::make_pair;
31 using boost::shared_ptr;
32 using boost::weak_ptr;
33 using boost::optional;
34
35 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
36         : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
37 #ifdef DCPOMATIC_OSX
38                 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
39                    the window above all others (and not just our own) it's better than nothing for now.
40                 */
41                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
42 #else
43                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
44 #endif
45                 )
46         , _current_in_lines (false)
47 {
48         _lines.resize (CLOSED_CAPTION_LINES);
49         Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
50 }
51
52 void
53 ClosedCaptionsDialog::paint ()
54 {
55         wxPaintDC dc (this);
56         dc.SetBackground (*wxBLACK_BRUSH);
57         dc.Clear ();
58         dc.SetTextForeground (*wxWHITE);
59
60         /* Choose a font which fits vertically */
61         int const line_height = max (8, dc.GetSize().GetHeight() / CLOSED_CAPTION_LINES);
62         wxFont font (*wxNORMAL_FONT);
63         font.SetPixelSize (wxSize (0, line_height * 0.8));
64         dc.SetFont (font);
65
66         for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
67                 wxString const good = _lines[i].Left (CLOSED_CAPTION_LENGTH);
68                 dc.DrawText (good, 8, line_height * i);
69                 if (_lines[i].Length() > CLOSED_CAPTION_LENGTH) {
70                         wxString const bad = _lines[i].Right (_lines[i].Length() - CLOSED_CAPTION_LENGTH);
71                         wxSize size = dc.GetTextExtent (good);
72                         dc.SetTextForeground (*wxRED);
73                         dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
74                         dc.SetTextForeground (*wxWHITE);
75                 }
76         }
77 }
78
79 class ClosedCaptionSorter
80 {
81 public:
82         bool operator() (StringText const & a, StringText const & b)
83         {
84                 return from_top(a) < from_top(b);
85         }
86
87 private:
88         float from_top (StringText const & c) const
89         {
90                 switch (c.v_align()) {
91                 case dcp::VALIGN_TOP:
92                         return c.v_position();
93                 case dcp::VALIGN_CENTER:
94                         return c.v_position() + 0.5;
95                 case dcp::VALIGN_BOTTOM:
96                         return 1.0 - c.v_position();
97                 }
98                 DCPOMATIC_ASSERT (false);
99                 return 0;
100         }
101 };
102
103 void
104 ClosedCaptionsDialog::update (DCPTime time)
105 {
106         if (_current_in_lines && _current->second.to > time) {
107                 /* Current one is fine */
108                 return;
109         }
110
111         if (_current && _current->second.to < time) {
112                 /* Current one has finished; clear out */
113                 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
114                         _lines[j] = "";
115                 }
116                 Refresh ();
117                 _current = optional<pair<PlayerText, DCPTimePeriod> >();
118         }
119
120         if (!_current) {
121                 /* We have no current one: get another */
122                 shared_ptr<Butler> butler = _butler.lock ();
123                 DCPOMATIC_ASSERT (butler);
124                 _current = butler->get_closed_caption ();
125                 _current_in_lines = false;
126         }
127
128         if (_current && _current->second.contains(time)) {
129                 /* We need to set this new one up */
130
131                 list<StringText> to_show = _current->first.string;
132
133                 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
134                         _lines[j] = "";
135                 }
136
137                 to_show.sort (ClosedCaptionSorter());
138
139                 list<StringText>::const_iterator j = to_show.begin();
140                 int k = 0;
141                 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
142                         _lines[k] = j->text();
143                         ++j;
144                         ++k;
145                 }
146
147                 Refresh ();
148                 _current_in_lines = true;
149         }
150 }
151
152 void
153 ClosedCaptionsDialog::clear ()
154 {
155         _current = optional<pair<PlayerText, DCPTimePeriod> >();
156         _current_in_lines = false;
157         Refresh ();
158 }
159
160 void
161 ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
162 {
163         _butler = butler;
164 }