Option to choose CCAP track (part of #1516).
[dcpomatic.git] / src / wx / closed_captions_dialog.cc
1 /*
2     Copyright (C) 2018-2019 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 "wx_util.h"
23 #include "film_viewer.h"
24 #include "lib/string_text.h"
25 #include "lib/butler.h"
26 #include "lib/text_content.h"
27 #include "lib/compose.hpp"
28 #include <boost/bind.hpp>
29
30 using std::list;
31 using std::max;
32 using std::cout;
33 using std::pair;
34 using std::make_pair;
35 using boost::shared_ptr;
36 using boost::weak_ptr;
37 using boost::optional;
38
39 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
40         : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
41 #ifdef DCPOMATIC_OSX
42                     /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
43                        the window above all others (and not just our own) it's better than nothing for now.
44                     */
45                     wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
46 #else
47                     wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
48 #endif
49                 )
50         , _viewer (viewer)
51           /* XXX: empirical and probably unhelpful default size here; needs to be related to font metrics */
52         , _display (new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(640, (640 / 10) + 64)))
53         , _track (new wxChoice(this, wxID_ANY))
54         , _current_in_lines (false)
55 {
56         _lines.resize (CLOSED_CAPTION_LINES);
57
58         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
59
60         wxBoxSizer* track_sizer = new wxBoxSizer (wxHORIZONTAL);
61         add_label_to_sizer (track_sizer, this, _("Track"), true);
62         track_sizer->Add (_track, 0, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
63
64         sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
65         sizer->Add (_display, 1, wxEXPAND);
66
67         _display->Bind (wxEVT_PAINT, boost::bind(&ClosedCaptionsDialog::paint, this));
68         _track->Bind (wxEVT_CHOICE, boost::bind(&ClosedCaptionsDialog::track_selected, this));
69
70         SetSizerAndFit (sizer);
71 }
72
73 void
74 ClosedCaptionsDialog::track_selected ()
75 {
76         _current = optional<TextRingBuffers::Data> ();
77         _viewer->slow_refresh ();
78         update (_last_update);
79 }
80
81 void
82 ClosedCaptionsDialog::paint ()
83 {
84         wxPaintDC dc (_display);
85         dc.SetBackground (*wxBLACK_BRUSH);
86         dc.Clear ();
87         dc.SetTextForeground (*wxWHITE);
88
89         /* Choose a font which fits vertically */
90         int const line_height = max (8, dc.GetSize().GetHeight() / CLOSED_CAPTION_LINES);
91         wxFont font (*wxNORMAL_FONT);
92         font.SetPixelSize (wxSize (0, line_height * 0.8));
93         dc.SetFont (font);
94
95         for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
96                 wxString const good = _lines[i].Left (CLOSED_CAPTION_LENGTH);
97                 dc.DrawText (good, 8, line_height * i);
98                 if (_lines[i].Length() > CLOSED_CAPTION_LENGTH) {
99                         wxString const bad = _lines[i].Right (_lines[i].Length() - CLOSED_CAPTION_LENGTH);
100                         wxSize size = dc.GetTextExtent (good);
101                         dc.SetTextForeground (*wxRED);
102                         dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
103                         dc.SetTextForeground (*wxWHITE);
104                 }
105         }
106 }
107
108 class ClosedCaptionSorter
109 {
110 public:
111         bool operator() (StringText const & a, StringText const & b)
112         {
113                 return from_top(a) < from_top(b);
114         }
115
116 private:
117         float from_top (StringText const & c) const
118         {
119                 switch (c.v_align()) {
120                 case dcp::VALIGN_TOP:
121                         return c.v_position();
122                 case dcp::VALIGN_CENTER:
123                         return c.v_position() + 0.5;
124                 case dcp::VALIGN_BOTTOM:
125                         return 1.0 - c.v_position();
126                 }
127                 DCPOMATIC_ASSERT (false);
128                 return 0;
129         }
130 };
131
132 void
133 ClosedCaptionsDialog::update (DCPTime time)
134 {
135         _last_update = time;
136
137         if (_current_in_lines && _current && _current->period.to > time) {
138                 /* Current one is fine */
139                 return;
140         }
141
142         if (_current && _current->period.to < time) {
143                 /* Current one has finished; clear out */
144                 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
145                         _lines[j] = "";
146                 }
147                 Refresh ();
148                 _current = optional<TextRingBuffers::Data>();
149         }
150
151         if (!_current) {
152                 /* We have no current one: get another */
153                 shared_ptr<Butler> butler = _butler.lock ();
154                 DCPOMATIC_ASSERT (butler);
155                 DCPOMATIC_ASSERT (_track->GetSelection() < int(_tracks.size()));
156                 DCPTextTrack track = _tracks[_track->GetSelection()];
157                 while (true) {
158                         optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
159                         if (!d) {
160                                 break;
161                         }
162                         if (d->track == track) {
163                                 _current = d;
164                                 break;
165                         }
166                 }
167
168                 _current_in_lines = false;
169         }
170
171         if (_current && _current->period.contains(time)) {
172                 /* We need to set this new one up */
173
174                 list<StringText> to_show = _current->text.string;
175
176                 for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
177                         _lines[j] = "";
178                 }
179
180                 to_show.sort (ClosedCaptionSorter());
181
182                 list<StringText>::const_iterator j = to_show.begin();
183                 int k = 0;
184                 while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
185                         _lines[k] = j->text();
186                         ++j;
187                         ++k;
188                 }
189
190                 Refresh ();
191                 _current_in_lines = true;
192         }
193 }
194
195 void
196 ClosedCaptionsDialog::clear ()
197 {
198         _current = optional<TextRingBuffers::Data>();
199         _current_in_lines = false;
200         Refresh ();
201 }
202
203 void
204 ClosedCaptionsDialog::set_film_and_butler (shared_ptr<Film> film, weak_ptr<Butler> butler)
205 {
206         _tracks.clear ();
207
208         BOOST_FOREACH (shared_ptr<Content> i, film->content()) {
209                 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
210                         if (j->use() && j->type() == TEXT_CLOSED_CAPTION && j->dcp_track()) {
211                                 if (find(_tracks.begin(), _tracks.end(), j->dcp_track()) == _tracks.end()) {
212                                         _tracks.push_back (*j->dcp_track());
213                                 }
214                         }
215                 }
216         }
217
218         _track->Clear ();
219         BOOST_FOREACH (DCPTextTrack const & i, _tracks) {
220                 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language)));
221         }
222
223         if (_track->GetCount() > 0) {
224                 _track->SetSelection (0);
225         }
226
227         _butler = butler;
228 }