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