summaryrefslogtreecommitdiff
path: root/src/wx/closed_captions_dialog.cc
blob: 0b2e630357e60ed83a26c0a3d0d890687c4b6898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
    Copyright (C) 2018 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "closed_captions_dialog.h"
#include "lib/text_caption.h"
#include <boost/bind.hpp>

using std::list;
using std::cout;
using std::make_pair;
using boost::shared_ptr;
using boost::weak_ptr;

int const ClosedCaptionsDialog::_num_lines = 3;
int const ClosedCaptionsDialog::_num_chars_per_line = 30;

ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
	: wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
#ifdef DCPOMATIC_OSX
		/* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
		   the window above all others (and not just our own) it's better than nothing for now.
		*/
		wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
#else
		wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
#endif
		)

{
	_lines.resize (_num_lines);
	Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
}

void
ClosedCaptionsDialog::paint ()
{
	wxPaintDC dc (this);
	dc.SetBackground (*wxBLACK_BRUSH);
	dc.Clear ();
	dc.SetTextForeground (*wxWHITE);

	/* Choose a font which fits vertically */
	int const line_height = dc.GetSize().GetHeight() / _num_lines;
	wxFont font (*wxNORMAL_FONT);
	font.SetPixelSize (wxSize (0, line_height * 0.8));
	dc.SetFont (font);

	for (int i = 0; i < _num_lines; ++i) {
		dc.DrawText (_lines[i], 8, line_height * i);
	}
}

class ClosedCaptionSorter
{
public:
	bool operator() (TextCaption const & a, TextCaption const & b)
	{
		return from_top(a) < from_top(b);
	}

private:
	float from_top (TextCaption const & c) const
	{
		switch (c.v_align()) {
		case dcp::VALIGN_TOP:
			return c.v_position();
		case dcp::VALIGN_CENTER:
			return c.v_position() + 0.5;
		case dcp::VALIGN_BOTTOM:
			return 1.0 - c.v_position();
		}
		DCPOMATIC_ASSERT (false);
		return 0;
	}
};

void
ClosedCaptionsDialog::update (DCPTime time)
{
	shared_ptr<Player> player = _player.lock ();
	DCPOMATIC_ASSERT (player);
	list<TextCaption> to_show;
	BOOST_FOREACH (PlayerCaption i, player->closed_captions_for_frame(time)) {
		BOOST_FOREACH (TextCaption j, i.text) {
			to_show.push_back (j);
		}
	}

	for (int j = 0; j < _num_lines; ++j) {
		_lines[j] = "";
	}

	to_show.sort (ClosedCaptionSorter());

	list<TextCaption>::const_iterator j = to_show.begin();
	int k = 0;
	while (j != to_show.end() && k < _num_lines) {
		_lines[k] = j->text();
		++j;
		++k;
	}

	Refresh ();
}

void
ClosedCaptionsDialog::clear ()
{
	Refresh ();
}

void
ClosedCaptionsDialog::set_player (weak_ptr<Player> player)
{
	_player = player;
}