More automated renaming.
[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::cout;
27 using std::make_pair;
28 using boost::shared_ptr;
29 using boost::weak_ptr;
30
31 int const ClosedCaptionsDialog::_num_lines = 3;
32 int const ClosedCaptionsDialog::_num_chars_per_line = 30;
33
34 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
35         : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
36 #ifdef DCPOMATIC_OSX
37                 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
38                    the window above all others (and not just our own) it's better than nothing for now.
39                 */
40                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
41 #else
42                 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
43 #endif
44                 )
45
46 {
47         _lines.resize (_num_lines);
48         Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
49 }
50
51 void
52 ClosedCaptionsDialog::paint ()
53 {
54         wxPaintDC dc (this);
55         dc.SetBackground (*wxBLACK_BRUSH);
56         dc.Clear ();
57         dc.SetTextForeground (*wxWHITE);
58
59         /* Choose a font which fits vertically */
60         int const line_height = dc.GetSize().GetHeight() / _num_lines;
61         wxFont font (*wxNORMAL_FONT);
62         font.SetPixelSize (wxSize (0, line_height * 0.8));
63         dc.SetFont (font);
64
65         for (int i = 0; i < _num_lines; ++i) {
66                 dc.DrawText (_lines[i], 8, line_height * i);
67         }
68 }
69
70 class ClosedCaptionSorter
71 {
72 public:
73         bool operator() (StringText const & a, StringText const & b)
74         {
75                 return from_top(a) < from_top(b);
76         }
77
78 private:
79         float from_top (StringText const & c) const
80         {
81                 switch (c.v_align()) {
82                 case dcp::VALIGN_TOP:
83                         return c.v_position();
84                 case dcp::VALIGN_CENTER:
85                         return c.v_position() + 0.5;
86                 case dcp::VALIGN_BOTTOM:
87                         return 1.0 - c.v_position();
88                 }
89                 DCPOMATIC_ASSERT (false);
90                 return 0;
91         }
92 };
93
94 void
95 ClosedCaptionsDialog::update (DCPTime time)
96 {
97         shared_ptr<Player> player = _player.lock ();
98         DCPOMATIC_ASSERT (player);
99         list<StringText> to_show;
100         BOOST_FOREACH (PlayerText i, player->closed_captions_for_frame(time)) {
101                 BOOST_FOREACH (StringText j, i.text) {
102                         to_show.push_back (j);
103                 }
104         }
105
106         for (int j = 0; j < _num_lines; ++j) {
107                 _lines[j] = "";
108         }
109
110         to_show.sort (ClosedCaptionSorter());
111
112         list<StringText>::const_iterator j = to_show.begin();
113         int k = 0;
114         while (j != to_show.end() && k < _num_lines) {
115                 _lines[k] = j->text();
116                 ++j;
117                 ++k;
118         }
119
120         Refresh ();
121 }
122
123 void
124 ClosedCaptionsDialog::clear ()
125 {
126         Refresh ();
127 }
128
129 void
130 ClosedCaptionsDialog::set_player (weak_ptr<Player> player)
131 {
132         _player = player;
133 }