Restore paint-panel timing.
[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 #include "simple_video_view.h"
22 #include "film_viewer.h"
23 #include "lib/image.h"
24 #include <dcp/util.h>
25 #include <wx/wx.h>
26 #include <boost/bind.hpp>
27
28 using std::max;
29
30 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
31         : VideoView (viewer)
32 {
33         _panel = new wxPanel (parent);
34
35 #ifndef __WXOSX__
36         _panel->SetDoubleBuffered (true);
37 #endif
38
39         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
40         _panel->SetBackgroundColour (*wxBLACK);
41
42         _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
43         _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
44 }
45
46 void
47 SimpleVideoView::paint ()
48 {
49         _viewer->state_timer().set("paint-panel");
50         wxPaintDC dc (_panel);
51
52 #ifdef DCPOMATIC_VARIANT_SWAROOP
53         if (viewer->background_image()) {
54                 dc.Clear ();
55                 maybe_draw_background_image (dc);
56                 return;
57         }
58 #endif
59
60         dcp::Size const out_size = _viewer->out_size ();
61         wxSize const panel_size = _panel->GetSize ();
62
63         if (!out_size.width || !out_size.height || !_image || out_size != _image->size()) {
64                 dc.Clear ();
65         } else {
66
67                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
68                 wxBitmap frame_bitmap (frame);
69                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
70
71 #ifdef DCPOMATIC_VARIANT_SWAROOP
72                 XXX
73                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
74                 int64_t n = _video_position.get() / period.get();
75                 DCPTime from(n * period.get());
76                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
77                 if (from <= _video_position && _video_position <= to) {
78                         if (!_in_watermark) {
79                                 _in_watermark = true;
80                                 _watermark_x = rand() % _panel_size.width;
81                                 _watermark_y = rand() % _panel_size.height;
82                         }
83                         dc.SetTextForeground(*wxWHITE);
84                         string wm = Config::instance()->player_watermark_theatre();
85                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
86                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
87                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
88                 } else {
89                         _in_watermark = false;
90                 }
91 #endif
92         }
93
94         if (out_size.width < panel_size.GetWidth()) {
95                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
96                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
97                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
98                 dc.SetPen (p);
99                 dc.SetBrush (b);
100                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
101         }
102
103         if (out_size.height < panel_size.GetHeight()) {
104                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
105                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
106                 dc.SetPen (p);
107                 dc.SetBrush (b);
108                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
109                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
110                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
111         }
112
113         if (_viewer->outline_content()) {
114                 Position<int> inter_position = _viewer->inter_position ();
115                 dcp::Size inter_size = _viewer->inter_size ();
116                 wxPen p (wxColour (255, 0, 0), 2);
117                 dc.SetPen (p);
118                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
119                 dc.DrawRectangle (inter_position.x, inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, inter_size.width, inter_size.height);
120         }
121         _viewer->state_timer().unset();
122 }
123
124 void
125 SimpleVideoView::update ()
126 {
127         _panel->Refresh ();
128         _panel->Update ();
129 }