Basics of OpenGL viewer.
[dcpomatic.git] / src / wx / simple_video_view.cc
1 /*
2     Copyright (C) 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 SimpleVideoView::SimpleVideoView (wxWindow* parent)
22 {
23         _panel = new wxPanel (parent);
24
25 #ifndef __WXOSX__
26         _panel->SetDoubleBuffered (true);
27 #endif
28
29         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
30         _panel->SetBackgroundColour (*wxBLACK);
31
32         _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint_panel, this));
33         _panel->Bind (wxEVT_SIZE,  boost::bind (&SimpleVideoView::panel_sized, this, _1));
34 }
35
36 void
37 SimpleVideoView::paint_panel ()
38 {
39         wxPaintDC dc (_panel);
40
41 #ifdef DCPOMATIC_VARIANT_SWAROOP
42         if (_background_image) {
43                 dc.Clear ();
44                 maybe_draw_background_image (dc);
45                 _state_timer.unset ();
46                 return;
47         }
48 #endif
49
50         if (!_out_size.width || !_out_size.height || !_film || !_frame || _out_size != _frame->size()) {
51                 dc.Clear ();
52         } else {
53
54                 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
55                 wxBitmap frame_bitmap (frame);
56                 dc.DrawBitmap (frame_bitmap, 0, max(0, (_panel_size.height - _out_size.height) / 2));
57
58 #ifdef DCPOMATIC_VARIANT_SWAROOP
59                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
60                 int64_t n = _video_position.get() / period.get();
61                 DCPTime from(n * period.get());
62                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
63                 if (from <= _video_position && _video_position <= to) {
64                         if (!_in_watermark) {
65                                 _in_watermark = true;
66                                 _watermark_x = rand() % _panel_size.width;
67                                 _watermark_y = rand() % _panel_size.height;
68                         }
69                         dc.SetTextForeground(*wxWHITE);
70                         string wm = Config::instance()->player_watermark_theatre();
71                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
72                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
73                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
74                 } else {
75                         _in_watermark = false;
76                 }
77 #endif
78         }
79
80         if (_out_size.width < _panel_size.width) {
81                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
82                 wxPen   p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
83                 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
84                 dc.SetPen (p);
85                 dc.SetBrush (b);
86                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
87         }
88
89         if (_out_size.height < _panel_size.height) {
90                 wxPen   p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
91                 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
92                 dc.SetPen (p);
93                 dc.SetBrush (b);
94                 int const gap = (_panel_size.height - _out_size.height) / 2;
95                 dc.DrawRectangle (0, 0, _panel_size.width, gap);
96                 dc.DrawRectangle (0, gap + _out_size.height + 1, _panel_size.width, gap + 1);
97         }
98
99         if (_outline_content) {
100                 wxPen p (wxColour (255, 0, 0), 2);
101                 dc.SetPen (p);
102                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
103                 dc.DrawRectangle (_inter_position.x, _inter_position.y + (_panel_size.height - _out_size.height) / 2, _inter_size.width, _inter_size.height);
104         }
105 }
106
107 void
108 SimpleVideoView::panel_sized (wxSizeEvent& ev)
109 {
110         _panel_size.width = ev.GetSize().GetWidth();
111         _panel_size.height = ev.GetSize().GetHeight();
112
113         calculate_sizes ();
114         if (!quick_refresh()) {
115                 slow_refresh ();
116         }
117         PositionChanged ();
118 }